From cb5c4c71c866362dce24eff79251fed6add53e9f Mon Sep 17 00:00:00 2001 From: dave caruso Date: Tue, 17 Oct 2023 19:42:37 -0700 Subject: yay --- src/codegen/builtin-parser.ts | 187 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 src/codegen/builtin-parser.ts (limited to 'src/codegen/builtin-parser.ts') diff --git a/src/codegen/builtin-parser.ts b/src/codegen/builtin-parser.ts new file mode 100644 index 000000000..4e35f13dd --- /dev/null +++ b/src/codegen/builtin-parser.ts @@ -0,0 +1,187 @@ +import { applyReplacements } from "./replacements"; + +/** + * Slices a string until it hits a }, but keeping in mind JS comments, + * regex, template literals, comments, and matching { + * + * Used to extract function bodies without parsing the code. + * + * If you pass replace=true, it will run replacements on the code + */ +export function sliceSourceCode( + contents: string, + replace: boolean, + replaceRequire?: (specifier: string) => string, + endOnComma = false, +): { result: string; rest: string } { + let bracketCount = 0; + let i = 0; + let result = ""; + while (contents.length) { + const match = contents.match( + endOnComma && bracketCount <= 1 + ? /((?:[(,=;:{]|return|\=\>)\s*)\/[^\/\*]|\/\*|\/\/|['"}`\),]|(?)\s*)\/[^\/\*]|\/\*|\/\/|['"}`\)]|(?