Skip to content

fix(check-values, no-undefined-types): avoid need for worker #1372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ quux();
/**
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
*/
/**
* @import LinterDef2, * as LinterDef3 from "eslint"
*/
/**
* @import { Linter } from "eslint"
*/
Expand All @@ -838,6 +841,12 @@ quux();
/**
* @type {LinterDef}
*/
/**
* @type {LinterDef2}
*/
/**
* @type {LinterDef3}
*/
/**
* @type {Something}
*/
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
"escape-string-regexp": "^4.0.0",
"espree": "^10.1.0",
"esquery": "^1.6.0",
"parse-imports": "^2.1.1",
"parse-imports-exports": "^0.2.4",
"semver": "^7.6.3",
"spdx-expression-parse": "^4.0.0",
"synckit": "^0.9.1"
"spdx-expression-parse": "^4.0.0"
},
"description": "JSDoc linting rules for ESLint.",
"devDependencies": {
Expand Down Expand Up @@ -146,7 +145,7 @@
"scripts": {
"tsc": "tsc",
"tsc-build": "tsc -p tsconfig-prod.json",
"build": "rimraf ./dist && cross-env NODE_ENV=production babel ./src --out-file-extension .cjs --out-dir ./dist --copy-files --source-maps --ignore ./src/bin/*.js --no-copy-ignored && replace 'require\\(\"\\.(.*?)\\.[^.]*?\"\\)' 'require(\".$1.cjs\")' 'dist' -r --include=\"*.cjs\" && cp src/import-worker.mjs dist/import-worker.mjs && pnpm tsc-build",
"build": "rimraf ./dist && cross-env NODE_ENV=production babel ./src --out-file-extension .cjs --out-dir ./dist --copy-files --source-maps --ignore ./src/bin/*.js --no-copy-ignored && replace 'require\\(\"\\.(.*?)\\.[^.]*?\"\\)' 'require(\".$1.cjs\")' 'dist' -r --include=\"*.cjs\" && pnpm tsc-build",
"check-docs": "babel-node ./src/bin/generateDocs.js --check",
"create-docs": "npm run create-options && babel-node ./src/bin/generateDocs.js",
"create-rule": "babel-node ./src/bin/generateRule.js",
Expand Down
21 changes: 15 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignoredBuiltDependencies:
- core-js-pure
11 changes: 0 additions & 11 deletions src/import-worker.mjs

This file was deleted.

8 changes: 4 additions & 4 deletions src/rules/checkValues.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import { createSyncFn } from 'synckit';
import semver from 'semver';
import spdxExpressionParse from 'spdx-expression-parse';
import {parseImportsExports} from 'parse-imports-exports';
import iterateJsdoc from '../iterateJsdoc.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const pathName = join(__dirname, '../import-worker.mjs');

const allowedKinds = new Set([
'class',
Expand Down Expand Up @@ -175,8 +174,9 @@ export default iterateJsdoc(({
? `${typePart}${name} ${description}`
: `${typePart}${name}`);

const getImports = createSyncFn(pathName);
if (!getImports(imprt)) {
const importsExports = parseImportsExports(imprt);

if (importsExports.errors) {
report(
`Bad @import tag`,
null,
Expand Down
41 changes: 20 additions & 21 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

import { createSyncFn } from 'synckit';
import {parseImportsExports} from 'parse-imports-exports';
import {
getJSDocComment,
parse as parseType,
Expand All @@ -13,7 +13,6 @@ import iterateJsdoc, {
} from '../iterateJsdoc.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const pathName = join(__dirname, '../import-worker.mjs');

const extraTypes = [
'null', 'undefined', 'void', 'string', 'boolean', 'object',
Expand Down Expand Up @@ -152,30 +151,30 @@ export default iterateJsdoc(({
? `${typePart}${name} ${description}`
: `${typePart}${name}`);

const getImports = createSyncFn(pathName);
const imports = /** @type {import('parse-imports').Import[]} */ (getImports(imprt));
if (!imports) {
return null;
}
const importsExports = parseImportsExports(imprt);

return imports.flatMap(({importClause}) => {
/* c8 ignore next */
const {default: dflt, named, namespace} = importClause || {};
const types = [];
if (dflt) {
types.push(dflt);
const types = [];
const namedImports = Object.values(importsExports.namedImports || {})[0]?.[0];
if (namedImports) {
if (namedImports.default) {
types.push(namedImports.default);
}
if (namespace) {
types.push(namespace);
if (namedImports.names) {
types.push(...Object.keys(namedImports.names));
}
if (named) {
for (const {binding} of named) {
types.push(binding);
}
}

const namespaceImports = Object.values(importsExports.namespaceImports || {})[0]?.[0];
if (namespaceImports) {
if (namespaceImports.namespace) {
types.push(namespaceImports.namespace);
}
if (namespaceImports.default) {
types.push(namespaceImports.default);
}
}

return types;
});
return types;
}).filter(Boolean)) : [];

const ancestorNodes = [];
Expand Down
9 changes: 9 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,9 @@ export default /** @type {import('../index.js').TestCases} */ ({
/**
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
*/
/**
* @import LinterDef2, * as LinterDef3 from "eslint"
*/
/**
* @import { Linter } from "eslint"
*/
Expand All @@ -1518,6 +1521,12 @@ export default /** @type {import('../index.js').TestCases} */ ({
/**
* @type {LinterDef}
*/
/**
* @type {LinterDef2}
*/
/**
* @type {LinterDef3}
*/
/**
* @type {Something}
*/
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"src/**/*.js",
"test/**/*.js",
"typings/gitdown.d.ts",
"typings/babel__eslint-parser.d.ts",
"src/import-worker.mjs"
"typings/babel__eslint-parser.d.ts"
],
"exclude": ["node_modules"]
}