Skip to content

Commit 6f4e0f8

Browse files
perf: use more performant ast-grep rule (#916)
1 parent 1626d50 commit 6f4e0f8

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

packages/plugin-dts/src/utils.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,41 +207,45 @@ export async function redirectDtsImports(
207207
const sgNode = (await parseAsync('typescript', content)).root();
208208
const matcher: NapiConfig = {
209209
rule: {
210-
kind: 'string_fragment',
211210
any: [
212211
{
213-
inside: {
214-
stopBy: 'end',
215-
kind: 'import_statement',
212+
kind: 'import_statement',
213+
has: {
216214
field: 'source',
215+
has: {
216+
pattern: '$IMP',
217+
kind: 'string_fragment',
218+
},
217219
},
218220
},
219221
{
220-
inside: {
221-
stopBy: 'end',
222-
kind: 'export_statement',
222+
kind: 'export_statement',
223+
has: {
223224
field: 'source',
225+
has: {
226+
pattern: '$IMP',
227+
kind: 'string_fragment',
228+
},
224229
},
225230
},
226231
{
227-
inside: {
228-
kind: 'string',
229-
inside: {
230-
kind: 'arguments',
231-
inside: {
232-
kind: 'call_expression',
233-
has: {
234-
field: 'function',
235-
regex: '^(import|require)$',
236-
},
232+
any: [{ pattern: 'require($A)' }, { pattern: 'import($A)' }],
233+
has: {
234+
field: 'arguments',
235+
has: {
236+
has: {
237+
pattern: '$IMP',
238+
kind: 'string_fragment',
237239
},
238240
},
239241
},
240242
},
241243
],
242244
},
243245
};
244-
const matchModule = sgNode.findAll(matcher).map((matchNode) => {
246+
const matchModule = sgNode.findAll(matcher).map((match) => {
247+
// we can guarantee $IMP is matched given the rule
248+
const matchNode = match.getMatch('IMP')!;
245249
return {
246250
n: matchNode.text(),
247251
s: matchNode.range().start.index,

0 commit comments

Comments
 (0)