Skip to content

Commit

Permalink
Add @__KEY__ annotation
Browse files Browse the repository at this point in the history
Adding a @__KEY__ annotation before a string literal will now cause
it to be mangled like a property name during the mangle_props phase.
  • Loading branch information
jpdutoit committed Apr 4, 2023
1 parent 36324f1 commit 7b8beff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -3134,6 +3134,7 @@ class TreeTransformer extends TreeWalker {
const _PURE = 0b00000001;
const _INLINE = 0b00000010;
const _NOINLINE = 0b00000100;
const _KEY = 0b00001000;

export {
AST_Accessor,
Expand Down Expand Up @@ -3278,4 +3279,5 @@ export {
_INLINE,
_NOINLINE,
_PURE,
_KEY,
};
8 changes: 7 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*)$/);
Expand Down Expand Up @@ -3111,6 +3113,10 @@ function parse($TEXT, options) {
set_annotation(node, _NOINLINE);
break;
}
if (/[@#]__KEY__/.test(comment.value)) {
set_annotation(node, _KEY);
break;
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import {
defaults,
push_uniq,
has_annotation,
} from "./utils/index.js";
import { base54 } from "./scope.js";
import {
Expand All @@ -67,6 +68,7 @@ import {
AST_Sub,
TreeTransformer,
TreeWalker,
_KEY,
} from "./ast.js";
import { domprops } from "../tools/domprops.js";

Expand Down Expand Up @@ -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);
}
}));

Expand Down Expand Up @@ -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);
}
}));

Expand Down

0 comments on commit 7b8beff

Please sign in to comment.