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

src/html/parser.ts

Lines changed: 1 addition & 0 deletions
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,

src/script-setup/index.ts

Lines changed: 5 additions & 7 deletions
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])

src/script-setup/parser-options.ts

Lines changed: 1 addition & 1 deletion
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

test/parser-options-project.js

Lines changed: 129 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy