@kamaalio/codemod-kit 0.0.22 → 0.0.24
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/types.d.ts +2 -2
- package/dist/index.cjs +11 -4
- package/dist/index.js +11 -4
- package/package.json +1 -1
package/dist/codemods/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Rule, SgNode, SgRoot } from '@ast-grep/napi';
|
|
1
|
+
import type { Edit, Rule, SgNode, SgRoot } from '@ast-grep/napi';
|
|
2
2
|
import type { NapiLang } from '@ast-grep/napi/types/lang.js';
|
|
3
3
|
import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
|
|
4
4
|
import type { Optional } from '../utils/type-utils.js';
|
|
@@ -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>) | string;
|
|
22
|
+
transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => Optional<string> | Array<Edit | string>) | string;
|
|
23
23
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -166,10 +166,17 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
166
166
|
return kamaal_namespaceObject.arrays.compactMap(nodes, (node)=>{
|
|
167
167
|
const transformed = 'string' == typeof transformer ? transformer : transformer(node, rule);
|
|
168
168
|
if (null == transformed) return null;
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
169
|
+
const edits = [];
|
|
170
|
+
const valuesToTransform = [];
|
|
171
|
+
if (Array.isArray(transformed)) for (const item of transformed)if ('string' == typeof item) valuesToTransform.push(item);
|
|
172
|
+
else edits.push(item);
|
|
173
|
+
else valuesToTransform.push(transformed);
|
|
174
|
+
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
175
|
+
return kamaal_namespaceObject.arrays.compactMap(valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue)), (transformedValueWithMetaVariablesReplaced)=>{
|
|
176
|
+
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
177
|
+
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
178
|
+
}).concat(edits);
|
|
179
|
+
}).flat(1);
|
|
173
180
|
}
|
|
174
181
|
function extractMetaVariables(node, rule) {
|
|
175
182
|
const pattern = rule.pattern?.toString();
|
package/dist/index.js
CHANGED
|
@@ -120,10 +120,17 @@ function findAndReplaceEdits(content, rule, transformer) {
|
|
|
120
120
|
return arrays.compactMap(nodes, (node)=>{
|
|
121
121
|
const transformed = 'string' == typeof transformer ? transformer : transformer(node, rule);
|
|
122
122
|
if (null == transformed) return null;
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
const edits = [];
|
|
124
|
+
const valuesToTransform = [];
|
|
125
|
+
if (Array.isArray(transformed)) for (const item of transformed)if ('string' == typeof item) valuesToTransform.push(item);
|
|
126
|
+
else edits.push(item);
|
|
127
|
+
else valuesToTransform.push(transformed);
|
|
128
|
+
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
129
|
+
return arrays.compactMap(valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue)), (transformedValueWithMetaVariablesReplaced)=>{
|
|
130
|
+
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
131
|
+
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
132
|
+
}).concat(edits);
|
|
133
|
+
}).flat(1);
|
|
127
134
|
}
|
|
128
135
|
function extractMetaVariables(node, rule) {
|
|
129
136
|
const pattern = rule.pattern?.toString();
|