Skip to content

fix: forward __NO_SIDE_EFFECTS__ annotations to function expressions in variable declarations#6272

Merged
lukastaegert merged 1 commit into
masterfrom
no-side-effects-function-expression
Feb 20, 2026
Merged

fix: forward __NO_SIDE_EFFECTS__ annotations to function expressions in variable declarations#6272
lukastaegert merged 1 commit into
masterfrom
no-side-effects-function-expression

Conversation

@lukastaegert
Copy link
Copy Markdown
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

When a /*#__NO_SIDE_EFFECTS__*/ annotation (or its JSDoc equivalents @__NO_SIDE_EFFECTS__ / #__NO_SIDE_EFFECTS__) precedes a const/let/var declaration, the Rust AST converter needs to forward the annotation from the VariableDeclaration node to the actual function value in the initializer so that annotationNoSideEffects gets set on the function node.

Previously, this forwarding only handled Expr::Arrow (arrow functions). As a result, function expressions (function() {}) assigned to a variable with a leading #__NO_SIDE_EFFECTS__ annotation were silently ignored—the annotation was consumed but never attached to the FunctionExpression node—so those variables were never tree-shaken.

The fix adds Expr::Fn to the match arm in variable_declarator.rs, so leading #__NO_SIDE_EFFECTS__ annotations are also forwarded to function expression initializers.

The existing test no-side-effects-function-declaration has been extended to cover function expressions alongside arrow functions for all annotation styles (inline, leading block comment, JSDoc @__NO_SIDE_EFFECTS__, and JSDoc #__NO_SIDE_EFFECTS__).

Note: inline annotations placed directly before the function keyword (e.g. const fn = /*#__NO_SIDE_EFFECTS__*/ function() {}) already worked correctly and continue to do so, as those comments are attached to the FunctionExpression node directly by the parser.

Copilot AI review requested due to automatic review settings February 20, 2026 10:44
@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
rollup Ready Ready Preview, Comment Feb 20, 2026 10:45am

Request Review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 20, 2026

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#no-side-effects-function-expression

Notice: Ensure you have installed the latest nightly Rust toolchain. If you haven't installed it yet, please see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust.

or load it into the REPL:
https://rollup-h456i9a2u-rollup-js.vercel.app/repl/?pr=6272

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Rollup’s Rust AST conversion so /*#__NO_SIDE_EFFECTS__*/ (and JSDoc equivalents) placed before a variable declaration are forwarded to function expression initializers (not just arrow functions), enabling correct tree-shaking.

Changes:

  • Forward NoSideEffects annotations from VariableDeclarator to Expr::Fn initializers in the Rust AST converter.
  • Extend the existing form test sample to cover function expressions (including async) across the different annotation styles.
  • Update the sample’s imports/usages to exercise the additional exported functions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
rust/parse_ast/src/ast_nodes/variable_declarator.rs Forwards collected NoSideEffects annotations for Expr::Fn initializers, matching existing behavior for Expr::Arrow.
test/form/samples/no-side-effects-function-declaration/functions.js Adds function-expression variants for the existing annotation styles to validate the forwarding behavior.
test/form/samples/no-side-effects-function-declaration/main.js Imports/calls the new functions so the form test asserts they are tree-shaken when annotated.

@github-actions
Copy link
Copy Markdown

Performance report

  • BUILD: 6347ms, 836 MB
    • initialize: 0ms, 23.8 MB (+8%)
    • generate module graph: 2278ms, 630 MB
      • generate ast: 1231ms, 624 MB
    • sort and bind modules: 350ms, 685 MB
    • mark included statements: 3714ms, 836 MB
      • treeshaking pass 1: 2175ms, 826 MB
      • treeshaking pass 2: 438ms, 841 MB
      • treeshaking pass 3: 370ms, 833 MB
      • treeshaking pass 4: 360ms, 839 MB
      • treeshaking pass 5: 356ms, 836 MB
  • GENERATE: 607ms, 931 MB
    • initialize render: 0ms, 836 MB
    • generate chunks: 36ms, 840 MB
      • optimize chunks: 0ms, 833 MB
    • render chunks: 556ms, 904 MB
    • transform chunks: 16ms, 931 MB
    • generate bundle: 0ms, 931 MB

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.77%. Comparing base (7f00689) to head (a8f0724).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6272   +/-   ##
=======================================
  Coverage   98.76%   98.77%           
=======================================
  Files         273      273           
  Lines       10712    10712           
  Branches     2855     2855           
=======================================
+ Hits        10580    10581    +1     
  Misses         89       89           
+ Partials       43       42    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lukastaegert lukastaegert merged commit e6f6f36 into master Feb 20, 2026
52 checks passed
@lukastaegert lukastaegert deleted the no-side-effects-function-expression branch February 20, 2026 12:03
lukastaegert added a commit that referenced this pull request Feb 20, 2026
…le declarations (#6272)

Handle __NO_SIDE_EFFECTS__ for function expressions in declarations.
@github-actions
Copy link
Copy Markdown

This PR has been released as part of rollup@4.58.0. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants