From caa7feca3faaff0160d3d76fc6e3ed162c79af34 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 6 Feb 2022 18:58:48 +0800 Subject: [PATCH 01/16] feat: bundle JSDoc-built TypeScript declaration file --- lib/index.js | 8 ++++---- lib/visitor-keys.js | 3 +++ package.json | 8 ++++++-- tsconfig.json | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 tsconfig.json diff --git a/lib/index.js b/lib/index.js index 44f73d6..747d024 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,7 +23,7 @@ function filterKey(key) { /** * Get visitor keys of a given node. * @param {Object} node The AST node to get keys. - * @returns {string[]} Visitor keys of the node. + * @returns {readonly string[]} Visitor keys of the node. */ export function getKeys(node) { return Object.keys(node).filter(filterKey); @@ -33,11 +33,11 @@ export function getKeys(node) { // eslint-disable-next-line valid-jsdoc /** * Make the union set with `KEYS` and given keys. - * @param {Object} additionalKeys The additional keys. - * @returns {{ [type: string]: string[] | undefined }} The union set. + * @param {{ readonly [type: string]: ReadonlyArray }} additionalKeys The additional keys. + * @returns {{ readonly [type: string]: ReadonlyArray }} The union set. */ export function unionWith(additionalKeys) { - const retv = Object.assign({}, KEYS); + const retv = /** @type {{ [type: string]: ReadonlyArray }} */ (Object.assign({}, KEYS)); for (const type of Object.keys(additionalKeys)) { if (Object.prototype.hasOwnProperty.call(retv, type)) { diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 4c7be3d..c55d8f3 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,3 +1,6 @@ +/** + * @type {{ readonly [type: string]: ReadonlyArray }} + */ const KEYS = { AssignmentExpression: [ "left", diff --git a/package.json b/package.json index 10ff44a..b3e4e17 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Constants and utilities about visitor keys to traverse AST.", "type": "module", "main": "dist/eslint-visitor-keys.cjs", + "types": "./dist/eslint-visitor.keys.d.ts", "exports": { ".": [ { @@ -15,6 +16,7 @@ "./package.json": "./package.json" }, "files": [ + "dist/eslint-visitor.keys.d.ts", "dist/eslint-visitor-keys.cjs", "lib" ], @@ -30,12 +32,14 @@ "eslint-release": "^3.2.0", "mocha": "^9.0.1", "opener": "^1.5.2", - "rollup": "^2.52.1" + "rollup": "^2.52.1", + "typescript": "^4.5.5" }, "scripts": { - "prepare": "npm run build", + "prepare": "npm run build && npm run tsc", "build": "rollup -c", "lint": "eslint .", + "tsc": "tsc", "test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js", "coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html", "generate-release": "eslint-generate-release", diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2cd1724 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "lib": ["es2020"], + "moduleResolution": "node", + "module": "esnext", + "resolveJsonModule": true, + "allowJs": true, + "checkJs": true, + "noEmit": false, + "declaration": true, + "declarationMap": true, + "emitDeclarationOnly": true, + "strict": true, + "target": "es5", + "outFile": "dist/eslint-visitor-keys.d.ts" + }, + "include": ["lib/**/*.js"], + "exclude": ["node_modules"] +} From 354f7208f3f6a13b5bb1de5e4a70a3bed43479b0 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 6 Feb 2022 19:07:32 +0800 Subject: [PATCH 02/16] refactor: allow undefined keys (but not for keys argument or return value of `unionWith`) --- .eslintrc.json | 9 +++++++++ lib/index.js | 2 +- lib/visitor-keys.js | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7bea33e..851ab42 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,6 +7,15 @@ "sourceType": "module", "ecmaVersion": 2020 }, + "settings": { + "jsdoc": { + "mode": "typescript", + "preferredTypes": { + "Object": "object", + "object<>": "Object" + } + } + }, "overrides": [ { "files": ["*.cjs"], diff --git a/lib/index.js b/lib/index.js index 747d024..93d22dc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,7 +22,7 @@ function filterKey(key) { /** * Get visitor keys of a given node. - * @param {Object} node The AST node to get keys. + * @param {object} node The AST node to get keys. * @returns {readonly string[]} Visitor keys of the node. */ export function getKeys(node) { diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index c55d8f3..9b626dd 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,5 +1,5 @@ /** - * @type {{ readonly [type: string]: ReadonlyArray }} + * @type {{ readonly [type: string]: ReadonlyArray | undefined }} */ const KEYS = { AssignmentExpression: [ From 0a1c5d00312cf68b00313c980aa9e7c98ea2f557 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 6 Feb 2022 23:48:39 +0800 Subject: [PATCH 03/16] fix: `types` and `files` location --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b3e4e17..baa0b9b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Constants and utilities about visitor keys to traverse AST.", "type": "module", "main": "dist/eslint-visitor-keys.cjs", - "types": "./dist/eslint-visitor.keys.d.ts", + "types": "./dist/eslint-visitor-keys.d.ts", "exports": { ".": [ { @@ -16,7 +16,7 @@ "./package.json": "./package.json" }, "files": [ - "dist/eslint-visitor.keys.d.ts", + "dist/eslint-visitor-keys.d.ts", "dist/eslint-visitor-keys.cjs", "lib" ], From 0e06d8f1116c9b4ee6b46133431e4888af638fea Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 7 Feb 2022 00:36:04 +0800 Subject: [PATCH 04/16] fix: resolve "Cannot find module" error by using `outDir` --- package.json | 8 +++++--- tsconfig.json | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index baa0b9b..13b477b 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,11 @@ "description": "Constants and utilities about visitor keys to traverse AST.", "type": "module", "main": "dist/eslint-visitor-keys.cjs", - "types": "./dist/eslint-visitor-keys.d.ts", + "types": "./dist/index.d.ts", "exports": { ".": [ { + "types": "./dist/index.d.ts", "import": "./lib/index.js", "require": "./dist/eslint-visitor-keys.cjs" }, @@ -16,7 +17,8 @@ "./package.json": "./package.json" }, "files": [ - "dist/eslint-visitor-keys.d.ts", + "dist/index.d.ts", + "dist/visitor-keys.d.ts", "dist/eslint-visitor-keys.cjs", "lib" ], @@ -33,7 +35,7 @@ "mocha": "^9.0.1", "opener": "^1.5.2", "rollup": "^2.52.1", - "typescript": "^4.5.5" + "typescript": "^4.6.0-dev.20220206" }, "scripts": { "prepare": "npm run build && npm run tsc", diff --git a/tsconfig.json b/tsconfig.json index 2cd1724..2d79219 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "lib": ["es2020"], - "moduleResolution": "node", + "moduleResolution": "nodenext", "module": "esnext", "resolveJsonModule": true, "allowJs": true, @@ -12,7 +12,7 @@ "emitDeclarationOnly": true, "strict": true, "target": "es5", - "outFile": "dist/eslint-visitor-keys.d.ts" + "outDir": "dist" }, "include": ["lib/**/*.js"], "exclude": ["node_modules"] From e4d83bc780b07e2469dc13710d6d74e84687ba0f Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 8 Feb 2022 13:22:41 +0800 Subject: [PATCH 05/16] refactor: Shorten types with typedefs --- lib/index.js | 11 ++++++++--- lib/visitor-keys.js | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 93d22dc..5222cf1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,11 @@ */ import KEYS from "./visitor-keys.js"; +/** + * @typedef {{ [type: string]: ReadonlyArray }} KeysStrict + * @typedef {{ readonly [type: string]: ReadonlyArray }} KeysStrictReadonly + */ + // List to ignore keys. const KEY_BLACKLIST = new Set([ "parent", @@ -33,11 +38,11 @@ export function getKeys(node) { // eslint-disable-next-line valid-jsdoc /** * Make the union set with `KEYS` and given keys. - * @param {{ readonly [type: string]: ReadonlyArray }} additionalKeys The additional keys. - * @returns {{ readonly [type: string]: ReadonlyArray }} The union set. + * @param {KeysStrictReadonly} additionalKeys The additional keys. + * @returns {KeysStrictReadonly} The union set. */ export function unionWith(additionalKeys) { - const retv = /** @type {{ [type: string]: ReadonlyArray }} */ (Object.assign({}, KEYS)); + const retv = /** @type {KeysStrict} */ (Object.assign({}, KEYS)); for (const type of Object.keys(additionalKeys)) { if (Object.prototype.hasOwnProperty.call(retv, type)) { diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 9b626dd..3c92456 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,5 +1,9 @@ /** - * @type {{ readonly [type: string]: ReadonlyArray | undefined }} + * @typedef {{ readonly [type: string]: ReadonlyArray }} KeysLooseReadonly + */ + +/** + * @type {KeysLooseReadonly} */ const KEYS = { AssignmentExpression: [ From 4283795861fcc0f3036ac001b8bcd07154ddd095 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 8 Feb 2022 13:40:45 +0800 Subject: [PATCH 06/16] refactor: Remove extra `types` --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 13b477b..19f4dbf 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "exports": { ".": [ { - "types": "./dist/index.d.ts", "import": "./lib/index.js", "require": "./dist/eslint-visitor-keys.cjs" }, From ef69df66d5e891b003ae08cba38646020fa95222 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 9 Feb 2022 07:29:51 +0800 Subject: [PATCH 07/16] refactor: revert switch to `nodenext` --- package.json | 2 +- tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 19f4dbf..1fcffca 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "mocha": "^9.0.1", "opener": "^1.5.2", "rollup": "^2.52.1", - "typescript": "^4.6.0-dev.20220206" + "typescript": "^4.5.5" }, "scripts": { "prepare": "npm run build && npm run tsc", diff --git a/tsconfig.json b/tsconfig.json index 2d79219..dc1ccbd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "lib": ["es2020"], - "moduleResolution": "nodenext", + "moduleResolution": "node", "module": "esnext", "resolveJsonModule": true, "allowJs": true, From 33621338ccce8cdc4daad32fdd03bafa1b5618f5 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 9 Feb 2022 07:56:39 +0800 Subject: [PATCH 08/16] refactor: Drop use of `KeysLooseReadonly` --- lib/visitor-keys.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 3c92456..425604e 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,9 +1,9 @@ /** - * @typedef {{ readonly [type: string]: ReadonlyArray }} KeysLooseReadonly + * @typedef {import('./index.js').KeysStrictReadonly} KeysStrictReadonly */ /** - * @type {KeysLooseReadonly} + * @type {KeysStrictReadonly} */ const KEYS = { AssignmentExpression: [ From 03da79c92c366a5d2485c462db4a245f5e6a929a Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 9 Feb 2022 09:33:48 +0800 Subject: [PATCH 09/16] refactor: set target to `es6` --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index dc1ccbd..5afc060 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ "declarationMap": true, "emitDeclarationOnly": true, "strict": true, - "target": "es5", + "target": "es6", "outDir": "dist" }, "include": ["lib/**/*.js"], From 8413d10ef3bd9713de766f2cd8d86926ed3f3fbc Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 10 Feb 2022 19:05:46 +0800 Subject: [PATCH 10/16] chore: move `tsc` to build step --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1fcffca..d6d00f9 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ "typescript": "^4.5.5" }, "scripts": { - "prepare": "npm run build && npm run tsc", - "build": "rollup -c", + "prepare": "npm run build", + "build": "rollup -c && npm run tsc", "lint": "eslint .", "tsc": "tsc", "test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js", From ab6d321f59a9770a80a4d93d8098b6fb56a4eee6 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 9 Feb 2022 18:18:42 +0800 Subject: [PATCH 11/16] test: tsd tests of declaration file --- package.json | 4 +++- test-d/index.test-d.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test-d/index.test-d.ts diff --git a/package.json b/package.json index d6d00f9..1bb9a42 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "mocha": "^9.0.1", "opener": "^1.5.2", "rollup": "^2.52.1", + "tsd": "^0.19.1", "typescript": "^4.5.5" }, "scripts": { @@ -41,7 +42,8 @@ "build": "rollup -c && npm run tsc", "lint": "eslint .", "tsc": "tsc", - "test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js", + "tsd": "tsd", + "test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js && npm run tsd", "coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html", "generate-release": "eslint-generate-release", "generate-alpharelease": "eslint-generate-prerelease alpha", diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts new file mode 100644 index 0000000..821c59f --- /dev/null +++ b/test-d/index.test-d.ts @@ -0,0 +1,45 @@ +import {expectType, expectAssignable} from 'tsd'; + +import { KEYS, getKeys, unionWith, KeysStrict, KeysStrictReadonly } from "../lib/index.js"; + +const assignmentExpression = { + type: "AssignmentExpression", + operator: "=", + left: { + type: "Identifier", + name: "a", + range: [ + 0, + 1 + ] + }, + right: { + type: "Literal", + value: 5, + raw: "5", + range: [ + 4, + 5 + ] + }, + range: [ + 0, + 5 + ] +}; + +expectType<{readonly [type: string]: readonly string[]}>(KEYS); + +expectType(getKeys(assignmentExpression)); + +expectType<{readonly [type: string]: readonly string[]}>(unionWith({ + TestInterface1: ["left", "right"], + TestInterface2: ["expression"] +})); + +expectAssignable({ + TestInterface1: ["left", "right"] +}); +expectAssignable({ + TestInterface1: ["left", "right"] +}); From fe4725ee643e6c7c9db7cddd1238bf02dd7c3e21 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 9 Feb 2022 19:37:34 +0800 Subject: [PATCH 12/16] test: exported type checks --- test-d/index.test-d.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index 821c59f..8ea4d8e 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -1,4 +1,4 @@ -import {expectType, expectAssignable} from 'tsd'; +import { expectType, expectAssignable, expectError } from 'tsd'; import { KEYS, getKeys, unionWith, KeysStrict, KeysStrictReadonly } from "../lib/index.js"; @@ -37,9 +37,33 @@ expectType<{readonly [type: string]: readonly string[]}>(unionWith({ TestInterface2: ["expression"] })); -expectAssignable({ +const keys: { + [type: string]: readonly string[] +} = { TestInterface1: ["left", "right"] -}); -expectAssignable({ +}; + +const readonlyKeys: { + readonly [type: string]: readonly string[] +} = { TestInterface1: ["left", "right"] +}; + +expectAssignable(keys); + +expectAssignable(readonlyKeys); + +expectError(() => { + const erring: KeysStrict = { + TestInterface1: ["left", "right"] + }; + erring.TestInterface1 = "badType"; }); + +// https://github.com/SamVerschueren/tsd/issues/143 +// expectError(() => { +// const erring: KeysStrictReadonly = { +// TestInterface1: ["left", "right"] +// }; +// erring.TestInterface1 = ["badAttemptOverwrite"]; +// }); From d132ea551fdea8d836f50c72b9530451e6e5fd41 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 11 Feb 2022 05:41:03 +0800 Subject: [PATCH 13/16] refactor: rename KeysStrict -> VisitorKeysWritable, and KeysStrictReadonly -> VisitorKeys --- lib/index.js | 10 +++++----- lib/visitor-keys.js | 4 ++-- test-d/index.test-d.ts | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5222cf1..20dbf3f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,8 +5,8 @@ import KEYS from "./visitor-keys.js"; /** - * @typedef {{ [type: string]: ReadonlyArray }} KeysStrict - * @typedef {{ readonly [type: string]: ReadonlyArray }} KeysStrictReadonly + * @typedef {{ [type: string]: ReadonlyArray }} VisitorKeysWritable + * @typedef {{ readonly [type: string]: ReadonlyArray }} VisitorKeys */ // List to ignore keys. @@ -38,11 +38,11 @@ export function getKeys(node) { // eslint-disable-next-line valid-jsdoc /** * Make the union set with `KEYS` and given keys. - * @param {KeysStrictReadonly} additionalKeys The additional keys. - * @returns {KeysStrictReadonly} The union set. + * @param {VisitorKeys} additionalKeys The additional keys. + * @returns {VisitorKeys} The union set. */ export function unionWith(additionalKeys) { - const retv = /** @type {KeysStrict} */ (Object.assign({}, KEYS)); + const retv = /** @type {VisitorKeysWritable} */ (Object.assign({}, KEYS)); for (const type of Object.keys(additionalKeys)) { if (Object.prototype.hasOwnProperty.call(retv, type)) { diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 425604e..d456d64 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,9 +1,9 @@ /** - * @typedef {import('./index.js').KeysStrictReadonly} KeysStrictReadonly + * @typedef {import('./index.js').VisitorKeys} VisitorKeys */ /** - * @type {KeysStrictReadonly} + * @type {VisitorKeys} */ const KEYS = { AssignmentExpression: [ diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index 8ea4d8e..e0a3150 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -1,6 +1,6 @@ import { expectType, expectAssignable, expectError } from 'tsd'; -import { KEYS, getKeys, unionWith, KeysStrict, KeysStrictReadonly } from "../lib/index.js"; +import { KEYS, getKeys, unionWith, VisitorKeysWritable, VisitorKeys } from "../lib/index.js"; const assignmentExpression = { type: "AssignmentExpression", @@ -49,12 +49,12 @@ const readonlyKeys: { TestInterface1: ["left", "right"] }; -expectAssignable(keys); +expectAssignable(keys); -expectAssignable(readonlyKeys); +expectAssignable(readonlyKeys); expectError(() => { - const erring: KeysStrict = { + const erring: VisitorKeysWritable = { TestInterface1: ["left", "right"] }; erring.TestInterface1 = "badType"; @@ -62,7 +62,7 @@ expectError(() => { // https://github.com/SamVerschueren/tsd/issues/143 // expectError(() => { -// const erring: KeysStrictReadonly = { +// const erring: VisitorKeys = { // TestInterface1: ["left", "right"] // }; // erring.TestInterface1 = ["badAttemptOverwrite"]; From bbda5f287f59cb731d6eea2f1593f4ed1405562b Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 11 Feb 2022 09:01:32 +0800 Subject: [PATCH 14/16] test: fix reference to declaration file Co-authored-by: Milos Djermanovic --- test-d/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index e0a3150..1a74a5e 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -1,6 +1,6 @@ import { expectType, expectAssignable, expectError } from 'tsd'; -import { KEYS, getKeys, unionWith, VisitorKeysWritable, VisitorKeys } from "../lib/index.js"; +import { KEYS, getKeys, unionWith, VisitorKeysWritable, VisitorKeys } from "../"; const assignmentExpression = { type: "AssignmentExpression", From 79e9112109144cc43c89c2ed7c3a36f801784e79 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 11 Feb 2022 09:56:19 +0800 Subject: [PATCH 15/16] test: avoid exporting internal `VisitorKeysWritable` type --- lib/index.js | 5 +++-- test-d/index.test-d.ts | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 20dbf3f..83ebb1f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,6 @@ import KEYS from "./visitor-keys.js"; /** - * @typedef {{ [type: string]: ReadonlyArray }} VisitorKeysWritable * @typedef {{ readonly [type: string]: ReadonlyArray }} VisitorKeys */ @@ -42,7 +41,9 @@ export function getKeys(node) { * @returns {VisitorKeys} The union set. */ export function unionWith(additionalKeys) { - const retv = /** @type {VisitorKeysWritable} */ (Object.assign({}, KEYS)); + const retv = /** @type {{ + [type: string]: ReadonlyArray + }} */ (Object.assign({}, KEYS)); for (const type of Object.keys(additionalKeys)) { if (Object.prototype.hasOwnProperty.call(retv, type)) { diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index 1a74a5e..fa45907 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -1,6 +1,8 @@ import { expectType, expectAssignable, expectError } from 'tsd'; -import { KEYS, getKeys, unionWith, VisitorKeysWritable, VisitorKeys } from "../"; +import { KEYS, getKeys, unionWith, VisitorKeys } from "../"; + +type VisitorKeysWritable = { [type: string]: ReadonlyArray }; const assignmentExpression = { type: "AssignmentExpression", From bc07571fb6c0384e1b2b8d271f56bcaf842f83a3 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Fri, 11 Feb 2022 10:35:09 +0800 Subject: [PATCH 16/16] refactor: Remove need for legacy code tests --- test-d/index.test-d.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index fa45907..8ff6936 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -2,8 +2,6 @@ import { expectType, expectAssignable, expectError } from 'tsd'; import { KEYS, getKeys, unionWith, VisitorKeys } from "../"; -type VisitorKeysWritable = { [type: string]: ReadonlyArray }; - const assignmentExpression = { type: "AssignmentExpression", operator: "=", @@ -39,29 +37,14 @@ expectType<{readonly [type: string]: readonly string[]}>(unionWith({ TestInterface2: ["expression"] })); -const keys: { - [type: string]: readonly string[] -} = { - TestInterface1: ["left", "right"] -}; - const readonlyKeys: { readonly [type: string]: readonly string[] } = { TestInterface1: ["left", "right"] }; -expectAssignable(keys); - expectAssignable(readonlyKeys); -expectError(() => { - const erring: VisitorKeysWritable = { - TestInterface1: ["left", "right"] - }; - erring.TestInterface1 = "badType"; -}); - // https://github.com/SamVerschueren/tsd/issues/143 // expectError(() => { // const erring: VisitorKeys = {