Skip to content

Commit 68574cf

Browse files
authored
Changed default ecmaVersion to "latest" (#243)
1 parent aca17d7 commit 68574cf

File tree

9 files changed

+47
-69
lines changed

9 files changed

+47
-69
lines changed

Diff for: src/common/espree.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ export function getEspree(): Espree {
1919
return espreeCache || (espreeCache = getNewestEspree())
2020
}
2121

22+
export function getEcmaVersionIfUseEspree(
23+
parserOptions: ParserOptions,
24+
): number | undefined {
25+
if (parserOptions.parser != null && parserOptions.parser !== "espree") {
26+
return undefined
27+
}
28+
29+
if (
30+
parserOptions.ecmaVersion === "latest" ||
31+
parserOptions.ecmaVersion == null
32+
) {
33+
return getDefaultEcmaVersion()
34+
}
35+
return normalizeEcmaVersion(parserOptions.ecmaVersion)
36+
}
37+
2238
/**
2339
* Load `espree` from the user dir.
2440
*/
@@ -44,26 +60,8 @@ function getNewestEspree(): Espree {
4460
return newest
4561
}
4662

47-
export function getEcmaVersionIfUseEspree(
48-
parserOptions: ParserOptions,
49-
getDefault?: (defaultVer: number) => number,
50-
): number | undefined {
51-
if (parserOptions.parser != null && parserOptions.parser !== "espree") {
52-
return undefined
53-
}
54-
55-
if (parserOptions.ecmaVersion === "latest") {
56-
return getDefaultEcmaVersion()
57-
}
58-
if (parserOptions.ecmaVersion == null) {
59-
const defVer = getDefaultEcmaVersion()
60-
return getDefault?.(defVer) ?? defVer
61-
}
62-
return normalizeEcmaVersion(parserOptions.ecmaVersion)
63-
}
64-
6563
function getDefaultEcmaVersion(): number {
66-
return normalizeEcmaVersion(getLatestEcmaVersion(getNewestEspree()))
64+
return getLatestEcmaVersion(getEspree())
6765
}
6866

6967
/**

Diff for: src/script-setup/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
parseScriptFragment,
3232
} from "../script/index"
3333
import { extractGeneric } from "../script/generic"
34-
import { getScriptSetupParserOptions } from "./parser-options"
34+
import { DEFAULT_ECMA_VERSION } from "./parser-options"
3535

3636
type RemapBlock = {
3737
range: [number, number]
@@ -214,9 +214,10 @@ export function parseScriptSetupElements(
214214
linesAndColumns: LinesAndColumns,
215215
originalParserOptions: ParserOptions,
216216
): ESLintExtendedProgram {
217-
const parserOptions: ParserOptions = getScriptSetupParserOptions(
218-
originalParserOptions,
219-
)
217+
const parserOptions: ParserOptions = {
218+
...originalParserOptions,
219+
ecmaVersion: originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
220+
}
220221
const scriptSetupModuleCodeBlocks = getScriptSetupModuleCodeBlocks(
221222
scriptSetupElement,
222223
scriptElement,

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

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
import { getEcmaVersionIfUseEspree, getEspree } from "../common/espree"
2-
import type { ParserOptions } from "../common/parser-options"
1+
export const DEFAULT_ECMA_VERSION = "latest"
32

4-
export const DEFAULT_ECMA_VERSION = 2017
5-
6-
/**
7-
* Get parser options for <script setup>
8-
*/
9-
export function getScriptSetupParserOptions(
10-
parserOptions: ParserOptions,
11-
): ParserOptions {
12-
const espreeEcmaVersion = getEcmaVersionIfUseEspree(
13-
parserOptions,
14-
getDefaultEcmaVersion,
15-
)
16-
17-
return {
18-
...parserOptions,
19-
ecmaVersion: espreeEcmaVersion || parserOptions.ecmaVersion,
20-
}
21-
}
22-
23-
function getDefaultEcmaVersion() {
24-
return getEspree().latestEcmaVersion
25-
}
3+
export const ANALYZE_SCOPE_DEFAULT_ECMA_VERSION = 2022

Diff for: src/script/index.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ import {
5050
fixLocation,
5151
fixLocations,
5252
} from "../common/fix-locations"
53-
import {
54-
DEFAULT_ECMA_VERSION,
55-
getScriptSetupParserOptions,
56-
} from "../script-setup/parser-options"
57-
import { isScriptSetupElement } from "../common/ast-utils"
53+
import { DEFAULT_ECMA_VERSION } from "../script-setup/parser-options"
5854
import type { LinesAndColumns } from "../common/lines-and-columns"
5955
import type { ParserObject } from "../common/parser-object"
6056
import { isEnhancedParserObject, isParserObject } from "../common/parser-object"
@@ -612,13 +608,10 @@ export function parseScriptElement(
612608
linesAndColumns: LinesAndColumns,
613609
originalParserOptions: ParserOptions,
614610
): ESLintExtendedProgram {
615-
const parserOptions: ParserOptions = isScriptSetupElement(node)
616-
? getScriptSetupParserOptions(originalParserOptions)
617-
: {
618-
...originalParserOptions,
619-
ecmaVersion:
620-
originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
621-
}
611+
const parserOptions: ParserOptions = {
612+
...originalParserOptions,
613+
ecmaVersion: originalParserOptions.ecmaVersion || DEFAULT_ECMA_VERSION,
614+
}
622615

623616
let generic: GenericProcessInfo | null = null
624617
let code: string

Diff for: src/script/scope-analyzer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
import { getFallbackKeys } from "../ast/index"
1515
import { getEslintScope } from "../common/eslint-scope"
1616
import { getEcmaVersionIfUseEspree } from "../common/espree"
17+
import { ANALYZE_SCOPE_DEFAULT_ECMA_VERSION } from "../script-setup/parser-options"
1718

1819
type ParserResult = {
1920
ast: ESLintProgram
@@ -100,7 +101,9 @@ export function analyzeScope(
100101
ast: ESLintProgram,
101102
parserOptions: ParserOptions,
102103
): escopeTypes.ScopeManager {
103-
const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022
104+
const ecmaVersion =
105+
getEcmaVersionIfUseEspree(parserOptions) ||
106+
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION
104107
const ecmaFeatures = parserOptions.ecmaFeatures || {}
105108
const sourceType = parserOptions.sourceType || "script"
106109
const result = getEslintScope().analyze(ast, {

Diff for: src/sfc/custom-block/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import type { LocationCalculatorForHtml } from "../../common/location-calculator
1717
import type { ParserObject } from "../../common/parser-object"
1818
import { isEnhancedParserObject } from "../../common/parser-object"
1919
import type { ParserOptions } from "../../common/parser-options"
20-
import { DEFAULT_ECMA_VERSION } from "../../script-setup/parser-options"
20+
import {
21+
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION,
22+
DEFAULT_ECMA_VERSION,
23+
} from "../../script-setup/parser-options"
2124

2225
export type ESLintCustomBlockParser = ParserObject<any, any>
2326

@@ -291,7 +294,9 @@ export function createCustomBlockSharedContext({
291294
return parsedResult.scopeManager
292295
}
293296

294-
const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022
297+
const ecmaVersion =
298+
getEcmaVersionIfUseEspree(parserOptions) ||
299+
ANALYZE_SCOPE_DEFAULT_ECMA_VERSION
295300
const ecmaFeatures = parserOptions.ecmaFeatures || {}
296301
const sourceType = parserOptions.sourceType || "script"
297302
return getEslintScope().analyze(parsedResult.ast, {

Diff for: test/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ describe("Basic tests", async () => {
588588
parserOptions: {
589589
...BABEL_PARSER_OPTIONS,
590590
sourceType: "module",
591-
ecmaVersion: 2017,
591+
ecmaVersion: "latest",
592592
},
593593
globals: {},
594594
},
@@ -752,7 +752,7 @@ describe("Basic tests", async () => {
752752
const indexOfDecorator = code.indexOf("@Component")
753753
const ast = parse(code, {
754754
...BABEL_PARSER_OPTIONS,
755-
ecmaVersion: 2017,
755+
ecmaVersion: "latest",
756756
sourceType: "module",
757757

758758
// Implicit parserOptions to detect whether the current ESLint supports `result.scopeManager` and `result.visitorKeys`.
@@ -859,7 +859,7 @@ describe("Basic tests", async () => {
859859
languageOptions: {
860860
parser,
861861
parserOptions: {
862-
ecmaVersion: 2015,
862+
ecmaVersion: "latest",
863863
},
864864
},
865865
}

Diff for: test/parser-options-project.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
2828
{
2929
project: true,
3030
sourceType: "module",
31-
ecmaVersion: 2018,
31+
ecmaVersion: "latest",
3232
parser: {
3333
parseForESLint(code, options) {
3434
if (options.project) {
@@ -72,7 +72,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
7272
{
7373
project: true,
7474
sourceType: "module",
75-
ecmaVersion: 2018,
75+
ecmaVersion: "latest",
7676
parser: {
7777
parseForESLint(code, options) {
7878
if (options.project) {
@@ -115,7 +115,7 @@ describe("use `project: undefined` when parsing template script-let", () => {
115115
{
116116
project: true,
117117
sourceType: "module",
118-
ecmaVersion: 2018,
118+
ecmaVersion: "latest",
119119
parser: {
120120
parseForESLint(code, options) {
121121
if (options.project) {

Diff for: test/test-utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function scopeToJSON(scopeManager) {
105105
* Analyze scope
106106
*/
107107
function analyze(ast, parserOptions) {
108-
const ecmaVersion = parserOptions.ecmaVersion || 2017
108+
const ecmaVersion = parserOptions.ecmaVersion || 2022
109109
const ecmaFeatures = parserOptions.ecmaFeatures || {}
110110
const sourceType = parserOptions.sourceType || "script"
111111
const result = escope.analyze(ast, {

0 commit comments

Comments
 (0)