-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreplace.js
More file actions
54 lines (51 loc) · 1.59 KB
/
replace.js
File metadata and controls
54 lines (51 loc) · 1.59 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
export const magic = "MAGIC_ASSEMBLYSCRIPT_PRETTIER_1996";
const prefix = "/*" + magic;
const postfix = magic + "*/";
export async function preProcess(code) {
const assemblyscript = await import("assemblyscript");
const NodeKind = assemblyscript.NodeKind;
const visitDecorators = (node) => {
let list = [];
let _visit = (_node) => {
switch (_node.kind) {
case NodeKind.Source: {
_node.statements.forEach((statement) => {
_visit(statement);
});
break;
}
case NodeKind.ClassDeclaration:
case NodeKind.InterfaceDeclaration:
case NodeKind.NamespaceDeclaration: {
_node.members.forEach((statement) => {
_visit(statement);
});
break;
}
}
if (_node.decorators) {
list.push(
..._node.decorators.map((decorator) => {
return { start: decorator.range.start, end: decorator.range.end };
})
);
}
};
_visit(node);
return list;
};
let parser = new assemblyscript.Parser();
parser.parseFile(code, "pre_process.ts", false);
let source = parser.sources[0];
const decorators = visitDecorators(source);
decorators.sort((a, b) => a.start - b.start);
let cursor = 0;
const removeDecoratorCodes = decorators.map((decorator) => {
let s1 = code.slice(cursor, decorator.start);
let s2 = code.slice(decorator.start, decorator.end);
cursor = decorator.end;
return `${s1}${prefix}${s2}`;
});
removeDecoratorCodes.push(code.slice(cursor));
return removeDecoratorCodes.join(postfix);
}