@kamaalio/codemod-kit 0.0.12 → 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 +1 @@
1
- export declare const LANG_TO_EXTENSIONS_MAPPING: Partial<Record<string, Set<string>>>;
1
+ export declare const LANG_TO_EXTENSIONS_MAPPING: Record<string, Set<string>>;
@@ -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
  });
@@ -44,17 +45,31 @@ const external_fast_glob_namespaceObject = require("fast-glob");
44
45
  var external_fast_glob_default = /*#__PURE__*/ __webpack_require__.n(external_fast_glob_namespaceObject);
45
46
  const external_neverthrow_namespaceObject = require("neverthrow");
46
47
  const napi_namespaceObject = require("@ast-grep/napi");
47
- const LANG_TO_EXTENSIONS_MAPPING = {
48
- [napi_namespaceObject.Lang.TypeScript.toLowerCase()]: new Set([
49
- '.ts',
50
- '.tsx',
51
- '.js',
52
- '.jsx',
53
- '.cjs',
54
- '.mjs',
55
- '.mts'
56
- ])
57
- };
48
+ const JAVASCRIPT_EXTENSIONS = [
49
+ '.js',
50
+ '.cjs',
51
+ '.mjs'
52
+ ];
53
+ const TYPESCRIPT_EXTENSIONS = JAVASCRIPT_EXTENSIONS.concat([
54
+ '.ts',
55
+ '.mts'
56
+ ]);
57
+ const JSX_EXTENSIONS = [
58
+ '.jsx'
59
+ ];
60
+ const LANG_TO_EXTENSIONS_MAPPING = Object.fromEntries(Object.entries({
61
+ [napi_namespaceObject.Lang.TypeScript]: TYPESCRIPT_EXTENSIONS,
62
+ ts: TYPESCRIPT_EXTENSIONS,
63
+ [napi_namespaceObject.Lang.Tsx]: JSX_EXTENSIONS.concat([
64
+ '.tsx'
65
+ ]),
66
+ jsx: JSX_EXTENSIONS,
67
+ [napi_namespaceObject.Lang.JavaScript]: JAVASCRIPT_EXTENSIONS,
68
+ js: JAVASCRIPT_EXTENSIONS
69
+ }).map(([key, value])=>[
70
+ key.toLowerCase(),
71
+ new Set(value)
72
+ ]));
58
73
  function getCollectionCount(collection) {
59
74
  if (Array.isArray(collection)) return collection.length;
60
75
  return collection.size;
@@ -111,6 +126,22 @@ async function runCodemod(codemod, transformationPath, options) {
111
126
  }
112
127
  }));
113
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
+ }
114
145
  function defaultedOptions(options) {
115
146
  return {
116
147
  hooks: defaultedHooks(options?.hooks),
@@ -128,9 +159,11 @@ function defaultedHooks(hooks) {
128
159
  preCodemodRun
129
160
  };
130
161
  }
162
+ exports.commitEditModifications = __webpack_exports__.commitEditModifications;
131
163
  exports.runCodemod = __webpack_exports__.runCodemod;
132
164
  exports.runCodemods = __webpack_exports__.runCodemods;
133
165
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
166
+ "commitEditModifications",
134
167
  "runCodemod",
135
168
  "runCodemods"
136
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,18 +2,32 @@ 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";
6
- const LANG_TO_EXTENSIONS_MAPPING = {
7
- [Lang.TypeScript.toLowerCase()]: new Set([
8
- '.ts',
9
- '.tsx',
10
- '.js',
11
- '.jsx',
12
- '.cjs',
13
- '.mjs',
14
- '.mts'
15
- ])
16
- };
5
+ import { Lang, parseAsync } from "@ast-grep/napi";
6
+ const JAVASCRIPT_EXTENSIONS = [
7
+ '.js',
8
+ '.cjs',
9
+ '.mjs'
10
+ ];
11
+ const TYPESCRIPT_EXTENSIONS = JAVASCRIPT_EXTENSIONS.concat([
12
+ '.ts',
13
+ '.mts'
14
+ ]);
15
+ const JSX_EXTENSIONS = [
16
+ '.jsx'
17
+ ];
18
+ const LANG_TO_EXTENSIONS_MAPPING = Object.fromEntries(Object.entries({
19
+ [Lang.TypeScript]: TYPESCRIPT_EXTENSIONS,
20
+ ts: TYPESCRIPT_EXTENSIONS,
21
+ [Lang.Tsx]: JSX_EXTENSIONS.concat([
22
+ '.tsx'
23
+ ]),
24
+ jsx: JSX_EXTENSIONS,
25
+ [Lang.JavaScript]: JAVASCRIPT_EXTENSIONS,
26
+ js: JAVASCRIPT_EXTENSIONS
27
+ }).map(([key, value])=>[
28
+ key.toLowerCase(),
29
+ new Set(value)
30
+ ]));
17
31
  function getCollectionCount(collection) {
18
32
  if (Array.isArray(collection)) return collection.length;
19
33
  return collection.size;
@@ -70,6 +84,22 @@ async function runCodemod(codemod, transformationPath, options) {
70
84
  }
71
85
  }));
72
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
+ }
73
103
  function defaultedOptions(options) {
74
104
  return {
75
105
  hooks: defaultedHooks(options?.hooks),
@@ -87,4 +117,4 @@ function defaultedHooks(hooks) {
87
117
  preCodemodRun
88
118
  };
89
119
  }
90
- 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.12",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "author": "Kamaal Farah",
6
6
  "license": "MIT",