@kamaalio/codemod-kit 0.0.22 → 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.
@@ -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,16 @@ 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 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);
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
+ 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);
173
179
  }
174
180
  function extractMetaVariables(node, rule) {
175
181
  const pattern = rule.pattern?.toString();
package/dist/index.js CHANGED
@@ -120,10 +120,16 @@ 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 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);
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
+ 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);
127
133
  }
128
134
  function extractMetaVariables(node, rule) {
129
135
  const pattern = rule.pattern?.toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",