diff --git a/.eslintrc.json b/.eslintrc.json deleted file mode 100644 index 851ab42..0000000 --- a/.eslintrc.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "extends": "eslint", - "env": { - "es2020": true - }, - "parserOptions": { - "sourceType": "module", - "ecmaVersion": 2020 - }, - "settings": { - "jsdoc": { - "mode": "typescript", - "preferredTypes": { - "Object": "object", - "object<>": "Object" - } - } - }, - "overrides": [ - { - "files": ["*.cjs"], - "parserOptions": { - "sourceType": "script" - } - } - ], - "ignorePatterns": ["/dist", "/coverage"] -} diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..256d513 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,33 @@ +import eslintConfigESLint from "eslint-config-eslint"; +import globals from "globals"; + +export default [ + { + ignores: [ + "dist/", + "coverage/" + ] + }, + ...eslintConfigESLint, + { + linterOptions: { + reportUnusedDisableDirectives: "error" + }, + settings: { + jsdoc: { + preferredTypes: { + Object: "object", + "object<>": "Object" + } + } + } + }, + { + files: ["tests/lib/**"], + languageOptions: { + globals: { + ...globals.mocha + } + } + } +]; diff --git a/lib/index.js b/lib/index.js index 3622816..b4bb643 100644 --- a/lib/index.js +++ b/lib/index.js @@ -24,6 +24,10 @@ function filterKey(key) { return !KEY_BLACKLIST.has(key) && key[0] !== "_"; } + +/* eslint-disable jsdoc/valid-types -- doesn't allow `readonly`. + TODO: remove eslint-disable when https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/164 is fixed +*/ /** * Get visitor keys of a given node. * @param {object} node The AST node to get keys. @@ -32,18 +36,16 @@ function filterKey(key) { export function getKeys(node) { return Object.keys(node).filter(filterKey); } +/* eslint-enable jsdoc/valid-types -- doesn't allow `readonly` */ -// Disable valid-jsdoc rule because it reports syntax error on the type of @returns. -// eslint-disable-next-line valid-jsdoc /** * Make the union set with `KEYS` and given keys. * @param {VisitorKeys} additionalKeys The additional keys. * @returns {VisitorKeys} The union set. */ export function unionWith(additionalKeys) { - const retv = /** @type {{ - [type: string]: ReadonlyArray - }} */ (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 ccf2b1f..f54a347 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,6 +1,10 @@ +/* eslint-disable jsdoc/valid-types -- doesn't allow `readonly`. + TODO: remove eslint-disable when https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/164 is fixed +*/ /** * @typedef {{ readonly [type: string]: ReadonlyArray }} VisitorKeys */ +/* eslint-enable jsdoc/valid-types -- doesn't allow `readonly string[]`. TODO: check why */ /** * @type {VisitorKeys} diff --git a/package.json b/package.json index 51a5dad..1e27f09 100644 --- a/package.json +++ b/package.json @@ -31,12 +31,11 @@ "@typescript-eslint/parser": "^5.14.0", "c8": "^7.11.0", "chai": "^4.3.6", - "eslint": "^7.29.0", - "eslint-config-eslint": "^7.0.0", - "eslint-plugin-jsdoc": "^35.4.0", - "eslint-plugin-node": "^11.1.0", + "eslint": "^8.56.0", + "eslint-config-eslint": "^9.0.0", "eslint-release": "^3.2.0", "esquery": "^1.4.0", + "globals": "^13.21.0", "json-diff": "^0.7.3", "mocha": "^9.2.1", "opener": "^1.5.2", diff --git a/tests/lib/.eslintrc.json b/tests/lib/.eslintrc.json deleted file mode 100644 index 4668ae7..0000000 --- a/tests/lib/.eslintrc.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "env": { - "mocha": true - } -} diff --git a/tests/lib/commonjs.cjs b/tests/lib/commonjs.cjs index 47b2cf7..ff37729 100644 --- a/tests/lib/commonjs.cjs +++ b/tests/lib/commonjs.cjs @@ -3,7 +3,6 @@ * @author Mike Reinstein */ -// eslint-disable-next-line strict "use strict"; //------------------------------------------------------------------------------ @@ -49,7 +48,7 @@ describe("commonjs", () => { for (const type of Object.keys(additionalKeys)) { for (const key of additionalKeys[type]) { - assert(unionKeys[type].indexOf(key) !== -1, `'${key}' should be included in '${type}'.`); + assert(unionKeys[type].includes(key), `'${key}' should be included in '${type}'.`); } } }); diff --git a/tests/lib/index.js b/tests/lib/index.js index c7497eb..f3f0ccc 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -42,7 +42,7 @@ describe("eslint-visitor-keys", () => { it("should include all keys of lib/visitor-keys.js", () => { for (const type of Object.keys(keys)) { for (const key of keys[type]) { - assert(unionKeys[type].indexOf(key) !== -1, `'${key}' should be included in '${type}'.`); + assert(unionKeys[type].includes(key), `'${key}' should be included in '${type}'.`); } } }); @@ -50,7 +50,7 @@ describe("eslint-visitor-keys", () => { it("should include all additional keys", () => { for (const type of Object.keys(additionalKeys)) { for (const key of additionalKeys[type]) { - assert(unionKeys[type].indexOf(key) !== -1, `'${key}' should be included in '${type}'.`); + assert(unionKeys[type].includes(key), `'${key}' should be included in '${type}'.`); } } }); diff --git a/tools/get-keys-from-ts.js b/tools/get-keys-from-ts.js index 4a13b87..0591c9d 100644 --- a/tools/get-keys-from-ts.js +++ b/tools/get-keys-from-ts.js @@ -150,6 +150,7 @@ function alphabetizeKeyInterfaces(initialNodes) { * @param {Node} declNode The TS declaration node * @param {Function} handler The callback * @returns {any[]} Return value of handler + * @throws {Error} If it finds an unknown type parameter. */ function traverseExtends(declNode, handler) { const ret = []; @@ -209,6 +210,7 @@ function traverseProperties(tsDeclarationNode, handler) { * @param {string} code TypeScript declaration file as code to parse. * @param {{supplementaryDeclarations: Node[]}} [options] The options * @returns {VisitorKeysExport} The built visitor keys + * @throws {Error} If it finds an unknown type. */ function getKeysFromTs(code, { @@ -285,6 +287,7 @@ function getKeysFromTs(code, { * @param {string} cfg.property The property name * @param {Node} cfg.tsAnnotation The annotation node * @returns {boolean} Whether has a traverseable type + * @throws {Error} If it finds an unknown type. */ function hasValidType({ property, tsAnnotation }) { const tsPropertyType = tsAnnotation.type; @@ -302,7 +305,6 @@ function getKeysFromTs(code, { // Ok, but not sufficient return false; case "TSUnionType": - // eslint-disable-next-line no-use-before-define -- Circular return tsAnnotation.types.some(annType => hasValidType({ property: "type", tsAnnotation: annType @@ -316,6 +318,7 @@ function getKeysFromTs(code, { * Whether the interface has a valid type ancestor * @param {string} interfaceName The interface to check * @returns {void} + * @throws {Error} If it finds an unknown type. */ function hasValidTypeAncestor(interfaceName) { let decl = findTsInterfaceDeclaration(interfaceName); @@ -515,16 +518,13 @@ function getKeysFromTs(code, { } /** - * @typedef {{tsInterfaceDeclarations: { - * allTsInterfaceDeclarations: { - * Node[], - * keys: KeysStrict - * }, - * exportedTsInterfaceDeclarations: - * Node[], - * keys: KeysStrict + * @typedef {{ + * keys: KeysStrict, + * tsInterfaceDeclarations: { + * allTsInterfaceDeclarations: Node[], + * exportedTsInterfaceDeclarations: Node[] * } - * }}} VisitorKeysExport + * }} VisitorKeysExport */ /**