Skip to content

[Bug]: performance issue about redirect.js.path with @ast-grep/napi matcher #914

@Timeless0911

Description

@Timeless0911

Version

-

Details

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 i
  • pnpm 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: 2228ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions