mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 05:32:09 +03:00
fix(es/minifier): Respect dead_code
(#4328)
This commit is contained in:
parent
62b021b5b2
commit
8886a4b572
@ -1,15 +1,14 @@
|
||||
#![deny(warnings)]
|
||||
#![cfg(feature = "node")]
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
env::{current_dir, set_current_dir},
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Error};
|
||||
use lazy_static::lazy_static;
|
||||
use swc_common::{collections::AHashMap, FileName};
|
||||
use swc_common::FileName;
|
||||
extern crate swc_ecma_loader;
|
||||
use swc_ecma_loader::{resolve::Resolve, resolvers::node::NodeModulesResolver, TargetEnv};
|
||||
|
||||
@ -37,7 +36,7 @@ fn basic_import() {
|
||||
|
||||
// When
|
||||
let resolved = node_resolver
|
||||
.resolve(&FileName::Real(PathBuf::from(&"jquery")), &"jquery")
|
||||
.resolve(&FileName::Real(PathBuf::from("jquery")), "jquery")
|
||||
.expect("should resolve");
|
||||
|
||||
// Expect
|
||||
@ -56,7 +55,7 @@ fn hoisting() {
|
||||
|
||||
// When
|
||||
let resolved = node_resolver
|
||||
.resolve(&FileName::Real(PathBuf::from(&"jquery")), &"jquery")
|
||||
.resolve(&FileName::Real(PathBuf::from("jquery")), "jquery")
|
||||
.expect("should resolve");
|
||||
|
||||
// Expect
|
||||
|
@ -2652,10 +2652,7 @@ where
|
||||
return;
|
||||
}
|
||||
|
||||
let ctx = Ctx {
|
||||
top_level: false,
|
||||
..self.ctx
|
||||
};
|
||||
let ctx = Ctx { ..self.ctx };
|
||||
|
||||
self.with_ctx(ctx).inject_else(stmts);
|
||||
|
||||
|
@ -64,9 +64,11 @@ where
|
||||
T: StmtLike,
|
||||
{
|
||||
if !self.options.sequences() {
|
||||
log_abort!("sequences: make_sequence for statements is disabled");
|
||||
return;
|
||||
}
|
||||
if self.ctx.in_asm {
|
||||
log_abort!("sequences: asm.js is not supported");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -718,6 +720,7 @@ where
|
||||
};
|
||||
|
||||
if !self.options.sequences() && !e.span.has_mark(self.marks.synthesized_seq) {
|
||||
log_abort!("sequences: Disabled && no mark");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,26 @@ where
|
||||
return;
|
||||
}
|
||||
|
||||
if !self.options.top_level()
|
||||
&& (self.ctx.is_top_level_for_block_level_vars() || self.ctx.in_top_level())
|
||||
&& !var.span.has_mark(self.marks.non_top_level)
|
||||
{
|
||||
match self.ctx.var_kind {
|
||||
Some(VarDeclKind::Const) | Some(VarDeclKind::Let) => {
|
||||
if self.ctx.is_top_level_for_block_level_vars() {
|
||||
log_abort!("unused: Top-level (block level)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if self.ctx.in_top_level() {
|
||||
log_abort!("unused: Top-level");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let had_init = var.init.is_some();
|
||||
|
||||
match &mut var.init {
|
||||
@ -94,7 +114,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
self.take_pat_if_unused(DUMMY_SP, pat, None)
|
||||
self.take_pat_if_unused(DUMMY_SP, pat, None, false)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
|
||||
@ -163,13 +183,13 @@ where
|
||||
return;
|
||||
}
|
||||
|
||||
self.take_pat_if_unused(var_declarator_span, name, init);
|
||||
self.take_pat_if_unused(var_declarator_span, name, init, true);
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
|
||||
pub(super) fn drop_unused_params(&mut self, params: &mut Vec<Param>) {
|
||||
for param in params.iter_mut().rev() {
|
||||
self.take_pat_if_unused(DUMMY_SP, &mut param.pat, None);
|
||||
self.take_pat_if_unused(DUMMY_SP, &mut param.pat, None, false);
|
||||
|
||||
if !param.pat.is_invalid() {
|
||||
return;
|
||||
@ -312,6 +332,7 @@ where
|
||||
parent_span: Span,
|
||||
name: &mut Pat,
|
||||
mut init: Option<&mut Expr>,
|
||||
is_var_decl: bool,
|
||||
) {
|
||||
if self.ctx.is_exported {
|
||||
return;
|
||||
@ -360,7 +381,7 @@ where
|
||||
.as_mut()
|
||||
.and_then(|expr| self.access_numeric_property(expr, idx));
|
||||
|
||||
self.take_pat_if_unused(parent_span, p, elem);
|
||||
self.take_pat_if_unused(parent_span, p, elem, is_var_decl);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
@ -387,7 +408,7 @@ where
|
||||
continue;
|
||||
}
|
||||
|
||||
self.take_pat_if_unused(parent_span, &mut p.value, None);
|
||||
self.take_pat_if_unused(parent_span, &mut p.value, None, is_var_decl);
|
||||
}
|
||||
ObjectPatProp::Assign(AssignPatProp {
|
||||
key, value: None, ..
|
||||
|
@ -254,6 +254,10 @@ impl VisitMut for Pure<'_> {
|
||||
}
|
||||
|
||||
fn visit_mut_expr(&mut self, e: &mut Expr) {
|
||||
if let Expr::Paren(p) = e {
|
||||
*e = *p.expr.take();
|
||||
}
|
||||
|
||||
{
|
||||
let ctx = Ctx {
|
||||
in_first_expr: false,
|
||||
|
@ -118,9 +118,6 @@ dead_code/dead_code_2_should_warn/input.js
|
||||
dead_code/dead_code_2_should_warn_strict/input.js
|
||||
dead_code/dead_code_const_annotation/input.js
|
||||
dead_code/dead_code_const_annotation_complex_scope/input.js
|
||||
dead_code/dead_code_const_declaration/input.js
|
||||
dead_code/dead_code_constant_boolean_should_warn_more/input.js
|
||||
dead_code/dead_code_constant_boolean_should_warn_more_strict/input.js
|
||||
dead_code/global_fns/input.js
|
||||
dead_code/issue_2749/input.js
|
||||
dead_code/issue_2860_2/input.js
|
||||
|
@ -279,6 +279,9 @@ dead_code/collapse_vars_misc1/input.js
|
||||
dead_code/dead_code_1/input.js
|
||||
dead_code/dead_code_block_decls_die/input.js
|
||||
dead_code/dead_code_const_annotation_regex/input.js
|
||||
dead_code/dead_code_const_declaration/input.js
|
||||
dead_code/dead_code_constant_boolean_should_warn_more/input.js
|
||||
dead_code/dead_code_constant_boolean_should_warn_more_strict/input.js
|
||||
dead_code/global_timeout_and_interval_symbols/input.js
|
||||
dead_code/issue_2233_1/input.js
|
||||
dead_code/issue_2233_2/input.js
|
||||
|
@ -1,7 +1,7 @@
|
||||
var unused;
|
||||
var CONST_FOO_ANN = false;
|
||||
/** @const */ var CONST_FOO_ANN = false;
|
||||
if (CONST_FOO_ANN) {
|
||||
console.log("unreachable");
|
||||
var moo;
|
||||
function bar() {}
|
||||
function bar() { }
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
var unused_var;
|
||||
var test = "test";
|
||||
/** @const */ var test = "test";
|
||||
// @const
|
||||
var CONST_FOO_ANN = false;
|
||||
var unused_var_2;
|
||||
if (CONST_FOO_ANN) {
|
||||
console.log("unreachable");
|
||||
var moo;
|
||||
function bar() {}
|
||||
function bar() { }
|
||||
}
|
||||
if (test === "test") {
|
||||
var beef = "good";
|
||||
var meat = "beef";
|
||||
/** @const */ var meat = "beef";
|
||||
var pork = "bad";
|
||||
if (meat === "pork") {
|
||||
console.log("also unreachable");
|
||||
|
@ -1,4 +1,3 @@
|
||||
var unused;
|
||||
const CONST_FOO = !1;
|
||||
var moo;
|
||||
var bar;
|
||||
var moo, bar;
|
||||
|
@ -1,6 +1,4 @@
|
||||
var foo;
|
||||
var bar;
|
||||
var foo, bar;
|
||||
var x = 10, y;
|
||||
var moo;
|
||||
var x = 10,
|
||||
y;
|
||||
bar();
|
||||
|
@ -1,6 +1,5 @@
|
||||
"use strict";
|
||||
var foo;
|
||||
var x = 10, y;
|
||||
var moo;
|
||||
var x = 10,
|
||||
y;
|
||||
bar();
|
||||
|
@ -412,14 +412,20 @@ pub struct Hoister {
|
||||
impl Visit for Hoister {
|
||||
noop_visit_type!();
|
||||
|
||||
fn visit_assign_expr(&mut self, node: &AssignExpr) {
|
||||
node.right.visit_children_with(self);
|
||||
}
|
||||
|
||||
fn visit_assign_pat_prop(&mut self, node: &AssignPatProp) {
|
||||
node.value.visit_with(self);
|
||||
|
||||
self.vars.push(node.key.clone());
|
||||
}
|
||||
|
||||
fn visit_assign_expr(&mut self, node: &AssignExpr) {
|
||||
node.right.visit_children_with(self);
|
||||
fn visit_fn_decl(&mut self, f: &FnDecl) {
|
||||
self.vars.push(f.ident.clone());
|
||||
|
||||
f.visit_children_with(self)
|
||||
}
|
||||
|
||||
fn visit_pat(&mut self, p: &Pat) {
|
||||
|
Loading…
Reference in New Issue
Block a user