Skip to content

Commit 63b6902

Browse files
committed
feat(compiler): correct source map generation
1 parent 2e3a1ff commit 63b6902

File tree

8 files changed

+120
-111
lines changed

8 files changed

+120
-111
lines changed

packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ return function render() {
77
const { createVNode: _createVNode, toString: _toString, renderList: _renderList } = _Vue
88
return _createVNode(\\"div\\", {
99
id: \\"foo\\",
10-
class: bar
10+
class: bar.baz
1111
}, [
12-
_toString(world),
12+
_toString(world.burn()),
1313
ok
1414
? _createVNode(\\"div\\", 0, \\"yes\\")
1515
: \\"no\\",
@@ -21,35 +21,16 @@ return function render() {
2121
}"
2222
`;
2323

24-
exports[`compiler: integration tests function mode 2`] = `
25-
Object {
26-
"mappings": ";;;;WAAA,oBAAA;MAAK,IAAG;MAAO,OAAO;;gBACpB;MACW;UAAX,uBAAe;UACE;MACjB,YAA8B,OAAjB,OAAO;eAApB,wBAAoC,kCAAM",
27-
"names": Array [],
28-
"sources": Array [
29-
"foo.vue",
30-
],
31-
"sourcesContent": Array [
32-
"<div id=\\"foo\\" :class=\\"bar\\">
33-
{{ world }}
34-
<div v-if=\\"ok\\">yes</div>
35-
<template v-else>no</template>
36-
<div v-for=\\"(value, index) in list\\"><span>{{ value + index }}</span></div>
37-
</div>",
38-
],
39-
"version": 3,
40-
}
41-
`;
42-
4324
exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 1`] = `
4425
"const { createVNode, toString, renderList } = Vue
4526
4627
return function render() {
4728
const _ctx = this
4829
return createVNode(\\"div\\", {
4930
id: \\"foo\\",
50-
class: _ctx.bar
31+
class: _ctx.bar.baz
5132
}, [
52-
toString(_ctx.world),
33+
toString(_ctx.world.burn()),
5334
(_ctx.ok)
5435
? createVNode(\\"div\\", 0, \\"yes\\")
5536
: \\"no\\",
@@ -60,40 +41,16 @@ return function render() {
6041
}"
6142
`;
6243

63-
exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 2`] = `
64-
Object {
65-
"mappings": ";;;;SAAA,mBAAA;IAAK,IAAG;IAAO,OAAOA;;aACpBC;KACWC;QAAX,sBAAe;QACE;IACjB,WAA8BC,YAAjB,OAAO;aAApB,uBAAoC,gCAAS,QAAQ",
66-
"names": Array [
67-
"bar",
68-
"world",
69-
"ok",
70-
"list",
71-
],
72-
"sources": Array [
73-
"foo.vue",
74-
],
75-
"sourcesContent": Array [
76-
"<div id=\\"foo\\" :class=\\"bar\\">
77-
{{ world }}
78-
<div v-if=\\"ok\\">yes</div>
79-
<template v-else>no</template>
80-
<div v-for=\\"(value, index) in list\\"><span>{{ value + index }}</span></div>
81-
</div>",
82-
],
83-
"version": 3,
84-
}
85-
`;
86-
8744
exports[`compiler: integration tests module mode 1`] = `
8845
"import { createVNode, toString, renderList } from \\"vue\\"
8946
9047
export default function render() {
9148
const _ctx = this
9249
return createVNode(\\"div\\", {
9350
id: \\"foo\\",
94-
class: _ctx.bar
51+
class: _ctx.bar.baz
9552
}, [
96-
_toString(_ctx.world),
53+
_toString(_ctx.world.burn()),
9754
(_ctx.ok)
9855
? createVNode(\\"div\\", 0, \\"yes\\")
9956
: \\"no\\",

packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,15 +3889,15 @@ Object {
38893889
"isStatic": false,
38903890
"loc": Object {
38913891
"end": Object {
3892-
"column": 20,
3892+
"column": 18,
38933893
"line": 1,
3894-
"offset": 19,
3894+
"offset": 17,
38953895
},
3896-
"source": "{{a < b}}",
3896+
"source": "a < b",
38973897
"start": Object {
3898-
"column": 11,
3898+
"column": 13,
38993899
"line": 1,
3900-
"offset": 10,
3900+
"offset": 12,
39013901
},
39023902
},
39033903
"type": 4,
@@ -6965,15 +6965,15 @@ Object {
69656965
"isStatic": false,
69666966
"loc": Object {
69676967
"end": Object {
6968-
"column": 23,
6968+
"column": 21,
69696969
"line": 1,
6970-
"offset": 22,
6970+
"offset": 20,
69716971
},
6972-
"source": "{{'</div>'}}",
6972+
"source": "'</div>'",
69736973
"start": Object {
6974-
"column": 11,
6974+
"column": 13,
69756975
"line": 1,
6976-
"offset": 10,
6976+
"offset": 12,
69776977
},
69786978
},
69796979
"type": 4,
@@ -7322,15 +7322,15 @@ Object {
73227322
"isStatic": true,
73237323
"loc": Object {
73247324
"end": Object {
7325-
"column": 5,
7325+
"column": 3,
73267326
"line": 1,
7327-
"offset": 4,
7327+
"offset": 2,
73287328
},
7329-
"source": "{{}}",
7329+
"source": "",
73307330
"start": Object {
7331-
"column": 1,
7331+
"column": 3,
73327332
"line": 1,
7333-
"offset": 0,
7333+
"offset": 2,
73347334
},
73357335
},
73367336
"type": 4,
@@ -7485,7 +7485,7 @@ Object {
74857485
"line": 1,
74867486
"offset": 32,
74877487
},
7488-
"source": "\\"{ some: condition }\\"",
7488+
"source": "{ some: condition }",
74897489
"start": Object {
74907490
"column": 14,
74917491
"line": 1,
@@ -7565,7 +7565,7 @@ Object {
75657565
"line": 2,
75667566
"offset": 70,
75677567
},
7568-
"source": "\\"{ color: 'red' }\\"",
7568+
"source": "{ color: 'red' }",
75697569
"start": Object {
75707570
"column": 18,
75717571
"line": 2,
@@ -7672,7 +7672,7 @@ Object {
76727672
"line": 2,
76737673
"offset": 70,
76747674
},
7675-
"source": "\\"{ color: 'red' }\\"",
7675+
"source": "{ color: 'red' }",
76767676
"start": Object {
76777677
"column": 20,
76787678
"line": 2,
@@ -7771,7 +7771,7 @@ Object {
77717771
"line": 1,
77727772
"offset": 32,
77737773
},
7774-
"source": "\\"{ some: condition }\\"",
7774+
"source": "{ some: condition }",
77757775
"start": Object {
77767776
"column": 14,
77777777
"line": 1,

packages/compiler-core/__tests__/compile.spec.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { SourceMapConsumer, RawSourceMap } from 'source-map'
33

44
describe('compiler: integration tests', () => {
55
const source = `
6-
<div id="foo" :class="bar">
7-
{{ world }}
6+
<div id="foo" :class="bar.baz">
7+
{{ world.burn() }}
88
<div v-if="ok">yes</div>
99
<template v-else>no</template>
1010
<div v-for="(value, index) in list"><span>{{ value + index }}</span></div>
@@ -55,7 +55,6 @@ describe('compiler: integration tests', () => {
5555
)
5656

5757
expect(code).toMatchSnapshot()
58-
expect(map).toMatchSnapshot()
5958
expect(map!.sources).toEqual([`foo.vue`])
6059
expect(map!.sourcesContent).toEqual([source])
6160

@@ -77,9 +76,21 @@ describe('compiler: integration tests', () => {
7776
consumer.originalPositionFor(getPositionInCode(code, `bar`))
7877
).toMatchObject(getPositionInCode(source, `bar`))
7978

79+
// without prefixIdentifiers: true, identifiers inside compound expressions
80+
// are mapped to closest parent expression.
81+
expect(
82+
consumer.originalPositionFor(getPositionInCode(code, `baz`))
83+
).toMatchObject(getPositionInCode(source, `bar`))
84+
8085
expect(
8186
consumer.originalPositionFor(getPositionInCode(code, `world`))
82-
).toMatchObject(getPositionInCode(source, `{{ world }}`))
87+
).toMatchObject(getPositionInCode(source, `world`))
88+
89+
// without prefixIdentifiers: true, identifiers inside compound expressions
90+
// are mapped to closest parent expression.
91+
expect(
92+
consumer.originalPositionFor(getPositionInCode(code, `burn()`))
93+
).toMatchObject(getPositionInCode(source, `world`))
8394

8495
expect(
8596
consumer.originalPositionFor(getPositionInCode(code, `ok`))
@@ -99,7 +110,7 @@ describe('compiler: integration tests', () => {
99110

100111
expect(
101112
consumer.originalPositionFor(getPositionInCode(code, `value + index`))
102-
).toMatchObject(getPositionInCode(source, `{{ value + index }}`))
113+
).toMatchObject(getPositionInCode(source, `value + index`))
103114
})
104115

105116
test('function mode w/ prefixIdentifiers: true', async () => {
@@ -112,7 +123,6 @@ describe('compiler: integration tests', () => {
112123
expect(code).toMatch(`const { createVNode, toString, renderList } = Vue`)
113124

114125
expect(code).toMatchSnapshot()
115-
expect(map).toMatchSnapshot()
116126
expect(map!.sources).toEqual([`foo.vue`])
117127
expect(map!.sourcesContent).toEqual([source])
118128

@@ -136,15 +146,22 @@ describe('compiler: integration tests', () => {
136146
expect(
137147
consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`))
138148
).toMatchObject(getPositionInCode(source, `bar`, true))
149+
expect(
150+
consumer.originalPositionFor(getPositionInCode(code, `baz`))
151+
).toMatchObject(getPositionInCode(source, `baz`))
139152

140153
expect(
141154
consumer.originalPositionFor(getPositionInCode(code, `world`, true))
142-
).toMatchObject(getPositionInCode(source, `{{ world }}`, `world`))
155+
).toMatchObject(getPositionInCode(source, `world`, `world`))
143156
expect(
144157
consumer.originalPositionFor(
145158
getPositionInCode(code, `_ctx.world`, `world`)
146159
)
147-
).toMatchObject(getPositionInCode(source, `{{ world }}`, `world`))
160+
).toMatchObject(getPositionInCode(source, `world`, `world`))
161+
162+
expect(
163+
consumer.originalPositionFor(getPositionInCode(code, `burn()`))
164+
).toMatchObject(getPositionInCode(source, `burn()`))
148165

149166
expect(
150167
consumer.originalPositionFor(getPositionInCode(code, `ok`))
@@ -208,15 +225,22 @@ describe('compiler: integration tests', () => {
208225
expect(
209226
consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`))
210227
).toMatchObject(getPositionInCode(source, `bar`, true))
228+
expect(
229+
consumer.originalPositionFor(getPositionInCode(code, `baz`))
230+
).toMatchObject(getPositionInCode(source, `baz`))
211231

212232
expect(
213233
consumer.originalPositionFor(getPositionInCode(code, `world`, true))
214-
).toMatchObject(getPositionInCode(source, `{{ world }}`, `world`))
234+
).toMatchObject(getPositionInCode(source, `world`, `world`))
215235
expect(
216236
consumer.originalPositionFor(
217237
getPositionInCode(code, `_ctx.world`, `world`)
218238
)
219-
).toMatchObject(getPositionInCode(source, `{{ world }}`, `world`))
239+
).toMatchObject(getPositionInCode(source, `world`, `world`))
240+
241+
expect(
242+
consumer.originalPositionFor(getPositionInCode(code, `burn()`))
243+
).toMatchObject(getPositionInCode(source, `burn()`))
220244

221245
expect(
222246
consumer.originalPositionFor(getPositionInCode(code, `ok`))

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ describe('compiler: parse', () => {
298298
isStatic: false,
299299
isInterpolation: true,
300300
loc: {
301-
start: { offset: 0, line: 1, column: 1 },
302-
end: { offset: 11, line: 1, column: 12 },
303-
source: '{{message}}'
301+
start: { offset: 2, line: 1, column: 3 },
302+
end: { offset: 9, line: 1, column: 10 },
303+
source: 'message'
304304
}
305305
})
306306
})
@@ -315,9 +315,9 @@ describe('compiler: parse', () => {
315315
isStatic: false,
316316
isInterpolation: true,
317317
loc: {
318-
start: { offset: 0, line: 1, column: 1 },
319-
end: { offset: 9, line: 1, column: 10 },
320-
source: '{{ a<b }}'
318+
start: { offset: 3, line: 1, column: 4 },
319+
end: { offset: 6, line: 1, column: 7 },
320+
source: 'a<b'
321321
}
322322
})
323323
})
@@ -333,9 +333,9 @@ describe('compiler: parse', () => {
333333
isStatic: false,
334334
isInterpolation: true,
335335
loc: {
336-
start: { offset: 0, line: 1, column: 1 },
337-
end: { offset: 9, line: 1, column: 10 },
338-
source: '{{ a<b }}'
336+
start: { offset: 3, line: 1, column: 4 },
337+
end: { offset: 6, line: 1, column: 7 },
338+
source: 'a<b'
339339
}
340340
})
341341
expect(interpolation2).toStrictEqual({
@@ -344,9 +344,9 @@ describe('compiler: parse', () => {
344344
isStatic: false,
345345
isInterpolation: true,
346346
loc: {
347-
start: { offset: 9, line: 1, column: 10 },
348-
end: { offset: 18, line: 1, column: 19 },
349-
source: '{{ c>d }}'
347+
start: { offset: 12, line: 1, column: 13 },
348+
end: { offset: 15, line: 1, column: 16 },
349+
source: 'c>d'
350350
}
351351
})
352352
})
@@ -362,9 +362,9 @@ describe('compiler: parse', () => {
362362
isStatic: false,
363363
isInterpolation: true,
364364
loc: {
365-
start: { offset: 5, line: 1, column: 6 },
366-
end: { offset: 19, line: 1, column: 20 },
367-
source: '{{ "</div>" }}'
365+
start: { offset: 8, line: 1, column: 9 },
366+
end: { offset: 16, line: 1, column: 17 },
367+
source: '"</div>"'
368368
}
369369
})
370370
})
@@ -896,7 +896,7 @@ describe('compiler: parse', () => {
896896
loc: {
897897
start: { offset: 11, line: 1, column: 12 },
898898
end: { offset: 12, line: 1, column: 13 },
899-
source: '"a"'
899+
source: 'a'
900900
}
901901
},
902902
loc: {
@@ -1313,17 +1313,20 @@ foo
13131313
offset += foo.loc.source.length
13141314
expect(foo.loc.end).toEqual({ line: 2, column: 5, offset })
13151315

1316-
expect(bar.loc.start).toEqual({ line: 2, column: 5, offset })
1316+
offset += 3
1317+
expect(bar.loc.start).toEqual({ line: 2, column: 8, offset })
13171318
offset += bar.loc.source.length
1318-
expect(bar.loc.end).toEqual({ line: 2, column: 14, offset })
1319+
expect(bar.loc.end).toEqual({ line: 2, column: 11, offset })
1320+
offset += 3
13191321

13201322
expect(but.loc.start).toEqual({ line: 2, column: 14, offset })
13211323
offset += but.loc.source.length
13221324
expect(but.loc.end).toEqual({ line: 2, column: 19, offset })
13231325

1324-
expect(baz.loc.start).toEqual({ line: 2, column: 19, offset })
1326+
offset += 3
1327+
expect(baz.loc.start).toEqual({ line: 2, column: 22, offset })
13251328
offset += baz.loc.source.length
1326-
expect(baz.loc.end).toEqual({ line: 2, column: 28, offset })
1329+
expect(baz.loc.end).toEqual({ line: 2, column: 25, offset })
13271330
})
13281331

13291332
describe('namedCharacterReferences option', () => {

0 commit comments

Comments
 (0)