Skip to content

Commit

Permalink
feat: Support 'latest' as ecmaVersion (#3873)
Browse files Browse the repository at this point in the history
* feat: support latest ecmaversion

* docs: add comment about ecmaVersion
  • Loading branch information
Hirotaka Tagawa / wafuwafu13 committed Sep 21, 2021
1 parent 477b0c7 commit 25a42c0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
11 changes: 6 additions & 5 deletions packages/parser/README.md
Expand Up @@ -53,7 +53,7 @@ interface ParserOptions {
jsx?: boolean;
globalReturn?: boolean;
};
ecmaVersion?: number;
ecmaVersion?: number | 'latest';

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

Default `2018`.

Accepts any valid ECMAScript version number:
Accepts any valid ECMAScript version number or `'latest'`:

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/parser.ts
Expand Up @@ -102,7 +102,7 @@ function parseForESLint(
jsx: validateBoolean(options.ecmaFeatures.jsx),
});
const analyzeOptions: AnalyzeOptions = {
ecmaVersion: options.ecmaVersion,
ecmaVersion: options.ecmaVersion === 'latest' ? 1e8 : options.ecmaVersion,
globalReturn: options.ecmaFeatures.globalReturn,
jsxPragma: options.jsxPragma,
jsxFragmentName: options.jsxFragmentName,
Expand Down
5 changes: 5 additions & 0 deletions packages/parser/tests/lib/parser.ts
Expand Up @@ -18,6 +18,11 @@ describe('parser', () => {
expect(() => parseForESLint(code, null)).not.toThrow();
});

it("parseForESLint() should work if options.ecmaVersion is `'latest'`", () => {
const code = 'const valid = true;';
expect(() => parseForESLint(code, { ecmaVersion: 'latest' })).not.toThrow();
});

it('parseAndGenerateServices() should be called with options', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
Expand Down
3 changes: 2 additions & 1 deletion packages/scope-manager/README.md
Expand Up @@ -39,8 +39,9 @@ interface AnalyzeOptions {
/**
* Which ECMAScript version is considered.
* Defaults to `2018`.
* `'latest'` is converted to 1e8 at parser.
*/
ecmaVersion?: EcmaVersion;
ecmaVersion?: EcmaVersion | 1e8;

/**
* Whether the whole script is executed under node.js environment.
Expand Down
9 changes: 7 additions & 2 deletions packages/scope-manager/src/analyze.ts
Expand Up @@ -17,8 +17,9 @@ interface AnalyzeOptions {
/**
* Which ECMAScript version is considered.
* Defaults to `2018`.
* `'latest'` is converted to 1e8 at parser.
*/
ecmaVersion?: EcmaVersion;
ecmaVersion?: EcmaVersion | 1e8;

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

function mapEcmaVersion(version: EcmaVersion | undefined): Lib {
/**
* Convert ecmaVersion to lib.
* `'latest'` is converted to 1e8 at parser.
*/
function mapEcmaVersion(version: EcmaVersion | 1e8 | undefined): Lib {
if (version == null || version === 3 || version === 5) {
return 'es5';
}
Expand Down
Expand Up @@ -31,6 +31,11 @@ describe('ecma version mapping', () => {
it("should map to 'es2018' when undefined", () => {
expectMapping(undefined, 'es2018');
});

it("should map to 'esnext' when 'latest'", () => {
// `'latest'` is converted to 1e8 at parser.
expectMapping(1e8, 'esnext');
});
});

const fakeNode = {} as unknown as TSESTree.Node;
Expand Down
8 changes: 6 additions & 2 deletions packages/types/src/parser-options.ts
Expand Up @@ -12,12 +12,16 @@ type EcmaVersion =
| 9
| 10
| 11
| 12
| 13
| 2015
| 2016
| 2017
| 2018
| 2019
| 2020;
| 2020
| 2021
| 2022;

type SourceType = 'script' | 'module';

Expand All @@ -26,7 +30,7 @@ interface ParserOptions {
globalReturn?: boolean;
jsx?: boolean;
};
ecmaVersion?: EcmaVersion;
ecmaVersion?: EcmaVersion | 'latest';

// scope-manager specific
jsxPragma?: string | null;
Expand Down

0 comments on commit 25a42c0

Please sign in to comment.