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
```ts [How to use @codemod-utils/ast-template]{1,4-10}
45
+
```ts [How to use @codemod-utils/ast-template]{1}
50
46
import { AST } from'@codemod-utils/ast-template';
51
47
52
48
function transform(file:string):string {
@@ -62,7 +58,7 @@ function transform(file: string): string {
62
58
63
59
:::
64
60
65
-
Based on the how-to's above, try using `AST` in `renameModule` so that it remains an identity function. That is, define `traverse` (on line 10) so that the input `file` and the returned file have the same value. How will you indicate if the file is written in JavaScript or TypeScript?
61
+
Based on the how-to's above, try using `AST` in `renameModule` so that it remains an identity function. That is, define `traverse` (on line 10) so that the input `file` and the returned file have the same value.
66
62
67
63
<details>
68
64
@@ -81,22 +77,16 @@ import { findFiles } from '@codemod-utils/files';
81
77
82
78
import type { Options } from '../types/index.js';
83
79
84
-
- function renameModule(file: string): string {
85
-
- return file;
86
-
- }
87
-
+ type Data = {
88
-
+ isTypeScript: boolean;
89
-
+ };
90
-
+
91
-
+ function renameModule(file: string, data: Data): string {
export function renameAcceptanceTests(options: Options): void {
102
92
const { projectRoot } = options;
@@ -109,12 +99,7 @@ export function renameAcceptanceTests(options: Options): void {
109
99
const oldPath = join(projectRoot, filePath);
110
100
const oldFile = readFileSync(oldPath, 'utf8');
111
101
112
-
- const newFile = renameModule(oldFile);
113
-
+ const data = {
114
-
+ isTypeScript: filePath.endsWith('.ts'),
115
-
+ };
116
-
+
117
-
+ const newFile = renameModule(oldFile, data);
102
+
const newFile = renameModule(oldFile);
118
103
119
104
writeFileSync(oldPath, newFile, 'utf8');
120
105
});
@@ -287,8 +272,6 @@ Use what are highlighted in orange (`path.node.callee` and `path.node.arguments`
287
272
288
273
<summary>Solution</summary>
289
274
290
-
An extra check `path.node.arguments[0]!.type !== 'Literal'` is needed for JavaScript files. Apparently, the `type` is `Literal` for JS and `StringLiteral` for TS? 😓
Once you arrive at an implementation in AST Explorer, moving the code to the codemod is trivial. Copy-paste the object with the visit methods, then replace `b.` with `AST.builders.`. (How should we pass `moduleName`?)
376
+
Once you arrive at an implementation in AST Explorer, moving the code to the codemod is trivial. Copy-paste the object with the visit methods, then replace `b.` with `AST.builders.`. How should we pass data like `moduleName`?
464
377
465
378
<details>
466
379
467
380
<summary>Solution</summary>
468
381
382
+
To pass data, we use an object. Objects help us maintain and extend code, because they don't create an order dependency among items that don't have a natural order.
383
+
469
384
::: code-group
470
385
471
386
```diff [src/steps/rename-acceptance-tests.ts]
@@ -477,13 +392,13 @@ import { findFiles } from '@codemod-utils/files';
477
392
478
393
import type { Options } from '../types/index.js';
479
394
480
-
type Data = {
481
-
isTypeScript: boolean;
395
+
+ type Data = {
482
396
+ moduleName: string;
483
-
};
484
-
485
-
function renameModule(file: string, data: Data): string {
486
-
const traverse = AST.traverse(data.isTypeScript);
397
+
+ };
398
+
+
399
+
- function renameModule(file: string): string {
400
+
+ function renameModule(file: string, data: Data): string {
0 commit comments