Skip to content

Commit

Permalink
New replace with false feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thinknathan committed Apr 10, 2024
1 parent a5e80c0 commit de3b4c6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![CI](https://github.com/thinknathan/tstl-remove-debug/actions/workflows/ci.yml/badge.svg)](https://github.com/thinknathan/tstl-remove-debug/actions/workflows/ci.yml) ![GitHub License](https://img.shields.io/github/license/thinknathan/tstl-remove-debug)

TypeScriptToLua plugin that strips calls to `print`, `pprint`, `assert`, and any functions attached to `profiler` and `debug`. The purpose is to reduce code size for production builds.
TypeScriptToLua plugin that strips calls to `print`, `pprint`, `assert`, and any functions attached to `profiler` and `debug`. Also replaces the statement `sys.get_engine_info().is_debug` with `false`.

The purpose is to reduce code size for production builds.

:exclamation: Use this and any code transformation plugin with caution. Mistakes are possible.

Expand All @@ -14,16 +16,16 @@ pprint(foo);
assert(foo === '');
profiler.start();
debug.draw_text('');
const is_debug = sys.get_engine_info().is_debug;
```

Becomes:

```lua

-- (This space intentionally left blank)
local is_debug = false
```

(This space intentionally left blank)

## Installation

Requires TSTL >= 1.22.0.
Expand Down
27 changes: 20 additions & 7 deletions dist/tstl-remove-debug.cjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
// Capture statements from the keyword, to the ending parenthesis
const pattern =
const patternToRemove =
/((?:print|pprint|assert|(?:debug|profiler)\.\w*)\s*\([^]*?(?:(?:(?:\([^()]*\))|[^()])*)\))/g;
// Variable to capture matches
let match;
// Running only once doesn't find all values
// so we re-run the function this many times per file
const maxLoop = 100;
let matchToRemove;
function removeDebug(file) {
while ((match = pattern.exec(file.code)) !== null) {
const statement = match[0];
while ((matchToRemove = patternToRemove.exec(file.code)) !== null) {
const statement = matchToRemove[0];
// @ts-expect-error Missing console definition
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;
function replaceDebug(file) {
while ((matchToReplace = patternToReplace.exec(file.code)) !== null) {
const statement = matchToReplace[0];
// @ts-expect-error Missing console definition
console.log(`Removing ${statement}`);
// Replace statement with false
file.code = file.code.replace(statement, 'false');
}
}
/**
* Plugin definition for TypeScript-to-Lua
*/
const plugin = {
afterEmit: (_program, _options, emitHost, result) => {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tstl-remove-debug",
"version": "1.0.2",
"version": "1.1.0",
"description": "TypeScriptToLua plugin that removes debugging expressions",
"repository": {
"type": "git",
Expand Down
29 changes: 21 additions & 8 deletions tstl-remove-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ import * as ts from 'typescript';
import * as tstl from 'typescript-to-lua';

// Capture statements from the keyword, to the ending parenthesis
const pattern =
const patternToRemove =
/((?:print|pprint|assert|(?:debug|profiler)\.\w*)\s*\([^]*?(?:(?:(?:\([^()]*\))|[^()])*)\))/g;
// Variable to capture matches
let match: RegExpExecArray | null;
// Running only once doesn't find all values
// so we re-run the function this many times per file
const maxLoop = 100;

let matchToRemove: RegExpExecArray | null;
function removeDebug(file: tstl.EmitFile) {
while ((match = pattern.exec(file.code)) !== null) {
const statement = match[0];
while ((matchToRemove = patternToRemove.exec(file.code)) !== null) {
const statement = matchToRemove[0];
// @ts-expect-error Missing console definition
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];
// @ts-expect-error Missing console definition
console.log(`Removing ${statement}`);
// Replace statement with false
file.code = file.code.replace(statement, 'false');
}
}

/**
* Plugin definition for TypeScript-to-Lua
*/
Expand All @@ -31,9 +39,14 @@ const plugin: tstl.Plugin = {
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);
}
Expand Down

0 comments on commit de3b4c6

Please sign in to comment.