aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-04-28 21:13:42 +0100
committerGitHub <noreply@github.com>2021-04-29 04:13:42 +0800
commitd576495e5a696302340580e999bc0446a1d4c1ca (patch)
tree7d07b009f8777a25a916faa4c1efbdfe2a1ebde8 /bin
parenta06e20304b042ab5cd2ad051fb60cf50a7934bb7 (diff)
downloadtracifyjs-d576495e5a696302340580e999bc0446a1d4c1ca.tar.gz
tracifyjs-d576495e5a696302340580e999bc0446a1d4c1ca.zip
support `#__PURE__` in ESTree (#4879)
Diffstat (limited to 'bin')
-rwxr-xr-xbin/uglifyjs29
1 files changed, 28 insertions, 1 deletions
diff --git a/bin/uglifyjs b/bin/uglifyjs
index 69a98a4b..2d03d99a 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -314,16 +314,43 @@ function run() {
try {
if (options.parse) {
if (options.parse.acorn) {
+ var annotations = Object.create(null);
files = convert_ast(function(toplevel, name) {
- return require("acorn").parse(files[name], {
+ var content = files[name];
+ var list = annotations[name] = [];
+ var prev = -1;
+ return require("acorn").parse(content, {
allowHashBang: true,
ecmaVersion: "latest",
locations: true,
+ onComment: function(block, text, start, end) {
+ var match = /[@#]__PURE__/.exec(text);
+ if (!match) {
+ if (start != prev) return;
+ match = [ list[prev] ];
+ }
+ while (/\s/.test(content[end])) end++;
+ list[end] = match[0];
+ prev = end;
+ },
+ preserveParens: true,
program: toplevel,
sourceFile: name,
sourceType: "module",
});
});
+ files.walk(new UglifyJS.TreeWalker(function(node) {
+ if (!(node instanceof UglifyJS.AST_Call)) return;
+ var list = annotations[node.start.file];
+ var pure = list[node.start.pos];
+ if (!pure) {
+ var pos = node.start.parens;
+ if (pos) for (var i = 0; !pure && i < pos.length; i++) {
+ pure = list[pos[i]];
+ }
+ }
+ if (pure) node.pure = pure;
+ }));
} else if (options.parse.spidermonkey) {
files = convert_ast(function(toplevel, name) {
var obj = JSON.parse(files[name]);