Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25a42c0

Browse files
author
Hirotaka Tagawa / wafuwafu13
authored
feat: Support 'latest' as ecmaVersion (typescript-eslint#3873)
* feat: support latest ecmaversion * docs: add comment about ecmaVersion
1 parent 477b0c7 commit 25a42c0

File tree

7 files changed

+32
-11
lines changed

7 files changed

+32
-11
lines changed

packages/parser/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface ParserOptions {
5353
jsx?: boolean;
5454
globalReturn?: boolean;
5555
};
56-
ecmaVersion?: number;
56+
ecmaVersion?: number | 'latest';
5757

5858
jsxPragma?: string | null;
5959
jsxFragmentName?: string | null;
@@ -97,12 +97,13 @@ This options allows you to tell the parser if you want to allow global `return`
9797

9898
Default `2018`.
9999

100-
Accepts any valid ECMAScript version number:
100+
Accepts any valid ECMAScript version number or `'latest'`:
101101

102-
- A version: es3, es5, es6, es7, es8, es9, es10, es11, ..., or
103-
- A year: es2015, es2016, es2017, es2018, es2019, es2020, ...
102+
- A version: es3, es5, es6, es7, es8, es9, es10, es11, es12, es13, ..., or
103+
- A year: es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022, ..., or
104+
- `'latest'`
104105

105-
The value **must** be a number - so do not include the `es` prefix.
106+
When it's a version or a year, the value **must** be a number - so do not include the `es` prefix.
106107

107108
Specifies the version of ECMAScript syntax you want to use. This is used by the parser to determine how to perform scope analysis, and it affects the default
108109

packages/parser/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function parseForESLint(
102102
jsx: validateBoolean(options.ecmaFeatures.jsx),
103103
});
104104
const analyzeOptions: AnalyzeOptions = {
105-
ecmaVersion: options.ecmaVersion,
105+
ecmaVersion: options.ecmaVersion === 'latest' ? 1e8 : options.ecmaVersion,
106106
globalReturn: options.ecmaFeatures.globalReturn,
107107
jsxPragma: options.jsxPragma,
108108
jsxFragmentName: options.jsxFragmentName,

packages/parser/tests/lib/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ describe('parser', () => {
1818
expect(() => parseForESLint(code, null)).not.toThrow();
1919
});
2020

21+
it("parseForESLint() should work if options.ecmaVersion is `'latest'`", () => {
22+
const code = 'const valid = true;';
23+
expect(() => parseForESLint(code, { ecmaVersion: 'latest' })).not.toThrow();
24+
});
25+
2126
it('parseAndGenerateServices() should be called with options', () => {
2227
const code = 'const valid = true;';
2328
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');

packages/scope-manager/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ interface AnalyzeOptions {
3939
/**
4040
* Which ECMAScript version is considered.
4141
* Defaults to `2018`.
42+
* `'latest'` is converted to 1e8 at parser.
4243
*/
43-
ecmaVersion?: EcmaVersion;
44+
ecmaVersion?: EcmaVersion | 1e8;
4445

4546
/**
4647
* Whether the whole script is executed under node.js environment.

packages/scope-manager/src/analyze.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ interface AnalyzeOptions {
1717
/**
1818
* Which ECMAScript version is considered.
1919
* Defaults to `2018`.
20+
* `'latest'` is converted to 1e8 at parser.
2021
*/
21-
ecmaVersion?: EcmaVersion;
22+
ecmaVersion?: EcmaVersion | 1e8;
2223

2324
/**
2425
* Whether the whole script is executed under node.js environment.
@@ -81,7 +82,11 @@ const DEFAULT_OPTIONS: Required<AnalyzeOptions> = {
8182
emitDecoratorMetadata: false,
8283
};
8384

84-
function mapEcmaVersion(version: EcmaVersion | undefined): Lib {
85+
/**
86+
* Convert ecmaVersion to lib.
87+
* `'latest'` is converted to 1e8 at parser.
88+
*/
89+
function mapEcmaVersion(version: EcmaVersion | 1e8 | undefined): Lib {
8590
if (version == null || version === 3 || version === 5) {
8691
return 'es5';
8792
}

packages/scope-manager/tests/eslint-scope/map-ecma-version.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('ecma version mapping', () => {
3131
it("should map to 'es2018' when undefined", () => {
3232
expectMapping(undefined, 'es2018');
3333
});
34+
35+
it("should map to 'esnext' when 'latest'", () => {
36+
// `'latest'` is converted to 1e8 at parser.
37+
expectMapping(1e8, 'esnext');
38+
});
3439
});
3540

3641
const fakeNode = {} as unknown as TSESTree.Node;

packages/types/src/parser-options.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ type EcmaVersion =
1212
| 9
1313
| 10
1414
| 11
15+
| 12
16+
| 13
1517
| 2015
1618
| 2016
1719
| 2017
1820
| 2018
1921
| 2019
20-
| 2020;
22+
| 2020
23+
| 2021
24+
| 2022;
2125

2226
type SourceType = 'script' | 'module';
2327

@@ -26,7 +30,7 @@ interface ParserOptions {
2630
globalReturn?: boolean;
2731
jsx?: boolean;
2832
};
29-
ecmaVersion?: EcmaVersion;
33+
ecmaVersion?: EcmaVersion | 'latest';
3034

3135
// scope-manager specific
3236
jsxPragma?: string | null;

0 commit comments

Comments
 (0)