From 817ff62011e6e6029c19f09d82fe82dbb588678d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sat, 22 Sep 2018 23:58:09 +0100 Subject: [PATCH 1/3] Fix order of node types --- lib/ast-node-types.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/ast-node-types.js b/lib/ast-node-types.js index 155abac..2849573 100644 --- a/lib/ast-node-types.js +++ b/lib/ast-node-types.js @@ -96,6 +96,13 @@ module.exports = { ThisExpression: "ThisExpression", ThrowStatement: "ThrowStatement", TryStatement: "TryStatement", + UnaryExpression: "UnaryExpression", + UpdateExpression: "UpdateExpression", + VariableDeclaration: "VariableDeclaration", + VariableDeclarator: "VariableDeclarator", + WhileStatement: "WhileStatement", + WithStatement: "WithStatement", + YieldExpression: "YieldExpression", /** * TS-prefixed nodes */ @@ -150,12 +157,5 @@ module.exports = { TSTypeReference: "TSTypeReference", TSUnionType: "TSUnionType", TSUndefinedKeyword: "TSUndefinedKeyword", - TSVoidKeyword: "TSVoidKeyword", - UnaryExpression: "UnaryExpression", - UpdateExpression: "UpdateExpression", - VariableDeclaration: "VariableDeclaration", - VariableDeclarator: "VariableDeclarator", - WhileStatement: "WhileStatement", - WithStatement: "WithStatement", - YieldExpression: "YieldExpression" + TSVoidKeyword: "TSVoidKeyword" }; From 1add9265a426d9cc69f317f5b12d77ab4a495b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sat, 22 Sep 2018 23:58:56 +0100 Subject: [PATCH 2/3] Add visitorKeys --- lib/visitor-keys.js | 82 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + parser.js | 9 ++--- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 lib/visitor-keys.js diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js new file mode 100644 index 0000000..510f0d9 --- /dev/null +++ b/lib/visitor-keys.js @@ -0,0 +1,82 @@ +/** + * @fileoverview The visitor keys for the new and updated node types + * @author Michał Sajnóg + * MIT License + */ + +"use strict"; + +const Evk = require("eslint-visitor-keys"); + +module.exports = Evk.unionWith({ + ArrayPattern: ["typeAnnotation"], + ArrowFunctionExpression: ["returnType", "typeParameters"], + AssignmentPattern: ["typeAnnotation"], + CallExpression: ["typeParameters"], + ClassDeclaration: ["superTypeParameters", "typeParameters"], + ClassExpression: ["superTypeParameters", "typeParameters"], + ClassImplements: ["typeParameters"], + ClassProperty: ["typeAnnotation"], + FunctionDeclaration: ["returnType"], + FunctionExpression: ["returnType", "typeParameters"], + Identifier: ["typeAnnotation"], + InterfaceDeclaration: ["typeParameters"], + NewExpression: ["typeParameters"], + ObjectPattern: ["typeAnnotation"], + RestElement: ["typeAnnotation"], + TaggedTemplateExpression: ["typeParameters"], + VariableDeclarator: ["typeParameters"], + + TSAbstractClassProperty: ["typeAnnotation", "key", "value"], + TSAbstractKeyword: [], + TSAbstractMethodDefinition: ["key", "value"], + TSAnyKeyword: [], + TSArrayType: [], + TSAsyncKeyword: [], + TSBooleanKeyword: [], + TSConstructorType: ["typeAnnotation"], + TSConstructSignature: ["typeAnnotation", "typeParameters"], + TSDeclareKeyword: [], + TSEnumDeclaration: ["members"], + TSEnumMember: ["initializer"], + TSExportAssignment: ["expression"], + TSExportKeyword: [], + TSImportType: ["parameter", "qualifier", "typeParameters"], + TSLiteralType: [], + TSIndexSignature: ["typeAnnotation", "index"], + TSInterfaceBody: ["body"], + TSInterfaceDeclaration: ["body", "id", "heritage"], + TSInterfaceHeritage: ["id", "typeParameters"], + TSFunctionType: ["typeAnnotation"], + TSMethodSignature: ["typeAnnotation", "typeParameters", "key", "params"], + TSModuleBlock: ["body"], + TSModuleDeclaration: ["id", "body"], + TSNamespaceFunctionDeclaration: [], + TSNonNullExpression: [], + TSNeverKeyword: [], + TSNullKeyword: [], + TSNumberKeyword: [], + TSObjectKeyword: [], + TSParameterProperty: ["parameter"], + TSPrivateKeyword: [], + TSPropertySignature: ["typeAnnotation", "key", "initializer"], + TSProtectedKeyword: [], + TSPublicKeyword: [], + TSQualifiedName: ["left", "right"], + TSQuestionToken: [], + TSReadonlyKeyword: [], + TSStaticKeyword: [], + TSStringKeyword: [], + TSSymbolKeyword: [], + TSTypeAnnotation: ["typeAnnotation"], + TSTypeLiteral: [], + TSTypeOperator: ["typeAnnotation"], + TSTypeParameter: ["constraint", "default"], + TSTypeParameterDeclaration: ["params"], + TSTypeParameterInstantiation: ["params"], + TSTypePredicate: ["typeAnnotation", "parameterName"], + TSTypeReference: ["typeName", "typeParameters"], + TSUnionType: [], + TSUndefinedKeyword: [], + TSVoidKeyword: [] +}); diff --git a/package.json b/package.json index 0a2d52a..e635264 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "betarelease": "eslint-prerelease beta" }, "dependencies": { + "eslint-visitor-keys": "^1.0.0", "lodash.unescape": "4.0.1", "semver": "5.5.0" }, diff --git a/parser.js b/parser.js index da4e0c5..cd3a237 100644 --- a/parser.js +++ b/parser.js @@ -8,10 +8,11 @@ "use strict"; -const astNodeTypes = require("./lib/ast-node-types"), - ts = require("typescript"), +const ts = require("typescript"), + semver = require("semver"), + astNodeTypes = require("./lib/ast-node-types"), convert = require("./lib/ast-converter"), - semver = require("semver"); + visitorKeys = require("./lib/visitor-keys"); const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.typescript; const ACTIVE_TYPESCRIPT_VERSION = ts.version; @@ -191,7 +192,7 @@ exports.parse = function parse(code, options) { exports.parseForESLint = function parseForESLint(code, options) { const ast = generateAST(code, options, { isParseForESLint: true }); - return { ast }; + return { ast, visitorKeys }; }; // Deep copy. From cd810f37578821fa10858f18a9596ca16476e30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sun, 23 Sep 2018 18:29:48 +0100 Subject: [PATCH 3/3] Update visitor keys --- lib/ast-node-types.js | 1 + lib/visitor-keys.js | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/ast-node-types.js b/lib/ast-node-types.js index 2849573..4306e8a 100644 --- a/lib/ast-node-types.js +++ b/lib/ast-node-types.js @@ -106,6 +106,7 @@ module.exports = { /** * TS-prefixed nodes */ + TSAbstractClassDeclaration: "TSAbstractClassDeclaration", TSAbstractClassProperty: "TSAbstractClassProperty", TSAbstractKeyword: "TSAbstractKeyword", TSAbstractMethodDefinition: "TSAbstractMethodDefinition", diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 510f0d9..18cf592 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -17,24 +17,29 @@ module.exports = Evk.unionWith({ ClassExpression: ["superTypeParameters", "typeParameters"], ClassImplements: ["typeParameters"], ClassProperty: ["typeAnnotation"], - FunctionDeclaration: ["returnType"], + FunctionDeclaration: ["returnType", "typeParameters"], FunctionExpression: ["returnType", "typeParameters"], Identifier: ["typeAnnotation"], InterfaceDeclaration: ["typeParameters"], NewExpression: ["typeParameters"], ObjectPattern: ["typeAnnotation"], - RestElement: ["typeAnnotation"], + /** + * According to https://github.com/estree/estree/blob/master/extensions/type-annotations.md + * RestElement should have "typeAnnotation", but has not. Annotation is added on the "parameter" node + */ + RestElement: [], TaggedTemplateExpression: ["typeParameters"], VariableDeclarator: ["typeParameters"], TSAbstractClassProperty: ["typeAnnotation", "key", "value"], + TSAbstractClassDeclaration: ["id", "body", "superClass", "implements"], TSAbstractKeyword: [], TSAbstractMethodDefinition: ["key", "value"], TSAnyKeyword: [], - TSArrayType: [], + TSArrayType: ["elementType"], TSAsyncKeyword: [], TSBooleanKeyword: [], - TSConstructorType: ["typeAnnotation"], + TSConstructorType: ["typeAnnotation", "parameters"], TSConstructSignature: ["typeAnnotation", "typeParameters"], TSDeclareKeyword: [], TSEnumDeclaration: ["members"], @@ -42,7 +47,7 @@ module.exports = Evk.unionWith({ TSExportAssignment: ["expression"], TSExportKeyword: [], TSImportType: ["parameter", "qualifier", "typeParameters"], - TSLiteralType: [], + TSLiteralType: ["literal"], TSIndexSignature: ["typeAnnotation", "index"], TSInterfaceBody: ["body"], TSInterfaceDeclaration: ["body", "id", "heritage"], @@ -52,7 +57,7 @@ module.exports = Evk.unionWith({ TSModuleBlock: ["body"], TSModuleDeclaration: ["id", "body"], TSNamespaceFunctionDeclaration: [], - TSNonNullExpression: [], + TSNonNullExpression: ["expression"], TSNeverKeyword: [], TSNullKeyword: [], TSNumberKeyword: [], @@ -69,14 +74,14 @@ module.exports = Evk.unionWith({ TSStringKeyword: [], TSSymbolKeyword: [], TSTypeAnnotation: ["typeAnnotation"], - TSTypeLiteral: [], + TSTypeLiteral: ["members"], TSTypeOperator: ["typeAnnotation"], TSTypeParameter: ["constraint", "default"], TSTypeParameterDeclaration: ["params"], TSTypeParameterInstantiation: ["params"], TSTypePredicate: ["typeAnnotation", "parameterName"], TSTypeReference: ["typeName", "typeParameters"], - TSUnionType: [], + TSUnionType: ["types"], TSUndefinedKeyword: [], TSVoidKeyword: [] });