Skip to content

Commit 79574b5

Browse files
authored
Revert default option for sourceType: module on <script setup>. (#111)
* Revert default option for `sourceType: module` on `<script setup>`. * update * Update * update * update
1 parent 68dde0f commit 79574b5

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

Diff for: src/index.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,32 @@ export function parseForESLint(
8585
options = Object.assign(
8686
{
8787
comment: true,
88-
ecmaVersion: 2017,
8988
loc: true,
9089
range: true,
9190
tokens: true,
9291
},
9392
options || {},
9493
)
9594

95+
const optionsWithEcmaVersion = {
96+
...options,
97+
ecmaVersion: options.ecmaVersion || 2017,
98+
}
99+
96100
let result: AST.ESLintExtendedProgram
97101
let document: AST.VDocumentFragment | null
98102
let locationCalculator: LocationCalculatorForHtml | null
99103
if (!isVueFile(code, options)) {
100-
result = parseScript(code, options)
104+
result = parseScript(code, optionsWithEcmaVersion)
101105
document = null
102106
locationCalculator = null
103107
} else {
104108
const skipParsingScript = options.parser === false
105-
const tokenizer = new HTMLTokenizer(code, options)
106-
const rootAST = new HTMLParser(tokenizer, options).parse()
109+
const tokenizer = new HTMLTokenizer(code, optionsWithEcmaVersion)
110+
const rootAST = new HTMLParser(
111+
tokenizer,
112+
optionsWithEcmaVersion,
113+
).parse()
107114

108115
locationCalculator = new LocationCalculatorForHtml(
109116
tokenizer.gaps,
@@ -124,7 +131,7 @@ export function parseForESLint(
124131

125132
let scriptSetup: VElement | undefined
126133
if (skipParsingScript || !scripts.length) {
127-
result = parseScript("", options)
134+
result = parseScript("", optionsWithEcmaVersion)
128135
} else if (
129136
scripts.length === 2 &&
130137
(scriptSetup = scripts.find(isScriptSetup))

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export function getScriptSetupParserOptions(
1616
)
1717

1818
return {
19-
// Script setup requires module support, so set module to sourceType.
20-
sourceType: "module",
2119
...parserOptions,
2220
ecmaVersion: espreeEcmaVersion,
2321
}
@@ -28,5 +26,5 @@ function getDefaultEcmaVersion(def: number) {
2826
// Script setup requires top level await support, so default the ecma version to 2022.
2927
return getEspreeFromLinter().latestEcmaVersion!
3028
}
31-
return Math.max(def, 2015)
29+
return Math.max(def, 2017)
3230
}

Diff for: src/script/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,10 @@ export function parseScriptElement(
562562
): ESLintExtendedProgram {
563563
const parserOptions: ParserOptions = isScriptSetup(node)
564564
? getScriptSetupParserOptions(originalParserOptions)
565-
: originalParserOptions
565+
: {
566+
...originalParserOptions,
567+
ecmaVersion: originalParserOptions.ecmaVersion || 2017,
568+
}
566569

567570
const text = node.children[0]
568571
const { code, offset } =
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"sourceType": "module",
23
"ecmaVersion": "latest"
34
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sourceType": "module"
3+
}

Diff for: test/parser-options.js

+16
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,21 @@ describe("parserOptions", () => {
3636
assert.strictEqual(messages.length, 1)
3737
assert.strictEqual(messages[0].ruleId, "vue/valid-template-root")
3838
})
39+
40+
it("Fail in <script setup> without sourceType.", () => {
41+
const code = `<template>Hello</template>
42+
<script setup>import Foo from './foo'</script>`
43+
const config = {
44+
parser: "vue-eslint-parser",
45+
parserOptions: {},
46+
rules: {
47+
"vue/valid-template-root": "error",
48+
},
49+
}
50+
const messages = linter.verify(code, config, "test.vue")
51+
52+
assert.strictEqual(messages.length, 1)
53+
assert.strictEqual(messages[0].fatal, true)
54+
})
3955
})
4056
})

Diff for: typings/eslint-scope/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See LICENSE file in root directory for full license.
55
*/
66
import type * as estree from "estree"
7+
import type { VisitorKeys } from "eslint-visitor-keys"
78

89
export interface AnalysisOptions {
910
optimistic?: boolean
@@ -14,6 +15,7 @@ export interface AnalysisOptions {
1415
fallback?: string | Function
1516
sourceType?: "script" | "module"
1617
ecmaVersion?: number
18+
childVisitorKeys?: VisitorKeys
1719
}
1820

1921
export interface ScopeManager {

0 commit comments

Comments
 (0)