@kamaalio/codemod-kit 0.0.24 → 0.0.26
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/dist/codemods/index.d.ts +1 -1
- package/dist/codemods/types.d.ts +1 -1
- package/dist/codemods/utils.d.ts +4 -2
- package/dist/index.cjs +26 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +20 -3
- package/package.json +2 -2
package/dist/codemods/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, } from './utils.js';
|
|
1
|
+
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, traverseUp, } from './utils.js';
|
|
2
2
|
export type { Codemod, Modifications, FindAndReplaceConfig } from './types.js';
|
package/dist/codemods/types.d.ts
CHANGED
|
@@ -19,5 +19,5 @@ export type Modifications = {
|
|
|
19
19
|
};
|
|
20
20
|
export type FindAndReplaceConfig = {
|
|
21
21
|
rule: Rule<TypesMap>;
|
|
22
|
-
transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => Optional<string> | Array<Edit | string>) | string;
|
|
22
|
+
transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => Optional<Edit | string> | Array<Edit | string>) | string;
|
|
23
23
|
};
|
package/dist/codemods/utils.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type Result } from 'neverthrow';
|
|
2
|
-
import { type Edit, type SgRoot } from '@ast-grep/napi';
|
|
3
|
-
import type { TypesMap } from '@ast-grep/napi/types/staticTypes.js';
|
|
2
|
+
import { type Edit, type SgRoot, type SgNode } from '@ast-grep/napi';
|
|
3
|
+
import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
|
|
4
4
|
import type { NapiLang } from '@ast-grep/napi/types/lang.js';
|
|
5
|
+
import { type types } from '@kamaalio/kamaal';
|
|
5
6
|
import type { Codemod, FindAndReplaceConfig, Modifications } from './types.js';
|
|
6
7
|
type RunCodemodHooks<C extends Codemod> = {
|
|
7
8
|
targetFiltering?: (filepath: string, codemod: C) => boolean;
|
|
@@ -21,6 +22,7 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
|
|
|
21
22
|
hasChanges: boolean;
|
|
22
23
|
content: string;
|
|
23
24
|
}, Error>>>;
|
|
25
|
+
export declare function traverseUp(node: SgNode<TypesMap, Kinds<TypesMap>>, until: (node: SgNode<TypesMap, Kinds<TypesMap>>) => boolean): types.Optional<SgNode<TypesMap, Kinds<TypesMap>>>;
|
|
24
26
|
export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<FindAndReplaceConfig>): Promise<Modifications>;
|
|
25
27
|
export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<FindAndReplaceConfig>): Promise<string>;
|
|
26
28
|
export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): Array<Edit>;
|
package/dist/index.cjs
CHANGED
|
@@ -33,13 +33,14 @@ var __webpack_require__ = {};
|
|
|
33
33
|
var __webpack_exports__ = {};
|
|
34
34
|
__webpack_require__.r(__webpack_exports__);
|
|
35
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
+
findAndReplace: ()=>findAndReplace,
|
|
37
|
+
findAndReplaceConfig: ()=>findAndReplaceConfig,
|
|
36
38
|
commitEditModifications: ()=>commitEditModifications,
|
|
37
|
-
runCodemods: ()=>runCodemods,
|
|
38
39
|
runCodemod: ()=>runCodemod,
|
|
39
|
-
findAndReplace: ()=>findAndReplace,
|
|
40
40
|
findAndReplaceConfigModifications: ()=>findAndReplaceConfigModifications,
|
|
41
41
|
findAndReplaceEdits: ()=>findAndReplaceEdits,
|
|
42
|
-
|
|
42
|
+
runCodemods: ()=>runCodemods,
|
|
43
|
+
traverseUp: ()=>traverseUp
|
|
43
44
|
});
|
|
44
45
|
const external_node_path_namespaceObject = require("node:path");
|
|
45
46
|
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
@@ -131,6 +132,21 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
131
132
|
}
|
|
132
133
|
}));
|
|
133
134
|
}
|
|
135
|
+
function traverseUp(node, until) {
|
|
136
|
+
let current = node.parent();
|
|
137
|
+
if (null == current) return null;
|
|
138
|
+
while(null != current){
|
|
139
|
+
const next = current.parent();
|
|
140
|
+
if (null == next) break;
|
|
141
|
+
if (until(next)) {
|
|
142
|
+
current = next;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
current = next;
|
|
146
|
+
}
|
|
147
|
+
if (!until(current)) return null;
|
|
148
|
+
return current;
|
|
149
|
+
}
|
|
134
150
|
async function findAndReplaceConfigModifications(modifications, config) {
|
|
135
151
|
let currentModifications = {
|
|
136
152
|
...modifications
|
|
@@ -170,9 +186,11 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
170
186
|
const valuesToTransform = [];
|
|
171
187
|
if (Array.isArray(transformed)) for (const item of transformed)if ('string' == typeof item) valuesToTransform.push(item);
|
|
172
188
|
else edits.push(item);
|
|
173
|
-
else valuesToTransform.push(transformed);
|
|
189
|
+
else if ('string' == typeof transformed) valuesToTransform.push(transformed);
|
|
190
|
+
else edits.push(transformed);
|
|
174
191
|
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
175
|
-
|
|
192
|
+
const transformedValuesWithMetaVariablesReplaced = valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue));
|
|
193
|
+
return kamaal_namespaceObject.arrays.compactMap(transformedValuesWithMetaVariablesReplaced, (transformedValueWithMetaVariablesReplaced)=>{
|
|
176
194
|
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
177
195
|
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
178
196
|
}).concat(edits);
|
|
@@ -264,6 +282,7 @@ exports.findAndReplaceConfigModifications = __webpack_exports__.findAndReplaceCo
|
|
|
264
282
|
exports.findAndReplaceEdits = __webpack_exports__.findAndReplaceEdits;
|
|
265
283
|
exports.runCodemod = __webpack_exports__.runCodemod;
|
|
266
284
|
exports.runCodemods = __webpack_exports__.runCodemods;
|
|
285
|
+
exports.traverseUp = __webpack_exports__.traverseUp;
|
|
267
286
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
268
287
|
"commitEditModifications",
|
|
269
288
|
"findAndReplace",
|
|
@@ -271,7 +290,8 @@ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
|
271
290
|
"findAndReplaceConfigModifications",
|
|
272
291
|
"findAndReplaceEdits",
|
|
273
292
|
"runCodemod",
|
|
274
|
-
"runCodemods"
|
|
293
|
+
"runCodemods",
|
|
294
|
+
"traverseUp"
|
|
275
295
|
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
276
296
|
Object.defineProperty(exports, '__esModule', {
|
|
277
297
|
value: true
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, type Codemod, type Modifications, type FindAndReplaceConfig, } from './codemods/index.js';
|
|
1
|
+
export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, traverseUp, type Codemod, type Modifications, type FindAndReplaceConfig, } from './codemods/index.js';
|
package/dist/index.js
CHANGED
|
@@ -85,6 +85,21 @@ async function runCodemod(codemod, transformationPath, options) {
|
|
|
85
85
|
}
|
|
86
86
|
}));
|
|
87
87
|
}
|
|
88
|
+
function traverseUp(node, until) {
|
|
89
|
+
let current = node.parent();
|
|
90
|
+
if (null == current) return null;
|
|
91
|
+
while(null != current){
|
|
92
|
+
const next = current.parent();
|
|
93
|
+
if (null == next) break;
|
|
94
|
+
if (until(next)) {
|
|
95
|
+
current = next;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
current = next;
|
|
99
|
+
}
|
|
100
|
+
if (!until(current)) return null;
|
|
101
|
+
return current;
|
|
102
|
+
}
|
|
88
103
|
async function findAndReplaceConfigModifications(modifications, config) {
|
|
89
104
|
let currentModifications = {
|
|
90
105
|
...modifications
|
|
@@ -124,9 +139,11 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
124
139
|
const valuesToTransform = [];
|
|
125
140
|
if (Array.isArray(transformed)) for (const item of transformed)if ('string' == typeof item) valuesToTransform.push(item);
|
|
126
141
|
else edits.push(item);
|
|
127
|
-
else valuesToTransform.push(transformed);
|
|
142
|
+
else if ('string' == typeof transformed) valuesToTransform.push(transformed);
|
|
143
|
+
else edits.push(transformed);
|
|
128
144
|
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
129
|
-
|
|
145
|
+
const transformedValuesWithMetaVariablesReplaced = valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue));
|
|
146
|
+
return arrays.compactMap(transformedValuesWithMetaVariablesReplaced, (transformedValueWithMetaVariablesReplaced)=>{
|
|
130
147
|
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
131
148
|
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
132
149
|
}).concat(edits);
|
|
@@ -211,4 +228,4 @@ function defaultedHooks(hooks) {
|
|
|
211
228
|
preCodemodRun
|
|
212
229
|
};
|
|
213
230
|
}
|
|
214
|
-
export { commitEditModifications, findAndReplace, findAndReplaceConfig, findAndReplaceConfigModifications, findAndReplaceEdits, runCodemod, runCodemods };
|
|
231
|
+
export { commitEditModifications, findAndReplace, findAndReplaceConfig, findAndReplaceConfigModifications, findAndReplaceEdits, runCodemod, runCodemods, traverseUp };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kamaalio/codemod-kit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "Kamaal Farah",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@ast-grep/napi": "^0.38.5",
|
|
23
|
-
"@kamaalio/kamaal": "^0.7.
|
|
23
|
+
"@kamaalio/kamaal": "^0.7.8",
|
|
24
24
|
"fast-glob": "^3.3.3",
|
|
25
25
|
"neverthrow": "^8.2.0"
|
|
26
26
|
},
|