Skip to content

Commit 3f13203

Browse files
authoredSep 5, 2024··
feat(compiler-core): parse modifiers as expression to provide location data (#11819)
1 parent 7a64b67 commit 3f13203

File tree

7 files changed

+199
-17
lines changed

7 files changed

+199
-17
lines changed
 

‎packages/compiler-core/__tests__/parse.spec.ts

+185-7
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,27 @@ describe('compiler: parse', () => {
13581358
name: 'on',
13591359
rawName: 'v-on.enter',
13601360
arg: undefined,
1361-
modifiers: ['enter'],
1361+
modifiers: [
1362+
{
1363+
constType: 3,
1364+
content: 'enter',
1365+
isStatic: true,
1366+
loc: {
1367+
end: {
1368+
column: 16,
1369+
line: 1,
1370+
offset: 15,
1371+
},
1372+
source: 'enter',
1373+
start: {
1374+
column: 11,
1375+
line: 1,
1376+
offset: 10,
1377+
},
1378+
},
1379+
type: 4,
1380+
},
1381+
],
13621382
exp: undefined,
13631383
loc: {
13641384
start: { offset: 5, line: 1, column: 6 },
@@ -1377,7 +1397,46 @@ describe('compiler: parse', () => {
13771397
name: 'on',
13781398
rawName: 'v-on.enter.exact',
13791399
arg: undefined,
1380-
modifiers: ['enter', 'exact'],
1400+
modifiers: [
1401+
{
1402+
constType: 3,
1403+
content: 'enter',
1404+
isStatic: true,
1405+
loc: {
1406+
end: {
1407+
column: 16,
1408+
line: 1,
1409+
offset: 15,
1410+
},
1411+
source: 'enter',
1412+
start: {
1413+
column: 11,
1414+
line: 1,
1415+
offset: 10,
1416+
},
1417+
},
1418+
type: 4,
1419+
},
1420+
{
1421+
constType: 3,
1422+
content: 'exact',
1423+
isStatic: true,
1424+
loc: {
1425+
end: {
1426+
column: 22,
1427+
line: 1,
1428+
offset: 21,
1429+
},
1430+
source: 'exact',
1431+
start: {
1432+
column: 17,
1433+
line: 1,
1434+
offset: 16,
1435+
},
1436+
},
1437+
type: 4,
1438+
},
1439+
],
13811440
exp: undefined,
13821441
loc: {
13831442
start: { offset: 5, line: 1, column: 6 },
@@ -1406,7 +1465,46 @@ describe('compiler: parse', () => {
14061465
source: 'click',
14071466
},
14081467
},
1409-
modifiers: ['enter', 'exact'],
1468+
modifiers: [
1469+
{
1470+
constType: 3,
1471+
content: 'enter',
1472+
isStatic: true,
1473+
loc: {
1474+
end: {
1475+
column: 22,
1476+
line: 1,
1477+
offset: 21,
1478+
},
1479+
source: 'enter',
1480+
start: {
1481+
column: 17,
1482+
line: 1,
1483+
offset: 16,
1484+
},
1485+
},
1486+
type: 4,
1487+
},
1488+
{
1489+
constType: 3,
1490+
content: 'exact',
1491+
isStatic: true,
1492+
loc: {
1493+
end: {
1494+
column: 28,
1495+
line: 1,
1496+
offset: 27,
1497+
},
1498+
source: 'exact',
1499+
start: {
1500+
column: 23,
1501+
line: 1,
1502+
offset: 22,
1503+
},
1504+
},
1505+
type: 4,
1506+
},
1507+
],
14101508
exp: undefined,
14111509
loc: {
14121510
start: { offset: 5, line: 1, column: 6 },
@@ -1435,7 +1533,27 @@ describe('compiler: parse', () => {
14351533
source: '[a.b]',
14361534
},
14371535
},
1438-
modifiers: ['camel'],
1536+
modifiers: [
1537+
{
1538+
constType: 3,
1539+
content: 'camel',
1540+
isStatic: true,
1541+
loc: {
1542+
end: {
1543+
column: 22,
1544+
line: 1,
1545+
offset: 21,
1546+
},
1547+
source: 'camel',
1548+
start: {
1549+
column: 17,
1550+
line: 1,
1551+
offset: 16,
1552+
},
1553+
},
1554+
type: 4,
1555+
},
1556+
],
14391557
exp: undefined,
14401558
loc: {
14411559
start: { offset: 5, line: 1, column: 6 },
@@ -1530,7 +1648,27 @@ describe('compiler: parse', () => {
15301648
source: 'a',
15311649
},
15321650
},
1533-
modifiers: ['prop'],
1651+
modifiers: [
1652+
{
1653+
constType: 0,
1654+
content: 'prop',
1655+
isStatic: false,
1656+
loc: {
1657+
end: {
1658+
column: 1,
1659+
line: 1,
1660+
offset: 0,
1661+
},
1662+
source: '',
1663+
start: {
1664+
column: 1,
1665+
line: 1,
1666+
offset: 0,
1667+
},
1668+
},
1669+
type: 4,
1670+
},
1671+
],
15341672
exp: {
15351673
type: NodeTypes.SIMPLE_EXPRESSION,
15361674
content: 'b',
@@ -1569,7 +1707,27 @@ describe('compiler: parse', () => {
15691707
source: 'a',
15701708
},
15711709
},
1572-
modifiers: ['sync'],
1710+
modifiers: [
1711+
{
1712+
constType: 3,
1713+
content: 'sync',
1714+
isStatic: true,
1715+
loc: {
1716+
end: {
1717+
column: 13,
1718+
line: 1,
1719+
offset: 12,
1720+
},
1721+
source: 'sync',
1722+
start: {
1723+
column: 9,
1724+
line: 1,
1725+
offset: 8,
1726+
},
1727+
},
1728+
type: 4,
1729+
},
1730+
],
15731731
exp: {
15741732
type: NodeTypes.SIMPLE_EXPRESSION,
15751733
content: 'b',
@@ -1649,7 +1807,27 @@ describe('compiler: parse', () => {
16491807
source: 'a',
16501808
},
16511809
},
1652-
modifiers: ['enter'],
1810+
modifiers: [
1811+
{
1812+
constType: 3,
1813+
content: 'enter',
1814+
isStatic: true,
1815+
loc: {
1816+
end: {
1817+
column: 14,
1818+
line: 1,
1819+
offset: 13,
1820+
},
1821+
source: 'enter',
1822+
start: {
1823+
column: 9,
1824+
line: 1,
1825+
offset: 8,
1826+
},
1827+
},
1828+
type: 4,
1829+
},
1830+
],
16531831
exp: {
16541832
type: NodeTypes.SIMPLE_EXPRESSION,
16551833
content: 'b',

‎packages/compiler-core/src/ast.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export interface DirectiveNode extends Node {
203203
rawName?: string
204204
exp: ExpressionNode | undefined
205205
arg: ExpressionNode | undefined
206-
modifiers: string[]
206+
modifiers: SimpleExpressionNode[]
207207
/**
208208
* optional property to cache the expression parse result for v-for
209209
*/

‎packages/compiler-core/src/parser.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const tokenizer = new Tokenizer(stack, {
225225
rawName: raw,
226226
exp: undefined,
227227
arg: undefined,
228-
modifiers: raw === '.' ? ['prop'] : [],
228+
modifiers: raw === '.' ? [createSimpleExpression('prop')] : [],
229229
loc: getLoc(start),
230230
}
231231
if (name === 'pre') {
@@ -273,7 +273,8 @@ const tokenizer = new Tokenizer(stack, {
273273
setLocEnd(arg.loc, end)
274274
}
275275
} else {
276-
;(currentProp as DirectiveNode).modifiers.push(mod)
276+
const exp = createSimpleExpression(mod, true, getLoc(start, end))
277+
;(currentProp as DirectiveNode).modifiers.push(exp)
277278
}
278279
},
279280

@@ -379,7 +380,9 @@ const tokenizer = new Tokenizer(stack, {
379380
if (
380381
__COMPAT__ &&
381382
currentProp.name === 'bind' &&
382-
(syncIndex = currentProp.modifiers.indexOf('sync')) > -1 &&
383+
(syncIndex = currentProp.modifiers.findIndex(
384+
mod => mod.content === 'sync',
385+
)) > -1 &&
383386
checkCompatEnabled(
384387
CompilerDeprecationTypes.COMPILER_V_BIND_SYNC,
385388
currentOptions,

‎packages/compiler-core/src/transforms/transformElement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export function buildProps(
665665
}
666666

667667
// force hydration for v-bind with .prop modifier
668-
if (isVBind && modifiers.includes('prop')) {
668+
if (isVBind && modifiers.some(mod => mod.content === 'prop')) {
669669
patchFlag |= PatchFlags.NEED_HYDRATION
670670
}
671671

‎packages/compiler-core/src/transforms/vBind.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
6969
}
7070

7171
// .sync is replaced by v-model:arg
72-
if (modifiers.includes('camel')) {
72+
if (modifiers.some(mod => mod.content === 'camel')) {
7373
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
7474
if (arg.isStatic) {
7575
arg.content = camelize(arg.content)
@@ -83,10 +83,10 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
8383
}
8484

8585
if (!context.inSSR) {
86-
if (modifiers.includes('prop')) {
86+
if (modifiers.some(mod => mod.content === 'prop')) {
8787
injectPrefix(arg, '.')
8888
}
89-
if (modifiers.includes('attr')) {
89+
if (modifiers.some(mod => mod.content === 'attr')) {
9090
injectPrefix(arg, '^')
9191
}
9292
}

‎packages/compiler-core/src/transforms/vModel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
131131
// modelModifiers: { foo: true, "bar-baz": true }
132132
if (dir.modifiers.length && node.tagType === ElementTypes.COMPONENT) {
133133
const modifiers = dir.modifiers
134+
.map(m => m.content)
134135
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
135136
.join(`, `)
136137
const modifiersKey = arg

‎packages/compiler-dom/src/transforms/vOn.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const isKeyboardEvent = /*@__PURE__*/ makeMap(
3535

3636
const resolveModifiers = (
3737
key: ExpressionNode,
38-
modifiers: string[],
38+
modifiers: SimpleExpressionNode[],
3939
context: TransformContext,
4040
loc: SourceLocation,
4141
) => {
@@ -44,7 +44,7 @@ const resolveModifiers = (
4444
const eventOptionModifiers = []
4545

4646
for (let i = 0; i < modifiers.length; i++) {
47-
const modifier = modifiers[i]
47+
const modifier = modifiers[i].content
4848

4949
if (
5050
__COMPAT__ &&

0 commit comments

Comments
 (0)
Please sign in to comment.