fix corner cases in parse & unused (#5000)

This commit is contained in:
Alex Lam S.L 2021-06-12 17:10:01 +01:00 committed by GitHub
parent 70ceda5398
commit f8b2215145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 43 deletions

View File

@ -344,9 +344,9 @@ function run() {
var list = annotations[node.start.file]; var list = annotations[node.start.file];
var pure = list[node.start.pos]; var pure = list[node.start.pos];
if (!pure) { if (!pure) {
var pos = node.start.parens; var tokens = node.start.parens;
if (pos) for (var i = 0; !pure && i < pos.length; i++) { if (tokens) for (var i = 0; !pure && i < tokens.length; i++) {
pure = list[pos[i]]; pure = list[tokens[i].pos];
} }
} }
if (pure) node.pure = pure; if (pure) node.pure = pure;

View File

@ -56,35 +56,31 @@ function DEFNODE(type, props, methods, base) {
code.push("this.", prop, "=props.", prop, ";"); code.push("this.", prop, "=props.", prop, ";");
}); });
code.push("}"); code.push("}");
var proto = base && new base; var proto = Object.create(base && base.prototype);
if (proto && proto.initialize || methods && methods.initialize) code.push("this.initialize();"); if (methods.initialize || proto.initialize) code.push("this.initialize();");
code.push("}"); code.push("};");
var ctor = new Function(code.join(""))(); var ctor = new Function(code.join(""))();
if (proto) { ctor.prototype = proto;
ctor.prototype = proto;
ctor.BASE = base;
}
if (base) base.SUBCLASSES.push(ctor);
ctor.prototype.CTOR = ctor; ctor.prototype.CTOR = ctor;
ctor.PROPS = props || null; ctor.prototype.TYPE = ctor.TYPE = type;
ctor.SELF_PROPS = self_props; if (base) {
ctor.SUBCLASSES = []; ctor.BASE = base;
if (type) { base.SUBCLASSES.push(ctor);
ctor.prototype.TYPE = ctor.TYPE = type;
}
if (methods) for (var name in methods) if (HOP(methods, name)) {
if (/^\$/.test(name)) {
ctor[name.substr(1)] = methods[name];
} else {
ctor.prototype[name] = methods[name];
}
} }
ctor.DEFMETHOD = function(name, method) { ctor.DEFMETHOD = function(name, method) {
this.prototype[name] = method; this.prototype[name] = method;
}; };
if (typeof exports !== "undefined") { ctor.PROPS = props;
exports["AST_" + type] = ctor; ctor.SELF_PROPS = self_props;
ctor.SUBCLASSES = [];
for (var name in methods) if (HOP(methods, name)) {
if (/^\$/.test(name)) {
ctor[name.substr(1)] = methods[name];
} else {
ctor.DEFMETHOD(name, methods[name]);
}
} }
if (typeof exports !== "undefined") exports["AST_" + type] = ctor;
return ctor; return ctor;
} }

View File

