-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostprocess.js
76 lines (72 loc) · 2.51 KB
/
postprocess.js
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
import { parse } from "acorn";
import { simple } from "acorn-walk";
import { generate } from "astring";
import { ImportStatement, writeImportStatement } from "./modules";
import { Update, update } from "./textual";
const generateImports = (imports) => {
const entries = Array.from(imports.entries());
entries.sort((a, b) => a[0].localeCompare(b[0]));
return entries
.map(([source, importStmt]) => writeImportStatement(importStmt, source))
.join("");
}
/**
* @param {string} content
* @param {!Map<string, ImportStatement>} missingImports
*/
const postprocess = (content, missingImports) => {
/** @const {!acorn.Program} */
const ast = parse(content, {
ecmaVersion: "latest",
sourceType: "module"
});
/**
* @const
* @type {!Array<Update>}
*/
const updates = [];
let assignmentCode = "";
let exportCode = "";
simple(ast, /** @type {!acorn.SimpleVisitor} */({
Literal(node) {
if (typeof node.value === 'bigint') {
const decimal = node.value.toString();
const hexadecimal = '0x' + node.value.toString(16);
if (hexadecimal.length < decimal.length)
updates.push({
beg: node.start,
end: node.end,
put: hexadecimal + "n"
})
}
},
AssignmentExpression(node) {
if (node.left.type === 'MemberExpression' &&
node.left.object.name === 'globalThis' &&
node.left.property.name === 'KimlikDAOCompiler_exports') {
const exportCount = node.right.properties.length;
if (exportCount == 1 && node.right.properties[0].key.name == "KDdefault") {
exportCode = `export default ${generate(node.right.properties[0].value)}`;
} else if (exportCount > 0) {
exportCode = "export{";
node.right.properties.forEach((prop) => {
const prefix = 'KimlikDAOCompiler_';
let exportName = prop.key.type === 'Identifier' ? prop.key.name : prop.key.value;
if (exportName == "KDdefault") exportName = exportName.slice(2);
assignmentCode += `const ${prefix}${exportName} = ${generate(prop.value)};\n`;
exportCode += `${prefix}${exportName} as ${exportName},`;
});
exportCode = exportCode.slice(0, -1) + "}";
}
updates.push({
beg: node.start,
end: node.end,
put: ""
})
}
}
}));
const newContent = generateImports(missingImports) + update(content, updates);
return newContent + assignmentCode + exportCode;
}
export { postprocess };