Skip to content

Commit 31eb80f

Browse files
authored
fix: Added TS 5.5.2 support
1 parent 46852a3 commit 31eb80f

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

projects/core/src/slice/module-slice.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Position } from '../system';
33
import semver from 'semver';
44
import { sliceTs54 } from './ts54';
55
import { sliceTs55 } from './ts55';
6+
import { sliceTs552 } from './ts552';
67

78

89
/* ****************************************************************************************************************** */
@@ -41,7 +42,11 @@ export function sliceModule(moduleFile: ModuleFile, tsVersion: string) {
4142
return sliceTs54(moduleFile);
4243
}
4344

44-
return sliceTs55(moduleFile);
45+
if (semver.lt(baseVersion, '5.5.2')) {
46+
return sliceTs55(moduleFile);
47+
}
48+
49+
return sliceTs552(moduleFile);
4550
}
4651

4752
/** @internal */

projects/core/src/slice/ts552.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { ModuleFile } from '../module';
2+
import { ModuleSlice } from './module-slice';
3+
4+
5+
/* ****************************************************************************************************************** */
6+
// region: Utils
7+
/* ****************************************************************************************************************** */
8+
9+
/**
10+
* Slice 5.5.2+
11+
*/
12+
export function sliceTs552(moduleFile: ModuleFile): ModuleSlice {
13+
let firstSourceFileStart: number;
14+
let wrapperStart: number | undefined;
15+
let wrapperEnd: number | undefined;
16+
let bodyStart: number;
17+
let bodyEnd: number;
18+
let sourceFileStarts: [ name: string, position: number ][] = [];
19+
20+
const { content } = moduleFile;
21+
22+
/* Find Wrapper or First File */
23+
let matcher = /^(?:\s*\/\/\s*src\/)|(?:var\s+ts\s*=.+)/gm;
24+
25+
const firstMatch = matcher.exec(content);
26+
if (!firstMatch?.[0]) throw ModuleSlice.createError();
27+
let bodyWrapper: undefined | { start: string; end: string } = undefined;
28+
29+
/* Handle wrapped */
30+
if (firstMatch[0].startsWith('var')) {
31+
wrapperStart = firstMatch.index;
32+
bodyStart = firstMatch.index + firstMatch[0].length + 1;
33+
34+
/* Find First File */
35+
matcher = /^\s*\/\/\s*src\//gm;
36+
matcher.lastIndex = wrapperStart;
37+
38+
const firstFileMatch = matcher.exec(content);
39+
if (!firstFileMatch?.[0]) throw ModuleSlice.createError();
40+
41+
firstSourceFileStart = firstFileMatch.index;
42+
43+
/* Find Wrapper end */
44+
// TODO - We may later want to find a better approach, but this will work for now
45+
matcher = /^}\)\({ get exports\(\) { return ts; }.+$/gm;
46+
matcher.lastIndex = firstFileMatch.index;
47+
const wrapperEndMatch = matcher.exec(content);
48+
if (!wrapperEndMatch?.[0]) throw ModuleSlice.createError();
49+
50+
bodyEnd = wrapperEndMatch.index - 1;
51+
wrapperEnd = wrapperEndMatch.index + wrapperEndMatch[0].length;
52+
53+
bodyWrapper = { start: firstMatch[0], end: wrapperEndMatch[0] };
54+
}
55+
/* Handle non-wrapped */
56+
else {
57+
firstSourceFileStart = firstMatch.index;
58+
bodyStart = firstMatch.index + firstMatch[0].length;
59+
bodyEnd = content.length;
60+
}
61+
62+
/* Get Source File Positions */
63+
matcher = /^\s*\/\/\s*(src\/.+)$/gm;
64+
matcher.lastIndex = firstSourceFileStart;
65+
for (let match = matcher.exec(content); match != null; match = matcher.exec(content)) {
66+
sourceFileStarts.push([ match[1], match.index ]);
67+
}
68+
69+
return {
70+
moduleFile,
71+
firstSourceFileStart,
72+
wrapperPos: wrapperStart != null ? { start: wrapperStart, end: wrapperEnd! } : undefined,
73+
fileEnd: content.length,
74+
bodyPos: { start: bodyStart, end: bodyEnd },
75+
sourceFileStarts,
76+
bodyWrapper
77+
};
78+
}
79+
80+
// endregion

0 commit comments

Comments
 (0)