Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Changed default ecmaVersion to "latest" #243

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/common/espree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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())
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/script-setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 2 additions & 24 deletions src/script-setup/parser-options.ts
Original file line number Diff line number Diff line change
@@ -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 <script setup>
*/
export function getScriptSetupParserOptions(
parserOptions: ParserOptions,
): ParserOptions {
const espreeEcmaVersion = getEcmaVersionIfUseEspree(
parserOptions,
getDefaultEcmaVersion,
)

return {
...parserOptions,
ecmaVersion: espreeEcmaVersion || parserOptions.ecmaVersion,
}
}

function getDefaultEcmaVersion() {
return getEspree().latestEcmaVersion
}
export const ANALYZE_SCOPE_DEFAULT_ECMA_VERSION = 2022
17 changes: 5 additions & 12 deletions src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ import {
fixLocation,
fixLocations,
} from "../common/fix-locations"
import {
DEFAULT_ECMA_VERSION,
getScriptSetupParserOptions,
} from "../script-setup/parser-options"
import { isScriptSetupElement } from "../common/ast-utils"
import { DEFAULT_ECMA_VERSION } from "../script-setup/parser-options"
import type { LinesAndColumns } from "../common/lines-and-columns"
import type { ParserObject } from "../common/parser-object"
import { isEnhancedParserObject, isParserObject } from "../common/parser-object"
Expand Down Expand Up @@ -612,13 +608,10 @@ export function parseScriptElement(
linesAndColumns: LinesAndColumns,
originalParserOptions: ParserOptions,
): ESLintExtendedProgram {
const parserOptions: ParserOptions = isScriptSetupElement(node)
? getScriptSetupParserOptions(originalParserOptions)
: {
...originalParserOptions,
ecmaVersion:
originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
}
const parserOptions: ParserOptions = {
...originalParserOptions,
ecmaVersion: originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
}

let generic: GenericProcessInfo | null = null
let code: string
Expand Down
5 changes: 4 additions & 1 deletion src/script/scope-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
import { getFallbackKeys } from "../ast/index"
import { getEslintScope } from "../common/eslint-scope"
import { getEcmaVersionIfUseEspree } from "../common/espree"
import { ANALYZE_SCOPE_DEFAULT_ECMA_VERSION } from "../script-setup/parser-options"

type ParserResult = {
ast: ESLintProgram
Expand Down Expand Up @@ -100,7 +101,9 @@ export function analyzeScope(
ast: ESLintProgram,
parserOptions: ParserOptions,
): escopeTypes.ScopeManager {
const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022
const ecmaVersion =
getEcmaVersionIfUseEspree(parserOptions) ||
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const sourceType = parserOptions.sourceType || "script"
const result = getEslintScope().analyze(ast, {
Expand Down
9 changes: 7 additions & 2 deletions src/sfc/custom-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import type { LocationCalculatorForHtml } from "../../common/location-calculator
import type { ParserObject } from "../../common/parser-object"
import { isEnhancedParserObject } from "../../common/parser-object"
import type { ParserOptions } from "../../common/parser-options"
import { DEFAULT_ECMA_VERSION } from "../../script-setup/parser-options"
import {
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION,
DEFAULT_ECMA_VERSION,
} from "../../script-setup/parser-options"

export type ESLintCustomBlockParser = ParserObject<any, any>

Expand Down Expand Up @@ -291,7 +294,9 @@ export function createCustomBlockSharedContext({
return parsedResult.scopeManager
}

const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022
const ecmaVersion =
getEcmaVersionIfUseEspree(parserOptions) ||
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const sourceType = parserOptions.sourceType || "script"
return getEslintScope().analyze(parsedResult.ast, {
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ describe("Basic tests", async () => {
parserOptions: {
...BABEL_PARSER_OPTIONS,
sourceType: "module",
ecmaVersion: 2017,
ecmaVersion: "latest",
},
globals: {},
},
Expand Down Expand Up @@ -752,7 +752,7 @@ describe("Basic tests", async () => {
const indexOfDecorator = code.indexOf("@Component")
const ast = parse(code, {
...BABEL_PARSER_OPTIONS,
ecmaVersion: 2017,
ecmaVersion: "latest",
sourceType: "module",

// Implicit parserOptions to detect whether the current ESLint supports `result.scopeManager` and `result.visitorKeys`.
Expand Down Expand Up @@ -859,7 +859,7 @@ describe("Basic tests", async () => {
languageOptions: {
parser,
parserOptions: {
ecmaVersion: 2015,
ecmaVersion: "latest",
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions test/parser-options-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
ecmaVersion: "latest",
parser: {
parseForESLint(code, options) {
if (options.project) {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
ecmaVersion: "latest",
parser: {
parseForESLint(code, options) {
if (options.project) {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
{
project: true,
sourceType: "module",
ecmaVersion: 2018,
ecmaVersion: "latest",
parser: {
parseForESLint(code, options) {
if (options.project) {
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function scopeToJSON(scopeManager) {
* Analyze scope
*/
function analyze(ast, parserOptions) {
const ecmaVersion = parserOptions.ecmaVersion || 2017
const ecmaVersion = parserOptions.ecmaVersion || 2022
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const sourceType = parserOptions.sourceType || "script"
const result = escope.analyze(ast, {
Expand Down