Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Breaking: typescript-estree to 12.0.0 and typescript to 3.2.1 #591

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _test.js
.vscode
yarn.lock
.eslint-release-info.json
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The following additional configuration options are available by specifying them

We will always endeavor to support the latest stable version of TypeScript.

The version of TypeScript currently supported by this parser is `~3.1.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript.
The version of TypeScript currently supported by this parser is `~3.2.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript.

If you use a non-supported version of TypeScript, the parser will log a warning to the console.

Expand Down
191 changes: 153 additions & 38 deletions analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,6 @@ class Referencer extends OriginalReferencer {
super.MethodDefinition(node);
}

/**
* Override.
* Don't make variable if `kind === "type"`.
* It doesn't declare variables but declare types.
* @param {VariableDeclaration} node The VariableDeclaration node to visit.
* @returns {void}
*/
VariableDeclaration(node) {
if (node.kind !== "type") {
super.VariableDeclaration(node);
return;
}

// To detect typeof.
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}

/**
* Don't create the reference object for the key if not computed.
* @param {TSEmptyBodyFunctionDeclaration} node The TSEmptyBodyFunctionDeclaration node to visit.
Expand Down Expand Up @@ -337,6 +318,7 @@ class Referencer extends OriginalReferencer {
this.visit(returnType);
this.typeMode = upperTypeMode;
}

TSEmptyBodyDeclareFunction(node) {
this.TSEmptyBodyFunctionDeclaration(node);
}
Expand Down Expand Up @@ -364,43 +346,59 @@ class Referencer extends OriginalReferencer {
* @returns {void}
*/
TSInterfaceDeclaration(node) {
this.visitTypeNodes(node);
}

/**
* Visit type assertion.
* @param {TSTypeAssertion} node The TSTypeAssertion node to visit.
* @returns {void}
*/
TSTypeAssertion(node) {
if (this.typeMode) {
this.visitChildren(node);
this.visit(node.typeAnnotation);
} else {
this.typeMode = true;
this.visitChildren(node);
this.visit(node.typeAnnotation);
this.typeMode = false;
}

this.visit(node.expression);
}

/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param {TSTypeAnnotation} node The TSTypeAnnotation node to visit.
* Visit as expression.
* @param {TSAsExpression} node The TSAsExpression node to visit.
* @returns {void}
*/
TSTypeAnnotation(node) {
TSAsExpression(node) {
this.visit(node.expression);

if (this.typeMode) {
this.visitChildren(node);
this.visit(node.typeAnnotation);
} else {
this.typeMode = true;
this.visitChildren(node);
this.visit(node.typeAnnotation);
this.typeMode = false;
}
}

/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param {TSTypeAnnotation} node The TSTypeAnnotation node to visit.
* @returns {void}
*/
TSTypeAnnotation(node) {
this.visitTypeNodes(node);
}

/**
* Switch to the type mode and visit child nodes to find `typeof x` expression in type declarations.
* @param {TSTypeParameterDeclaration} node The TSTypeParameterDeclaration node to visit.
* @returns {void}
*/
TSTypeParameterDeclaration(node) {
if (this.typeMode) {
this.visitChildren(node);
} else {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
this.visitTypeNodes(node);
}

/**
Expand All @@ -418,9 +416,105 @@ class Referencer extends OriginalReferencer {
}
}

/**
* @param {TSInferType} node The TSInferType node to visit.
* @returns {void}
*/
TSInferType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSTypeReference} node The TSTypeReference node to visit.
* @returns {void}
*/
TSTypeReference(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSTypeLiteral} node The TSTypeLiteral node to visit.
* @returns {void}
*/
TSTypeLiteral(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSLiteralType} node The TSLiteralType node to visit.
* @returns {void}
*/
TSLiteralType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSIntersectionType} node The TSIntersectionType node to visit.
* @returns {void}
*/
TSIntersectionType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSConditionalType} node The TSConditionalType node to visit.
* @returns {void}
*/
TSConditionalType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSIndexedAccessType} node The TSIndexedAccessType node to visit.
* @returns {void}
*/
TSIndexedAccessType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSMappedType} node The TSMappedType node to visit.
* @returns {void}
*/
TSMappedType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSOptionalType} node The TSOptionalType node to visit.
* @returns {void}
*/
TSOptionalType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSParenthesizedType} node The TSParenthesizedType node to visit.
* @returns {void}
*/
TSParenthesizedType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSRestType} node The TSRestType node to visit.
* @returns {void}
*/
TSRestType(node) {
this.visitTypeNodes(node);
}

/**
* @param {TSTupleType} node The TSTupleType node to visit.
* @returns {void}
*/
TSTupleType(node) {
this.visitTypeNodes(node);
}

