diff --git a/src/index.ts b/src/index.ts index c5b47e07..c392bbb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,7 +85,6 @@ export function parseForESLint( options = Object.assign( { comment: true, - ecmaVersion: 2017, loc: true, range: true, tokens: true, @@ -93,17 +92,25 @@ export function parseForESLint( options || {}, ) + const optionsWithEcmaVersion = { + ...options, + ecmaVersion: options.ecmaVersion || 2017, + } + let result: AST.ESLintExtendedProgram let document: AST.VDocumentFragment | null let locationCalculator: LocationCalculatorForHtml | null if (!isVueFile(code, options)) { - result = parseScript(code, options) + result = parseScript(code, optionsWithEcmaVersion) document = null locationCalculator = null } else { const skipParsingScript = options.parser === false - const tokenizer = new HTMLTokenizer(code, options) - const rootAST = new HTMLParser(tokenizer, options).parse() + const tokenizer = new HTMLTokenizer(code, optionsWithEcmaVersion) + const rootAST = new HTMLParser( + tokenizer, + optionsWithEcmaVersion, + ).parse() locationCalculator = new LocationCalculatorForHtml( tokenizer.gaps, @@ -124,7 +131,7 @@ export function parseForESLint( let scriptSetup: VElement | undefined if (skipParsingScript || !scripts.length) { - result = parseScript("", options) + result = parseScript("", optionsWithEcmaVersion) } else if ( scripts.length === 2 && (scriptSetup = scripts.find(isScriptSetup)) diff --git a/src/script-setup/parser-options.ts b/src/script-setup/parser-options.ts index 25a4c934..29c1803e 100644 --- a/src/script-setup/parser-options.ts +++ b/src/script-setup/parser-options.ts @@ -16,8 +16,6 @@ export function getScriptSetupParserOptions( ) return { - // Script setup requires module support, so set module to sourceType. - sourceType: "module", ...parserOptions, ecmaVersion: espreeEcmaVersion, } @@ -28,5 +26,5 @@ function getDefaultEcmaVersion(def: number) { // Script setup requires top level await support, so default the ecma version to 2022. return getEspreeFromLinter().latestEcmaVersion! } - return Math.max(def, 2015) + return Math.max(def, 2017) } diff --git a/src/script/index.ts b/src/script/index.ts index 2311ef0c..fb7ca746 100644 --- a/src/script/index.ts +++ b/src/script/index.ts @@ -562,7 +562,10 @@ export function parseScriptElement( ): ESLintExtendedProgram { const parserOptions: ParserOptions = isScriptSetup(node) ? getScriptSetupParserOptions(originalParserOptions) - : originalParserOptions + : { + ...originalParserOptions, + ecmaVersion: originalParserOptions.ecmaVersion || 2017, + } const text = node.children[0] const { code, offset } = diff --git a/test/fixtures/ast/script-setup-top-level-await-with-latest/parser-options.json b/test/fixtures/ast/script-setup-top-level-await-with-latest/parser-options.json index 59478da8..c88b43d6 100644 --- a/test/fixtures/ast/script-setup-top-level-await-with-latest/parser-options.json +++ b/test/fixtures/ast/script-setup-top-level-await-with-latest/parser-options.json @@ -1,3 +1,4 @@ { + "sourceType": "module", "ecmaVersion": "latest" } diff --git a/test/fixtures/ast/script-setup-top-level-await/parser-options.json b/test/fixtures/ast/script-setup-top-level-await/parser-options.json new file mode 100644 index 00000000..2104ca43 --- /dev/null +++ b/test/fixtures/ast/script-setup-top-level-await/parser-options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +} diff --git a/test/parser-options.js b/test/parser-options.js index c8efe226..b9141ff9 100644 --- a/test/parser-options.js +++ b/test/parser-options.js @@ -36,5 +36,21 @@ describe("parserOptions", () => { assert.strictEqual(messages.length, 1) assert.strictEqual(messages[0].ruleId, "vue/valid-template-root") }) + + it("Fail in ` + const config = { + parser: "vue-eslint-parser", + parserOptions: {}, + rules: { + "vue/valid-template-root": "error", + }, + } + const messages = linter.verify(code, config, "test.vue") + + assert.strictEqual(messages.length, 1) + assert.strictEqual(messages[0].fatal, true) + }) }) }) diff --git a/typings/eslint-scope/index.d.ts b/typings/eslint-scope/index.d.ts index 45c4af98..5e904df2 100644 --- a/typings/eslint-scope/index.d.ts +++ b/typings/eslint-scope/index.d.ts @@ -4,6 +4,7 @@ * See LICENSE file in root directory for full license. */ import type * as estree from "estree" +import type { VisitorKeys } from "eslint-visitor-keys" export interface AnalysisOptions { optimistic?: boolean @@ -14,6 +15,7 @@ export interface AnalysisOptions { fallback?: string | Function sourceType?: "script" | "module" ecmaVersion?: number + childVisitorKeys?: VisitorKeys } export interface ScopeManager {