From 31eb80f1dcca45d15917e4b6621769f80e57ebe1 Mon Sep 17 00:00:00 2001 From: Osyris Date: Wed, 26 Jun 2024 15:38:32 -0400 Subject: [PATCH] fix: Added TS 5.5.2 support --- projects/core/src/slice/module-slice.ts | 7 ++- projects/core/src/slice/ts552.ts | 80 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 projects/core/src/slice/ts552.ts diff --git a/projects/core/src/slice/module-slice.ts b/projects/core/src/slice/module-slice.ts index e14c81d..e27fd84 100644 --- a/projects/core/src/slice/module-slice.ts +++ b/projects/core/src/slice/module-slice.ts @@ -3,6 +3,7 @@ import { Position } from '../system'; import semver from 'semver'; import { sliceTs54 } from './ts54'; import { sliceTs55 } from './ts55'; +import { sliceTs552 } from './ts552'; /* ****************************************************************************************************************** */ @@ -41,7 +42,11 @@ export function sliceModule(moduleFile: ModuleFile, tsVersion: string) { return sliceTs54(moduleFile); } - return sliceTs55(moduleFile); + if (semver.lt(baseVersion, '5.5.2')) { + return sliceTs55(moduleFile); + } + + return sliceTs552(moduleFile); } /** @internal */ diff --git a/projects/core/src/slice/ts552.ts b/projects/core/src/slice/ts552.ts new file mode 100644 index 0000000..e9b3709 --- /dev/null +++ b/projects/core/src/slice/ts552.ts @@ -0,0 +1,80 @@ +import { ModuleFile } from '../module'; +import { ModuleSlice } from './module-slice'; + + +/* ****************************************************************************************************************** */ +// region: Utils +/* ****************************************************************************************************************** */ + +/** + * Slice 5.5.2+ + */ +export function sliceTs552(moduleFile: ModuleFile): ModuleSlice { + let firstSourceFileStart: number; + let wrapperStart: number | undefined; + let wrapperEnd: number | undefined; + let bodyStart: number; + let bodyEnd: number; + let sourceFileStarts: [ name: string, position: number ][] = []; + + const { content } = moduleFile; + + /* Find Wrapper or First File */ + let matcher = /^(?:\s*\/\/\s*src\/)|(?:var\s+ts\s*=.+)/gm; + + const firstMatch = matcher.exec(content); + if (!firstMatch?.[0]) throw ModuleSlice.createError(); + let bodyWrapper: undefined | { start: string; end: string } = undefined; + + /* Handle wrapped */ + if (firstMatch[0].startsWith('var')) { + wrapperStart = firstMatch.index; + bodyStart = firstMatch.index + firstMatch[0].length + 1; + + /* Find First File */ + matcher = /^\s*\/\/\s*src\//gm; + matcher.lastIndex = wrapperStart; + + const firstFileMatch = matcher.exec(content); + if (!firstFileMatch?.[0]) throw ModuleSlice.createError(); + + firstSourceFileStart = firstFileMatch.index; + + /* Find Wrapper end */ + // TODO - We may later want to find a better approach, but this will work for now + matcher = /^}\)\({ get exports\(\) { return ts; }.+$/gm; + matcher.lastIndex = firstFileMatch.index; + const wrapperEndMatch = matcher.exec(content); + if (!wrapperEndMatch?.[0]) throw ModuleSlice.createError(); + + bodyEnd = wrapperEndMatch.index - 1; + wrapperEnd = wrapperEndMatch.index + wrapperEndMatch[0].length; + + bodyWrapper = { start: firstMatch[0], end: wrapperEndMatch[0] }; + } + /* Handle non-wrapped */ + else { + firstSourceFileStart = firstMatch.index; + bodyStart = firstMatch.index + firstMatch[0].length; + bodyEnd = content.length; + } + + /* Get Source File Positions */ + matcher = /^\s*\/\/\s*(src\/.+)$/gm; + matcher.lastIndex = firstSourceFileStart; + for (let match = matcher.exec(content); match != null; match = matcher.exec(content)) { + sourceFileStarts.push([ match[1], match.index ]); + } + + return { + moduleFile, + firstSourceFileStart, + wrapperPos: wrapperStart != null ? { start: wrapperStart, end: wrapperEnd! } : undefined, + fileEnd: content.length, + bodyPos: { start: bodyStart, end: bodyEnd }, + sourceFileStarts, + bodyWrapper + }; +} + +// endregion