/**
* Create reference objects for the object part. (This is `obj.prop`)
* @param {TSTypeQuery} node The TSTypeQuery node to visit.
* @param {TSQualifiedName} node The TSQualifiedName node to visit.
* @returns {void}
*/
TSQualifiedName(node) {
Expand Down Expand Up @@ -556,6 +650,12 @@ class Referencer extends OriginalReferencer {
this.visit(body);
}

TSTypeAliasDeclaration(node) {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}

/**
* Process the module block.
* @param {TSModuleBlock} node The TSModuleBlock node to visit.
Expand Down Expand Up @@ -583,11 +683,11 @@ class Referencer extends OriginalReferencer {
* @returns {void}
*/
TSImportEqualsDeclaration(node) {
const { name, moduleReference } = node;
if (name && name.type === "Identifier") {
const { id, moduleReference } = node;
if (id && id.type === "Identifier") {
this.currentScope().__define(
name,
new Definition("ImportBinding", name, node, null, null, null)
id,
new Definition("ImportBinding", id, node, null, null, null)
);
}
this.visit(moduleReference);
Expand Down Expand Up @@ -628,6 +728,21 @@ class Referencer extends OriginalReferencer {
decorators.forEach(this.visit, this);
}
}

/**
* Process all child of type nodes
* @param {any} node node to be processed
* @returns {void}
*/
visitTypeNodes(node) {
if (this.typeMode) {
this.visitChildren(node);
} else {
this.typeMode = true;
this.visitChildren(node);
this.typeMode = false;
}
}
}

module.exports = function(ast, parserOptions, extraOptions) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"eslint-scope": "^4.0.0",
"eslint-visitor-keys": "^1.0.0",
"typescript-estree": "5.3.0"
"typescript-estree": "12.0.0"
},
"devDependencies": {
"eslint": "^4.19.1",
Expand All @@ -57,7 +57,7 @@
"npm-license": "0.3.3",
"shelljs": "0.8.2",
"shelljs-nodecli": "0.1.1",
"typescript": "~3.1.1"
"typescript": "~3.2.1"
},
"jest": {
"testEnvironment": "node",
Expand Down
4 changes: 2 additions & 2 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ exports.parseForESLint = function parseForESLint(code, options) {
enter: node => {
switch (node.type) {
// Just for backward compatibility.
case "DeclareFunction":
case "TSDeclareFunction":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
node.type = "TSEmptyBodyDeclareFunction";
}
break;

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/ecma-features/bigIntLiterals/binary.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0b1n;
1 change: 1 addition & 0 deletions tests/fixtures/ecma-features/bigIntLiterals/decimal.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1n;
1 change: 1 addition & 0 deletions tests/fixtures/ecma-features/bigIntLiterals/hex.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x1n;
1 change: 1 addition & 0 deletions tests/fixtures/ecma-features/bigIntLiterals/octal.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0o1n;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/export-as-namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export as namespace a;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/expression-as.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a as number as any) = 42;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/type-alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type foo = string
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-array-type.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = string[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: number extends string ? boolean : null;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-conditional.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: number extends string ? boolean : string;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-indexed.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: T[K];
5 changes: 5 additions & 0 deletions tests/fixtures/scope-analysis/types-infer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends infer U ? U :
T extends Promise<infer U> ? U :
T;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type LinkedList<T> = T & { next: LinkedList<T> };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let map: { -readonly [P in string]-?: number };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let map: { +readonly [P in string]+?: number; };
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-mapped-readonly.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let map: { readonly [P in string]?: number; };
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-mapped.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let map: { [P in string]: number; };
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-nested-types.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = [number, string?, boolean?] | [{}, [number?] | null & boolean[]] & {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = (string | number)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: Array<Array<number>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: Array<number>;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-reference.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: T;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-tuple-empty.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: [];
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-tuple-optional.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: [string, number?, (string | number)?]
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-tuple-rest.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: [string, ...number[]]
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-tuple-type.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = [string, string?]
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-tuple.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: [number, number, number];
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-type-literal.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let obj: { x: number };
2 changes: 2 additions & 0 deletions tests/fixtures/scope-analysis/types-type-operator.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x: keyof T;
let y: unique symbol;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-typeof.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: typeof y.z;
4 changes: 4 additions & 0 deletions tests/fixtures/scope-analysis/types-union-intersection.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let union: number | null | undefined;
let intersection: number & string;
let precedence1: number | string & boolean;
let precedence2: number & string | boolean;
1 change: 1 addition & 0 deletions tests/fixtures/scope-analysis/types-union-type.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = string & number
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"devDependencies": {
"eslint": "4.19.1",
"jest": "23.1.0",
"typescript": "~3.1.1"
"typescript": "~3.2.1"
}
}
}
Loading