From e3d1ba92879a85fa89784355f47e05590d39bf61 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 2 Jul 2021 11:01:27 +0900 Subject: [PATCH 1/5] Revert default option for `sourceType: module` on `` + 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 { From bd848a922ef7d66bd365c85e042e926c15e29b6d Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 2 Jul 2021 11:07:41 +0900 Subject: [PATCH 2/5] update --- src/script-setup/index.ts | 12 ------------ src/script/index.ts | 1 - 2 files changed, 13 deletions(-) diff --git a/src/script-setup/index.ts b/src/script-setup/index.ts index 31e7dd39..2795b3c0 100644 --- a/src/script-setup/index.ts +++ b/src/script-setup/index.ts @@ -15,7 +15,6 @@ import type { LinesAndColumns } from "../common/lines-and-columns" import type { LocationCalculator } from "../common/location-calculator" import type { ParserOptions } from "../common/parser-options" import { parseScript, parseScriptFragment } from "../script" -import { analyzeScope } from "../script/scope-analyzer" import { getScriptSetupParserOptions } from "./parser-options" type RemapBlock = { @@ -202,17 +201,6 @@ export function parseScriptSetupElements( ) } - if ( - originalParserOptions.sourceType !== parserOptions.sourceType && - !result.scopeManager - ) { - result.scopeManager = analyzeScope( - result.ast, - parserOptions, - result.visitorKeys, - ) - } - return result } diff --git a/src/script/index.ts b/src/script/index.ts index 1dd93b07..fb00e7b0 100644 --- a/src/script/index.ts +++ b/src/script/index.ts @@ -40,7 +40,6 @@ import type { } from "../common/location-calculator" import { analyzeExternalReferences, - analyzeScope, analyzeVariablesAndExternalReferences, } from "./scope-analyzer" import type { ESLintCustomParser } from "../common/espree" From 795c7bc0c655d0d21d6f8691bed41773dad86426 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 2 Jul 2021 11:20:21 +0900 Subject: [PATCH 3/5] Update --- src/index.ts | 16 +++++++++------- src/script/index.ts | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7ce11cc2..7343e3db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,20 +92,22 @@ export function parseForESLint( options || {}, ) + const optionWithEcmaVersion = { + ...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, { - ecmaVersion: 2017, - ...options, - }) + result = parseScript(code, optionWithEcmaVersion) 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, optionWithEcmaVersion) + const rootAST = new HTMLParser(tokenizer, optionWithEcmaVersion).parse() locationCalculator = new LocationCalculatorForHtml( tokenizer.gaps, @@ -126,7 +128,7 @@ export function parseForESLint( let scriptSetup: VElement | undefined if (skipParsingScript || !scripts.length) { - result = parseScript("", options) + result = parseScript("", optionWithEcmaVersion) } else if ( scripts.length === 2 && (scriptSetup = scripts.find(isScriptSetup)) diff --git a/src/script/index.ts b/src/script/index.ts index fb00e7b0..fb7ca746 100644 --- a/src/script/index.ts +++ b/src/script/index.ts @@ -563,8 +563,8 @@ export function parseScriptElement( const parserOptions: ParserOptions = isScriptSetup(node) ? getScriptSetupParserOptions(originalParserOptions) : { - ecmaVersion: 2017, ...originalParserOptions, + ecmaVersion: originalParserOptions.ecmaVersion || 2017, } const text = node.children[0] From dbbf686892eb1ebfe26fa53ca4fccb1ed76bae62 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 2 Jul 2021 11:21:34 +0900 Subject: [PATCH 4/5] update --- src/script/scope-analyzer.ts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/script/scope-analyzer.ts b/src/script/scope-analyzer.ts index 62324312..4895e66e 100644 --- a/src/script/scope-analyzer.ts +++ b/src/script/scope-analyzer.ts @@ -14,8 +14,6 @@ import type { import { getFallbackKeys } from "../ast" import { getEslintScope } from "../common/eslint-scope" import { getEcmaVersionIfUseEspree } from "../common/espree" -import type { VisitorKeys } from "eslint-visitor-keys" -import evk from "eslint-visitor-keys" /** * Check whether the given reference is unique in the belonging array. @@ -88,36 +86,28 @@ function getForScope(scope: escopeTypes.Scope): escopeTypes.Scope { return child.block === scope.block ? child.childScopes[0] : child } -export function analyzeScope( +/** + * + * @param ast + * @param parserOptions + */ +function analyze( ast: ESLintProgram, parserOptions: ParserOptions, - visitorKeys?: VisitorKeys, -): escopeTypes.ScopeManager { +): escopeTypes.Scope { const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022 const ecmaFeatures = parserOptions.ecmaFeatures || {} const sourceType = parserOptions.sourceType || "script" - return getEslintScope().analyze(ast, { + const result = getEslintScope().analyze(ast, { ignoreEval: true, nodejsScope: false, impliedStrict: ecmaFeatures.impliedStrict, ecmaVersion, sourceType, - childVisitorKeys: visitorKeys || evk.KEYS, fallback: getFallbackKeys, }) -} -/** - * - * @param ast - * @param parserOptions - */ -function analyze( - ast: ESLintProgram, - parserOptions: ParserOptions, - visitorKeys?: VisitorKeys, -): escopeTypes.Scope { - return analyzeScope(ast, parserOptions, visitorKeys).globalScope + return result.globalScope } /** From 02e70ca008b6c8f4066cd7da5d5d87b30f9c6760 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 2 Jul 2021 11:23:02 +0900 Subject: [PATCH 5/5] update --- src/index.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7343e3db..c392bbb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,7 @@ export function parseForESLint( options || {}, ) - const optionWithEcmaVersion = { + const optionsWithEcmaVersion = { ...options, ecmaVersion: options.ecmaVersion || 2017, } @@ -101,13 +101,16 @@ export function parseForESLint( let document: AST.VDocumentFragment | null let locationCalculator: LocationCalculatorForHtml | null if (!isVueFile(code, options)) { - result = parseScript(code, optionWithEcmaVersion) + result = parseScript(code, optionsWithEcmaVersion) document = null locationCalculator = null } else { const skipParsingScript = options.parser === false - const tokenizer = new HTMLTokenizer(code, optionWithEcmaVersion) - const rootAST = new HTMLParser(tokenizer, optionWithEcmaVersion).parse() + const tokenizer = new HTMLTokenizer(code, optionsWithEcmaVersion) + const rootAST = new HTMLParser( + tokenizer, + optionsWithEcmaVersion, + ).parse() locationCalculator = new LocationCalculatorForHtml( tokenizer.gaps, @@ -128,7 +131,7 @@ export function parseForESLint( let scriptSetup: VElement | undefined if (skipParsingScript || !scripts.length) { - result = parseScript("", optionWithEcmaVersion) + result = parseScript("", optionsWithEcmaVersion) } else if ( scripts.length === 2 && (scriptSetup = scripts.find(isScriptSetup))