Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Support 'latest' as ecmaVersion #3873

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref: #3710 (comment)

you don't need/want this check any more

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref: #3710 (comment)

Added 2021 and 2022


type SourceType = 'script' | 'module';

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref: #3710 (comment)

let's remove latest from here and add it to the parser types as we're now handling it specifically within the parser


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