Skip to content

Commit 7dea20c

Browse files
committed
feat!: execute tagged template literals immediately
BREAKING CHANGE: whereas before you had to do`` astx.find`$a + $b`() ``, now you can omit the parentheses: `` astx.find`$a + $b` ``. The same goes for `.replace`, `.closest`, `.destruct`, `.findImports`, `.addImports`, `.removeImports`, and `.replaceImport`.
1 parent c4ead3c commit 7dea20c

File tree

53 files changed

+314
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+314
-289
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ But this is just the beginning; `astx` patterns can be much more complex and pow
135135
an intuitive [API](#api) you can use:
136136

137137
```ts
138-
for (const match of astx.find`rmdir($path, $force)`()) {
138+
for (const match of astx.find`rmdir($path, $force)`) {
139139
const { $path, $force } = match
140140
// do stuff with $path.node, $path.code, etc...
141141
}
@@ -536,23 +536,23 @@ For example if you do `astx.find('foo($$args)').find('$a + $b')`, the second `fi
536536

537537
You can call `.find` as a method or tagged template literal:
538538

539-
- `` .find`pattern`(options?: FindOptions) ``
539+
- `` .find`pattern` ``
540540
- `.find(pattern: string | string[] | Node | Node[] | NodePath | NodePath[] | ((wrapper: Astx) => boolean), options?: FindOptions)`
541541

542542
If you give the pattern as a string, it must be a valid expression or statement(s). Otherwise it should be valid
543543
AST node(s) you already parsed or constructed.
544544
You can interpolate strings, AST nodes, arrays of AST nodes, and `Astx` instances in the tagged template literal.
545545

546-
For example you could do `` astx.find`${t.identifier('foo')} + 3`() ``.
546+
For example you could do `` astx.find`${t.identifier('foo')} + 3` ``.
547547

548548
Or you could match multiple statements by doing
549549

550550
```ts
551-
astx.findStatements`
551+
astx.find`
552552
const $a = $b;
553553
$$c;
554554
const $d = $a + $e;
555-
`()
555+
`
556556
```
557557

558558
This would match (for example) the statements `const foo = 1; const bar = foo + 5;`, with any number of statements between them.
@@ -582,7 +582,7 @@ Finds and replaces matches for the given pattern within `root`.
582582

583583
There are several different ways you can call `.replace`. You can call `.find` in any way described above.
584584

585-
- `` .find(...).replace`replacement`() ``
585+
- `` .find(...).replace`replacement` ``
586586
- `.find(...).replace(replacement: string | string | Node | Node[])`
587587
- `.find(...).replace(replacement: (match: Astx, parse: ParsePattern) => string)`
588588
- `.find(...).replace(replacement: (match: Astx, parse: ParsePattern) => Node | Node[])`
@@ -621,7 +621,7 @@ Gets an `Astx` instance focused on the capture(s) with the given `name`.
621621
For example, you can do:
622622

623623
```ts
624-
for (const { $v } of astx.find`process.env.$v`()) {
624+
for (const { $v } of astx.find`process.env.$v`) {
625625
report($v.code)
626626
}
627627
```
@@ -631,7 +631,7 @@ for (const { $v } of astx.find`process.env.$v`()) {
631631
The name of the placeholder this instance represents. For example:
632632

633633
```ts
634-
const match = astx.find`function $fn($$params) { $$body }`()
634+
const match = astx.find`function $fn($$params) { $$body }`
635635
console.log(match.placeholder) // undefined
636636
const { $fn, $$params } = match
637637
console.log($fn.placeholder) // $fn
@@ -775,9 +775,7 @@ See [`FindOptions.where` (`{ [captureName: string]: (path: NodePath<any>) => boo
775775

776776
A code string, AST node, or replace function to replace matches of `exports.find` with.
777777

778-
The function arguments are the same as described in [`.find().replace()`](#findreplace) or
779-
[`.findStatements().replace()`](#findstatementsreplace), depending on whether `exports.find`
780-
is multiple statements or not.
778+
The function arguments are the same as described in [`.find().replace()`](#findreplace-void).
781779

782780
## `exports.astx` (optional)
783781

src/Astx.ts

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,10 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
343343
)
344344
}
345345

346-
private _execPattern<Options, Return>(
346+
private _execPattern<Return>(
347347
name: string,
348348
exec: (
349-
pattern: NodePath<Node, any> | readonly NodePath<Node, any>[],
350-
options?: Options
349+
pattern: NodePath<Node, any> | readonly NodePath<Node, any>[]
351350
) => Return,
352351
arg0:
353352
| string
@@ -358,31 +357,26 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
358357
| string[]
359358
| TemplateStringsArray,
360359
...rest: any[]
361-
): Return | ((options?: Options) => Return) {
360+
): Return {
362361
const { backend } = this
363362
const { parsePattern } = backend
364363
const { NodePath } = backend.t
365364
try {
366-
let pattern: NodePath<Node, any> | readonly NodePath<Node, any>[],
367-
options: Options | undefined
365+
let pattern: NodePath<Node, any> | readonly NodePath<Node, any>[]
368366
if (typeof arg0 === 'string') {
369367
pattern = parsePattern(arg0)
370-
options = rest[0]
371368
} else if (
372369
Array.isArray(arg0)
373370
? arg0[0] instanceof NodePath
374371
: arg0 instanceof NodePath
375372
) {
376373
pattern = ensureArray(arg0 as NodePath | NodePath[])
377-
options = rest[0]
378374
} else if (isNode(arg0) || isNodeArray(arg0)) {
379375
pattern = ensureArray(arg0).map((node) => new NodePath(node))
380-
options = rest[0]
381376
} else {
382377
pattern = parsePattern(arg0 as any, ...rest)
383-
return (options?: Options) => exec(pattern, options)
384378
}
385-
return exec(pattern, options)
379+
return exec(pattern)
386380
} catch (error) {
387381
if (error instanceof Error) {
388382
CodeFrameError.rethrow(error, {
@@ -394,9 +388,9 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
394388
}
395389
}
396390

397-
private _execPatternOrPredicate<Options, Return>(
391+
private _execPatternOrPredicate<Return>(
398392
name: string,
399-
exec: (match: CompiledMatcher['match'], options?: Options) => Return,
393+
exec: (match: CompiledMatcher['match']) => Return,
400394
arg0:
401395
| string
402396
| Node
@@ -407,19 +401,18 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
407401
| TemplateStringsArray
408402
| FindPredicate,
409403
...rest: any[]
410-
): Return | ((options?: Options) => Return) {
404+
): Return {
411405
const { backend } = this
412406
if (arg0 instanceof Function) {
413407
const predicate = arg0
414-
const options = rest[0]
415408
const match = (path: NodePath): MatchResult => {
416409
const wrapper = new Astx(this.context, [path], {
417410
withCaptures: this._matches,
418411
})
419412
return predicate(wrapper) ? wrapper.initialMatch || {} : null
420413
}
421414
try {
422-
return exec(match, options)
415+
return exec(match)
423416
} catch (error) {
424417
if (error instanceof Error) {
425418
CodeFrameError.rethrow(error, {
@@ -431,30 +424,23 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
431424
} else {
432425
return this._execPattern(
433426
name,
434-
(
435-
pattern: NodePath<Node, any> | readonly NodePath<Node, any>[],
436-
options?: Options
437-
) => {
427+
(pattern: NodePath<Node, any> | readonly NodePath<Node, any>[]) => {
438428
pattern = ensureArray(pattern)
439429
if (pattern.length !== 1) {
440430
throw new Error(`must be a single node`)
441431
}
442432
const matcher = compileMatcher(pattern[0], {
443-
...options,
444433
backend,
445434
})
446-
return exec(matcher.match, options)
435+
return exec(matcher.match)
447436
},
448437
arg0,
449438
...rest
450439
)
451440
}
452441
}
453442

454-
closest(
455-
strings: TemplateStringsArray,
456-
...quasis: any[]
457-
): (options?: FindOptions) => Astx
443+
closest(strings: TemplateStringsArray, ...quasis: any[]): Astx
458444
closest(
459445
pattern:
460446
| string
@@ -475,7 +461,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
475461
| TemplateStringsArray
476462
| FindPredicate,
477463
...rest: any[]
478-
): Astx | ((options?: FindOptions) => Astx) {
464+
): Astx {
479465
const { context } = this
480466
return this._execPatternOrPredicate(
481467
'closest',
@@ -500,10 +486,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
500486
)
501487
}
502488

503-
destruct(
504-
strings: TemplateStringsArray,
505-
...quasis: any[]
506-
): (options?: FindOptions) => Astx
489+
destruct(strings: TemplateStringsArray, ...quasis: any[]): Astx
507490
destruct(
508491
pattern:
509492
| string
@@ -524,7 +507,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
524507
| TemplateStringsArray
525508
| FindPredicate,
526509
...rest: any[]
527-
): Astx | ((options?: FindOptions) => Astx) {
510+
): Astx {
528511
const { context } = this
529512
return this._execPatternOrPredicate(
530513
'destruct',
@@ -541,10 +524,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
541524
)
542525
}
543526

544-
find(
545-
strings: string[] | TemplateStringsArray,
546-
...quasis: any[]
547-
): (options?: FindOptions) => Astx
527+
find(strings: string[] | TemplateStringsArray, ...quasis: any[]): Astx
548528
find(
549529
pattern:
550530
| string
@@ -566,7 +546,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
566546
| TemplateStringsArray
567547
| FindPredicate,
568548
...rest: any[]
569-
): Astx | ((options?: FindOptions) => Astx) {
549+
): Astx {
570550
const { context, backend } = this
571551
if (arg0 instanceof Function) {
572552
const predicate = arg0
@@ -600,10 +580,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
600580
)
601581
}
602582

603-
replace(
604-
strings: string[] | TemplateStringsArray,
605-
...quasis: any[]
606-
): () => void
583+
replace(strings: string[] | TemplateStringsArray, ...quasis: any[]): void
607584
replace(replacement: string | Node | Node[] | GetReplacement): void
608585
replace(
609586
arg0:
@@ -614,7 +591,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
614591
| string[]
615592
| TemplateStringsArray,
616593
...quasis: any[]
617-
): void | (() => void) {
594+
): void {
618595
const { backend } = this
619596
const { parsePatternToNodes } = backend
620597
try {
@@ -643,7 +620,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
643620
}
644621
} else {
645622
const finalPaths = parsePatternToNodes(arg0, ...quasis)
646-
return () => this.replace(finalPaths)
623+
this.replace(finalPaths)
647624
}
648625
} catch (error) {
649626
if (error instanceof Error) {
@@ -660,10 +637,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
660637
this.replace([])
661638
}
662639

663-
addImports(
664-
strings: string[] | TemplateStringsArray,
665-
...quasis: any[]
666-
): () => Astx
640+
addImports(strings: string[] | TemplateStringsArray, ...quasis: any[]): Astx
667641
addImports(
668642
pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[]
669643
): Astx
@@ -677,7 +651,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
677651
| string[]
678652
| TemplateStringsArray,
679653
...rest: any[]
680-
): Astx | (() => Astx) {
654+
): Astx {
681655
return this._execPattern(
682656
'addImports',
683657
(pattern: NodePath<Node, any> | readonly NodePath<Node, any>[]): Astx =>
@@ -687,10 +661,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
687661
)
688662
}
689663

690-
findImports(
691-
strings: string[] | TemplateStringsArray,
692-
...quasis: any[]
693-
): () => Astx
664+
findImports(strings: string[] | TemplateStringsArray, ...quasis: any[]): Astx
694665
findImports(
695666
pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[]
696667
): Astx
@@ -704,7 +675,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
704675
| string[]
705676
| TemplateStringsArray,
706677
...rest: any[]
707-
): Astx | (() => Astx) {
678+
): Astx {
708679
return this._execPattern(
709680
'findImports',
710681
(pattern: NodePath<Node, any> | readonly NodePath<Node, any>[]): Astx =>
@@ -717,7 +688,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
717688
removeImports(
718689
strings: string[] | TemplateStringsArray,
719690
...quasis: any[]
720-
): () => boolean
691+
): boolean
721692
removeImports(
722693
pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[]
723694
): boolean
@@ -731,7 +702,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
731702
| string[]
732703
| TemplateStringsArray,
733704
...rest: any[]
734-
): boolean | (() => boolean) {
705+
): boolean {
735706
return this._execPattern(
736707
'removeImports',
737708
(
@@ -746,7 +717,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
746717
replaceImport(
747718
strings: string[] | TemplateStringsArray,
748719
...quasis: any[]
749-
): () => ImportReplacer
720+
): ImportReplacer
750721
replaceImport(
751722
pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[]
752723
): ImportReplacer
@@ -760,7 +731,7 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
760731
| string[]
761732
| TemplateStringsArray,
762733
...rest: any[]
763-
): ImportReplacer | (() => ImportReplacer) {
734+
): ImportReplacer {
764735
return this._execPattern(
765736
'replaceImport',
766737
(
@@ -795,10 +766,7 @@ class ImportReplacer {
795766
public decl: ImportDeclaration
796767
) {}
797768

798-
with(
799-
strings: string[] | TemplateStringsArray,
800-
...quasis: any[]
801-
): () => boolean
769+
with(strings: string[] | TemplateStringsArray, ...quasis: any[]): boolean
802770
with(
803771
pattern:
804772
| string
@@ -819,7 +787,7 @@ class ImportReplacer {
819787
| string[]
820788
| TemplateStringsArray,
821789
...rest: any[]
822-
): boolean | (() => boolean) {
790+
): boolean {
823791
const { backend } = this.astx
824792
const { parsePatternToNodes } = backend
825793

@@ -864,7 +832,7 @@ class ImportReplacer {
864832
return doReplace(arg0)
865833
} else {
866834
const rawReplacement = parsePatternToNodes(arg0 as any, ...rest)
867-
return () => doReplace(rawReplacement)
835+
return doReplace(rawReplacement)
868836
}
869837
} catch (error) {
870838
if (error instanceof Error) {

0 commit comments

Comments
 (0)