@kamaalio/codemod-kit 0.0.13 → 0.0.14

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 } from './utils.js';
1
+ export { runCodemods, runCodemod, commitEditModifications } from './utils.js';
2
2
  export type { Codemod, Modifications } from './types.js';
@@ -1,5 +1,6 @@
1
1
  import { type Result } from 'neverthrow';
2
- import type { Codemod } from './types.js';
2
+ import { type Edit } from '@ast-grep/napi';
3
+ import type { Codemod, Modifications } from './types.js';
3
4
  type RunCodemodHooks<C extends Codemod> = {
4
5
  targetFiltering?: (filepath: string, codemod: C) => boolean;
5
6
  preCodemodRun?: (codemod: C) => Promise<void>;
@@ -18,4 +19,5 @@ export declare function runCodemod<C extends Codemod>(codemod: C, transformation
18
19
  hasChanges: boolean;
19
20
  content: string;
20
21
  }, Error>>>;
22
+ export declare function commitEditModifications(edits: Array<Edit>, modifications: Modifications): Promise<Modifications>;
21
23
  export {};
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
+ commitEditModifications: ()=>commitEditModifications,
36
37
  runCodemod: ()=>runCodemod,
37
38
  runCodemods: ()=>runCodemods
38
39
  });
@@ -125,6 +126,22 @@ async function runCodemod(codemod, transformationPath, options) {
125
126
  }
126
127
  }));
127
128
  }
129
+ async function commitEditModifications(edits, modifications) {
130
+ if (0 === edits.length) return modifications;
131
+ const root = modifications.ast.root();
132
+ const committed = root.commitEdits(edits);
133
+ const modifiedAST = await (0, napi_namespaceObject.parseAsync)(modifications.lang, committed);
134
+ return {
135
+ ...modifications,
136
+ ast: modifiedAST,
137
+ report: {
138
+ changesApplied: modifications.report.changesApplied + edits.length
139
+ },
140
+ history: modifications.history.concat([
141
+ modifiedAST
142
+ ])
143
+ };
144
+ }
128
145
  function defaultedOptions(options) {
129
146
  return {
130
147
  hooks: defaultedHooks(options?.hooks),
@@ -142,9 +159,11 @@ function defaultedHooks(hooks) {
142
159
  preCodemodRun
143
160
  };
144
161
  }
162
+ exports.commitEditModifications = __webpack_exports__.commitEditModifications;
145
163
  exports.runCodemod = __webpack_exports__.runCodemod;
146
164
  exports.runCodemods = __webpack_exports__.runCodemods;
147
165
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
166
+ "commitEditModifications",
148
167
  "runCodemod",
149
168
  "runCodemods"
150
169
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { runCodemods, runCodemod, type Codemod, type Modifications } from './codemods/index.js';
1
+ export { runCodemods, runCodemod, commitEditModifications, type Codemod, type Modifications, } from './codemods/index.js';
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import node_path from "node:path";
2
2
  import promises from "node:fs/promises";
3
3
  import fast_glob from "fast-glob";
4
4
  import { err, ok } from "neverthrow";
5
- import { Lang } from "@ast-grep/napi";
5
+ import { Lang, parseAsync } from "@ast-grep/napi";
6
6
  const JAVASCRIPT_EXTENSIONS = [
7
7
  '.js',
8
8
  '.cjs',
@@ -84,6 +84,22 @@ async function runCodemod(codemod, transformationPath, options) {
84
84
  }
85
85
  }));
86
86
  }
87
+ async function commitEditModifications(edits, modifications) {
88
+ if (0 === edits.length) return modifications;
89
+ const root = modifications.ast.root();
90
+ const committed = root.commitEdits(edits);
91
+ const modifiedAST = await parseAsync(modifications.lang, committed);
92
+ return {
93
+ ...modifications,
94
+ ast: modifiedAST,
95
+ report: {
96
+ changesApplied: modifications.report.changesApplied + edits.length
97
+ },
98
+ history: modifications.history.concat([
99
+ modifiedAST
100
+ ])
101
+ };
102
+ }
87
103
  function defaultedOptions(options) {
88
104
  return {
89
105
  hooks: defaultedHooks(options?.hooks),
@@ -101,4 +117,4 @@ function defaultedHooks(hooks) {
101
117
  preCodemodRun
102
118
  };
103
119
  }
104
- export { runCodemod, runCodemods };
120
+ export { commitEditModifications, runCodemod, runCodemods };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaalio/codemod-kit",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",