@kamaalio/codemod-kit 0.0.25 → 0.0.27

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,2 +1,2 @@
1
- export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, } from './utils.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, traverseUp, } from './utils.js';
2
2
  export type { Codemod, Modifications, FindAndReplaceConfig } from './types.js';
@@ -1,11 +1,12 @@
1
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
- import type { Optional } from '../utils/type-utils.js';
4
+ import type { types } from '@kamaalio/kamaal';
5
5
  export type Codemod = {
6
6
  name: string;
7
7
  languages: Set<NapiLang> | Array<NapiLang>;
8
- transformer: (content: string, filename?: Optional<string>) => Promise<string>;
8
+ transformer: (content: string, filename?: types.Optional<string>) => Promise<string>;
9
+ postTransform?: (rootPath: string) => Promise<void>;
9
10
  };
10
11
  export type ModificationsReport = {
11
12
  changesApplied: number;
@@ -14,10 +15,10 @@ export type Modifications = {
14
15
  ast: SgRoot<TypesMap>;
15
16
  report: ModificationsReport;
16
17
  lang: NapiLang;
17
- filename: Optional<string>;
18
+ filename: types.Optional<string>;
18
19
  history: Array<SgRoot<TypesMap>>;
19
20
  };
20
21
  export type FindAndReplaceConfig = {
21
22
  rule: Rule<TypesMap>;
22
- transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => Optional<Edit | string> | Array<Edit | string>) | string;
23
+ transformer: ((node: SgNode<TypesMap, Kinds<TypesMap>>, rule: Rule<TypesMap>) => types.Optional<Edit | string> | Array<Edit | string>) | string;
23
24
  };
@@ -1,7 +1,8 @@
1
1
  import { type Result } from 'neverthrow';
2
- import { type Edit, type SgRoot } from '@ast-grep/napi';
3
- import type { TypesMap } from '@ast-grep/napi/types/staticTypes.js';
2
+ import { type Edit, type SgRoot, type SgNode } from '@ast-grep/napi';
3
+ import type { Kinds, TypesMap } from '@ast-grep/napi/types/staticTypes.js';
4
4
  import type { NapiLang } from '@ast-grep/napi/types/lang.js';
5
+ import { type types } from '@kamaalio/kamaal';
5
6
  import type { Codemod, FindAndReplaceConfig, Modifications } from './types.js';
6
7
  type RunCodemodHooks<C extends Codemod> = {
7
8
  targetFiltering?: (filepath: string, codemod: C) => boolean;
@@ -12,6 +13,7 @@ type RunCodemodOptions<C extends Codemod> = {
12
13
  hooks?: RunCodemodHooks<C>;
13
14
  log?: boolean;
14
15
  dry?: boolean;
16
+ rootPaths?: Array<string>;
15
17
  };
16
18
  export declare function runCodemods<C extends Codemod>(codemods: Array<C>, transformationPath: string, options?: RunCodemodOptions<C>): Promise<Record<string, Array<Result<{
17
19
  hasChanges: boolean;
@@ -21,6 +23,7 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
21
23
  hasChanges: boolean;
22
24
  content: string;
23
25
  }, Error>>>;
26
+ export declare function traverseUp(node: SgNode<TypesMap, Kinds<TypesMap>>, until: (node: SgNode<TypesMap, Kinds<TypesMap>>) => boolean): types.Optional<SgNode<TypesMap, Kinds<TypesMap>>>;
24
27
  export declare function findAndReplaceConfigModifications(modifications: Modifications, config: Array<FindAndReplaceConfig>): Promise<Modifications>;
25
28
  export declare function findAndReplaceConfig(content: SgRoot<TypesMap>, lang: NapiLang, config: Array<FindAndReplaceConfig>): Promise<string>;
26
29
  export declare function findAndReplaceEdits(content: SgRoot<TypesMap>, rule: FindAndReplaceConfig['rule'], transformer: FindAndReplaceConfig['transformer']): Array<Edit>;
package/dist/index.cjs CHANGED
@@ -33,13 +33,14 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
+ findAndReplace: ()=>findAndReplace,
37
+ findAndReplaceConfig: ()=>findAndReplaceConfig,
36
38
  commitEditModifications: ()=>commitEditModifications,
37
- runCodemods: ()=>runCodemods,
38
39
  runCodemod: ()=>runCodemod,
39
- findAndReplace: ()=>findAndReplace,
40
40
  findAndReplaceConfigModifications: ()=>findAndReplaceConfigModifications,
41
41
  findAndReplaceEdits: ()=>findAndReplaceEdits,
42
- findAndReplaceConfig: ()=>findAndReplaceConfig
42
+ runCodemods: ()=>runCodemods,
43
+ traverseUp: ()=>traverseUp
43
44
  });
