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: add Object.hasOwn as pure function #4482

merged 2 commits into from Apr 30, 2022


Copy link

@dnalborczyk dnalborczyk commented Apr 29, 2022

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:


Copy link
Contributor Author

@dnalborczyk dnalborczyk commented Apr 29, 2022

@lukastaegert I noticed Object.entries was added recently as pure function. I was always wondering what constitutes a pure function?

I thought a pure function is one without side effects, e.g.


compare to: Object.values:

which should behave the same as Object.entries:

Copy link

@codecov codecov bot commented Apr 29, 2022

Codecov Report

Merging #4482 (54861c2) into master (1b1df9b) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #4482   +/-   ##
  Coverage   98.75%   98.75%           
  Files         205      205           
  Lines        7327     7327           
  Branches     2080     2080           
  Hits         7236     7236           
  Misses         33       33           
  Partials       58       58           
Impacted Files Coverage Δ
src/ast/nodes/shared/knownGlobals.ts 92.00% <ø> (ø)

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 1b1df9b...54861c2. Read the comment docs.

Copy link

@lukastaegert lukastaegert commented Apr 30, 2022

@lukastaegert I noticed Object.entries was added recently as pure function. I was always wondering what constitutes a pure function?

You are right, with the current simple logic all of them should not be marked as pure. To solve this in a good way, we would need a way to specify a function iterates the keys of its argument and is pure if there are no own property getters.

The global logic is very dumb at the moment as it only understands a single flag—pure.

Compare this with the much more advanced logic for builtin prototypes (which ideally should be merged with the globals logic at some point):

export interface MemberDescription {
callsArgs: number[] | null;
returns: ExpressionEntity;

Here we know a return value and can specify that an argument is called (but do not know about iterating keys because that was not needed).

So we could add a flag that says something like accessesArgumentProps and calling it has a side effect if

callOptions.args[0].hasEffectsWhenAccessedAtPath(UNKNOWN_PATH, context)

@lukastaegert lukastaegert merged commit 6518775 into rollup:master Apr 30, 2022
12 checks passed
@dnalborczyk dnalborczyk deleted the object-hasown branch May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants