Skip to content

Commit

Permalink
fix(typescript-estree): correct AST regression introduced by TS4.0 up…
Browse files Browse the repository at this point in the history
…grade (#2316)
  • Loading branch information
bradzacher committed Jul 21, 2020
1 parent 30fafb0 commit d7fefba
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
18 changes: 15 additions & 3 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -29,6 +29,7 @@ import {
TSNode,
TSESTreeToTSNode,
} from './ts-estree';
import { typescriptVersionIsAtLeast } from './version-check';

const SyntaxKind = ts.SyntaxKind;

Expand Down Expand Up @@ -1998,12 +1999,20 @@ export class Converter {
raw: 'false',
});

case SyntaxKind.NullKeyword:
case SyntaxKind.NullKeyword: {
if (!typescriptVersionIsAtLeast['4.0'] && this.inTypeMode) {
// 4.0 started nesting null types inside a LiteralType node, but we still need to support pre-4.0
return this.createNode<TSESTree.TSNullKeyword>(node, {
type: AST_NODE_TYPES.TSNullKeyword,
});
}

return this.createNode<TSESTree.Literal>(node, {
type: AST_NODE_TYPES.Literal,
value: null,
raw: 'null',
});
}

case SyntaxKind.EmptyStatement:
return this.createNode<TSESTree.EmptyStatement>(node, {
Expand Down Expand Up @@ -2672,8 +2681,11 @@ export class Converter {
});
}
case SyntaxKind.LiteralType: {
if (node.literal.kind === SyntaxKind.NullKeyword) {
// 4.0.0 started nesting null types inside a LiteralType node
if (
typescriptVersionIsAtLeast['4.0'] &&
node.literal.kind === SyntaxKind.NullKeyword
) {
// 4.0 started nesting null types inside a LiteralType node
// but our AST is designed around the old way of null being a keyword
return this.createNode<TSESTree.TSNullKeyword>(
node.literal as ts.NullLiteral,
Expand Down
11 changes: 2 additions & 9 deletions packages/typescript-estree/src/node-utils.ts
@@ -1,7 +1,7 @@
import unescape from 'lodash/unescape';
import * as semver from 'semver';
import * as ts from 'typescript';
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from './ts-estree';
import { typescriptVersionIsAtLeast } from './version-check';

const SyntaxKind = ts.SyntaxKind;

Expand Down Expand Up @@ -452,13 +452,6 @@ export function isOptionalChain(
);
}

/**
* Returns true if the current TS version is TS 3.9
*/
function isTSv3dot9(): boolean {
return !semver.satisfies(ts.version, '< 3.9.0 || < 3.9.1-rc || < 3.9.0-beta');
}

/**
* Returns true of the child of property access expression is an optional chain
*/
Expand All @@ -477,7 +470,7 @@ export function isChildOptionalChain(
return true;
}

if (!isTSv3dot9()) {
if (!typescriptVersionIsAtLeast['3.9']) {
return false;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/typescript-estree/src/version-check.ts
@@ -0,0 +1,28 @@
import * as semver from 'semver';
import * as ts from 'typescript';

function semverCheck(version: string): boolean {
return semver.satisfies(
ts.version,
`>= ${version}.0 || >= ${version}.1-rc || >= ${version}.0-beta`,
{
includePrerelease: true,
},
);
}

const versions = [
//
'3.7',
'3.8',
'3.9',
'4.0',
] as const;
type Versions = typeof versions extends ArrayLike<infer U> ? U : never;

const typescriptVersionIsAtLeast = {} as Record<Versions, boolean>;
for (const version of versions) {
typescriptVersionIsAtLeast[version] = semverCheck(version);
}

export { typescriptVersionIsAtLeast };

0 comments on commit d7fefba

Please sign in to comment.