Skip to content

Commit 361a450

Browse files
committed
Make to use project: undefined when parsing template script-let.
1 parent a2d5182 commit 361a450

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
@@ -513,14 +513,12 @@ function getScriptSetupCodeBlocks(
513513
const offsetLocationCalculator =
514514
linesAndColumns.createOffsetLocationCalculator(scriptSetupStartOffset)
515515

516-
const result = parseScript(
516+
const { ast, visitorKeys } = parseScript(
517517
scriptCode,
518-
parserOptions,
518+
{ ...parserOptions, project: undefined },
519519
offsetLocationCalculator,
520520
)
521521

522-
const { ast } = result
523-
524522
// Holds the `import` and re-`export` statements.
525523
// All import and re-`export` statements are hoisted to the top.
526524
const importCodeBlocks = new CodeBlocks()
@@ -594,7 +592,7 @@ function getScriptSetupCodeBlocks(
594592
}
595593
fixNodeLocations(
596594
body,
597-
result.visitorKeys,
595+
visitorKeys,
598596
offsetLocationCalculator,
599597
)
600598
fixLocation(exportToken, offsetLocationCalculator)
@@ -692,7 +690,7 @@ function getScriptSetupCodeBlocks(
692690
// restore
693691
fixNodeLocations(
694692
body,
695-
result.visitorKeys,
693+
visitorKeys,
696694
offsetLocationCalculator,
697695
)
698696
for (const token of restoreTokens) {
@@ -823,7 +821,7 @@ function getScriptSetupCodeBlocks(
823821
let start = n.range[0]
824822
let end = n.range[1]
825823
traverseNodes(n, {
826-
visitorKeys: result.visitorKeys,
824+
visitorKeys,
827825
enterNode(c) {
828826
start = Math.min(start, c.range[0])
829827
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
@@ -17,7 +17,7 @@ export function getScriptSetupParserOptions(
1717

1818
return {
1919
...parserOptions,
20-
ecmaVersion: espreeEcmaVersion,
20+
ecmaVersion: espreeEcmaVersion || parserOptions.ecmaVersion,
2121
}
2222
}
2323

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)