@kamaalio/codemod-kit 0.0.21 → 0.0.23
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 +51 -3
- package/dist/index.js +51 -3
- 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,9 +166,57 @@ 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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
+
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
174
|
+
return kamaal_namespaceObject.arrays.compactMap(valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue)), (transformedValueWithMetaVariablesReplaced)=>{
|
|
175
|
+
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
176
|
+
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
177
|
+
}).concat(edits);
|
|
178
|
+
}).flat(1);
|
|
179
|
+
}
|
|
180
|
+
function extractMetaVariables(node, rule) {
|
|
181
|
+
const pattern = rule.pattern?.toString();
|
|
182
|
+
if (null == pattern) return {};
|
|
183
|
+
const metaVarRegex = /\$(\$\$)?([A-Z]+)/g;
|
|
184
|
+
const patternMetaVars = [];
|
|
185
|
+
let match;
|
|
186
|
+
while(null !== (match = metaVarRegex.exec(pattern))){
|
|
187
|
+
const isMultiple = null != match[1];
|
|
188
|
+
const varName = match[2];
|
|
189
|
+
patternMetaVars.push({
|
|
190
|
+
name: varName,
|
|
191
|
+
fullMatch: match[0],
|
|
192
|
+
patternIndex: match.index,
|
|
193
|
+
isMultiple
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (0 === patternMetaVars.length) return {};
|
|
197
|
+
let regexPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
198
|
+
for (const metaVar of patternMetaVars){
|
|
199
|
+
const escapedFullMatch = metaVar.fullMatch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
200
|
+
regexPattern = metaVar.isMultiple ? regexPattern.replace(escapedFullMatch, '(.*?)') : regexPattern.replace(escapedFullMatch, '(.+?)');
|
|
201
|
+
}
|
|
202
|
+
const nodeText = node.text();
|
|
203
|
+
const textMatch = nodeText.match(new RegExp(regexPattern));
|
|
204
|
+
if (null == textMatch) return {};
|
|
205
|
+
const metaVariables = {};
|
|
206
|
+
for(let index = 0; index < patternMetaVars.length; index += 1){
|
|
207
|
+
const metaVar = patternMetaVars[index];
|
|
208
|
+
const capturedValue = textMatch[index + 1];
|
|
209
|
+
if (!capturedValue) continue;
|
|
210
|
+
const valueStart = nodeText.indexOf(capturedValue);
|
|
211
|
+
const valueEnd = valueStart + capturedValue.length;
|
|
212
|
+
metaVariables[metaVar.name] = {
|
|
213
|
+
start: valueStart,
|
|
214
|
+
end: valueEnd,
|
|
215
|
+
value: capturedValue,
|
|
216
|
+
original: metaVar.fullMatch
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return metaVariables;
|
|
172
220
|
}
|
|
173
221
|
function findAndReplace(content, rule, transformer) {
|
|
174
222
|
const root = content.root();
|
package/dist/index.js
CHANGED
|
@@ -120,9 +120,57 @@ 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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
const metaVariables = Object.values(extractMetaVariables(node, rule));
|
|
128
|
+
return arrays.compactMap(valuesToTransform.map((transformedValue)=>metaVariables.reduce((acc, { original, value })=>acc.replaceAll(original, value), transformedValue)), (transformedValueWithMetaVariablesReplaced)=>{
|
|
129
|
+
if (transformedValueWithMetaVariablesReplaced === node.text()) return null;
|
|
130
|
+
return node.replace(transformedValueWithMetaVariablesReplaced);
|
|
131
|
+
}).concat(edits);
|
|
132
|
+
}).flat(1);
|
|
133
|
+
}
|
|
134
|
+
function extractMetaVariables(node, rule) {
|
|
135
|
+
const pattern = rule.pattern?.toString();
|
|
136
|
+
if (null == pattern) return {};
|
|
137
|
+
const metaVarRegex = /\$(\$\$)?([A-Z]+)/g;
|
|
138
|
+
const patternMetaVars = [];
|
|
139
|
+
let match;
|
|
140
|
+
while(null !== (match = metaVarRegex.exec(pattern))){
|
|
141
|
+
const isMultiple = null != match[1];
|
|
142
|
+
const varName = match[2];
|
|
143
|
+
patternMetaVars.push({
|
|
144
|
+
name: varName,
|
|
145
|
+
fullMatch: match[0],
|
|
146
|
+
patternIndex: match.index,
|
|
147
|
+
isMultiple
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (0 === patternMetaVars.length) return {};
|
|
151
|
+
let regexPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
152
|
+
for (const metaVar of patternMetaVars){
|
|
153
|
+
const escapedFullMatch = metaVar.fullMatch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
154
|
+
regexPattern = metaVar.isMultiple ? regexPattern.replace(escapedFullMatch, '(.*?)') : regexPattern.replace(escapedFullMatch, '(.+?)');
|
|
155
|
+
}
|
|
156
|
+
const nodeText = node.text();
|
|
157
|
+
const textMatch = nodeText.match(new RegExp(regexPattern));
|
|
158
|
+
if (null == textMatch) return {};
|
|
159
|
+
const metaVariables = {};
|
|
160
|
+
for(let index = 0; index < patternMetaVars.length; index += 1){
|
|
161
|
+
const metaVar = patternMetaVars[index];
|
|
162
|
+
const capturedValue = textMatch[index + 1];
|
|
163
|
+
if (!capturedValue) continue;
|
|
164
|
+
const valueStart = nodeText.indexOf(capturedValue);
|
|
165
|
+
const valueEnd = valueStart + capturedValue.length;
|
|
166
|
+
metaVariables[metaVar.name] = {
|
|
167
|
+
start: valueStart,
|
|
168
|
+
end: valueEnd,
|
|
169
|
+
value: capturedValue,
|
|
170
|
+
original: metaVar.fullMatch
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return metaVariables;
|
|
126
174
|
}
|
|
127
175
|
function findAndReplace(content, rule, transformer) {
|
|
128
176
|
const root = content.root();
|