@esportsplus/typescript 0.11.0 → 0.12.0

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,5 +1,6 @@
1
1
  import ts from 'typescript';
2
2
  import type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate } from './types.js';
3
+ import program from './program.js';
3
4
  declare const addImport: (code: string, module: string, specifiers: string[]) => string;
4
5
  declare const applyReplacements: (code: string, replacements: Replacement[]) => string;
5
6
  declare const applyReplacementsReverse: (code: string, replacements: Replacement[]) => string;
@@ -9,5 +10,5 @@ declare const uid: (prefix?: string) => string;
9
10
  declare const updateImports: (code: string, modification: ImportModification) => string;
10
11
  declare const visitAst: <T>(sourceFile: ts.SourceFile, callback: VisitorCallback<T>, state: T, predicate?: VisitorPredicate) => T;
11
12
  declare const visitAstWithDepth: <T>(sourceFile: ts.SourceFile, callback: (node: ts.Node, depth: number, state: T) => void, state: T, depthTrigger: (node: ts.Node) => boolean) => T;
12
- export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, uid, updateImports, visitAst, visitAstWithDepth };
13
+ export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, program, uid, updateImports, visitAst, visitAstWithDepth };
13
14
  export type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate };
@@ -1,6 +1,7 @@
1
1
  import { uuid } from '@esportsplus/utilities';
2
2
  import ts from 'typescript';
3
3
  import { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX } from './constants.js';
4
+ import program from './program.js';
4
5
  function buildImportRegex(escapedModule) {
5
6
  return new RegExp(`(import\\s*\\{[^}]*\\}\\s*from\\s*['"]${escapedModule}['"])`);
6
7
  }
@@ -154,4 +155,4 @@ const visitAstWithDepth = (sourceFile, callback, state, depthTrigger) => {
154
155
  visit(sourceFile);
155
156
  return state;
156
157
  };
157
- export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, uid, updateImports, visitAst, visitAstWithDepth };
158
+ export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, program, uid, updateImports, visitAst, visitAstWithDepth };
@@ -0,0 +1,6 @@
1
+ import ts from 'typescript';
2
+ declare const _default: {
3
+ get: (root: string) => ts.Program;
4
+ delete: (root: string) => void;
5
+ };
6
+ export default _default;
@@ -0,0 +1,33 @@
1
+ import path from 'path';
2
+ import ts from 'typescript';
3
+ let cache = new Map();
4
+ function create(root) {
5
+ let tsconfig = ts.findConfigFile(root, ts.sys.fileExists, 'tsconfig.json');
6
+ if (!tsconfig) {
7
+ throw new Error('tsconfig.json not found');
8
+ }
9
+ let file = ts.readConfigFile(tsconfig, ts.sys.readFile);
10
+ if (file.error) {
11
+ throw new Error(`Error reading tsconfig.json: ${file.error.messageText}`);
12
+ }
13
+ let parsed = ts.parseJsonConfigFileContent(file.config, ts.sys, path.dirname(tsconfig));
14
+ if (parsed.errors.length > 0) {
15
+ throw new Error(`Error parsing tsconfig.json: ${parsed.errors[0].messageText}`);
16
+ }
17
+ return ts.createProgram({
18
+ options: parsed.options,
19
+ rootNames: parsed.fileNames
20
+ });
21
+ }
22
+ const get = (root) => {
23
+ let program = cache.get(root);
24
+ if (!program) {
25
+ program = create(root);
26
+ cache.set(root, program);
27
+ }
28
+ return program;
29
+ };
30
+ const del = (root) => {
31
+ cache.delete(root);
32
+ };
33
+ export default { get, delete: del };
package/package.json CHANGED
@@ -7,6 +7,7 @@
7
7
  "dependencies": {
8
8
  "@esportsplus/cli-passthrough": "^0.0.12",
9
9
  "@esportsplus/utilities": "^0.27.2",
10
+ "@types/node": "^25.0.3",
10
11
  "tsc-alias": "^1.8.16",
11
12
  "typescript": "^5.9.3"
12
13
  },
@@ -33,7 +34,7 @@
33
34
  },
34
35
  "type": "module",
35
36
  "types": "build/index.d.ts",
36
- "version": "0.11.0",
37
+ "version": "0.12.0",
37
38
  "scripts": {
38
39
  "build": "tsc && tsc-alias",
39
40
  "-": "-"
@@ -2,6 +2,7 @@ import { uuid } from '@esportsplus/utilities';
2
2
  import ts from 'typescript';
3
3
  import { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX } from './constants.js';
4
4
  import type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate } from './types.js';
5
+ import program from './program';
5
6
 
6
7
 
7
8
  function buildImportRegex(escapedModule: string): RegExp {
@@ -240,5 +241,18 @@ const visitAstWithDepth = <T>(
240
241
  };
241
242
 
242
243
 
243
- export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, uid, updateImports, visitAst, visitAstWithDepth };
244
- export type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate };
244
+ export {
245
+ addImport, applyReplacements, applyReplacementsReverse,
246
+ collectNodes,
247
+ mightNeedTransform,
248
+ program,
249
+ uid, updateImports,
250
+ visitAst, visitAstWithDepth
251
+ };
252
+ export type {
253
+ ImportModification,
254
+ NodeMatch,
255
+ QuickCheckPattern,
256
+ Replacement,
257
+ VisitorCallback, VisitorPredicate
258
+ };
@@ -0,0 +1,54 @@
1
+ import path from 'path';
2
+ import ts from 'typescript';
3
+
4
+
5
+ let cache = new Map<string, ts.Program>();
6
+
7
+
8
+ function create(root: string): ts.Program {
9
+ let tsconfig = ts.findConfigFile(root, ts.sys.fileExists, 'tsconfig.json');
10
+
11
+ if (!tsconfig) {
12
+ throw new Error('tsconfig.json not found');
13
+ }
14
+
15
+ let file = ts.readConfigFile(tsconfig, ts.sys.readFile);
16
+
17
+ if (file.error) {
18
+ throw new Error(`Error reading tsconfig.json: ${file.error.messageText}`);
19
+ }
20
+
21
+ let parsed = ts.parseJsonConfigFileContent(
22
+ file.config,
23
+ ts.sys,
24
+ path.dirname(tsconfig)
25
+ );
26
+
27
+ if (parsed.errors.length > 0) {
28
+ throw new Error(`Error parsing tsconfig.json: ${parsed.errors[0].messageText}`);
29
+ }
30
+
31
+ return ts.createProgram({
32
+ options: parsed.options,
33
+ rootNames: parsed.fileNames
34
+ });
35
+ }
36
+
37
+
38
+ const get = (root: string): ts.Program => {
39
+ let program = cache.get(root);
40
+
41
+ if (!program) {
42
+ program = create(root);
43
+ cache.set(root, program);
44
+ }
45
+
46
+ return program;
47
+ }
48
+
49
+ const del = (root: string): void => {
50
+ cache.delete(root);
51
+ }
52
+
53
+
54
+ export default { get, delete: del };