Ensure bare words don't start with " or {

This commit is contained in:
Tangent Wantwight 2023-08-25 16:28:48 -04:00
parent b031381ff7
commit 9ce7ec2598
2 changed files with 13 additions and 1 deletions

View file

@ -102,6 +102,9 @@ b`)
it("allows escaped quotes inside a quote", () =>
expect(parse('"a\\"b"')).toEqual([true, [[{ text: 'a"b' }]]]));
it("must close quoted words", () =>
expect(parse('"a b')).toMatchObject([false, {}]));
it("does not allow trailing characters after a closing quote", () =>
expect(parse('""a')).toMatchObject([false, {}]));
@ -138,6 +141,9 @@ b`)
it("can parse nested braces with text", () =>
expect(parse("{a{b}c}")).toEqual([true, [[{ text: "a{b}c" }]]]));
it("must be closed", () =>
expect(parse("{a b")).toMatchObject([false, {}]));
it("does not allow trailing characters after a closing brace", () =>
expect(parse("{}a")).toMatchObject([false, {}]));
@ -168,6 +174,11 @@ b`)
parse(String.raw`{\\\
}`)
).toEqual([true, [[{ text: "\\\\ " }]]]));
test.each(['{"}"', '"{"}'])(
"does not permit overlapping braces and quotes {%s}",
(text) => expect(parse(text)).toMatchObject([false, {}])
);
});
describe("Misc", () => {

View file

@ -27,6 +27,7 @@ const BARE_WORD_CHAR = /[^\s\\;]+/y;
function bareWordTmpl(charRegex: RegExp) {
return Sequence(
Regex(/(?!["\{])/y),
AtLeast(
1,
Choose<InterpolatedPiece>(
@ -36,7 +37,7 @@ function bareWordTmpl(charRegex: RegExp) {
.map(([text]) => ({ text }))
)
)
).map(([pieces]) => SimplifyWord(pieces));
).map(([, pieces]) => SimplifyWord(pieces));
}
const QuotedWord = Sequence(