diff --git a/src/document/builders/align.js b/src/document/builders/align.js index b4a47ab91..7af689790 100644 --- a/src/document/builders/align.js +++ b/src/document/builders/align.js @@ -1,4 +1,4 @@ -import { assertDoc } from "../utilities/assert-doc.js"; +import { assertAlignType, assertDoc } from "../utilities/assert-doc.js"; import { indent } from "./indent.js"; import { DOC_TYPE_ALIGN } from "./types.js"; @@ -22,6 +22,7 @@ import { DOC_TYPE_ALIGN } from "./types.js"; @returns {Omit & {readonly n: N, readonly contents: D}} */ function align(alignType, contents) { + assertAlignType(alignType); assertDoc(contents); return { type: DOC_TYPE_ALIGN, contents, n: alignType }; diff --git a/src/document/utilities/assert-doc.js b/src/document/utilities/assert-doc.js index 63152bfcf..743bc3916 100644 --- a/src/document/utilities/assert-doc.js +++ b/src/document/utilities/assert-doc.js @@ -73,6 +73,21 @@ const assertDocFillParts = } }; +const assertAlignType = + process.env.NODE_ENV === "production" + ? noop + : function (alignType) { + if ( + !( + typeof alignType === "number" || + typeof alignType === "string" || + alignType?.type === "root" + ) + ) { + throw new TypeError(`Invalid alignType '${alignType}'.`); + } + }; + /** * @param {Doc} doc * @returns {boolean} @@ -111,4 +126,4 @@ function isValidSeparator(doc) { return hasLine && !hasUnexpectedString; } -export { assertDoc, assertDocArray, assertDocFillParts }; +export { assertAlignType, assertDoc, assertDocArray, assertDocFillParts }; diff --git a/tests/unit/doc-builders.js b/tests/unit/doc-builders.js index a9b9fef7e..801bd1cac 100644 --- a/tests/unit/doc-builders.js +++ b/tests/unit/doc-builders.js @@ -27,6 +27,8 @@ describe("doc builders", () => { () => align(2), () => align(2, invalidDoc), + [() => align(["invalid"], validDoc), TypeError], + [() => align(undefined, validDoc), TypeError], () => group(), () => group(invalidDoc), @@ -87,6 +89,8 @@ describe("doc builders", () => { ifBreak(validDoc), // eslint-disable-next-line unicorn/no-useless-undefined ifBreak(validDoc, undefined), + + align("any string", validDoc), ]; describe("Invalid usage", () => {