-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
263 lines (216 loc) · 7.03 KB
/
index.ts
File metadata and controls
263 lines (216 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { type AST, parse } from 'svelte/compiler';
/** A regular expression that matches empty script tags */
export const EMPTY_SCRIPT_REGEX = new RegExp(/<script\b[^>]*>\s*<\/script>/gi);
export type Options = {
/** Used for debugging hints. */
filename?: string;
/** Should the output be formatted. (Turn this off if you are doing your own formatting) @default true */
format?: boolean;
/** Should empty script tags be removed. @default true */
removeEmptyScripts?: boolean;
};
/** Strips the types from the provided Svelte source.
*
* @param source TypeScript source which will have it's types stripped
* @param options Options for strip
* @returns
*
* ## Usage
* ```ts
* import assert from 'node:assert';
* import { strip } from '@svecosystem/strip-types';
*
* const source = `<script lang="ts">
* let value = $state<string>('');
* </script>
*
* <input bind:value/>`
*
* const stripped = strip(source);
*
* const expected = `<script>
* let value = $state('');
* </script>
*
* <input bind:value/>`;
*
* assert(stripped === expected);
* ```
*/
export function strip(
source: string,
{ filename = undefined, format = true, removeEmptyScripts = true }: Options = {}
): string {
const ast = parse(source, { filename });
const src = new MagicString(source);
const enter = (node: AST.BaseNode, parent: AST.BaseNode) => {
// remove lang="ts" if it exists in the script
if (node.type === 'Script') {
// @ts-expect-error wrong
const scriptDeclaration = src.original.slice(node.start, node.content.start);
const langIndex = scriptDeclaration.search(/ lang=["|']ts["|']/g);
if (langIndex !== -1) {
src.update(node.start + langIndex, node.start + langIndex + 10, '');
}
const genericsRegex = new RegExp(/ generics=["'][\s\S]+["']/g);
// @ts-expect-error wrong
const match = genericsRegex.exec(src.original.slice(node.start, node.content.start));
if (match !== null) {
src.update(
node.start + match.index,
node.start + match.index + match[0].length,
''
);
}
}
// expressions that can just be removed outright
const tsNodes = [
'TSTypeParameterInstantiation',
'TSTypeAnnotation',
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
];
if (tsNodes.includes(node.type)) {
if (['TSTypeAliasDeclaration', 'TSInterfaceDeclaration'].includes(node.type)) {
let start = node.start;
if (parent.type === 'ExportNamedDeclaration') {
start = parent.start;
}
removeNode(src, start, node.end, format);
return;
}
src.update(node.start, node.end, '');
return;
}
// remove type only imports
if (node.type === 'ImportDeclaration') {
// @ts-expect-error wrong
if (node.importKind === 'type') {
removeNode(src, node.start, node.end, format);
return;
}
// @ts-expect-error wrong
const remainingSpecifiers = node.specifiers.filter((s) => s.importKind !== 'type');
// if there were no type only imports do nothing
// @ts-expect-error wrong
if (remainingSpecifiers.length === node.specifiers.length) return;
// if all the specifiers were type only remove the entire thing
if (remainingSpecifiers.length === 0) {
removeNode(src, node.start, node.end, format);
return;
}
// combine the remaining specifiers into an import statement
const updated = remainingSpecifiers
.map((s: AST.BaseNode) => src.slice(s.start, s.end))
.join(', ');
// @ts-expect-error wrong
src.update(node.start, node.end, `import { ${updated} } from ${node.source.raw};`);
return;
}
// remove type only exports
if (node.type === 'ExportNamedDeclaration') {
// @ts-expect-error wrong
if (node.exportKind === 'type') {
removeNode(src, node.start, node.end, format);
return;
}
// @ts-expect-error wrong
const remainingSpecifiers = node.specifiers.filter((s) => s.exportKind !== 'type');
// if there were no type only imports do nothing
// @ts-expect-error wrong
if (remainingSpecifiers.length === node.specifiers.length) return;
// if all the specifiers were type only remove the entire thing
if (remainingSpecifiers.length === 0) {
removeNode(src, node.start, node.end, format);
return;
}
// combine the remaining specifiers into an import statement
const updated = remainingSpecifiers
.map((s: AST.BaseNode) => src.slice(s.start, s.end))
.join(', ');
src.update(node.start, node.end, `export { ${updated} };`);
return;
}
// remove any accessability modifiers from class property definitions
// @ts-expect-error wrong
if (node.type === 'PropertyDefinition' && node.accessibility !== undefined) {
// @ts-expect-error wrong
src.update(node.start, node.start + node.accessibility.length + 1, '');
}
// expressions are stripped by replacing their node with their expression
const tsExpressions = ['TSAsExpression', 'TSNonNullExpression', 'TSTypeAssertion'];
if (tsExpressions.includes(node.type)) {
// @ts-expect-error wrong
src.update(node.start, node.end, src.slice(node.expression.start, node.expression.end));
return;
}
// syntax that is unsupported results in an error
const unsupportedSyntax = ['TSEnumDeclaration', 'TSParameterProperty'];
if (unsupportedSyntax.includes(node.type)) {
throw new Error(`Unsupported syntax! ${node.type} is not allowed!`);
}
};
// strip script tag
// @ts-expect-error It's fine dude
walk(ast.instance, { enter });
// strip <script module> tag
// @ts-expect-error It's fine dude
walk(ast.module, { enter });
// strip templates
// @ts-expect-error It's fine dude
walk(ast.html, { enter });
let content = src.toString();
if (removeEmptyScripts) {
content = content.replaceAll(EMPTY_SCRIPT_REGEX, '');
}
if (format) {
return content.trim();
}
return content;
}
/** Removes the entire node and any leading / trailing whitespace
*
* @param src
* @param start
* @param end
*/
function removeNode(src: MagicString, start: number, end: number, format: boolean) {
let newStart = start;
let newEnd = end;
if (format) {
let isLast = false;
// remove whitespace beyond the node until the proceeding whitespace of the next node
for (let i = end; i < src.original.length; i++) {
if (/\S/.test(src.original[i])) {
if (src.original[i] === '<' && src.original.slice(i).startsWith('</script')) {
isLast = true;
// back up 1 character so that the last node isn't on the same line as the script
newEnd -= 1;
}
break;
}
if (src.original[i] === '\n') {
newEnd = i + 1;
}
}
newStart = 0;
// remove whitespace proceeding the node until the next newline / none whitespace character
for (let i = start - 1; i > -1; i--) {
let regex: RegExp;
// if we are last then we get rid of all whitespace trailing the previous node
if (isLast) {
regex = new RegExp(/\S/);
} else {
// else we only remove to the new line
regex = new RegExp(/\S|\n/);
}
if (regex.test(src.original[i])) {
newStart = i + 1;
break;
}
}
}
src.update(newStart, newEnd, '');
}