Skip to content

Commit 2e65ea4

Browse files
committed
fix(compiler-core): fix line/column tracking when fast forwarding
1 parent 50cde7c commit 2e65ea4

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

packages/compiler-core/src/tokenizer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ export default class Tokenizer {
463463
*/
464464
private fastForwardTo(c: number): boolean {
465465
while (++this.index < this.buffer.length) {
466-
if (this.buffer.charCodeAt(this.index) === c) {
466+
const cc = this.buffer.charCodeAt(this.index)
467+
if (cc === CharCodes.NewLine) {
468+
this.newlines.push(this.index)
469+
}
470+
if (cc === c) {
467471
return true
468472
}
469473
}

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

+52-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,61 @@ describe('compiler:sfc', () => {
77
test('style block', () => {
88
// Padding determines how many blank lines will there be before the style block
99
const padding = Math.round(Math.random() * 10)
10-
const style = parse(
11-
`${'\n'.repeat(padding)}<style>\n.color {\n color: red;\n }\n</style>\n`
12-
).descriptor.styles[0]
10+
const src =
11+
`${'\n'.repeat(padding)}` +
12+
`<style>
13+
.css {
14+
color: red;
15+
}
16+
</style>
1317
14-
expect(style.map).not.toBeUndefined()
18+
<style module>
19+
.css-module {
20+
color: red;
21+
}
22+
</style>
1523
16-
const consumer = new SourceMapConsumer(style.map!)
24+
<style scoped>
25+
.css-scoped {
26+
color: red;
27+
}
28+
</style>
29+
30+
<style scoped>
31+
.css-scoped-nested {
32+
color: red;
33+
.dummy {
34+
color: green;
35+
}
36+
font-weight: bold;
37+
}
38+
</style>`
39+
const {
40+
descriptor: { styles }
41+
} = parse(src)
42+
43+
expect(styles[0].map).not.toBeUndefined()
44+
const consumer = new SourceMapConsumer(styles[0].map!)
45+
const lineOffset =
46+
src.slice(0, src.indexOf(`<style>`)).split('\n').length - 1
1747
consumer.eachMapping(mapping => {
18-
expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
48+
expect(mapping.generatedLine + lineOffset).toBe(mapping.originalLine)
49+
})
50+
51+
expect(styles[1].map).not.toBeUndefined()
52+
const consumer1 = new SourceMapConsumer(styles[1].map!)
53+
const lineOffset1 =
54+
src.slice(0, src.indexOf(`<style module>`)).split('\n').length - 1
55+
consumer1.eachMapping(mapping => {
56+
expect(mapping.generatedLine + lineOffset1).toBe(mapping.originalLine)
57+
})
58+
59+
expect(styles[2].map).not.toBeUndefined()
60+
const consumer2 = new SourceMapConsumer(styles[2].map!)
61+
const lineOffset2 =
62+
src.slice(0, src.indexOf(`<style scoped>`)).split('\n').length - 1
63+
consumer2.eachMapping(mapping => {
64+
expect(mapping.generatedLine + lineOffset2).toBe(mapping.originalLine)
1965
})
2066
})
2167

0 commit comments

Comments
 (0)