@kamaalio/codemod-kit 0.0.18 → 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -9
- package/dist/codemods/utils.d.ts +8 -10
- package/dist/index.cjs +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ A utility function for finding and replacing AST nodes based on a rule.
|
|
|
50
50
|
|
|
51
51
|
- `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
|
|
52
52
|
- `rule`: A `Rule<TypesMap>` object defining the pattern to search for.
|
|
53
|
-
- `transformer`: A function that takes a matched node and returns an optional string replacement.
|
|
53
|
+
- `transformer`: A function that takes a matched node and returns an optional string replacement, or a string for direct replacement.
|
|
54
54
|
|
|
55
55
|
Returns the transformed content as a string with all matching nodes replaced.
|
|
56
56
|
|
|
@@ -65,11 +65,20 @@ function oldFunction() {
|
|
|
65
65
|
`;
|
|
66
66
|
|
|
67
67
|
const ast = await parseAsync('javascript', code);
|
|
68
|
-
|
|
68
|
+
|
|
69
|
+
// Using a function transformer
|
|
70
|
+
const result1 = findAndReplace(
|
|
69
71
|
ast,
|
|
70
72
|
{ pattern: 'function oldFunction() { $$$ }' },
|
|
71
73
|
node => 'function newFunction() { return "hello world"; }',
|
|
72
74
|
);
|
|
75
|
+
|
|
76
|
+
// Using a string transformer
|
|
77
|
+
const result2 = findAndReplace(
|
|
78
|
+
ast,
|
|
79
|
+
{ pattern: 'function oldFunction() { $$$ }' },
|
|
80
|
+
'function newFunction() { return "hello world"; }',
|
|
81
|
+
);
|
|
73
82
|
```
|
|
74
83
|
|
|
75
84
|
### `findAndReplaceEdits(content, rule, transformer)`
|
|
@@ -78,7 +87,7 @@ A utility function for finding AST nodes and generating edit operations without
|
|
|
78
87
|
|
|
79
88
|
- `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
|
|
80
89
|
- `rule`: A `Rule<TypesMap>` object defining the pattern to search for.
|
|
81
|
-
- `transformer`: A function that takes a matched node and returns an optional string replacement.
|
|
90
|
+
- `transformer`: A function that takes a matched node and returns an optional string replacement, or a string for direct replacement.
|
|
82
91
|
|
|
83
92
|
Returns an array of `Edit` objects that can be committed later using `commitEdits()`.
|
|
84
93
|
|
|
@@ -93,14 +102,23 @@ function oldFunction() {
|
|
|
93
102
|
`;
|
|
94
103
|
|
|
95
104
|
const ast = await parseAsync('javascript', code);
|
|
96
|
-
|
|
105
|
+
|
|
106
|
+
// Using a function transformer
|
|
107
|
+
const edits1 = findAndReplaceEdits(
|
|
97
108
|
ast,
|
|
98
109
|
{ pattern: 'function oldFunction() { $$$ }' },
|
|
99
110
|
node => 'function newFunction() { return "hello world"; }',
|
|
100
111
|
);
|
|
101
112
|
|
|
113
|
+
// Using a string transformer
|
|
114
|
+
const edits2 = findAndReplaceEdits(
|
|
115
|
+
ast,
|
|
116
|
+
{ pattern: 'function oldFunction() { $$$ }' },
|
|
117
|
+
'function newFunction() { return "hello world"; }',
|
|
118
|
+
);
|
|
119
|
+
|
|
102
120
|
// Commit the edits later
|
|
103
|
-
const result = ast.root().commitEdits(
|
|
121
|
+
const result = ast.root().commitEdits(edits1);
|
|
104
122
|
```
|
|
105
123
|
|
|
106
124
|
### `findAndReplaceConfig(content, lang, config)`
|
|
@@ -109,7 +127,7 @@ A utility function for applying multiple find-and-replace operations sequentiall
|
|
|
109
127
|
|
|
110
128
|
- `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
|
|
111
129
|
- `lang`: A `NapiLang` value specifying the language for re-parsing after each transformation.
|
|
112
|
-
- `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially.
|
|
130
|
+
- `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially. The `transformer` can be either a function or a string.
|
|
113
131
|
|
|
114
132
|
Returns the final transformed content as a string after applying all transformations.
|
|
115
133
|
|
|
@@ -132,7 +150,7 @@ const result = await findAndReplaceConfig(ast, 'javascript', [
|
|
|
132
150
|
},
|
|
133
151
|
{
|
|
134
152
|
rule: { pattern: 'const value = $VAL' },
|
|
135
|
-
transformer:
|
|
153
|
+
transformer: 'const value = 100', // String transformer
|
|
136
154
|
},
|
|
137
155
|
]);
|
|
138
156
|
```
|
|
@@ -142,7 +160,7 @@ const result = await findAndReplaceConfig(ast, 'javascript', [
|
|
|
142
160
|
A utility function for applying multiple find-and-replace operations sequentially on a `Modifications` object.
|
|
143
161
|
|
|
144
162
|
- `modifications`: A `Modifications` object containing the AST, language, and transformation history.
|
|
145
|
-
- `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially.
|
|
163
|
+
- `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially. The `transformer` can be either a function or a string.
|
|
146
164
|
|
|
147
165
|
Returns a `Promise<Modifications>` with the updated AST, accumulated edit count, and transformation history.
|
|
148
166
|
|
|
@@ -173,7 +191,7 @@ const result = await findAndReplaceConfigModifications(initialModifications, [
|
|
|
173
191
|
},
|
|
174
192
|
{
|
|
175
193
|
rule: { pattern: 'const value = $VAL' },
|
|
176
|
-
transformer:
|
|
194
|
+
transformer: 'const value = 100', // String transformer
|
|
177
195
|
},
|
|
178
196
|
]);
|
|
179
197
|
|
package/dist/codemods/utils.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ type RunCodemodOptions<C extends Codemod> = {
|
|
|
14
14
|
log?: boolean;
|
|
15
15
|
dry?: boolean;
|
|
16
16
|
};
|
|
17
|
+
type FindAndReplaceConfig = {
|
|
18
|
+
rule: Rule<TypesMap>;
|
|
19
|
+
transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>) | string;
|
|
20
|
+
};
|
|
17
21
|
export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<{
|
|
18
22
|
hasChanges: boolean;
|
|
19
23
|
content: string;
|
|
@@ -22,15 +26,9 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
|
|
|
22
26
|
hasChanges: boolean;
|
|
23
27
|
content: string;
|
|
24
28
|
}, Error>>>;
|
|
25
|
-
export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<{
|
|
30
|
-
rule: Rule<TypesMap>;
|
|
31
|
-
transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>;
|
|
32
|
-
}>): Promise<string>;
|
|
33
|
-
export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): Array<Edit>;
|
|
34
|
-
export declare function findAndReplace(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): string;
|
|
29
|
+
export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<FindAndReplaceConfig>): Promise<Modifications>;
|
|
30
|
+
export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<FindAndReplaceConfig>): Promise<string>;
|
|
31
|
+
export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): Array<Edit>;
|
|
32
|
+
export declare function findAndReplace(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): string;
|
|
35
33
|
export declare function commitEditModifications(edits: Array<Edit>, modifications: Modifications): Promise<Modifications>;
|
|
36
34
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -164,8 +164,9 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
164
164
|
rule
|
|
165
165
|
});
|
|
166
166
|
return kamaal_namespaceObject.arrays.compactMap(nodes, (node)=>{
|
|
167
|
-
const transformed = transformer(node);
|
|
167
|
+
const transformed = 'string' == typeof transformer ? transformer : transformer(node);
|
|
168
168
|
if (null == transformed) return null;
|
|
169
|
+
if (transformed === node.text()) return null;
|
|
169
170
|
return node.replace(transformed);
|
|
170
171
|
});
|
|
171
172
|
}
|
package/dist/index.js
CHANGED
|
@@ -118,8 +118,9 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
118
118
|
rule
|
|
119
119
|
});
|
|
120
120
|
return arrays.compactMap(nodes, (node)=>{
|
|
121
|
-
const transformed = transformer(node);
|
|
121
|
+
const transformed = 'string' == typeof transformer ? transformer : transformer(node);
|
|
122
122
|
if (null == transformed) return null;
|
|
123
|
+
if (transformed === node.text()) return null;
|
|
123
124
|
return node.replace(transformed);
|
|
124
125
|
});
|
|
125
126
|
}
|