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(typescript-estree): correct AST regression introduced by TS4.0 upgrade #2316

Merged
merged 1 commit into from Jul 21, 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
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 };