eslint-plugin-import-x icon indicating copy to clipboard operation
eslint-plugin-import-x copied to clipboard

Ideas about how to significantly improve performance

Open SukkaW opened this issue 11 months ago • 9 comments
trafficstars

Currently, when eslint-plugin-import-x finds an import, it will resolve the real path of the module (through the eslint import resolvers), read the file, and then parse the module into the AST using the configured parsers (import-x/parser) or the default parser (ruleContext.parserPath). Then eslint-plugin-import-x tried to extract all exports/imports by walking through AST.

Parse any referenced modules are very heavy tasks, resulting eslint-plugin-import-x being slow. But if we use something like es-module-lexer or cjs-module-lexer to scan the imports/exports we skip the entire parsing-into-AST process and significantly improve performance for a few rules.

SukkaW avatar Dec 11 '24 12:12 SukkaW

We have es-module-lexer that works for ESM and cjs-module-lexer for CommonJS. But we still need lexers for JSX / TypeScript / TSX (or even flow). I still want TypeScript to benefit from this.

Oxc says they will have one for ESM and TypeScript/TSX, but they won't support the CJS.

And we also have rs-module-lexer @fz6m.

SukkaW avatar Dec 11 '24 15:12 SukkaW

We have es-module-lexer that works for ESM and cjs-module-lexer for CommonJS. But we still need lexers for JSX / TypeScript / TSX (or even flow). I still want TypeScript to benefit from this.

For Typescript could strip types with https://github.com/nodejs/amaro or the underlying https://swc.rs/docs/references/wasm-typescript and then pass the result to the lexers. Would be interesting to hear about the performance of that approach vs. full AST parsing.

silverwind avatar Dec 11 '24 18:12 silverwind

For Typescript could strip types with https://github.com/nodejs/amaro or the underlying https://swc.rs/docs/references/wasm-typescript and then pass the result to the lexers.

But IMHO it might not work with export =.

SukkaW avatar Dec 12 '24 01:12 SukkaW

For TS, Oxc parser is a possible alternative to es-module-lexer

privatenumber avatar Dec 26 '24 02:12 privatenumber

For TS, Oxc parser is a possible alternative to es-module-lexer

Not supported, yet: https://github.com/oxc-project/oxc/discussions/2608#discussioncomment-11532854

I have already raised this to the oxc team.

SukkaW avatar Dec 27 '24 07:12 SukkaW

https://github.com/guybedford/es-module-lexer/pull/185 is merged and released.

We can try to implement ESM and CJS scan with cjs-module-lexer and es-module-lexer. Currently, there is no good alternative for TypeScript, so TypeScript projects have to fall back to the current AST parsing.

SukkaW avatar Dec 27 '24 13:12 SukkaW

Not so much a "performance improvement" from the plugin. ~~But anyone landing on this issue looking at speeding up their run can consider disabling the namespace rule.~~

  • https://github.com/import-js/eslint-plugin-import/issues/2340

It's not really as useful with TypeScript anyway

Edit: Answer below is what the linked issue is saying as well. I'm not posting it as a fix. It's a (hopefully temporary) workaround with an explanation for users who'd like a linting performance boost by dropping the affected rules. Until a proper solution comes around.

SamuelT-Beslogic avatar Jan 21 '25 20:01 SamuelT-Beslogic

Not so much a "performance improvement" from the plugin. But anyone landing on this issue looking at speeding up their run can consider disabling the namespace rule.

It's not really as useful with TypeScript anyway

Disabling import/namespace just won't help. Any rule can initiate external module parsing, and after parsing the AST will be cached (so the other rules can benefit from this).

It just so happens that import/namespace initiates most external modules to be parsed. If you disable the import/namespace rule, other rules will initiate their parsing tasks and become slower. The total lint time won't reduce by much.

The only escape is what I am proposing here, replacing external module parsing with external module lexing completely.

SukkaW avatar Jan 22 '25 04:01 SukkaW

@SukkaW https://github.com/joomcode/parse-imports-exports supports TypeScript out of box, I found it at https://github.com/gajus/eslint-plugin-jsdoc/pull/1372.

JounQin avatar Apr 24 '25 09:04 JounQin