@ -1510,7 +1510,7 @@ merge(Compressor.prototype, {
function is_lhs_read_only(lhs, compressor) { function is_lhs_read_only(lhs, compressor) {
if (lhs instanceof AST_ObjectIdentity) return true; if (lhs instanceof AST_ObjectIdentity) return true;
if (lhs instanceof AST_PropAccess) { if (lhs instanceof AST_PropAccess) {
if (lhs.property == "__proto__") return true; if (lhs.property === "__proto__") return true;
lhs = lhs.expression; lhs = lhs.expression;
if (lhs instanceof AST_SymbolRef) { if (lhs instanceof AST_SymbolRef) {
if (lhs.is_immutable()) return false; if (lhs.is_immutable()) return false;
@ -3842,7 +3842,7 @@ merge(Compressor.prototype, {
def(AST_Object, function(compressor, force) { def(AST_Object, function(compressor, force) {
return is_strict(compressor, force) && !all(this.properties, function(prop) { return is_strict(compressor, force) && !all(this.properties, function(prop) {
if (!(prop instanceof AST_ObjectKeyVal)) return false; if (!(prop instanceof AST_ObjectKeyVal)) return false;
return !(prop.key == "__proto__" && prop.value._dot_throw(compressor, force)); return !(prop.key === "__proto__" && prop.value._dot_throw(compressor, force));
}); });
}); });
def(AST_ObjectIdentity, function(compressor, force) { def(AST_ObjectIdentity, function(compressor, force) {
@ -6727,7 +6727,7 @@ merge(Compressor.prototype, {
return insert_statements(body, node, in_list); return insert_statements(body, node, in_list);
} }
if (node instanceof AST_Import) { if (node instanceof AST_Import) {
if (node.properties && node.properties == 0) node.properties = null; if (node.properties && node.properties.length == 0) node.properties = null;
return node; return node;
} }
if (node instanceof AST_Sequence) { if (node instanceof AST_Sequence) {
@ -11809,7 +11809,7 @@ merge(Compressor.prototype, {
var props = expr.properties; var props = expr.properties;
for (var i = props.length; --i >= 0;) { for (var i = props.length; --i >= 0;) {
var prop = props[i]; var prop = props[i];
if (prop.key != key) continue; if (prop.key !== key) continue;
if (!all(props, can_hoist_property)) break; if (!all(props, can_hoist_property)) break;
if (!safe_to_flatten(prop.value, compressor)) break; if (!safe_to_flatten(prop.value, compressor)) break;
props = props.map(function(prop) { props = props.map(function(prop) {

View File

@ -1334,21 +1334,19 @@ function parse($TEXT, options) {
if (is("punc", "{")) { if (is("punc", "{")) {
body = block_(); body = block_();
value = null; value = null;
if (S.input.has_directive("use strict")) {
argnames.forEach(strict_verify_symbol);
}
} else { } else {
body = []; body = [];
handle_regexp(); handle_regexp();
value = maybe_assign(); value = maybe_assign();
} }
var is_strict = S.input.has_directive("use strict");
S.input.pop_directives_stack(); S.input.pop_directives_stack();
--S.in_function; --S.in_function;
S.in_loop = loop; S.in_loop = loop;
S.labels = labels; S.labels = labels;
S.in_generator = was_gen; S.in_generator = was_gen;
S.in_async = was_async; S.in_async = was_async;
return new (async ? AST_AsyncArrow : AST_Arrow)({ var node = new (async ? AST_AsyncArrow : AST_Arrow)({
start: start, start: start,
argnames: argnames, argnames: argnames,
rest: rest, rest: rest,
@ -1356,6 +1354,8 @@ function parse($TEXT, options) {
value: value, value: value,
end: prev(), end: prev(),
}); });
if (is_strict) node.each_argname(strict_verify_symbol);
return node;
} }
var function_ = function(ctor) { var function_ = function(ctor) {
@ -1388,23 +1388,24 @@ function parse($TEXT, options) {
S.in_loop = 0; S.in_loop = 0;
S.labels = []; S.labels = [];
var body = block_(); var body = block_();
if (S.input.has_directive("use strict")) { var is_strict = S.input.has_directive("use strict");
if (name) strict_verify_symbol(name);
argnames.forEach(strict_verify_symbol);
if (argnames.rest) strict_verify_symbol(argnames.rest);
}
S.input.pop_directives_stack(); S.input.pop_directives_stack();
--S.in_function; --S.in_function;
S.in_loop = loop; S.in_loop = loop;
S.labels = labels; S.labels = labels;
S.in_generator = was_gen; S.in_generator = was_gen;
S.in_async = was_async; S.in_async = was_async;
return new ctor({ var node = new ctor({
name: name, name: name,
argnames: argnames, argnames: argnames,
rest: argnames.rest || null, rest: argnames.rest || null,
body: body body: body
}); });
if (is_strict) {
if (name) strict_verify_symbol(name);
node.each_argname(strict_verify_symbol);
}
return node;
}; };
function if_() { function if_() {

View File

@ -94,13 +94,15 @@ drop_unused: {
} }
input: { input: {
import a, * as b from "foo"; import a, * as b from "foo";
import { c, bar as d } from "baz"; import { c } from "bar";
console.log(c); import { d, _ as e } from "baz";
console.log(d);
} }
expect: { expect: {
import "foo"; import "foo";
import { c as c } from "baz"; import "bar";
console.log(c); import { d as d } from "baz";
console.log(d);
} }
} }