-
-
Notifications
You must be signed in to change notification settings - Fork 64
Closed
Copy link
Description
Version
-Details
rslib/packages/plugin-dts/src/utils.ts
Lines 205 to 250 in b7c29fc
| const content = await fsP.readFile(dtsFile, 'utf-8'); | |
| const code = new MagicString(content); | |
| const sgNode = (await parseAsync('typescript', content)).root(); | |
| const matcher: NapiConfig = { | |
| rule: { | |
| kind: 'string_fragment', | |
| any: [ | |
| { | |
| inside: { | |
| stopBy: 'end', | |
| kind: 'import_statement', | |
| field: 'source', | |
| }, | |
| }, | |
| { | |
| inside: { | |
| stopBy: 'end', | |
| kind: 'export_statement', | |
| field: 'source', | |
| }, | |
| }, | |
| { | |
| inside: { | |
| kind: 'string', | |
| inside: { | |
| kind: 'arguments', | |
| inside: { | |
| kind: 'call_expression', | |
| has: { | |
| field: 'function', | |
| regex: '^(import|require)$', | |
| }, | |
| }, | |
| }, | |
| }, | |
| }, | |
| ], | |
| }, | |
| }; | |
| const matchModule = sgNode.findAll(matcher).map((matchNode) => { | |
| return { | |
| n: matchNode.text(), | |
| s: matchNode.range().start.index, | |
| e: matchNode.range().end.index, | |
| }; | |
| }); |
When we have some large or complex dts files, finding import or require statement with @ast-grep/napi matcher become slow.
Here is a simple demo to show this: https://github.com/Timeless0911/ast-grep-matcher-performance.
Reproduce link
https://github.com/Timeless0911/ast-grep-matcher-performance
Reproduce Steps
pnpm ipnpm build
The result is as below:
File: ./file/less-import-no-format.d.ts, Time: 228ms
File: ./file/less-import-format.d.ts, Time: 187ms
File: ./file/much-import-no-format.d.ts, Time: 7119ms
File: ./file/much-import-format.d.ts, Time: 2228msReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels