Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/minifier): Drop redundant function parameters #9636

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ struct Analyzer<'a> {
#[allow(dead_code)]
config: &'a Config,
in_var_decl: bool,
in_param: bool,
scope: Scope<'a>,
data: &'a mut Data,
cur_class_id: Option<Id>,
Expand Down Expand Up @@ -518,10 +519,17 @@ impl Visit for Analyzer<'_> {
}
}

fn visit_param(&mut self, param: &Param) {
let old_is_in_param = self.in_param;
self.in_param = true;
param.visit_children_with(self);
self.in_param = old_is_in_param;
}

fn visit_pat(&mut self, p: &Pat) {
p.visit_children_with(self);

if !self.in_var_decl {
if !self.in_var_decl && !self.in_param {
if let Pat::Ident(i) = p {
self.add(i.to_id(), true);
}
Expand Down Expand Up @@ -844,6 +852,35 @@ impl VisitMut for TreeShaker {
self.in_fn = old_in_fn;
}

fn visit_mut_params(&mut self, params: &mut Vec<Param>) {
self.visit_mut_par(cpu_count() * 8, params);

// We iterate through trailing parameters until we either find a
// non-identifier parameter, or a parameter that can't be dropped.
let invalid_param_index = params.iter().rev().position(|param| {
let Some(id) = param.pat.as_ident() else {
// Non-identifier parameter
return true;
};

!self.can_drop_binding(id.to_id(), false)
});

match invalid_param_index {
None => {
// No invalid parameter found, we can remove all parameters.
params.clear();
}

Some(idx) if idx < params.len() - 1 => {
// Remove every parameter up to the invalid parameter.
params.truncate(params.len() - idx);
}

_ => {} // No change
}
}

fn visit_mut_import_specifiers(&mut self, ss: &mut Vec<ImportSpecifier>) {
ss.retain(|s| {
let local = match s {
Expand Down Expand Up @@ -880,6 +917,7 @@ impl VisitMut for TreeShaker {
let mut analyzer = Analyzer {
config: &self.config,
in_var_decl: false,
in_param: false,
scope: Default::default(),
data: &mut data,
cur_class_id: Default::default(),
Expand Down Expand Up @@ -943,6 +981,7 @@ impl VisitMut for TreeShaker {
let mut analyzer = Analyzer {
config: &self.config,
in_var_decl: false,
in_param: false,
scope: Default::default(),
data: &mut data,
cur_class_id: Default::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const resources = [
label: 'Instagram'
}
];
export function foo(websites) {
export function foo() {
const a = resources.map((resource)=>({
value: resource.value
}));
Expand Down
18 changes: 18 additions & 0 deletions crates/swc_ecma_transforms_optimization/tests/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@ fn test_template_strings_known_methods() {
test("x = parseFloat(`1.23`)", "x = 1.23");
}

#[test]
fn test_redundant_params() {
// One or more parameters can be dropped
test("x = function(a, b) {}", "x = function() {}");
test(
"x = function(a, b, c, d) {f(b);}",
"x = function(a, b) {f(b);}",
);
test(
"x = function(a, b, c, d) {f(c);}",
"x = function(a, b, c) {f(c);}",
);

// No parameters can be dropped
test_same("x = function(a, b) {f(b);}");
test_same("x = function(a) {f(a);}");
}

test!(
Syntax::Typescript(TsSyntax {
decorators: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function __swcpack_require__(mod) {
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
var load = __swcpack_require__.bind(void 0, function() {
console.log("foo");
console.log("bar");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ function __swcpack_require__(mod) {
cache = interop(module.exports);
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
var load = __swcpack_require__.bind(void 0, function() {
console.log("a");
});
var load1 = __swcpack_require__.bind(void 0, function(module, exports) {
var load1 = __swcpack_require__.bind(void 0, function() {
console.log("b");
});
var load2 = __swcpack_require__.bind(void 0, function(module, exports) {
var load2 = __swcpack_require__.bind(void 0, function() {
console.log("c");
});
load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var load4 = __swcpack_require__.bind(void 0, function(module, exports) {
bb: bb
};
});
var load5 = __swcpack_require__.bind(void 0, function(module, exports) {
var load5 = __swcpack_require__.bind(void 0, function() {
console.log("c");
});
load4();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class MuxAsyncIterator {
}
function emptyReader() {
return {
read (_) {
read () {
return Promise.resolve(null);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MuxAsyncIterator {
}
function emptyReader() {
return {
read (_) {
read () {
return Promise.resolve(null);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class ServerRequest {
}
}
console.log(ServerRequest);
async function writeResponse(w, r) {}
async function readRequest(conn, bufr) {}
async function writeResponse() {}
async function readRequest() {}
console.log(deferred, writeResponse, readRequest, MuxAsyncIterator);
async function listenAndServe(addr, handler) {}
async function listenAndServe() {}
listenAndServe({
port: 8080
}, async (req)=>{});
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function foo() {
}
function _foo() {
_foo = _async_to_generator(function() {
return _ts_generator(this, function(_state) {
return _ts_generator(this, function() {
return [
2
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function __swcpack_require__(mod) {
return cache;
}
var load = __swcpack_require__.bind(void 0, function(module, exports) {
function lodash(value) {
function lodash() {
console.log("lodash");
}
function memoize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function foo() {
}
function _foo() {
_foo = _async_to_generator(function() {
return _ts_generator(this, function(_state) {
return _ts_generator(this, function() {
return [
2
];
Expand Down
Loading