@kamaalio/codemod-kit 0.0.18 → 0.0.20

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
@@ -50,7 +50,7 @@ A utility function for finding and replacing AST nodes based on a rule.
50
50
 
51
51
  - `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
52
52
  - `rule`: A `Rule<TypesMap>` object defining the pattern to search for.
53
- - `transformer`: A function that takes a matched node and returns an optional string replacement.
53
+ - `transformer`: A function that takes a matched node and returns an optional string replacement, or a string for direct replacement.
54
54
 
55
55
  Returns the transformed content as a string with all matching nodes replaced.
56
56
 
@@ -65,11 +65,20 @@ function oldFunction() {
65
65
  `;
66
66
 
67
67
  const ast = await parseAsync('javascript', code);
68
- const result = findAndReplace(
68
+
69
+ // Using a function transformer
70
+ const result1 = findAndReplace(
69
71
  ast,
70
72
  { pattern: 'function oldFunction() { $$$ }' },
71
73
  node => 'function newFunction() { return "hello world"; }',
72
74
  );
75
+
76
+ // Using a string transformer
77
+ const result2 = findAndReplace(
78
+ ast,
79
+ { pattern: 'function oldFunction() { $$$ }' },
80
+ 'function newFunction() { return "hello world"; }',
81
+ );
73
82
  ```
74
83
 
75
84
  ### `findAndReplaceEdits(content, rule, transformer)`
@@ -78,7 +87,7 @@ A utility function for finding AST nodes and generating edit operations without
78
87
 
79
88
  - `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
80
89
  - `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.
90
+ - `transformer`: A function that takes a matched node and returns an optional string replacement, or a string for direct replacement.
82
91
 
83
92
  Returns an array of `Edit` objects that can be committed later using `commitEdits()`.
84
93
 
@@ -93,14 +102,23 @@ function oldFunction() {
93
102
  `;
94
103
 
95
104
  const ast = await parseAsync('javascript', code);
96
- const edits = findAndReplaceEdits(
105
+
106
+ // Using a function transformer
107
+ const edits1 = findAndReplaceEdits(
97
108
  ast,
98
109
  { pattern: 'function oldFunction() { $$$ }' },
99
110
  node => 'function newFunction() { return "hello world"; }',
100
111
  );
101
112
 
113
+ // Using a string transformer
114
+ const edits2 = findAndReplaceEdits(
115
+ ast,
116
+ { pattern: 'function oldFunction() { $$$ }' },
117
+ 'function newFunction() { return "hello world"; }',
118
+ );
119
+
102
120
  // Commit the edits later
103
- const result = ast.root().commitEdits(edits);
121
+ const result = ast.root().commitEdits(edits1);
104
122
  ```
105
123
 
106
124
  ### `findAndReplaceConfig(content, lang, config)`
@@ -109,7 +127,7 @@ A utility function for applying multiple find-and-replace operations sequentiall
109
127
 
110
128
  - `content`: An `SgRoot<TypesMap>` object representing the parsed AST.
111
129
  - `lang`: A `NapiLang` value specifying the language for re-parsing after each transformation.
112
- - `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially.
130
+ - `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially. The `transformer` can be either a function or a string.
113
131
 
114
132
  Returns the final transformed content as a string after applying all transformations.
115
133
 
@@ -132,7 +150,7 @@ const result = await findAndReplaceConfig(ast, 'javascript', [
132
150
  },
133
151
  {
134
152
  rule: { pattern: 'const value = $VAL' },
135
- transformer: node => 'const value = 100',
153
+ transformer: 'const value = 100', // String transformer
136
154
  },
137
155
  ]);
138
156
  ```
@@ -142,7 +160,7 @@ const result = await findAndReplaceConfig(ast, 'javascript', [
142
160
  A utility function for applying multiple find-and-replace operations sequentially on a `Modifications` object.
143
161
 
144
162
  - `modifications`: A `Modifications` object containing the AST, language, and transformation history.
145
- - `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially.
163
+ - `config`: An array of objects containing `rule` and `transformer` pairs to apply sequentially. The `transformer` can be either a function or a string.
146
164
 
147
165
  Returns a `Promise<Modifications>` with the updated AST, accumulated edit count, and transformation history.
148
166
 
@@ -173,7 +191,7 @@ const result = await findAndReplaceConfigModifications(initialModifications, [
173
191
  },
174
192
  {
175
193
  rule: { pattern: 'const value = $VAL' },
176
- transformer: node => 'const value = 100',
194
+ transformer: 'const value = 100', // String transformer
177
195
  },
178
196
  ]);
179
197
 
@@ -1,2 +1,2 @@
1
1
  export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, } from './utils.js';
2
- export type { Codemod, Modifications } from './types.js';
2
+ export type { Codemod, Modifications, FindAndReplaceConfig } from './types.js';
@@ -1,6 +1,6 @@
1
- import type { SgRoot } from '@ast-grep/napi';
1
+ import type { Rule, SgNode, SgRoot } from '@ast-grep/napi';
2
2
  import type { NapiLang } from '@ast-grep/napi/types/lang.js';
3
- import type { TypesMap } from '@ast-grep/napi/types/staticTypes.js';
3
+ import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
4
4
  import type { Optional } from '../utils/type-utils.js';
5
5
  export type Codemod = {
6
6
  name: string;
@@ -17,3 +17,7 @@ export type Modifications = {
17
17
  filename: Optional<string>;
18
18
  history: Array<SgRoot<TypesMap>>;
19
19
  };
20
+ export type FindAndReplaceConfig = {
21
+ rule: Rule<TypesMap>;
22
+ transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>) | string;
23
+ };
@@ -1,9 +1,8 @@
1
1
  import { type Result } from 'neverthrow';
2
- import { type Rule, type Edit, type SgNode, type SgRoot } from '@ast-grep/napi';
3
- import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
2
+ import { type Edit, type SgRoot } from '@ast-grep/napi';
3
+ import type { TypesMap } from '@ast-grep/napi/types/staticTypes.js';
4
4
  import type { NapiLang } from '@ast-grep/napi/types/lang.js';
5
- import type { Codemod, Modifications } from './types.js';
6
- import type { Optional } from '../utils/type-utils.js';
5
+ import type { Codemod, FindAndReplaceConfig, Modifications } from './types.js';
7
6
  type RunCodemodHooks<C extends Codemod> = {
8
7
  targetFiltering?: (filepath: string, codemod: C) => boolean;
9
8
  preCodemodRun?: (codemod: C) => Promise<void>;
@@ -22,15 +21,9 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
22
21
  hasChanges: boolean;
23
22
  content: string;
24
23
  }, Error>>>;
25
- export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<{
26
- rule: Rule<TypesMap>;
27
- transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>;
28
- }>): Promise<Modifications>;
29
- export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<{
30
- rule: Rule<TypesMap>;
31
- transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>;
32
- }>): Promise<string>;
33
- export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): Array<Edit>;
34
- export declare function findAndReplace(content: SgRoot<TypesMap>, rule: Rule<TypesMap>, transformer: (node: SgNode<TypesMap, Kinds<TypesMap>>) => Optional<string>): string;
24
+ export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<FindAndReplaceConfig>): Promise<Modifications>;
25
+ export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<FindAndReplaceConfig>): Promise<string>;
26
+ export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): Array<Edit>;
27
+ export declare function findAndReplace(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): string;
35
28
  export declare function commitEditModifications(edits: Array<Edit>, modifications: Modifications): Promise<Modifications>;
36
29
  export {};
package/dist/index.cjs CHANGED
@@ -164,8 +164,9 @@ function findAndReplaceEdits(content, rule, transformer) {
164
164
  rule
165
165
  });
166
166
  return kamaal_namespaceObject.arrays.compactMap(nodes, (node)=>{
167
- const transformed = transformer(node);
167
+ const transformed = 'string' == typeof transformer ? transformer : transformer(node);
168
168
  if (null == transformed) return null;
169
+ if (transformed === node.text()) return null;
169
170
  return node.replace(transformed);
170
171
  });
171
172
  }
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, type Codemod, type Modifications, } from './codemods/index.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, type Codemod, type Modifications, type FindAndReplaceConfig, } from './codemods/index.js';
package/dist/index.js CHANGED
@@ -118,8 +118,9 @@ function findAndReplaceEdits(content, rule, transformer) {
118
118
  rule
119
119
  });
120
120
  return arrays.compactMap(nodes, (node)=>{
121
- const transformed = transformer(node);
121
+ const transformed = 'string' == typeof transformer ? transformer : transformer(node);
122
122
  if (null == transformed) return null;
123
+ if (transformed === node.text()) return null;
123
124
  return node.replace(transformed);
124
125
  });
125
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",