diff --git a/src/common/espree.ts b/src/common/espree.ts index 92bac80..bcb3fd7 100644 --- a/src/common/espree.ts +++ b/src/common/espree.ts @@ -19,6 +19,22 @@ export function getEspree(): Espree { return espreeCache || (espreeCache = getNewestEspree()) } +export function getEcmaVersionIfUseEspree( + parserOptions: ParserOptions, +): number | undefined { + if (parserOptions.parser != null && parserOptions.parser !== "espree") { + return undefined + } + + if ( + parserOptions.ecmaVersion === "latest" || + parserOptions.ecmaVersion == null + ) { + return getDefaultEcmaVersion() + } + return normalizeEcmaVersion(parserOptions.ecmaVersion) +} + /** * Load `espree` from the user dir. */ @@ -44,26 +60,8 @@ function getNewestEspree(): Espree { return newest } -export function getEcmaVersionIfUseEspree( - parserOptions: ParserOptions, - getDefault?: (defaultVer: number) => number, -): number | undefined { - if (parserOptions.parser != null && parserOptions.parser !== "espree") { - return undefined - } - - if (parserOptions.ecmaVersion === "latest") { - return getDefaultEcmaVersion() - } - if (parserOptions.ecmaVersion == null) { - const defVer = getDefaultEcmaVersion() - return getDefault?.(defVer) ?? defVer - } - return normalizeEcmaVersion(parserOptions.ecmaVersion) -} - function getDefaultEcmaVersion(): number { - return normalizeEcmaVersion(getLatestEcmaVersion(getNewestEspree())) + return getLatestEcmaVersion(getEspree()) } /** diff --git a/src/script-setup/index.ts b/src/script-setup/index.ts index d5c3701..990615e 100644 --- a/src/script-setup/index.ts +++ b/src/script-setup/index.ts @@ -31,7 +31,7 @@ import { parseScriptFragment, } from "../script/index" import { extractGeneric } from "../script/generic" -import { getScriptSetupParserOptions } from "./parser-options" +import { DEFAULT_ECMA_VERSION } from "./parser-options" type RemapBlock = { range: [number, number] @@ -214,9 +214,10 @@ export function parseScriptSetupElements( linesAndColumns: LinesAndColumns, originalParserOptions: ParserOptions, ): ESLintExtendedProgram { - const parserOptions: ParserOptions = getScriptSetupParserOptions( - originalParserOptions, - ) + const parserOptions: ParserOptions = { + ...originalParserOptions, + ecmaVersion: originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION, + } const scriptSetupModuleCodeBlocks = getScriptSetupModuleCodeBlocks( scriptSetupElement, scriptElement, diff --git a/src/script-setup/parser-options.ts b/src/script-setup/parser-options.ts index 0a3b686..bdcf54a 100644 --- a/src/script-setup/parser-options.ts +++ b/src/script-setup/parser-options.ts @@ -1,25 +1,3 @@ -import { getEcmaVersionIfUseEspree, getEspree } from "../common/espree" -import type { ParserOptions } from "../common/parser-options" +export const DEFAULT_ECMA_VERSION = "latest" -export const DEFAULT_ECMA_VERSION = 2017 - -/** - * Get parser options for