@kamaalio/codemod-kit 0.0.15 → 0.0.16

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/README.md CHANGED
@@ -72,6 +72,37 @@ const result = findAndReplace(
72
72
  );
73
73
  ```
74
74
 
75
+ ### `findAndReplaceEdits(content, rule, transformer)`
76
+
77
+ A utility function for finding AST nodes and generating edit operations without committing them.
78
+
79
+ - `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
80
+ - `rule`: A `Rule<TypesMap>` object defining the pattern to search for.
81
+ - `transformer`: A function that takes a matched node and returns an optional string replacement.
82
+
83
+ Returns an array of `Edit` objects that can be committed later using `commitEdits()`.
84
+
85
+ ```typescript
86
+ import { findAndReplaceEdits } from '@kamaalio/codemod-kit';
87
+ import { parseAsync } from '@ast-grep/napi';
88
+
89
+ const code = `
90
+ function oldFunction() {
91
+ return "hello";
92
+ }
93
+ `;
94
+
95
+ const ast = await parseAsync('javascript', code);
96
+ const edits = findAndReplaceEdits(
97
+ ast,
98
+ { pattern: 'function oldFunction() { $$$ }' },
99
+ node => 'function newFunction() { return "hello world"; }',
100
+ );
101
+
102
+ // Commit the edits later
103
+ const result = ast.root().commitEdits(edits);
104
+ ```
105
+
75
106
  ### `Codemod`
76
107
 
77
108
  A codemod is defined by the `Codemod` type:
@@ -1,2 +1,2 @@
1
- export { runCodemods, runCodemod, commitEditModifications, findAndReplace } from './utils.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits } from './utils.js';
2
2
  export type { Codemod, Modifications } from './types.js';
@@ -21,6 +21,7 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
21
21
  hasChanges: boolean;
22
22
  content: string;
23
23
  }, Error>>>;
24
+ export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): Array<Edit>;
24
25
  export declare function findAndReplace(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): string;
25
26
  export declare function commitEditModifications(edits: Array<Edit>, modifications: Modifications): Promise<Modifications>;
26
27
  export {};
package/dist/index.cjs CHANGED
@@ -36,6 +36,7 @@ __webpack_require__.d(__webpack_exports__, {
36
36
  commitEditModifications: ()=>commitEditModifications,
37
37
  runCodemod: ()=>runCodemod,
38
38
  findAndReplace: ()=>findAndReplace,
39
+ findAndReplaceEdits: ()=>findAndReplaceEdits,
39
40
  runCodemods: ()=>runCodemods
40
41
  });
41
42
  const external_node_path_namespaceObject = require("node:path");
@@ -128,15 +129,19 @@ async function runCodemod(codemod, transformationPath, options) {
128
129
  }
129
130
  }));
130
131
  }
131
- function findAndReplace(content, rule, transformer) {
132
- const root = content.root();
133
- const edits = kamaal_namespaceObject.arrays.compactMap(root.findAll({
132
+ function findAndReplaceEdits(content, rule, transformer) {
133
+ const nodes = content.root().findAll({
134
134
  rule
135
- }), (node)=>{
135
+ });
136
+ return kamaal_namespaceObject.arrays.compactMap(nodes, (node)=>{
136
137
  const transformed = transformer(node);
137
138
  if (null == transformed) return null;
138
139
  return node.replace(transformed);
139
140
  });
141
+ }
142
+ function findAndReplace(content, rule, transformer) {
143
+ const root = content.root();
144
+ const edits = findAndReplaceEdits(content, rule, transformer);
140
145
  return root.commitEdits(edits);
141
146
  }
142
147
  async function commitEditModifications(edits, modifications) {
@@ -174,11 +179,13 @@ function defaultedHooks(hooks) {
174
179
  }
175
180
  exports.commitEditModifications = __webpack_exports__.commitEditModifications;
176
181
  exports.findAndReplace = __webpack_exports__.findAndReplace;
182
+ exports.findAndReplaceEdits = __webpack_exports__.findAndReplaceEdits;
177
183
  exports.runCodemod = __webpack_exports__.runCodemod;
178
184
  exports.runCodemods = __webpack_exports__.runCodemods;
179
185
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
180
186
  "commitEditModifications",
181
187
  "findAndReplace",
188
+ "findAndReplaceEdits",
182
189
  "runCodemod",
183
190
  "runCodemods"
184
191
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { runCodemods, runCodemod, commitEditModifications, findAndReplace, type Codemod, type Modifications, } from './codemods/index.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, type Codemod, type Modifications, } from './codemods/index.js';
package/dist/index.js CHANGED
@@ -85,15 +85,19 @@ async function runCodemod(codemod, transformationPath, options) {
85
85
  }
86
86
  }));
87
87
  }
88
- function findAndReplace(content, rule, transformer) {
89
- const root = content.root();
90
- const edits = arrays.compactMap(root.findAll({
88
+ function findAndReplaceEdits(content, rule, transformer) {
89
+ const nodes = content.root().findAll({
91
90
  rule
92
- }), (node)=>{
91
+ });
92
+ return arrays.compactMap(nodes, (node)=>{
93
93
  const transformed = transformer(node);
94
94
  if (null == transformed) return null;
95
95
  return node.replace(transformed);
96
96
  });
97
+ }
98
+ function findAndReplace(content, rule, transformer) {
99
+ const root = content.root();
100
+ const edits = findAndReplaceEdits(content, rule, transformer);
97
101
  return root.commitEdits(edits);
98
102
  }
99
103
  async function commitEditModifications(edits, modifications) {
@@ -129,4 +133,4 @@ function defaultedHooks(hooks) {
129
133
  preCodemodRun
130
134
  };
131
135
  }
132
- export { commitEditModifications, findAndReplace, runCodemod, runCodemods };
136
+ export { commitEditModifications, findAndReplace, findAndReplaceEdits, runCodemod, runCodemods };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",