44
45
  const external_node_path_namespaceObject = require("node:path");
45
46
  var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
@@ -88,7 +89,7 @@ async function runCodemods(codemods, transformationPath, options) {
88
89
  return results;
89
90
  }
90
91
  async function runCodemod(codemod, transformationPath, options) {
91
- const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
92
+ const { hooks, log: enableLogging, dry: runInDryMode, rootPaths } = defaultedOptions(options);
92
93
  await hooks.preCodemodRun(codemod);
93
94
  const globItems = await external_fast_glob_default().glob([
94
95
  '**/*'
@@ -106,7 +107,7 @@ async function runCodemod(codemod, transformationPath, options) {
106
107
  });
107
108
  if (0 === targets.length) return [];
108
109
  if (enableLogging) console.log(`\u{1F9C9} '${codemod.name}' targeting ${targets.length} ${1 === targets.length ? 'file' : 'files'} to transform, chill and grab some mat\xe9`);
109
- return Promise.all(targets.map(async (filepath)=>{
110
+ const results = await Promise.all(targets.map(async (filepath)=>{
110
111
  const fullPath = external_node_path_default().join(transformationPath, filepath);
111
112
  try {
112
113
  const content = await promises_default().readFile(fullPath, {
@@ -130,6 +131,23 @@ async function runCodemod(codemod, transformationPath, options) {
130
131
  return (0, external_neverthrow_namespaceObject.err)(error);
131
132
  }
132
133
  }));
134
+ await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
135
+ return results;
136
+ }
137
+ function traverseUp(node, until) {
138
+ let current = node.parent();
139
+ if (null == current) return null;
140
+ while(null != current){
141
+ const next = current.parent();
142
+ if (null == next) break;
143
+ if (until(next)) {
144
+ current = next;
145
+ break;
146
+ }
147
+ current = next;
148
+ }
149
+ if (!until(current)) return null;
150
+ return current;
133
151
  }
134
152
  async function findAndReplaceConfigModifications(modifications, config) {
135
153
  let currentModifications = {
@@ -246,7 +264,8 @@ function defaultedOptions(options) {
246
264
  return {
247
265
  hooks: defaultedHooks(options?.hooks),
248
266
  log: options?.log ?? true,
249
- dry: options?.dry ?? false
267
+ dry: options?.dry ?? false,
268
+ rootPaths: options?.rootPaths ?? []
250
269
  };
251
270
  }
252
271
  function defaultedHooks(hooks) {
@@ -266,6 +285,7 @@ exports.findAndReplaceConfigModifications = __webpack_exports__.findAndReplaceCo
266
285
  exports.findAndReplaceEdits = __webpack_exports__.findAndReplaceEdits;
267
286
  exports.runCodemod = __webpack_exports__.runCodemod;
268
287
  exports.runCodemods = __webpack_exports__.runCodemods;
288
+ exports.traverseUp = __webpack_exports__.traverseUp;
269
289
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
270
290
  "commitEditModifications",
271
291
  "findAndReplace",
@@ -273,7 +293,8 @@ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
273
293
  "findAndReplaceConfigModifications",
274
294
  "findAndReplaceEdits",
275
295
  "runCodemod",
276
- "runCodemods"
296
+ "runCodemods",
297
+ "traverseUp"
277
298
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
278
299
  Object.defineProperty(exports, '__esModule', {
279
300
  value: true
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, type Codemod, type Modifications, type FindAndReplaceConfig, } from './codemods/index.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, findAndReplace, findAndReplaceEdits, findAndReplaceConfig, findAndReplaceConfigModifications, traverseUp, type Codemod, type Modifications, type FindAndReplaceConfig, } from './codemods/index.js';
package/dist/index.js CHANGED
@@ -42,7 +42,7 @@ async function runCodemods(codemods, transformationPath, options) {
42
42
  return results;
43
43
  }
44
44
  async function runCodemod(codemod, transformationPath, options) {
45
- const { hooks, log: enableLogging, dry: runInDryMode } = defaultedOptions(options);
45
+ const { hooks, log: enableLogging, dry: runInDryMode, rootPaths } = defaultedOptions(options);
46
46
  await hooks.preCodemodRun(codemod);
47
47
  const globItems = await fast_glob.glob([
48
48
  '**/*'
@@ -60,7 +60,7 @@ async function runCodemod(codemod, transformationPath, options) {
60
60
  });
61
61
  if (0 === targets.length) return [];
62
62
  if (enableLogging) console.log(`\u{1F9C9} '${codemod.name}' targeting ${targets.length} ${1 === targets.length ? 'file' : 'files'} to transform, chill and grab some mat\xe9`);
63
- return Promise.all(targets.map(async (filepath)=>{
63
+ const results = await Promise.all(targets.map(async (filepath)=>{
64
64
  const fullPath = node_path.join(transformationPath, filepath);
65
65
  try {
66
66
  const content = await promises.readFile(fullPath, {
@@ -84,6 +84,23 @@ async function runCodemod(codemod, transformationPath, options) {
84
84
  return err(error);
85
85
  }
86
86
  }));
87
+ await Promise.all(rootPaths.map((rootPath)=>(codemod.postTransform ?? (async ()=>{}))(rootPath)));
88
+ return results;
89
+ }
90
+ function traverseUp(node, until) {
91
+ let current = node.parent();
92
+ if (null == current) return null;
93
+ while(null != current){
94
+ const next = current.parent();
95
+ if (null == next) break;
96
+ if (until(next)) {
97
+ current = next;
98
+ break;
99
+ }
100
+ current = next;
101
+ }
102
+ if (!until(current)) return null;
103
+ return current;
87
104
  }
88
105
  async function findAndReplaceConfigModifications(modifications, config) {
89
106
  let currentModifications = {
@@ -200,7 +217,8 @@ function defaultedOptions(options) {
200
217
  return {
201
218
  hooks: defaultedHooks(options?.hooks),
202
219
  log: options?.log ?? true,
203
- dry: options?.dry ?? false
220
+ dry: options?.dry ?? false,
221
+ rootPaths: options?.rootPaths ?? []
204
222
  };
205
223
  }
206
224
  function defaultedHooks(hooks) {
@@ -213,4 +231,4 @@ function defaultedHooks(hooks) {
213
231
  preCodemodRun
214
232
  };
215
233
  }
216
- export { commitEditModifications, findAndReplace, findAndReplaceConfig, findAndReplaceConfigModifications, findAndReplaceEdits, runCodemod, runCodemods };
234
+ export { commitEditModifications, findAndReplace, findAndReplaceConfig, findAndReplaceConfigModifications, findAndReplaceEdits, runCodemod, runCodemods, traverseUp };
@@ -1,4 +1,3 @@
1
- export type Optional<T> = T | undefined | null;
2
1
  export type ReplaceObjectProperty<A extends Record<string, unknown>, K extends keyof A, B> = Omit<A, K> & {
3
2
  [P in K]: B;
4
3
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "@ast-grep/napi": "^0.38.5",
23
- "@kamaalio/kamaal": "^0.7.6",
23
+ "@kamaalio/kamaal": "^0.7.8",
24
24
  "fast-glob": "^3.3.3",
25
25
  "neverthrow": "^8.2.0"
26
26
  },