Skip to content

Commit

Permalink
refactor: work synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jun 24, 2024
1 parent ad31b14 commit 5497b03
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .babelrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"plugins": [
"add-module-exports"
"add-module-exports",
"babel-plugin-transform-import-meta"
],
"presets": [
[
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"debug": "^4.3.4",
"escape-string-regexp": "^4.0.0",
"esquery": "^1.5.0",
"parse-imports": "^2.0.0",
"parse-imports": "^2.1.0",
"semver": "^7.6.2",
"spdx-expression-parse": "^4.0.0"
"spdx-expression-parse": "^4.0.0",
"synckit": "^0.9.0"
},
"description": "JSDoc linting rules for ESLint.",
"devDependencies": {
Expand Down Expand Up @@ -44,6 +45,7 @@
"@typescript-eslint/parser": "^7.11.0",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-istanbul": "^6.1.1",
"babel-plugin-transform-import-meta": "^2.2.1",
"c8": "^9.1.0",
"camelcase": "^6.3.0",
"chai": "^4.3.10",
Expand Down Expand Up @@ -126,7 +128,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\" && 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\" && cp src/import-worker.mjs dist/import-worker.mjs && 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
48 changes: 32 additions & 16 deletions pnpm-lock.yaml

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

11 changes: 11 additions & 0 deletions src/import-worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { runAsWorker } from 'synckit'

runAsWorker(async (imprt) => {
const { parseImports } = await import('parse-imports');
try {
// ESLint doesn't support async rules
return [...await parseImports(imprt)];
} catch (err) {
return false;
}
})
15 changes: 9 additions & 6 deletions src/rules/checkValues.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { parseImportsSync } from 'parse-imports';
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 iterateJsdoc from '../iterateJsdoc.js';

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

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

try {
// Should technically await non-sync, but ESLint doesn't support async rules;
// thankfully, the Wasm load time is safely fast
parseImportsSync(imprt);
} catch (err) {
const getImports = createSyncFn(pathName);
if (!getImports(imprt)) {
report(
`Bad @import tag`,
null,
Expand Down
19 changes: 11 additions & 8 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { parseImportsSync } from 'parse-imports';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

import { createSyncFn } from 'synckit';
import {
getJSDocComment,
parse as parseType,
Expand All @@ -9,6 +12,9 @@ import iterateJsdoc, {
parseComment,
} 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',
'function', 'symbol',
Expand Down Expand Up @@ -146,16 +152,13 @@ export default iterateJsdoc(({
? `${typePart}${name} ${description}`
: `${typePart}${name}`);

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

return [...imports].flatMap(({importClause}) => {
return imports.flatMap(({importClause}) => {
/* c8 ignore next */
const {default: dflt, named, namespace} = importClause || {};
const types = [];
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"src/**/*.js",
"test/**/*.js",
"typings/gitdown.d.ts",
"typings/babel__eslint-parser.d.ts"
"typings/babel__eslint-parser.d.ts",
"src/import-worker.mjs"
],
"exclude": ["node_modules"]
}

0 comments on commit 5497b03

Please sign in to comment.