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

Correctly associate pure annotations and remove invalid ones #4095

Merged
merged 3 commits into from May 26, 2021

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented May 25, 2021

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

This PR will improve the handling of pure annotations in several respects:

  • annotations in front of conditional, logical, binary or sequence expressions will be associated with the first element unless the expression is surrounded by parentheses REPL
  • annotations in front of call or new expressions will be associated with the expression even when there are parentheses
  • annotations that cannot be associated with a call or new expression are always removed from the code REPL

I verified that the implemented behaviour seems to be in line with how terser behaves. This would also resolve discussions from #4068

@lukastaegert
Copy link
Member Author

cc @kzc

@github-actions
Copy link

github-actions bot commented May 25, 2021

Thank you for your contribution! ❤️

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

npm install rollup/rollup#correctly-associate-annotations

or load it into the REPL:
https://rollupjs.org/repl/?pr=4095

@codecov
Copy link

codecov bot commented May 25, 2021

Codecov Report

Merging #4095 (12d5b03) into master (2febefa) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4095   +/-   ##
=======================================
  Coverage   98.01%   98.02%           
=======================================
  Files         201      201           
  Lines        6994     7029   +35     
  Branches     2054     2060    +6     
=======================================
+ Hits         6855     6890   +35     
  Misses         66       66           
  Partials       73       73           
Impacted Files Coverage Δ
src/Module.ts 100.00% <ø> (ø)
src/ast/nodes/CallExpression.ts 100.00% <ø> (ø)
src/ast/nodes/IfStatement.ts 100.00% <ø> (ø)
src/ast/nodes/NewExpression.ts 100.00% <ø> (ø)
src/Graph.ts 100.00% <100.00%> (ø)
src/ast/keys.ts 100.00% <100.00%> (ø)
src/ast/nodes/shared/Node.ts 100.00% <100.00%> (ø)
src/utils/pureComments.ts 100.00% <100.00%> (ø)
src/utils/sourceMappingURL.ts 100.00% <100.00%> (ø)
src/utils/treeshakeNode.ts 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2febefa...12d5b03. Read the comment docs.

@kzc
Copy link
Contributor

kzc commented May 25, 2021

Good stuff.

This is difficult to get right. I trust if the results were vetted with terser then it ought to be fine. Should there be pure annotation disagreements between minifiers, use the defacto behavior of uglify-js which happens to support ES2020+ these days.

With the recent PR on sequences it ought to be easy to support pure annotated calls with side effects in their parameters:

$ echo 'var foo = /*@__PURE__*/ bar(0, baz(), /*@__PURE__*/ boo(), 3);' | rollup --silent
/*@__PURE__*/ bar(0, baz(), /*@__PURE__*/ boo(), 3);
$ echo 'var foo = /*@__PURE__*/ bar(0, baz(), /*@__PURE__*/ boo(), 3);' | uglify-js --toplevel -mc
baz();

@kzc
Copy link
Contributor

kzc commented May 25, 2021

Side note... the surprising complexity of correctly associating comments with AST nodes and preserving their intent across various javascript tools is why I strongly discourage extending pure annotations beyond calls and new expressions. Anything can be wrapped in pure IIFEs in the worst case anyway.

@lukastaegert lukastaegert merged commit ce2592d into master May 26, 2021
@lukastaegert lukastaegert deleted the correctly-associate-annotations branch May 26, 2021 07:19
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.

None yet

2 participants