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