From de3b4c606582fc8ee8836d51247b623f0515a7df Mon Sep 17 00:00:00 2001 From: thinknathan Date: Tue, 9 Apr 2024 22:13:08 -0700 Subject: [PATCH] New replace with false feature --- README.md | 10 ++++++---- dist/tstl-remove-debug.cjs | 27 ++++++++++++++++++++------- package.json | 2 +- tstl-remove-debug.ts | 29 +++++++++++++++++++++-------- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 49442d0..c4b064f 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/dist/tstl-remove-debug.cjs b/dist/tstl-remove-debug.cjs index a2d611b..9a325ff 100644 --- a/dist/tstl-remove-debug.cjs +++ b/dist/tstl-remove-debug.cjs @@ -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); } diff --git a/package.json b/package.json index 8fdb3d6..0558091 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tstl-remove-debug.ts b/tstl-remove-debug.ts index 72fe4c4..1c6f92b 100644 --- a/tstl-remove-debug.ts +++ b/tstl-remove-debug.ts @@ -2,17 +2,13 @@ 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 @@ -20,6 +16,18 @@ function removeDebug(file: tstl.EmitFile) { } } +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 */ @@ -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); }