You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Like `jscodeshift`, you can put code to perform a transform in a `.js` file (defaults to `astx.js` in the working directory, unless you use the `-t` CLI option).
443
+
444
+
The transform file API is a bit different from `jscodeshift` though. You can have the following exports:
445
+
446
+
## `exports.find` (optional)
447
+
448
+
A code string or AST node of the pattern to find in the files being transformed.
449
+
450
+
## `exports.where` (optional)
451
+
452
+
Where conditions for capture variables in `exports.find`.
453
+
See [`FindOptions.where` (`{ [captureName: string]: (path: ASTPath<any>) => boolean }`)](#findoptionswhere--capturename-string-path-astpathany--boolean-) for more information.
454
+
455
+
## `exports.replace` (optional)
456
+
457
+
A code string, AST node, or replace function to replace matches of `exports.find` with.
458
+
459
+
The function arguments are the same as described in [`.find().replace()`](#findreplace) or
460
+
[`.findStatements().replace()`](#findstatementsreplace), depending on whether `exports.find`
461
+
is multiple statements or not.
462
+
463
+
## `exports.astx` (optional)
464
+
465
+
A function to perform an arbitrary transform using the `Astx` API. It gets called with an object with the following properties:
466
+
467
+
-`source` (`string`) - The source code of the file being transformed
468
+
-`path` (`string`) - The path to the file being transformed
469
+
-`root` (`Collection`) - the JSCodeshift Collection wrapping the parsed AST
470
+
-`astx` (`Astx`) - the `Astx` API instance
471
+
-`jscodeshift` (`JSCodeshift`) - the JSCodeshift instance
472
+
-`j` (`JSCodeshift`) - shorthand for the same JSCodeshift instance
473
+
-`expression` - tagged template literal for parsing code as an expression, like `jscodeshift.template.expression`
474
+
-`statement` - tagged template literal for parsing code as a statement, like `jscodeshift.template.statement`
475
+
-`statements` - tagged template literal for parsing code as an array of statements, like `jscodeshift.template.statements`
476
+
-`report` (`(message: any) => void`)
477
+
478
+
# CLI
479
+
480
+
Astx includes a CLI for performing transforms. The CLI will process the given files, then print out a diff of what will be
481
+
changed, and prompt you to confirm you want to write the changes.
482
+
483
+
It will parse with babel by default using the version installed in your project and your project's babel config, if any.
484
+
You can pass other parsers with the `--parser` option, just like `jscodeshift`.
0 commit comments