-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtstl-remove-debug.ts
55 lines (51 loc) · 1.68 KB
/
tstl-remove-debug.ts
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
import * as ts from 'typescript';
import * as tstl from 'typescript-to-lua';
// Capture statements from the keyword, to the ending parenthesis
const patternToRemove =
/((?:print|pprint|assert|(?:debug|profiler)\.\w*)\s*\([^]*?(?:(?:(?:\([^()]*\))|[^()])*)\))/g;
// Variable to capture matches
let matchToRemove: RegExpExecArray | null;
function removeDebug(file: tstl.EmitFile) {
while ((matchToRemove = patternToRemove.exec(file.code)) !== null) {
const statement = matchToRemove[0];
console.log(`Removing ${statement}`);
// Replace statement with an empty line
file.code = file.code.replace(statement, '');
}
}
const patternToReplace = /sys\.get_engine_info\(\)\.is_debug/g;
let matchToReplace: RegExpExecArray | null;
function replaceDebug(file: tstl.EmitFile) {
while ((matchToReplace = patternToReplace.exec(file.code)) !== null) {
const statement = matchToReplace[0];
console.log(`Removing ${statement}`);
// Replace statement with false
file.code = file.code.replace(statement, 'false');
}
}
/**
* Plugin definition for TypeScript-to-Lua
*/
const plugin: tstl.Plugin = {
afterEmit: (
_program: ts.Program,
_options: tstl.CompilerOptions,
emitHost: tstl.EmitHost,
result: tstl.EmitFile[],
) => {
for (const file of result) {
// Running only once doesn't find all values
// so we re-run the function this many times per file
const maxLoop = 100;
for (let index = 0; index < maxLoop; index++) {
removeDebug(file);
}
// Replace call to `is_debug` with `false`
replaceDebug(file);
// Write the changed code
emitHost.writeFile(file.outputPath, file.code, false);
}
},
};
// Export the plugin for use in TypeScript-to-Lua
export default plugin;