|
| 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