Skip to content

Commit

Permalink
fix(scope-manager): fallback to lib 'esnext' or 'es5' when ecma versi…
Browse files Browse the repository at this point in the history
…on is unsupported (#2474)
  • Loading branch information
gcangussu committed Sep 3, 2020
1 parent bfe255f commit 20a7dcc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
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),
);
}

0 comments on commit 20a7dcc

Please sign in to comment.