update class attribute lexers to fix matching of "computed" attributes

master
Brad Cornes 2020-06-29 19:08:09 +01:00
parent 825b067391
commit 81446acdb3
1 changed files with 21 additions and 9 deletions

View File

@ -3,31 +3,43 @@ import { lazy } from './lazy'
const classAttributeStates: { [x: string]: moo.Rules } = { const classAttributeStates: { [x: string]: moo.Rules } = {
doubleClassList: { doubleClassList: {
lbrace: { match: /(?<!\\)\{/, push: 'interp' }, lbrace: { match: /(?<!\\)\{/, push: 'interpBrace' },
rbrace: { match: /(?<!\\)\}/, pop: 1 }, rbrace: { match: /(?<!\\)\}/, pop: 1 },
end: { match: /(?<!\\)"/, pop: 1 }, end: { match: /(?<!\\)"/, pop: 1 },
classlist: { match: /[\s\S]/, lineBreaks: true }, classlist: { match: /[\s\S]/, lineBreaks: true },
}, },
singleClassList: { singleClassList: {
lbrace: { match: /(?<!\\)\{/, push: 'interp' }, lbrace: { match: /(?<!\\)\{/, push: 'interpBrace' },
rbrace: { match: /(?<!\\)\}/, pop: 1 }, rbrace: { match: /(?<!\\)\}/, pop: 1 },
end: { match: /(?<!\\)'/, pop: 1 }, end: { match: /(?<!\\)'/, pop: 1 },
classlist: { match: /[\s\S]/, lineBreaks: true }, classlist: { match: /[\s\S]/, lineBreaks: true },
}, },
tickClassList: { tickClassList: {
lbrace: { match: /(?<=(?<!\\)\$)\{/, push: 'interp' }, lbrace: { match: /(?<=(?<!\\)\$)\{/, push: 'interpBrace' },
rbrace: { match: /(?<!\\)\}/, pop: 1 }, rbrace: { match: /(?<!\\)\}/, pop: 1 },
end: { match: /(?<!\\)`/, pop: 1 }, end: { match: /(?<!\\)`/, pop: 1 },
classlist: { match: /[\s\S]/, lineBreaks: true }, classlist: { match: /[\s\S]/, lineBreaks: true },
}, },
interp: { interpBrace: {
startSingle: { match: /(?<!\\)'/, push: 'singleClassList' }, startSingle: { match: /(?<!\\)'/, push: 'singleClassList' },
startDouble: { match: /(?<!\\)"/, push: 'doubleClassList' }, startDouble: { match: /(?<!\\)"/, push: 'doubleClassList' },
startTick: { match: /(?<!\\)`/, push: 'tickClassList' }, startTick: { match: /(?<!\\)`/, push: 'tickClassList' },
lbrace: { match: /(?<!\\)\{/, push: 'interp' }, lbrace: { match: /(?<!\\)\{/, push: 'interpBrace' },
rbrace: { match: /(?<!\\)\}/, pop: 1 }, rbrace: { match: /(?<!\\)\}/, pop: 1 },
text: { match: /[\s\S]/, lineBreaks: true }, text: { match: /[\s\S]/, lineBreaks: true },
}, },
interpSingle: {
startDouble: { match: /(?<!\\)"/, push: 'doubleClassList' },
startTick: { match: /(?<!\\)`/, push: 'tickClassList' },
single: { match: /(?<!\\)'/, pop: 1 },
text: { match: /[\s\S]/, lineBreaks: true },
},
interpDouble: {
startSingle: { match: /(?<!\\)'/, push: 'singleClassList' },
startTick: { match: /(?<!\\)`/, push: 'tickClassList' },
double: { match: /(?<!\\)"/, pop: 1 },
text: { match: /[\s\S]/, lineBreaks: true },
},
} }
export const getClassAttributeLexer = lazy(() => export const getClassAttributeLexer = lazy(() =>
@ -35,7 +47,7 @@ export const getClassAttributeLexer = lazy(() =>
main: { main: {
start1: { match: '"', push: 'doubleClassList' }, start1: { match: '"', push: 'doubleClassList' },
start2: { match: "'", push: 'singleClassList' }, start2: { match: "'", push: 'singleClassList' },
start3: { match: '{', push: 'interp' }, start3: { match: '{', push: 'interpBrace' },
}, },
...classAttributeStates, ...classAttributeStates,
}) })
@ -44,10 +56,10 @@ export const getClassAttributeLexer = lazy(() =>
export const getComputedClassAttributeLexer = lazy(() => export const getComputedClassAttributeLexer = lazy(() =>
moo.states({ moo.states({
main: { main: {
quote: { match: /['"{]/, push: 'interp' }, lbrace: { match: '{', push: 'interpBrace' },
single: { match: "'", push: 'interpSingle' },
double: { match: '"', push: 'interpDouble' },
}, },
// TODO: really this should use a different interp definition that is
// terminated correctly based on the initial quote type
...classAttributeStates, ...classAttributeStates,
}) })
) )