From 689dae37392d527c64ae83db2a4c3e6b7fecece7 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 24 Jun 2020 17:30:06 -0700 Subject: [PATCH] feat: split visitor keys into their own package (#2230) --- .github/workflows/ci.yml | 12 +++++ .vscode/launch.json | 10 ++++ packages/typescript-estree/package.json | 2 +- packages/typescript-estree/src/index.ts | 4 +- .../typescript-estree/src/simple-traverse.ts | 2 +- .../tests/lib/visitor-keys.ts | 28 ----------- packages/typescript-estree/tsconfig.json | 3 +- packages/visitor-keys/LICENSE | 21 ++++++++ packages/visitor-keys/README.md | 13 +++++ packages/visitor-keys/jest.config.js | 20 ++++++++ packages/visitor-keys/package.json | 48 +++++++++++++++++++ packages/visitor-keys/src/index.ts | 1 + .../src/visitor-keys.ts | 8 +++- .../visitor-keys/tests/visitor-keys.test.ts | 31 ++++++++++++ packages/visitor-keys/tsconfig.build.json | 10 ++++ packages/visitor-keys/tsconfig.json | 8 ++++ tests/integration/docker-compose.yml | 12 +++++ tests/integration/fixtures/eslint-v6/test.sh | 1 + tests/integration/fixtures/markdown/test.sh | 1 + .../test.sh | 1 + .../test.sh | 1 + tests/integration/fixtures/vue-jsx/test.sh | 1 + tests/integration/fixtures/vue-sfc/test.sh | 1 + 23 files changed, 206 insertions(+), 33 deletions(-) delete mode 100644 packages/typescript-estree/tests/lib/visitor-keys.ts create mode 100644 packages/visitor-keys/LICENSE create mode 100644 packages/visitor-keys/README.md create mode 100644 packages/visitor-keys/jest.config.js create mode 100644 packages/visitor-keys/package.json create mode 100644 packages/visitor-keys/src/index.ts rename packages/{typescript-estree => visitor-keys}/src/visitor-keys.ts (96%) create mode 100644 packages/visitor-keys/tests/visitor-keys.test.ts create mode 100644 packages/visitor-keys/tsconfig.build.json create mode 100644 packages/visitor-keys/tsconfig.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e7a0a39c3c1..b60c976543f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,6 +97,12 @@ jobs: env: CI: true + - name: Run unit tests for visitor-keys + run: yarn test + working-directory: packages/visitor-keys + env: + CI: true + - name: Run unit tests for experimental-utils run: yarn test working-directory: packages/experimental-utils @@ -257,6 +263,12 @@ jobs: env: CI: true + - name: Run unit tests for visitor-keys + run: yarn test + working-directory: packages/visitor-keys + env: + CI: true + - name: Run unit tests for experimental-utils run: yarn test working-directory: packages/experimental-utils diff --git a/.vscode/launch.json b/.vscode/launch.json index 6e7e47da1a5..ff1507f8c3a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -30,6 +30,8 @@ "${workspaceFolder}/packages/typescript-estree/dist/index.js", "${workspaceFolder}/packages/types/src/index.ts", "${workspaceFolder}/packages/types/dist/index.js", + "${workspaceFolder}/packages/visitor-keys/src/index.ts", + "${workspaceFolder}/packages/visitor-keys/dist/index.js", ], }, { @@ -58,6 +60,8 @@ "${workspaceFolder}/packages/typescript-estree/dist/index.js", "${workspaceFolder}/packages/types/src/index.ts", "${workspaceFolder}/packages/types/dist/index.js", + "${workspaceFolder}/packages/visitor-keys/src/index.ts", + "${workspaceFolder}/packages/visitor-keys/dist/index.js", ], }, { @@ -86,6 +90,8 @@ "${workspaceFolder}/packages/typescript-estree/dist/index.js", "${workspaceFolder}/packages/types/src/index.ts", "${workspaceFolder}/packages/types/dist/index.js", + "${workspaceFolder}/packages/visitor-keys/src/index.ts", + "${workspaceFolder}/packages/visitor-keys/dist/index.js", ], }, { @@ -114,6 +120,8 @@ "${workspaceFolder}/packages/typescript-estree/dist/index.js", "${workspaceFolder}/packages/types/src/index.ts", "${workspaceFolder}/packages/types/dist/index.js", + "${workspaceFolder}/packages/visitor-keys/src/index.ts", + "${workspaceFolder}/packages/visitor-keys/dist/index.js", ], }, { @@ -142,6 +150,8 @@ "${workspaceFolder}/packages/typescript-estree/dist/index.js", "${workspaceFolder}/packages/types/src/index.ts", "${workspaceFolder}/packages/types/dist/index.js", + "${workspaceFolder}/packages/visitor-keys/src/index.ts", + "${workspaceFolder}/packages/visitor-keys/dist/index.js", ], } ] diff --git a/packages/typescript-estree/package.json b/packages/typescript-estree/package.json index c520548770c..7d0d139337b 100644 --- a/packages/typescript-estree/package.json +++ b/packages/typescript-estree/package.json @@ -41,8 +41,8 @@ }, "dependencies": { "@typescript-eslint/types": "3.4.0", + "@typescript-eslint/visitor-keys": "3.4.0", "debug": "^4.1.1", - "eslint-visitor-keys": "^1.1.0", "glob": "^7.1.6", "is-glob": "^4.0.1", "lodash": "^4.17.15", diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index d7dc009d4b4..88af46a2091 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -6,9 +6,11 @@ export { } from './parser'; export { ParserServices, TSESTreeOptions } from './parser-options'; export { simpleTraverse } from './simple-traverse'; -export { visitorKeys } from './visitor-keys'; export * from './ts-estree'; export { clearCaches } from './create-program/createWatchProgram'; +// re-export for backwards-compat +export { visitorKeys } from '@typescript-eslint/visitor-keys'; + // note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder export const version: string = require('../package.json').version; diff --git a/packages/typescript-estree/src/simple-traverse.ts b/packages/typescript-estree/src/simple-traverse.ts index ab4a07937d0..1a25187ab26 100644 --- a/packages/typescript-estree/src/simple-traverse.ts +++ b/packages/typescript-estree/src/simple-traverse.ts @@ -1,5 +1,5 @@ +import { visitorKeys } from '@typescript-eslint/visitor-keys'; import { TSESTree } from './ts-estree'; -import { visitorKeys } from './visitor-keys'; // eslint-disable-next-line @typescript-eslint/no-explicit-any function isValidNode(x: any): x is TSESTree.Node { diff --git a/packages/typescript-estree/tests/lib/visitor-keys.ts b/packages/typescript-estree/tests/lib/visitor-keys.ts deleted file mode 100644 index aceb5b75eb5..00000000000 --- a/packages/typescript-estree/tests/lib/visitor-keys.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { AST_NODE_TYPES } from '../../src/ts-estree'; -import { visitorKeys } from '../../src/visitor-keys'; - -//------------------------------------------------------------------------------ -// Setup -//------------------------------------------------------------------------------ - -const astTypes = Object.keys(AST_NODE_TYPES); -astTypes.push(AST_NODE_TYPES.TSEmptyBodyFunctionExpression); // node created by parser.ts - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -describe('visitor-keys', () => { - for (const type of astTypes) { - it(`type ${type} should be present in visitor-keys`, () => { - expect(visitorKeys).toHaveProperty(type); - }); - } - - it('check if there is no deprecated TS nodes', () => { - const TSTypes = Object.keys(visitorKeys).filter(type => - type.startsWith('TS'), - ); - expect(astTypes).toEqual(expect.arrayContaining(TSTypes)); - }); -}); diff --git a/packages/typescript-estree/tsconfig.json b/packages/typescript-estree/tsconfig.json index 77267ff8f9c..f17eec80716 100644 --- a/packages/typescript-estree/tsconfig.json +++ b/packages/typescript-estree/tsconfig.json @@ -8,6 +8,7 @@ "exclude": ["tests/fixtures/**/*"], "references": [ { "path": "../shared-fixtures/tsconfig.build.json" }, - { "path": "../types/tsconfig.build.json" } + { "path": "../types/tsconfig.build.json" }, + { "path": "../visitor-keys/tsconfig.build.json" } ] } diff --git a/packages/visitor-keys/LICENSE b/packages/visitor-keys/LICENSE new file mode 100644 index 00000000000..7e7370143b2 --- /dev/null +++ b/packages/visitor-keys/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 TypeScript ESLint and other contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/visitor-keys/README.md b/packages/visitor-keys/README.md new file mode 100644 index 00000000000..839c20c4954 --- /dev/null +++ b/packages/visitor-keys/README.md @@ -0,0 +1,13 @@ +

TypeScript-ESTree Visitor Keys

+ +

Visitor keys used to help traverse the TypeScript-ESTree AST

+ +

+ CI + NPM Version + NPM Downloads +

+ +## Contributing + +[See the contributing guide here](../../CONTRIBUTING.md) diff --git a/packages/visitor-keys/jest.config.js b/packages/visitor-keys/jest.config.js new file mode 100644 index 00000000000..dd66fc204b3 --- /dev/null +++ b/packages/visitor-keys/jest.config.js @@ -0,0 +1,20 @@ +'use strict'; + +// @ts-check +/** @type {import('@jest/types').Config.InitialOptions} */ +module.exports = { + globals: { + 'ts-jest': { + isolatedModules: true, + }, + }, + testEnvironment: 'node', + transform: { + [/^.+\.tsx?$/.source]: 'ts-jest', + }, + testRegex: [/.\/tests\/.+\.test\.ts$/.source], + collectCoverage: false, + collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'], + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], + coverageReporters: ['text-summary', 'lcov'], +}; diff --git a/packages/visitor-keys/package.json b/packages/visitor-keys/package.json new file mode 100644 index 00000000000..a2b27ad8d8a --- /dev/null +++ b/packages/visitor-keys/package.json @@ -0,0 +1,48 @@ +{ + "name": "@typescript-eslint/visitor-keys", + "version": "3.4.0", + "description": "Visitor keys used to help traverse the TypeScript-ESTree AST", + "keywords": [ + "eslint", + "typescript", + "estree" + ], + "engines": { + "node": "^8.10.0 || ^10.13.0 || >=11.10.1" + }, + "files": [ + "dist", + "package.json", + "README.md", + "LICENSE" + ], + "repository": { + "type": "git", + "url": "https://github.com/typescript-eslint/typescript-eslint.git", + "directory": "packages/visitor-keys" + }, + "bugs": { + "url": "https://github.com/typescript-eslint/typescript-eslint/issues" + }, + "license": "MIT", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc -b tsconfig.build.json", + "clean": "tsc -b tsconfig.build.json --clean", + "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "test": "jest --coverage", + "typecheck": "tsc -p tsconfig.json --noEmit" + }, + "dependencies": { + "eslint-visitor-keys": "^1.1.0" + }, + "devDependencies": { + "@typescript-eslint/types": "3.4.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } +} diff --git a/packages/visitor-keys/src/index.ts b/packages/visitor-keys/src/index.ts new file mode 100644 index 00000000000..24571eff0c3 --- /dev/null +++ b/packages/visitor-keys/src/index.ts @@ -0,0 +1 @@ +export { visitorKeys, VisitorKeys } from './visitor-keys'; diff --git a/packages/typescript-estree/src/visitor-keys.ts b/packages/visitor-keys/src/visitor-keys.ts similarity index 96% rename from packages/typescript-estree/src/visitor-keys.ts rename to packages/visitor-keys/src/visitor-keys.ts index bba4a2f378b..66b7db0ea13 100644 --- a/packages/typescript-estree/src/visitor-keys.ts +++ b/packages/visitor-keys/src/visitor-keys.ts @@ -1,6 +1,10 @@ import * as eslintVisitorKeys from 'eslint-visitor-keys'; -export const visitorKeys = eslintVisitorKeys.unionWith({ +interface VisitorKeys { + readonly [type: string]: readonly string[] | undefined; +} + +const visitorKeys: VisitorKeys = eslintVisitorKeys.unionWith({ // Additional estree nodes. Import: [], // ES2020 @@ -125,3 +129,5 @@ export const visitorKeys = eslintVisitorKeys.unionWith({ TSUnknownKeyword: [], TSVoidKeyword: [], }); + +export { visitorKeys, VisitorKeys }; diff --git a/packages/visitor-keys/tests/visitor-keys.test.ts b/packages/visitor-keys/tests/visitor-keys.test.ts new file mode 100644 index 00000000000..a8ee4a78336 --- /dev/null +++ b/packages/visitor-keys/tests/visitor-keys.test.ts @@ -0,0 +1,31 @@ +import { AST_NODE_TYPES } from '@typescript-eslint/types'; +import { visitorKeys } from '../src'; + +const types = new Set(Object.keys(AST_NODE_TYPES)); +const keys = new Set(Object.keys(visitorKeys)); + +describe('Every ast node type should have a visitor key defined', () => { + for (const type of types) { + it(type, () => { + expect(keys.has(type)).toBeTruthy(); + }); + } +}); + +// these keys are defined by the base eslint module, and are not covered by our AST +const IGNORED_KEYS = new Set([ + 'ExperimentalRestProperty', + 'ExperimentalSpreadProperty', + 'JSXNamespacedName', +]); +describe('Every visitor key should have an ast node type defined', () => { + for (const key of keys) { + if (IGNORED_KEYS.has(key)) { + continue; + } + + it(key, () => { + expect(types.has(key)).toBeTruthy(); + }); + } +}); diff --git a/packages/visitor-keys/tsconfig.build.json b/packages/visitor-keys/tsconfig.build.json new file mode 100644 index 00000000000..215a0282df2 --- /dev/null +++ b/packages/visitor-keys/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "composite": true, + "outDir": "./dist", + "rootDir": "./src", + "resolveJsonModule": true + }, + "include": ["src", "typings"] +} diff --git a/packages/visitor-keys/tsconfig.json b/packages/visitor-keys/tsconfig.json new file mode 100644 index 00000000000..9cea515ba6b --- /dev/null +++ b/packages/visitor-keys/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.build.json", + "compilerOptions": { + "composite": false, + "rootDir": "." + }, + "include": ["src", "typings", "tests", "tools"] +} diff --git a/tests/integration/docker-compose.yml b/tests/integration/docker-compose.yml index 5f1a1be49d9..69f2e157f01 100644 --- a/tests/integration/docker-compose.yml +++ b/tests/integration/docker-compose.yml @@ -21,6 +21,8 @@ services: - /usr/eslint-plugin-tslint/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/typescript-and-tslint-plugins-together:/usr/linked @@ -42,6 +44,8 @@ services: - /usr/eslint-plugin/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/vue-sfc:/usr/linked @@ -63,6 +67,8 @@ services: - /usr/eslint-plugin/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/vue-jsx:/usr/linked @@ -84,6 +90,8 @@ services: - /usr/eslint-plugin/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/recommended-does-not-require-program:/usr/linked @@ -105,6 +113,8 @@ services: - /usr/eslint-plugin/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/markdown:/usr/linked @@ -126,5 +136,7 @@ services: - /usr/eslint-plugin/tests - ../../packages/types/:/usr/types - /usr/types/tests + - ../../packages/visitor-keys/:/usr/visitor-keys + - /usr/types/visitor-keys # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/eslint-v6:/usr/linked diff --git a/tests/integration/fixtures/eslint-v6/test.sh b/tests/integration/fixtures/eslint-v6/test.sh index cf39bca322e..9f392c3687d 100755 --- a/tests/integration/fixtures/eslint-v6/test.sh +++ b/tests/integration/fixtures/eslint-v6/test.sh @@ -10,6 +10,7 @@ npm install eslint@6.0.0 # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/markdown/test.sh b/tests/integration/fixtures/markdown/test.sh index 1da9c082f72..77dd83eaa21 100755 --- a/tests/integration/fixtures/markdown/test.sh +++ b/tests/integration/fixtures/markdown/test.sh @@ -9,6 +9,7 @@ npm install # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/recommended-does-not-require-program/test.sh b/tests/integration/fixtures/recommended-does-not-require-program/test.sh index 058e0e4d575..4db9f0c4491 100755 --- a/tests/integration/fixtures/recommended-does-not-require-program/test.sh +++ b/tests/integration/fixtures/recommended-does-not-require-program/test.sh @@ -9,6 +9,7 @@ npm install # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh b/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh index cea5ee07aa8..bb0389abea2 100755 --- a/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh +++ b/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh @@ -9,6 +9,7 @@ npm install # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/vue-jsx/test.sh b/tests/integration/fixtures/vue-jsx/test.sh index 252dfa6e6b1..71932d4e7ae 100755 --- a/tests/integration/fixtures/vue-jsx/test.sh +++ b/tests/integration/fixtures/vue-jsx/test.sh @@ -9,6 +9,7 @@ npm install # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/vue-sfc/test.sh b/tests/integration/fixtures/vue-sfc/test.sh index 252dfa6e6b1..71932d4e7ae 100755 --- a/tests/integration/fixtures/vue-sfc/test.sh +++ b/tests/integration/fixtures/vue-sfc/test.sh @@ -9,6 +9,7 @@ npm install # Use the local volumes for our own packages npm install $(npm pack /usr/types | tail -1) +npm install $(npm pack /usr/visitor-keys | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1)