@kamaalio/codemod-kit 0.0.17 → 0.0.18
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 +22 -9
- package/dist/codemods/index.d.ts +1 -1
- package/dist/codemods/utils.d.ts +2 -5
- package/dist/index.cjs +14 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -137,36 +137,49 @@ const result = await findAndReplaceConfig(ast, 'javascript', [
|
|
|
137
137
|
]);
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
-
### `
|
|
140
|
+
### `findAndReplaceConfigModifications(modifications, config)`
|
|
141
141
|
|
|
142
|
-
A utility function for applying multiple find-and-replace operations sequentially
|
|
142
|
+
A utility function for applying multiple find-and-replace operations sequentially on a `Modifications` object.
|
|
143
143
|
|
|
144
|
-
- `
|
|
145
|
-
- `lang`: A `NapiLang` value specifying the language for re-parsing after each transformation.
|
|
144
|
+
- `modifications`: A `Modifications` object containing the AST, language, and transformation history.
|
|
146
145
|
- `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially.
|
|
147
146
|
|
|
148
|
-
Returns
|
|
147
|
+
Returns a `Promise<Modifications>` with the updated AST, accumulated edit count, and transformation history.
|
|
149
148
|
|
|
150
149
|
```typescript
|
|
151
|
-
import {
|
|
150
|
+
import { findAndReplaceConfigModifications } from '@kamaalio/codemod-kit';
|
|
152
151
|
import { parseAsync } from '@ast-grep/napi';
|
|
153
152
|
|
|
154
153
|
const code = `
|
|
155
154
|
function oldFunction() {
|
|
156
155
|
return "hello";
|
|
157
156
|
}
|
|
157
|
+
const value = 42;
|
|
158
158
|
`;
|
|
159
159
|
|
|
160
160
|
const ast = await parseAsync('javascript', code);
|
|
161
|
-
const
|
|
161
|
+
const initialModifications = {
|
|
162
|
+
ast,
|
|
163
|
+
lang: 'javascript' as const,
|
|
164
|
+
filename: 'example.js',
|
|
165
|
+
report: { changesApplied: 0 },
|
|
166
|
+
history: [ast],
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const result = await findAndReplaceConfigModifications(initialModifications, [
|
|
162
170
|
{
|
|
163
171
|
rule: { pattern: 'function oldFunction() { $$$ }' },
|
|
164
172
|
transformer: node => 'function newFunction() { return "hello world"; }',
|
|
165
173
|
},
|
|
174
|
+
{
|
|
175
|
+
rule: { pattern: 'const value = $VAL' },
|
|
176
|
+
transformer: node => 'const value = 100',
|
|
177
|
+
},
|
|
166
178
|
]);
|
|
167
179
|
|
|
168
|
-
//
|
|
169
|
-
//
|
|
180
|
+
// result.ast contains the final transformed AST
|
|
181
|
+
// result.report.changesApplied contains the total number of edits applied
|
|
182
|
+
// result.history contains the transformation history
|
|
170
183
|
```
|
|
171
184
|
|
|
172
185
|
### `Codemod`
|
package/dist/codemods/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig,
|
|
1
|
+
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, } from './utils.js';
|
|
2
2
|
export type { Codemod, Modifications } from './types.js';
|
package/dist/codemods/utils.d.ts
CHANGED
|
@@ -22,13 +22,10 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
|
|
|
22
22
|
hasChanges: boolean;
|
|
23
23
|
content: string;
|
|
24
24
|
}, Error>>>;
|
|
25
|
-
export declare function
|
|
25
|
+
export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<{
|
|
26
26
|
rule: Rule<TypesMap>;
|
|
27
27
|
transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>;
|
|
28
|
-
}>): Promise<
|
|
29
|
-
content: SgRoot<TypesMap>;
|
|
30
|
-
edits: Array<Edit>;
|
|
31
|
-
}>>;
|
|
28
|
+
}>): Promise<Modifications>;
|
|
32
29
|
export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<{
|
|
33
30
|
rule: Rule<TypesMap>;
|
|
34
31
|
transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>;
|
package/dist/index.cjs
CHANGED
|
@@ -34,11 +34,11 @@ var __webpack_exports__ = {};
|
|
|
34
34
|
__webpack_require__.r(__webpack_exports__);
|
|
35
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
36
36
|
commitEditModifications: ()=>commitEditModifications,
|
|
37
|
-
|
|
37
|
+
runCodemods: ()=>runCodemods,
|
|
38
38
|
runCodemod: ()=>runCodemod,
|
|
39
39
|
findAndReplace: ()=>findAndReplace,
|
|
40
|
+
findAndReplaceConfigModifications: ()=>findAndReplaceConfigModifications,
|
|
40
41
|
findAndReplaceEdits: ()=>findAndReplaceEdits,
|
|
41
|
-
runCodemods: ()=>runCodemods,
|
|
42
42
|
findAndReplaceConfig: ()=>findAndReplaceConfig
|
|
43
43
|
});
|
|
44
44
|
const external_node_path_namespaceObject = require("node:path");
|
|
@@ -131,6 +131,16 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
131
131
|
}
|
|
132
132
|
}));
|
|
133
133
|
}
|
|
134
|
+
async function findAndReplaceConfigModifications(modifications, config) {
|
|
135
|
+
let currentModifications = {
|
|
136
|
+
...modifications
|
|
137
|
+
};
|
|
138
|
+
for (const { rule, transformer } of config){
|
|
139
|
+
const edits = findAndReplaceEdits(currentModifications.ast, rule, transformer);
|
|
140
|
+
currentModifications = await commitEditModifications(edits, currentModifications);
|
|
141
|
+
}
|
|
142
|
+
return currentModifications;
|
|
143
|
+
}
|
|
134
144
|
async function findAndReplaceConfigEdits(content, lang, config) {
|
|
135
145
|
let currentContent = content;
|
|
136
146
|
const editsAndContent = [];
|
|
@@ -200,7 +210,7 @@ function defaultedHooks(hooks) {
|
|
|
200
210
|
exports.commitEditModifications = __webpack_exports__.commitEditModifications;
|
|
201
211
|
exports.findAndReplace = __webpack_exports__.findAndReplace;
|
|
202
212
|
exports.findAndReplaceConfig = __webpack_exports__.findAndReplaceConfig;
|
|
203
|
-
exports.
|
|
213
|
+
exports.findAndReplaceConfigModifications = __webpack_exports__.findAndReplaceConfigModifications;
|
|
204
214
|
exports.findAndReplaceEdits = __webpack_exports__.findAndReplaceEdits;
|
|
205
215
|
exports.runCodemod = __webpack_exports__.runCodemod;
|
|
206
216
|
exports.runCodemods = __webpack_exports__.runCodemods;
|
|
@@ -208,7 +218,7 @@ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
|
208
218
|
"commitEditModifications",
|
|
209
219
|
"findAndReplace",
|
|
210
220
|
"findAndReplaceConfig",
|
|
211
|
-
"
|
|
221
|
+
"findAndReplaceConfigModifications",
|
|
212
222
|
"findAndReplaceEdits",
|
|
213
223
|
"runCodemod",
|
|
214
224
|
"runCodemods"
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig,
|
|
1
|
+
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, type Codemod, type Modifications, } from './codemods/index.js';
|
package/dist/index.js
CHANGED
|
@@ -85,6 +85,16 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
85
85
|
}
|
|
86
86
|
}));
|
|
87
87
|
}
|
|
88
|
+
async function findAndReplaceConfigModifications(modifications, config) {
|
|
89
|
+
let currentModifications = {
|
|
90
|
+
...modifications
|
|
91
|
+
};
|
|
92
|
+
for (const { rule, transformer } of config){
|
|
93
|
+
const edits = findAndReplaceEdits(currentModifications.ast, rule, transformer);
|
|
94
|
+
currentModifications = await commitEditModifications(edits, currentModifications);
|
|
95
|
+
}
|
|
96
|
+
return currentModifications;
|
|
97
|
+
}
|
|
88
98
|
async function findAndReplaceConfigEdits(content, lang, config) {
|
|
89
99
|
let currentContent = content;
|
|
90
100
|
const editsAndContent = [];
|
|
@@ -151,4 +161,4 @@ function defaultedHooks(hooks) {
|
|
|
151
161
|
preCodemodRun
|
|
152
162
|
};
|
|
153
163
|
}
|
|
154
|
-
export { commitEditModifications, findAndReplace, findAndReplaceConfig,
|
|
164
|
+
export { commitEditModifications, findAndReplace, findAndReplaceConfig, findAndReplaceConfigModifications, findAndReplaceEdits, runCodemod, runCodemods };
|