Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
45 lines
1.4 KiB
Plaintext
45 lines
1.4 KiB
Plaintext
import { ErrorKind } from "./error.js";
|
|
import { Parser } from "./parser.js";
|
|
import { isDateElement, isDateTimeSkeleton, isNumberElement, isNumberSkeleton, isPluralElement, isSelectElement, isTagElement, isTimeElement } from "./types.js";
|
|
function pruneLocation(els) {
|
|
els.forEach((el) => {
|
|
delete el.location;
|
|
if (isSelectElement(el) || isPluralElement(el)) {
|
|
for (const k in el.options) {
|
|
delete el.options[k].location;
|
|
pruneLocation(el.options[k].value);
|
|
}
|
|
} else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
|
|
delete el.style.location;
|
|
} else if ((isDateElement(el) || isTimeElement(el)) && isDateTimeSkeleton(el.style)) {
|
|
delete el.style.location;
|
|
} else if (isTagElement(el)) {
|
|
pruneLocation(el.children);
|
|
}
|
|
});
|
|
}
|
|
export function parse(message, opts = {}) {
|
|
opts = {
|
|
shouldParseSkeletons: true,
|
|
requiresOtherClause: true,
|
|
...opts
|
|
};
|
|
const result = new Parser(message, opts).parse();
|
|
if (result.err) {
|
|
const error = SyntaxError(ErrorKind[result.err.kind]);
|
|
// @ts-expect-error Assign to error object
|
|
error.location = result.err.location;
|
|
// @ts-expect-error Assign to error object
|
|
error.originalMessage = result.err.message;
|
|
throw error;
|
|
}
|
|
if (!opts?.captureLocation) {
|
|
pruneLocation(result.val);
|
|
}
|
|
return result.val;
|
|
}
|
|
export * from "./types.js";
|
|
// only for testing
|
|
export const _Parser = Parser;
|
|
export { isStructurallySame } from "./manipulator.js";
|