From 7b8beffd5ec867893401c0fe5bdf32a4fe46e35c Mon Sep 17 00:00:00 2001 From: "Jacques P. du Toit" Date: Tue, 4 Apr 2023 17:42:53 +0200 Subject: [PATCH] Add @__KEY__ annotation Adding a @__KEY__ annotation before a string literal will now cause it to be mangled like a property name during the mangle_props phase. --- lib/ast.js | 2 ++ lib/parse.js | 8 +++++++- lib/propmangle.js | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/ast.js b/lib/ast.js index 59c6a9e9e..42e6fc27f 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -3134,6 +3134,7 @@ class TreeTransformer extends TreeWalker { const _PURE = 0b00000001; const _INLINE = 0b00000010; const _NOINLINE = 0b00000100; +const _KEY = 0b00001000; export { AST_Accessor, @@ -3278,4 +3279,5 @@ export { _INLINE, _NOINLINE, _PURE, + _KEY, }; diff --git a/lib/parse.js b/lib/parse.js index 56eca92d0..75a536f69 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -162,7 +162,8 @@ import { AST_Yield, _INLINE, _NOINLINE, - _PURE + _PURE, + _KEY } from "./ast.js"; var LATEST_RAW = ""; // Only used for numbers and template strings @@ -2225,6 +2226,7 @@ function parse($TEXT, options) { value : tok.value, quote : tok.quote }); + annotate(ret) break; case "regexp": const [_, source, flags] = tok.value.match(/^\/(.*)\/(\w*)$/); @@ -3111,6 +3113,10 @@ function parse($TEXT, options) { set_annotation(node, _NOINLINE); break; } + if (/[@#]__KEY__/.test(comment.value)) { + set_annotation(node, _KEY); + break; + } } } } diff --git a/lib/propmangle.js b/lib/propmangle.js index 2fc13e005..7786cda5b 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -47,6 +47,7 @@ import { defaults, push_uniq, + has_annotation, } from "./utils/index.js"; import { base54 } from "./scope.js"; import { @@ -67,6 +68,7 @@ import { AST_Sub, TreeTransformer, TreeWalker, + _KEY, } from "./ast.js"; import { domprops } from "../tools/domprops.js"; @@ -263,6 +265,8 @@ function mangle_properties(ast, options) { addStrings(node.args[1], add); } else if (node instanceof AST_Binary && node.operator === "in") { addStrings(node.left, add); + } else if (node instanceof AST_String && has_annotation(node, _KEY)) { + add(node.value); } })); @@ -296,6 +300,8 @@ function mangle_properties(ast, options) { node.args[1] = mangleStrings(node.args[1]); } else if (node instanceof AST_Binary && node.operator === "in") { node.left = mangleStrings(node.left); + } else if (node instanceof AST_String && has_annotation(node, _KEY)) { + node.value = mangle(node.value); } }));