Skip to content

Commit 5497b03

Browse files
committed
refactor: work synchronously
1 parent ad31b14 commit 5497b03

File tree

7 files changed

+72
-35
lines changed

7 files changed

+72
-35
lines changed

.babelrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"plugins": [
3-
"add-module-exports"
3+
"add-module-exports",
4+
"babel-plugin-transform-import-meta"
45
],
56
"presets": [
67
[

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
"debug": "^4.3.4",
1212
"escape-string-regexp": "^4.0.0",
1313
"esquery": "^1.5.0",
14-
"parse-imports": "^2.0.0",
14+
"parse-imports": "^2.1.0",
1515
"semver": "^7.6.2",
16-
"spdx-expression-parse": "^4.0.0"
16+
"spdx-expression-parse": "^4.0.0",
17+
"synckit": "^0.9.0"
1718
},
1819
"description": "JSDoc linting rules for ESLint.",
1920
"devDependencies": {
@@ -44,6 +45,7 @@
4445
"@typescript-eslint/parser": "^7.11.0",
4546
"babel-plugin-add-module-exports": "^1.0.4",
4647
"babel-plugin-istanbul": "^6.1.1",
48+
"babel-plugin-transform-import-meta": "^2.2.1",
4749
"c8": "^9.1.0",
4850
"camelcase": "^6.3.0",
4951
"chai": "^4.3.10",
@@ -126,7 +128,7 @@
126128
"scripts": {
127129
"tsc": "tsc",
128130
"tsc-build": "tsc -p tsconfig-prod.json",
129-
"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",
131+
"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",
130132
"check-docs": "babel-node ./src/bin/generateDocs.js --check",
131133
"create-docs": "npm run create-options && babel-node ./src/bin/generateDocs.js",
132134
"create-rule": "babel-node ./src/bin/generateRule.js",

pnpm-lock.yaml

Lines changed: 32 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/import-worker.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { runAsWorker } from 'synckit'
2+
3+
runAsWorker(async (imprt) => {
4+
const { parseImports } = await import('parse-imports');
5+
try {
6+
// ESLint doesn't support async rules
7+
return [...await parseImports(imprt)];
8+
} catch (err) {
9+
return false;
10+
}
11+
})

src/rules/checkValues.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { parseImportsSync } from 'parse-imports';
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import { createSyncFn } from 'synckit';
25
import semver from 'semver';
36
import spdxExpressionParse from 'spdx-expression-parse';
47
import iterateJsdoc from '../iterateJsdoc.js';
58

9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
const pathName = join(__dirname, '../import-worker.mjs');
11+
612
const allowedKinds = new Set([
713
'class',
814
'constant',
@@ -169,11 +175,8 @@ export default iterateJsdoc(({
169175
? `${typePart}${name} ${description}`
170176
: `${typePart}${name}`);
171177

172-
try {
173-
// Should technically await non-sync, but ESLint doesn't support async rules;
174-
// thankfully, the Wasm load time is safely fast
175-
parseImportsSync(imprt);
176-
} catch (err) {
178+
const getImports = createSyncFn(pathName);
179+
if (!getImports(imprt)) {
177180
report(
178181
`Bad @import tag`,
179182
null,

src/rules/noUndefinedTypes.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { parseImportsSync } from 'parse-imports';
1+
import { dirname, join } from 'path';
2+
import { fileURLToPath } from 'url';
3+
4+
import { createSyncFn } from 'synckit';
25
import {
36
getJSDocComment,
47
parse as parseType,
@@ -9,6 +12,9 @@ import iterateJsdoc, {
912
parseComment,
1013
} from '../iterateJsdoc.js';
1114

15+
const __dirname = dirname(fileURLToPath(import.meta.url));
16+
const pathName = join(__dirname, '../import-worker.mjs');
17+
1218
const extraTypes = [
1319
'null', 'undefined', 'void', 'string', 'boolean', 'object',
1420
'function', 'symbol',
@@ -146,16 +152,13 @@ export default iterateJsdoc(({
146152
? `${typePart}${name} ${description}`
147153
: `${typePart}${name}`);
148154

149-
let imports;
150-
try {
151-
// Should technically await non-sync, but ESLint doesn't support async rules;
152-
// thankfully, the Wasm load time is safely fast
153-
imports = parseImportsSync(imprt);
154-
} catch (err) {
155+
const getImports = createSyncFn(pathName);
156+
const imports = /** @type {import('parse-imports').Import[]} */ (getImports(imprt));
157+
if (!imports) {
155158
return null;
156159
}
157160

158-
return [...imports].flatMap(({importClause}) => {
161+
return imports.flatMap(({importClause}) => {
159162
/* c8 ignore next */
160163
const {default: dflt, named, namespace} = importClause || {};
161164
const types = [];

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"src/**/*.js",
1818
"test/**/*.js",
1919
"typings/gitdown.d.ts",
20-
"typings/babel__eslint-parser.d.ts"
20+
"typings/babel__eslint-parser.d.ts",
21+
"src/import-worker.mjs"
2122
],
2223
"exclude": ["node_modules"]
2324
}

0 commit comments

Comments
 (0)