-
-
Notifications
You must be signed in to change notification settings - Fork 856
feat(minfier): support . separated values for compress.treeshake.manualPureFunctions
#16529
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
Merged
graphite-app
merged 1 commit into
main
from
12-05-fix_minfier_support_._separated_values_for_compress.treeshake.manualpurefunctions_
Dec 5, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
crates/oxc_ecmascript/src/side_effects/pure_function.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| use oxc_ast::ast::{ChainElement, Expression}; | ||
|
|
||
| /// Check if the callee is a pure function based on a list of pure function names. | ||
| /// | ||
| /// This handles: | ||
| /// - Simple identifiers: `foo()` matches `["foo"]` | ||
| /// - Member expressions: `console.log()` matches `["console"]` or `["console.log"]` | ||
| /// - Chained calls: `styled()()` or `styled().div()` matches `["styled"]` | ||
| /// - Optional chaining: `styled?.div()` or `console?.log()` matches `["styled"]` or `["console.log"]` | ||
| /// | ||
| /// Besides any functions matching that name, any properties on a pure function | ||
| /// and any functions returned from a pure function will also be considered pure. | ||
| /// For example, if `["console.log"]` is specified: | ||
| /// - `console.log()` is pure | ||
| /// - `console.log.foo()` is pure (property on pure function) | ||
| /// - `console.log()()` is pure (function returned from pure function) | ||
| /// - `console.log().foo()` is pure (property on returned function) | ||
| pub fn is_pure_function(callee: &Expression, pure_functions: &[String]) -> bool { | ||
| if pure_functions.is_empty() { | ||
| return false; | ||
| } | ||
| let Some(path_parts) = extract_callee_path(callee) else { | ||
| return false; | ||
| }; | ||
| pure_functions.iter().any(|pure_fn| is_path_match(&path_parts, pure_fn)) | ||
| } | ||
|
|
||
| /// Extract the path parts from a callee expression. | ||
| /// Returns None if the callee cannot be matched (e.g., contains non-string computed properties). | ||
| /// The returned Vec contains path parts in reverse order (from outermost to root). | ||
| fn extract_callee_path<'a>(callee: &'a Expression<'a>) -> Option<Vec<&'a str>> { | ||
| let mut path_parts: Vec<&str> = Vec::new(); | ||
| let mut current = callee; | ||
|
|
||
| loop { | ||
| match current { | ||
| Expression::Identifier(ident) => { | ||
| path_parts.push(ident.name.as_str()); | ||
| break; | ||
| } | ||
| Expression::StaticMemberExpression(member) => { | ||
| path_parts.push(member.property.name.as_str()); | ||
| current = &member.object; | ||
| } | ||
| Expression::ComputedMemberExpression(member) => { | ||
| let Expression::StringLiteral(lit) = &member.expression else { | ||
| return None; | ||
| }; | ||
| path_parts.push(lit.value.as_str()); | ||
| current = &member.object; | ||
| } | ||
| Expression::CallExpression(call) => { | ||
| // Call expressions don't add to the path, they just pass through | ||
| // But they do "seal" the previous path - anything before a call is an extension | ||
| path_parts.clear(); | ||
| current = &call.callee; | ||
| } | ||
| Expression::ChainExpression(chain) => match &chain.expression { | ||
| ChainElement::StaticMemberExpression(member) => { | ||
| path_parts.push(member.property.name.as_str()); | ||
| current = &member.object; | ||
| } | ||
| ChainElement::ComputedMemberExpression(member) => { | ||
| let Expression::StringLiteral(lit) = &member.expression else { | ||
| return None; | ||
| }; | ||
| path_parts.push(lit.value.as_str()); | ||
| current = &member.object; | ||
| } | ||
| ChainElement::CallExpression(call) => { | ||
| // Call expressions don't add to the path, they just pass through | ||
| // But they do "seal" the previous path - anything before a call is an extension | ||
| path_parts.clear(); | ||
| current = &call.callee; | ||
| } | ||
| ChainElement::TSNonNullExpression(ts) => { | ||
| current = &ts.expression; | ||
| } | ||
| ChainElement::PrivateFieldExpression(_) => { | ||
| return None; | ||
| } | ||
| }, | ||
| Expression::ParenthesizedExpression(paren) => { | ||
| current = &paren.expression; | ||
| } | ||
| _ => { | ||
| return None; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Some(path_parts) | ||
| } | ||
|
|
||
| /// Check if the extracted path matches the given pure function name. | ||
| /// The pure function name can be a dotted path like "console.log". | ||
| /// Returns true if the pure function name is a prefix of the callee's path. | ||
| fn is_path_match(path_parts: &[&str], pure_fn: &str) -> bool { | ||
| let pure_parts_count = pure_fn.bytes().filter(|&b| b == b'.').count() + 1; | ||
| if pure_parts_count > path_parts.len() { | ||
| return false; | ||
| } | ||
| pure_fn.split('.').zip(path_parts.iter().rev()).all(|(a, b)| a == *b) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
crates/oxc_minifier/tests/ecmascript/is_pure_function.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| use oxc_allocator::Allocator; | ||
| use oxc_ast::ast::{ChainElement, Expression, Statement}; | ||
| use oxc_ecmascript::side_effects::is_pure_function; | ||
| use oxc_parser::Parser; | ||
| use oxc_span::SourceType; | ||
|
|
||
| #[track_caller] | ||
| fn test(source: &str, pure_functions: &[&str], expected: bool) { | ||
| let allocator = Allocator::default(); | ||
| let ret = Parser::new(&allocator, source, SourceType::mjs()).parse(); | ||
| assert!(!ret.panicked, "{source}"); | ||
| assert!(ret.errors.is_empty(), "{source}"); | ||
|
|
||
| let Some(Statement::ExpressionStatement(stmt)) = ret.program.body.first() else { | ||
| panic!("should have an expression statement body: {source}"); | ||
| }; | ||
| let callee = get_callee(stmt.expression.without_parentheses(), source); | ||
|
|
||
| let pure_fns: Vec<String> = pure_functions.iter().map(ToString::to_string).collect(); | ||
| assert_eq!( | ||
| is_pure_function(callee, &pure_fns), | ||
| expected, | ||
| "{source} with pure_functions={pure_functions:?}" | ||
| ); | ||
| } | ||
|
|
||
| fn get_callee<'a>(expr: &'a Expression<'a>, source: &str) -> &'a Expression<'a> { | ||
| match expr { | ||
| Expression::CallExpression(call) => &call.callee, | ||
| Expression::NewExpression(new_expr) => &new_expr.callee, | ||
| Expression::TaggedTemplateExpression(tagged) => &tagged.tag, | ||
| Expression::ChainExpression(chain) => match &chain.expression { | ||
| ChainElement::CallExpression(call) => &call.callee, | ||
| _ => panic!("should have a call expression inside chain: {source}"), | ||
| }, | ||
| _ => panic!("should have a call, new, or tagged template expression: {source}"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_simple_identifiers() { | ||
| test("foo()", &["foo"], true); | ||
| test("bar()", &["foo"], false); | ||
| test("Foo()", &["foo"], false); | ||
| test("foo()", &["Foo"], false); | ||
| test("Foo()", &["Foo"], true); | ||
| test("foo()", &["foo", "bar"], true); | ||
| test("bar()", &["foo", "bar"], true); | ||
| test("baz()", &["foo", "bar"], false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_member_expressions() { | ||
| test("console.log()", &["console"], true); | ||
| test("console['log']()", &["console"], true); | ||
| test("console.warn()", &["console"], true); | ||
| test("other.log()", &["console"], false); | ||
| test("other.console.log()", &["console"], false); | ||
| test("other['console'].log()", &["console"], false); | ||
| test("console.log()", &["console.log"], true); | ||
| test("console['log']()", &["console.log"], true); | ||
| test("console.warn()", &["console.log"], false); | ||
| test("console['warn']()", &["console.log"], false); | ||
| test("console.console.console()", &["console"], true); | ||
| test("console.foo.console()", &["console"], true); | ||
| test("console.other.log()", &["console.log"], false); | ||
|
|
||
| test("a.b.c()", &["a"], true); | ||
| test("a.b.c()", &["a.b"], true); | ||
| test("a.b.c()", &["a.b.c"], true); | ||
| test("a['b'].c()", &["a.b.c"], true); | ||
| test("a.b['c']()", &["a.b.c"], true); | ||
| test("a['b']['c']()", &["a.b.c"], true); | ||
| test("a.b.d()", &["a.b.c"], false); | ||
| test("a.b()", &["a.b.c"], false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_chained_calls() { | ||
| test("styled()()", &["styled"], true); | ||
| test("styled()('div')", &["styled"], true); | ||
| test("other()()", &["styled"], false); | ||
| test("console.log()()", &["console.log"], true); | ||
| test("console.log().foo()", &["console.log"], true); | ||
|
|
||
| test("styled().div()", &["styled"], true); | ||
| test("styled().button()", &["styled"], true); | ||
| test("other().div()", &["styled"], false); | ||
| test("console.log.foo()", &["console.log"], true); | ||
| test("console.log.bar.baz()", &["console.log"], true); | ||
|
|
||
| test("a()()()", &["a"], true); | ||
| test("a()().b()", &["a"], true); | ||
| test("a().b().c()", &["a"], true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_optional_chaining() { | ||
| test("styled?.()", &["styled"], true); | ||
| test("other?.()", &["styled"], false); | ||
| test("styled?.div()", &["styled"], true); | ||
| test("console?.log()", &["console"], true); | ||
| test("console?.log()", &["console.log"], true); | ||
| test("console?.warn()", &["console.log"], false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_new_expressions() { | ||
| test("new Foo()", &["Foo"], true); | ||
| test("new Bar()", &["Foo"], false); | ||
|
|
||
| test("new styled.div()", &["styled"], true); | ||
| test("new styled.div()", &["styled.div"], true); | ||
| test("new styled.button()", &["styled.div"], false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_tagged_template_expressions() { | ||
| test("foo``", &["foo"], true); | ||
| test("bar``", &["foo"], false); | ||
|
|
||
| test("styled.div``", &["styled"], true); | ||
| test("other.div``", &["styled"], false); | ||
| test("styled.div``", &["styled.div"], true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_edge_cases() { | ||
| test("foo()", &[], false); | ||
| test("console.log()", &[], false); | ||
| test("styled()()", &[], false); | ||
|
|
||
| test("(foo)()", &["foo"], true); | ||
| test("(bar)()", &["foo"], false); | ||
| test("((foo))()", &["foo"], true); | ||
| test("(((console.log)))()", &["console.log"], true); | ||
|
|
||
| test("foo()", &[""], false); // should not match anything | ||
| test("console.log()", &["console."], false); // should not match anything | ||
| test("console()", &["console."], false); // should not match anything | ||
| test("console.log()", &["."], false); // should not match anything | ||
| } | ||
|
|
||
| /// Based on https://github.com/rollup/rollup/blob/v4.53.3/test/form/samples/manual-pure-functions | ||
| #[test] | ||
| fn test_rollup_manual_pure_functions() { | ||
| test("foo()", &["foo", "bar.a"], true); | ||
| test("foo.a()", &["foo", "bar.a"], true); | ||
| test("foo.a()()", &["foo", "bar.a"], true); | ||
| test("foo.a().a()", &["foo", "bar.a"], true); | ||
| test("foo.a().a()()", &["foo", "bar.a"], true); | ||
| test("foo.a().a().a()", &["foo", "bar.a"], true); | ||
|
|
||
| test("bar()", &["foo", "bar.a"], false); | ||
| test("bar.b()", &["foo", "bar.a"], false); | ||
|
|
||
| test("bar.a()", &["foo", "bar.a"], true); | ||
| test("bar?.a()", &["foo", "bar.a"], true); | ||
| test("bar.a.a()", &["foo", "bar.a"], true); | ||
| test("bar.a()()", &["foo", "bar.a"], true); | ||
| test("bar.a().a()", &["foo", "bar.a"], true); | ||
| test("bar.a()()()", &["foo", "bar.a"], true); | ||
| test("bar.a()().a()", &["foo", "bar.a"], true); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.