@esportsplus/typescript 0.11.0 → 0.12.1
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/build/transformer/constants.d.ts +2 -1
- package/build/transformer/constants.js +2 -1
- package/build/transformer/index.d.ts +4 -2
- package/build/transformer/index.js +3 -1
- package/build/transformer/program.d.ts +6 -0
- package/build/transformer/program.js +33 -0
- package/build/transformer/types.d.ts +6 -6
- package/package.json +2 -1
- package/src/transformer/constants.ts +3 -1
- package/src/transformer/index.ts +11 -2
- package/src/transformer/program.ts +54 -0
- package/src/transformer/types.ts +7 -6
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
declare const BRACES_CONTENT_REGEX: RegExp;
|
|
2
2
|
declare const REGEX_ESCAPE_PATTERN: RegExp;
|
|
3
|
+
declare const TRAILING_SEMICOLON: RegExp;
|
|
3
4
|
declare const UUID_DASH_REGEX: RegExp;
|
|
4
|
-
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX };
|
|
5
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON, UUID_DASH_REGEX };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const BRACES_CONTENT_REGEX = /\{([^}]*)\}/;
|
|
2
2
|
const REGEX_ESCAPE_PATTERN = /[.*+?^${}()|[\]\\]/g;
|
|
3
|
+
const TRAILING_SEMICOLON = /;$/;
|
|
3
4
|
const UUID_DASH_REGEX = /-/g;
|
|
4
|
-
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX };
|
|
5
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON, UUID_DASH_REGEX };
|
|
@@ -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,6 @@ 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 type
|
|
13
|
+
export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, program, uid, updateImports, visitAst, visitAstWithDepth };
|
|
14
|
+
export type * from './types.js';
|
|
15
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON } from './constants.js';
|
|
@@ -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,5 @@ 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 };
|
|
159
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON } from './constants.js';
|
|
@@ -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 };
|
|
@@ -3,21 +3,21 @@ type ImportModification = {
|
|
|
3
3
|
module: string;
|
|
4
4
|
specifiers: Set<string>;
|
|
5
5
|
};
|
|
6
|
-
type NodeMatch<T> = {
|
|
6
|
+
type NodeMatch<T> = Range & {
|
|
7
7
|
data: T;
|
|
8
|
-
end: number;
|
|
9
8
|
node: ts.Node;
|
|
10
|
-
start: number;
|
|
11
9
|
};
|
|
12
10
|
type QuickCheckPattern = {
|
|
13
11
|
patterns?: string[];
|
|
14
12
|
regex?: RegExp;
|
|
15
13
|
};
|
|
16
|
-
type
|
|
14
|
+
type Range = {
|
|
17
15
|
end: number;
|
|
18
|
-
newText: string;
|
|
19
16
|
start: number;
|
|
20
17
|
};
|
|
18
|
+
type Replacement = Range & {
|
|
19
|
+
newText: string;
|
|
20
|
+
};
|
|
21
21
|
type VisitorCallback<T> = (node: ts.Node, state: T) => void;
|
|
22
22
|
type VisitorPredicate = (node: ts.Node) => boolean;
|
|
23
|
-
export type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate };
|
|
23
|
+
export type { ImportModification, NodeMatch, QuickCheckPattern, Range, Replacement, VisitorCallback, VisitorPredicate };
|
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.
|
|
37
|
+
"version": "0.12.1",
|
|
37
38
|
"scripts": {
|
|
38
39
|
"build": "tsc && tsc-alias",
|
|
39
40
|
"-": "-"
|
|
@@ -2,7 +2,9 @@ const BRACES_CONTENT_REGEX = /\{([^}]*)\}/;
|
|
|
2
2
|
|
|
3
3
|
const REGEX_ESCAPE_PATTERN = /[.*+?^${}()|[\]\\]/g;
|
|
4
4
|
|
|
5
|
+
const TRAILING_SEMICOLON = /;$/;
|
|
6
|
+
|
|
5
7
|
const UUID_DASH_REGEX = /-/g;
|
|
6
8
|
|
|
7
9
|
|
|
8
|
-
export {
|
|
10
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON, UUID_DASH_REGEX };
|
package/src/transformer/index.ts
CHANGED
|
@@ -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,13 @@ const visitAstWithDepth = <T>(
|
|
|
240
241
|
};
|
|
241
242
|
|
|
242
243
|
|
|
243
|
-
export {
|
|
244
|
-
|
|
244
|
+
export {
|
|
245
|
+
addImport, applyReplacements, applyReplacementsReverse,
|
|
246
|
+
collectNodes,
|
|
247
|
+
mightNeedTransform,
|
|
248
|
+
program,
|
|
249
|
+
uid, updateImports,
|
|
250
|
+
visitAst, visitAstWithDepth
|
|
251
|
+
};
|
|
252
|
+
export type * from './types';
|
|
253
|
+
export { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, TRAILING_SEMICOLON } from './constants';
|
|
@@ -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 };
|
package/src/transformer/types.ts
CHANGED
|
@@ -6,11 +6,9 @@ type ImportModification = {
|
|
|
6
6
|
specifiers: Set<string>;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
type NodeMatch<T> = {
|
|
9
|
+
type NodeMatch<T> = Range & {
|
|
10
10
|
data: T;
|
|
11
|
-
end: number;
|
|
12
11
|
node: ts.Node;
|
|
13
|
-
start: number;
|
|
14
12
|
};
|
|
15
13
|
|
|
16
14
|
type QuickCheckPattern = {
|
|
@@ -18,12 +16,15 @@ type QuickCheckPattern = {
|
|
|
18
16
|
regex?: RegExp;
|
|
19
17
|
};
|
|
20
18
|
|
|
21
|
-
type
|
|
19
|
+
type Range = {
|
|
22
20
|
end: number;
|
|
23
|
-
newText: string;
|
|
24
21
|
start: number;
|
|
25
22
|
};
|
|
26
23
|
|
|
24
|
+
type Replacement = Range & {
|
|
25
|
+
newText: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
27
28
|
type VisitorCallback<T> = (node: ts.Node, state: T) => void;
|
|
28
29
|
|
|
29
30
|
type VisitorPredicate = (node: ts.Node) => boolean;
|
|
@@ -33,6 +34,6 @@ export type {
|
|
|
33
34
|
ImportModification,
|
|
34
35
|
NodeMatch,
|
|
35
36
|
QuickCheckPattern,
|
|
36
|
-
Replacement,
|
|
37
|
+
Range, Replacement,
|
|
37
38
|
VisitorCallback, VisitorPredicate
|
|
38
39
|
};
|