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

Make accessing unknown globals a side-effect #3068

Merged
merged 6 commits into from Sep 8, 2019

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Aug 20, 2019

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:
Resolves #3045

Description

This will make accessing unknown global variables a side-effect so that unchecked variable accesses are not removed by tree-shaking. To avoid too many false positives, the global handling was revised to use a common table for existing globals as well as pure global functions. The plan is to extend this in the future to add return types to global functions as well as handle global functions that have no side-effect except calling their arguments. In the end, I hope to merge this at some point with the existing logic for builtin prototypes.

The known globals have been mostly taken from the builtin section of https://github.com/sindresorhus/globals. Additionally, this also adds the global object to global detection as either globalThis, window, global or self. I know that in any real environment, only globalThis is kind of a standard, but maybe this is a good compromise for the start.

It also adds an new treeshake.unknownGlobalSideEffects option to deactivate this.

@codecov
Copy link

codecov bot commented Aug 20, 2019

Codecov Report

Merging #3068 into master will increase coverage by 0.14%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3068      +/-   ##
==========================================
+ Coverage   89.06%   89.21%   +0.14%     
==========================================
  Files         165      165              
  Lines        5715     5719       +4     
  Branches     1738     1737       -1     
==========================================
+ Hits         5090     5102      +12     
+ Misses        383      380       -3     
+ Partials      242      237       -5
Impacted Files Coverage Δ
src/Graph.ts 92.94% <ø> (ø) ⬆️
src/ast/nodes/shared/knownGlobals.ts 100% <100%> (ø)
src/Module.ts 94.27% <100%> (+0.01%) ⬆️
src/ast/nodes/Identifier.ts 100% <100%> (+14.28%) ⬆️
src/ast/nodes/shared/FunctionNode.ts 100% <100%> (ø) ⬆️
src/ast/variables/GlobalVariable.ts 100% <100%> (ø) ⬆️

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 bba1466...eec0138. Read the comment docs.

@lukastaegert lukastaegert force-pushed the gh-3045-unknown-global-side-effects branch from a84ba59 to f5441a9 Compare August 20, 2019 15:21
@lukastaegert lukastaegert force-pushed the gh-3045-unknown-global-side-effects branch from f5441a9 to 4ed5632 Compare August 28, 2019 14:24
@lukastaegert lukastaegert changed the title [WIP] Make accessing unknown globals a side-effect Make accessing unknown globals a side-effect Aug 28, 2019
@lukastaegert lukastaegert marked this pull request as ready for review August 28, 2019 14:51
@lukastaegert lukastaegert force-pushed the gh-3045-unknown-global-side-effects branch from c505f11 to 322c3e5 Compare August 29, 2019 15:13
@Conduitry
Copy link
Contributor

Conduitry commented Sep 15, 2019

This seems to be causing this issue. Would it make sense to not assume typeof some_unknown_global has side effects? Maybe this doesn't actually make sense as a solution to the problem, I don't know. Probably disregard this. I'm still confused.

@Conduitry
Copy link
Contributor

Figured out what I meant, and opened #3115.

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

Successfully merging this pull request may close these issues.

Rollup silently removes undefined variables from final build
2 participants