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
merged 2 commits into from Apr 30, 2022

Conversation

dnalborczyk
Copy link
Contributor

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

@dnalborczyk
Copy link
Contributor Author

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.

Object.keys:
https://rollupjs.org/repl/?version=2.70.2&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmNvbnN0JTIwZm9vJTIwJTNEJTIwJTdCJTVDbiUyMCUyMGdldCUyMGJhcigpJTIwJTdCJTVDbiUyMCUyMCUyMCUyMGNvbnNvbGUubG9nKCdiYXInKSU1Q24lMjAlMjAlN0QlNUNuJTdEJTVDbiU1Q25PYmplY3Qua2V5cyhmb28pJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyZXMlMjIlMkMlMjJuYW1lJTIyJTNBJTIybXlCdW5kbGUlMjIlMkMlMjJhbWQlMjIlM0ElN0IlMjJpZCUyMiUzQSUyMiUyMiU3RCUyQyUyMmdsb2JhbHMlMjIlM0ElN0IlN0QlN0QlMkMlMjJleGFtcGxlJTIyJTNBbnVsbCU3RA==

compare to: Object.values:
https://rollupjs.org/repl/?version=2.70.2&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUy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FtcGxlJTIyJTNBbnVsbCU3RA==

@codecov
Copy link

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.

@lukastaegert
Copy link
Member

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
@dnalborczyk dnalborczyk deleted the object-hasown branch May 5, 2022 03:37
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