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

fix(scope-manager): fallback to lib 'esnext' or 'es5' when ecma version is unsupported #2474

Merged
merged 3 commits into from Sep 3, 2020
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
9 changes: 4 additions & 5 deletions packages/scope-manager/src/analyze.ts
Expand Up @@ -2,6 +2,7 @@ import { TSESTree, EcmaVersion, Lib } from '@typescript-eslint/types';
import { visitorKeys } from '@typescript-eslint/visitor-keys';
import { Referencer, ReferencerOptions } from './referencer';
import { ScopeManager } from './ScopeManager';
import { lib as TSLibraries } from './lib';

////////////////////////////////////////////////////
// MAKE SURE THIS IS KEPT IN SYNC WITH THE README //
Expand Down Expand Up @@ -61,12 +62,10 @@ function mapEcmaVersion(version: EcmaVersion | undefined): Lib {
return 'es5';
}

if (version > 2000) {
return `es${version}` as Lib;
}
const year = version > 2000 ? version : 2015 + (version - 6);
const lib = `es${year}`;

const year = 2015 + (version - 6);
return `es${year}` as Lib;
return lib in TSLibraries ? (lib as Lib) : year > 2020 ? 'esnext' : 'es5';
}

/**
Expand Down
45 changes: 45 additions & 0 deletions packages/scope-manager/tests/eslint-scope/map-ecma-version.test.ts
@@ -0,0 +1,45 @@
import { analyze } from '../../src/analyze';
import { Referencer } from '../../src/referencer';
import { TSESTree, EcmaVersion, Lib } from '@typescript-eslint/types';

jest.mock('../../src/referencer');
jest.mock('../../src/ScopeManager');

describe('ecma version mapping', () => {
it("should map to 'esnext' when unsuported and new", () => {
expectMapping(2042, 'esnext');
expectMapping(42, 'esnext');
});

it("should map to 'es5' when unsuported and old", () => {
expectMapping(2002, 'es5');
expectMapping(2, 'es5');
});

it("should map to 'es{year}' when supported and >= 6", () => {
expectMapping(2015, 'es2015');
expectMapping(6, 'es2015');
expectMapping(2020, 'es2020');
expectMapping(11, 'es2020');
});

it("should map to 'es5' when 5 or 3", () => {
expectMapping(5, 'es5');
expectMapping(3, 'es5');
});

it("should map to 'es2018' when undefined", () => {
expectMapping(undefined, 'es2018');
});
});

const fakeNode = ({} as unknown) as TSESTree.Node;

function expectMapping(ecmaVersion: number | undefined, lib: Lib): void {
(Referencer as jest.Mock).mockClear();
analyze(fakeNode, { ecmaVersion: ecmaVersion as EcmaVersion });
expect(Referencer).toHaveBeenCalledWith(
expect.objectContaining({ lib: [lib] }),
expect.any(Object),
);
}