Skip to content

Commit 01c8138

Browse files
committed
Make to use project: undefined when parsing template script-let.
1 parent d1d2540 commit 01c8138

File tree

4 files changed

+136
-8
lines changed

4 files changed

+136
-8
lines changed

Diff for: src/html/parser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ export class Parser {
302302
yield getParserLangFromSFC(doc)
303303
},
304304
),
305+
project: undefined,
305306
}
306307
const scriptParserOptions = {
307308
...this.baseParserOptions,

Diff for: src/script-setup/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,12 @@ function getScriptSetupCodeBlocks(
516516
const offsetLocationCalculator =
517517
linesAndColumns.createOffsetLocationCalculator(scriptSetupStartOffset)
518518

519-
const result = parseScript(
519+
const { ast, visitorKeys } = parseScript(
520520
scriptCode,
521-
parserOptions,
521+
{ ...parserOptions, project: undefined },
522522
offsetLocationCalculator,
523523
)
524524

525-
const { ast } = result
526-
527525
// Holds the `import` and re-`export` statements.
528526
// All import and re-`export` statements are hoisted to the top.
529527
const importCodeBlocks = new CodeBlocks()
@@ -597,7 +595,7 @@ function getScriptSetupCodeBlocks(
597595
}
598596
fixNodeLocations(
599597
body,
600-
result.visitorKeys,
598+
visitorKeys,
601599
offsetLocationCalculator,
602600
)
603601
fixLocation(exportToken, offsetLocationCalculator)
@@ -695,7 +693,7 @@ function getScriptSetupCodeBlocks(
695693
// restore
696694
fixNodeLocations(
697695
body,
698-
result.visitorKeys,
696+
visitorKeys,
699697
offsetLocationCalculator,
700698
)
701699
for (const token of restoreTokens) {
@@ -826,7 +824,7 @@ function getScriptSetupCodeBlocks(
826824
let start = n.range[0]
827825
let end = n.range[1]
828826
traverseNodes(n, {
829-
visitorKeys: result.visitorKeys,
827+
visitorKeys,
830828
enterNode(c) {
831829
start = Math.min(start, c.range[0])
832830
end = Math.max(end, c.range[1])

Diff for: src/script-setup/parser-options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function getScriptSetupParserOptions(
1616

1717
return {
1818
...parserOptions,
19-
ecmaVersion: espreeEcmaVersion,
19+
ecmaVersion: espreeEcmaVersion || parserOptions.ecmaVersion,
2020
}
2121
}
2222

Diff for: test/parser-options-project.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"use strict"
2+
3+
const assert = require("assert")
4+
const { parseForESLint } = require("../src")
5+
const espree = require("espree")
6+
7+
describe("use `project: undefined` when parsing template script-let", () => {
8+
it("should be the project option is defined only once in Simple SFC.", () => {
9+
let projectCount = 0
10+
parseForESLint(
11+
`<template>
12+
<div v-bind:class="{}">
13+
<template v-for="item in items">
14+
{{ 'str' }}
15+
<button v-on:click="handler()"></button>
16+
</template>
17+
<MyComponent>
18+
<template v-slot="{a}">
19+
<div v-if="a">A</div>
20+
</template>
21+
</MyComponent>
22+
</div>
23+
</template>
24+
<script>
25+
export default {}
26+
</script>
27+
`,
28+
{
29+
project: true,
30+
sourceType: "module",
31+
ecmaVersion: 2018,
32+
parser: {
33+
parseForESLint(code, options) {
34+
if (options.project) {
35+
projectCount++
36+
}
37+
38+
return {
39+
ast: espree.parse(code, options),
40+
}
41+
},
42+
},
43+
},
44+
)
45+
assert.strictEqual(projectCount, 1)
46+
})
47+
it("should be the project option is defined only once in <script setup>.", () => {
48+
let projectCount = 0
49+
parseForESLint(
50+
`<script setup>
51+
let items = ["foo"]
52+
</script>
53+
<template>
54+
<div v-bind:class="{}">
55+
<template v-for="item in items">
56+
{{ 'str' }}
57+
<button v-on:click="handler()"></button>
58+
</template>
59+
<MyComponent>
60+
<template v-slot="{a}">
61+
<div v-if="a">A</div>
62+
</template>
63+
</MyComponent>
64+
</div>
65+
</template>
66+
`,
67+
{
68+
project: true,
69+
sourceType: "module",
70+
ecmaVersion: 2018,
71+
parser: {
72+
parseForESLint(code, options) {
73+
if (options.project) {
74+
projectCount++
75+
}
76+
77+
return {
78+
ast: espree.parse(code, options),
79+
}
80+
},
81+
},
82+
},
83+
)
84+
assert.strictEqual(projectCount, 1)
85+
})
86+
87+
it("should be the project option is defined only once in <script setup> with <script>.", () => {
88+
let projectCount = 0
89+
parseForESLint(
90+
`<script>
91+
import { ref } from 'vue'
92+
</script>
93+
<script setup>
94+
let items = ref(["foo"])
95+
</script>
96+
<template>
97+
<div v-bind:class="{}">
98+
<template v-for="item in items">
99+
{{ 'str' }}
100+
<button v-on:click="handler()"></button>
101+
</template>
102+
<MyComponent>
103+
<template v-slot="{a}">
104+
<div v-if="a">A</div>
105+
</template>
106+
</MyComponent>
107+
</div>
108+
</template>
109+
`,
110+
{
111+
project: true,
112+
sourceType: "module",
113+
ecmaVersion: 2018,
114+
parser: {
115+
parseForESLint(code, options) {
116+
if (options.project) {
117+
projectCount++
118+
}
119+
120+
return {
121+
ast: espree.parse(code, options),
122+
}
123+
},
124+
},
125+
},
126+
)
127+
assert.strictEqual(projectCount, 1)
128+
})
129+
})

0 commit comments

Comments
 (0)