Skip to content
This repository was archived by the owner on Aug 20, 2024. It is now read-only.

Commit 7f10327

Browse files
feat: Bundle JSDoc-built TypeScript declaration file (#34)
* feat: bundle JSDoc-built TypeScript declaration file * refactor: allow undefined keys (but not for keys argument or return value of `unionWith`) * fix: `types` and `files` location * fix: resolve "Cannot find module" error by using `outDir` * refactor: Shorten types with typedefs * refactor: Remove extra `types` * refactor: revert switch to `nodenext` * refactor: Drop use of `KeysLooseReadonly` * refactor: set target to `es6` * chore: move `tsc` to build step * test: tsd tests of declaration file * test: exported type checks * refactor: rename KeysStrict -> VisitorKeysWritable, and KeysStrictReadonly -> VisitorKeys * test: fix reference to declaration file Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * test: avoid exporting internal `VisitorKeysWritable` type * refactor: Remove need for legacy code tests Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 67c0a8b commit 7f10327

File tree

6 files changed

+110
-8
lines changed

6 files changed

+110
-8
lines changed

.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
"sourceType": "module",
88
"ecmaVersion": 2020
99
},
10+
"settings": {
11+
"jsdoc": {
12+
"mode": "typescript",
13+
"preferredTypes": {
14+
"Object": "object",
15+
"object<>": "Object"
16+
}
17+
}
18+
},
1019
"overrides": [
1120
{
1221
"files": ["*.cjs"],

lib/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*/
55
import KEYS from "./visitor-keys.js";
66

7+
/**
8+
* @typedef {{ readonly [type: string]: ReadonlyArray<string> }} VisitorKeys
9+
*/
10+
711
// List to ignore keys.
812
const KEY_BLACKLIST = new Set([
913
"parent",
@@ -22,8 +26,8 @@ function filterKey(key) {
2226

2327
/**
2428
* Get visitor keys of a given node.
25-
* @param {Object} node The AST node to get keys.
26-
* @returns {string[]} Visitor keys of the node.
29+
* @param {object} node The AST node to get keys.
30+
* @returns {readonly string[]} Visitor keys of the node.
2731
*/
2832
export function getKeys(node) {
2933
return Object.keys(node).filter(filterKey);
@@ -33,11 +37,13 @@ export function getKeys(node) {
3337
// eslint-disable-next-line valid-jsdoc
3438
/**
3539
* Make the union set with `KEYS` and given keys.
36-
* @param {Object} additionalKeys The additional keys.
37-
* @returns {{ [type: string]: string[] | undefined }} The union set.
40+
* @param {VisitorKeys} additionalKeys The additional keys.
41+
* @returns {VisitorKeys} The union set.
3842
*/
3943
export function unionWith(additionalKeys) {
40-
const retv = Object.assign({}, KEYS);
44+
const retv = /** @type {{
45+
[type: string]: ReadonlyArray<string>
46+
}} */ (Object.assign({}, KEYS));
4147

4248
for (const type of Object.keys(additionalKeys)) {
4349
if (Object.prototype.hasOwnProperty.call(retv, type)) {

lib/visitor-keys.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @typedef {import('./index.js').VisitorKeys} VisitorKeys
3+
*/
4+
5+
/**
6+
* @type {VisitorKeys}
7+
*/
18
const KEYS = {
29
AssignmentExpression: [
310
"left",

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Constants and utilities about visitor keys to traverse AST.",
55
"type": "module",
66
"main": "dist/eslint-visitor-keys.cjs",
7+
"types": "./dist/index.d.ts",
78
"exports": {
89
".": [
910
{
@@ -15,6 +16,8 @@
1516
"./package.json": "./package.json"
1617
},
1718
"files": [
19+
"dist/index.d.ts",
20+
"dist/visitor-keys.d.ts",
1821
"dist/eslint-visitor-keys.cjs",
1922
"lib"
2023
],
@@ -30,13 +33,17 @@
3033
"eslint-release": "^3.2.0",
3134
"mocha": "^9.0.1",
3235
"opener": "^1.5.2",
33-
"rollup": "^2.52.1"
36+
"rollup": "^2.52.1",
37+
"tsd": "^0.19.1",
38+
"typescript": "^4.5.5"
3439
},
3540
"scripts": {
3641
"prepare": "npm run build",
37-
"build": "rollup -c",
42+
"build": "rollup -c && npm run tsc",
3843
"lint": "eslint .",
39-
"test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js",
44+
"tsc": "tsc",
45+
"tsd": "tsd",
46+
"test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js && npm run tsd",
4047
"coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html",
4148
"generate-release": "eslint-generate-release",
4249
"generate-alpharelease": "eslint-generate-prerelease alpha",

test-d/index.test-d.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expectType, expectAssignable, expectError } from 'tsd';
2+
3+
import { KEYS, getKeys, unionWith, VisitorKeys } from "../";
4+
5+
const assignmentExpression = {
6+
type: "AssignmentExpression",
7+
operator: "=",
8+
left: {
9+
type: "Identifier",
10+
name: "a",
11+
range: [
12+
0,
13+
1
14+
]
15+
},
16+
right: {
17+
type: "Literal",
18+
value: 5,
19+
raw: "5",
20+
range: [
21+
4,
22+
5
23+
]
24+
},
25+
range: [
26+
0,
27+
5
28+
]
29+
};
30+
31+
expectType<{readonly [type: string]: readonly string[]}>(KEYS);
32+
33+
expectType<readonly string[]>(getKeys(assignmentExpression));
34+
35+
expectType<{readonly [type: string]: readonly string[]}>(unionWith({
36+
TestInterface1: ["left", "right"],
37+
TestInterface2: ["expression"]
38+
}));
39+
40+
const readonlyKeys: {
41+
readonly [type: string]: readonly string[]
42+
} = {
43+
TestInterface1: ["left", "right"]
44+
};
45+
46+
expectAssignable<VisitorKeys>(readonlyKeys);
47+
48+
// https://github.com/SamVerschueren/tsd/issues/143
49+
// expectError(() => {
50+
// const erring: VisitorKeys = {
51+
// TestInterface1: ["left", "right"]
52+
// };
53+
// erring.TestInterface1 = ["badAttemptOverwrite"];
54+
// });

tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2020"],
4+
"moduleResolution": "node",
5+
"module": "esnext",
6+
"resolveJsonModule": true,
7+
"allowJs": true,
8+
"checkJs": true,
9+
"noEmit": false,
10+
"declaration": true,
11+
"declarationMap": true,
12+
"emitDeclarationOnly": true,
13+
"strict": true,
14+
"target": "es6",
15+
"outDir": "dist"
16+
},
17+
"include": ["lib/**/*.js"],
18+
"exclude": ["node_modules"]
19+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy