|
1 | | -import type { Parsed as RawGlimmerTemplate } from 'content-tag'; |
2 | | - |
3 | | -const EMPTY_SPACE = ' '; |
| 1 | +export interface Template { |
| 2 | + contents: string; |
| 3 | + type: string; |
| 4 | + range: { |
| 5 | + start: number; |
| 6 | + end: number; |
| 7 | + }; |
| 8 | + utf16Range: { |
| 9 | + start: number; |
| 10 | + end: number; |
| 11 | + }; |
| 12 | +} |
4 | 13 |
|
5 | | -/** |
6 | | - * Given a string (`original`), replaces the bytes in the given `range` with |
7 | | - * equivalent bytes of empty space (' ') surrounded by the given prefix and |
8 | | - * suffix. The total byte length will not change. |
9 | | - * |
10 | | - * Returns the resulting string. |
11 | | - */ |
12 | | -function replaceByteRange( |
13 | | - originalBuffer: Buffer, |
14 | | - range: { start: number; end: number }, |
15 | | - options: { prefix: string; suffix: string }, |
16 | | -): string { |
17 | | - const prefixBuffer = Buffer.from(options.prefix); |
18 | | - const suffixBuffer = Buffer.from(options.suffix); |
| 14 | +const BufferMap: Map<string, Buffer> = new Map(); |
19 | 15 |
|
20 | | - // Validate range |
21 | | - if ( |
22 | | - range.start < 0 || |
23 | | - range.end > originalBuffer.length || |
24 | | - range.start > range.end || |
25 | | - prefixBuffer.length + suffixBuffer.length > range.end - range.start |
26 | | - ) { |
27 | | - throw new Error( |
28 | | - `Invalid byte range:\n\tstart=${range.start}\n\tend=${ |
29 | | - range.end |
30 | | - }\n\tprefix=${options.prefix}\n\tsuffix=${ |
31 | | - options.suffix |
32 | | - }\n\tstring=\n\t${originalBuffer.toString()}`, |
33 | | - ); |
| 16 | +function getBuffer(s: string): Buffer { |
| 17 | + let buf = BufferMap.get(s); |
| 18 | + if (!buf) { |
| 19 | + buf = Buffer.from(s); |
| 20 | + BufferMap.set(s, buf); |
34 | 21 | } |
| 22 | + return buf; |
| 23 | +} |
35 | 24 |
|
36 | | - // Adjust the space length to account for the prefix and suffix lengths |
37 | | - const totalReplacementLength = range.end - range.start; |
38 | | - const spaceLength = |
39 | | - totalReplacementLength - prefixBuffer.length - suffixBuffer.length; |
40 | | - |
41 | | - // Create a buffer for the replacement |
42 | | - const spaceBuffer = Buffer.alloc(spaceLength, EMPTY_SPACE); |
43 | | - |
44 | | - // Concatenate prefix, space, and suffix buffers |
45 | | - const replacementBuffer = Buffer.concat([ |
46 | | - prefixBuffer, |
47 | | - spaceBuffer, |
48 | | - suffixBuffer, |
49 | | - ]); |
50 | | - |
51 | | - // Create buffers for before and after the range using subarray |
52 | | - const beforeRange = originalBuffer.subarray(0, range.start); |
53 | | - const afterRange = originalBuffer.subarray(range.end); |
| 25 | +/** Slice string using byte range */ |
| 26 | +export function sliceByteRange(s: string, a: number, b?: number): string { |
| 27 | + const buf = getBuffer(s); |
| 28 | + return buf.subarray(a, b).toString(); |
| 29 | +} |
54 | 30 |
|
55 | | - // Concatenate all parts and convert back to a string |
56 | | - const result = Buffer.concat([beforeRange, replacementBuffer, afterRange]); |
| 31 | +/** Converts byte index to js char index (utf16) */ |
| 32 | +export function byteToCharIndex(s: string, byteOffset: number): number { |
| 33 | + const buf = getBuffer(s); |
| 34 | + return buf.subarray(0, byteOffset).toString().length; |
| 35 | +} |
57 | 36 |
|
58 | | - if (result.length !== originalBuffer.length) { |
59 | | - throw new Error( |
60 | | - `Result length (${result.length}) does not match original length (${originalBuffer.length})`, |
61 | | - ); |
62 | | - } |
| 37 | +/** Calculate byte length */ |
| 38 | +export function byteLength(s: string): number { |
| 39 | + return getBuffer(s).length; |
| 40 | +} |
63 | 41 |
|
64 | | - return result.toString('utf8'); |
| 42 | +function replaceRange( |
| 43 | + s: string, |
| 44 | + start: number, |
| 45 | + end: number, |
| 46 | + substitute: string, |
| 47 | +): string { |
| 48 | + return sliceByteRange(s, 0, start) + substitute + sliceByteRange(s, end); |
65 | 49 | } |
66 | 50 |
|
67 | 51 | /** |
68 | 52 | * Replace the template with a parsable placeholder that takes up the same |
69 | 53 | * range. |
70 | 54 | */ |
71 | 55 | export function preprocessTemplateRange( |
72 | | - rawTemplate: RawGlimmerTemplate, |
| 56 | + template: Template, |
73 | 57 | code: string, |
74 | 58 | ): string { |
75 | | - const codeBuffer = Buffer.from(code); |
76 | | - |
77 | 59 | let prefix: string; |
78 | 60 | let suffix: string; |
79 | 61 |
|
80 | | - if (rawTemplate.type === 'class-member') { |
| 62 | + if (template.type === 'class-member') { |
81 | 63 | // Replace with StaticBlock |
82 | | - prefix = 'static{'; |
83 | | - suffix = '}'; |
| 64 | + prefix = 'static{/*'; |
| 65 | + suffix = '*/}'; |
84 | 66 | } else { |
85 | 67 | // Replace with BlockStatement or ObjectExpression |
86 | | - prefix = '{'; |
87 | | - suffix = '}'; |
| 68 | + prefix = '{/*'; |
| 69 | + suffix = '*/}'; |
88 | 70 |
|
89 | | - const nextToken = codeBuffer |
90 | | - .subarray(rawTemplate.range.end) |
91 | | - .toString() |
92 | | - .match(/\S+/); |
| 71 | + const nextToken = code.slice(template.range.end).toString().match(/\S+/); |
93 | 72 | if (nextToken && nextToken[0] === 'as') { |
94 | 73 | // Replace with parenthesized ObjectExpression |
95 | 74 | prefix = '(' + prefix; |
96 | 75 | suffix = suffix + ')'; |
97 | 76 | } |
98 | 77 | } |
99 | 78 |
|
100 | | - return replaceByteRange(codeBuffer, rawTemplate.range, { |
101 | | - prefix, |
102 | | - suffix, |
103 | | - }); |
| 79 | + const content = template.contents.replaceAll('/', '\\/'); |
| 80 | + const tplLength = template.range.end - template.range.start; |
| 81 | + const spaces = |
| 82 | + tplLength - byteLength(content) - prefix.length - suffix.length; |
| 83 | + const total = prefix + content + ' '.repeat(spaces) + suffix; |
| 84 | + return replaceRange(code, template.range.start, template.range.end, total); |
104 | 85 | } |
0 commit comments