Add assertion to align (#18201)
This commit is contained in:
1 parent
9d667ffa36
commit
bf95d3cdbe
3 files changed
+22
-2
No files matched your search
@@ -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<Align, "n" | "contents"> & {readonly n: N, readonly contents: D}}
|
||||
*/
|
||||
function align(alignType, contents) {
|
||||
assertAlignType(alignType);
|
||||
assertDoc(contents);
|
||||
|
||||
return { type: DOC_TYPE_ALIGN, contents, n: alignType };
|
||||
|
||||
@@ -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 };
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in new issue
Block a user