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
fix: do not mark computed React[...]
methods as pure
#14528
Merged
nicolo-ribaudo
merged 1 commit into
babel:main
from
JLHwung:improve-react-pure-annotations
May 17, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import annotateAsPure from "@babel/helper-annotate-as-pure"; | ||
import { types as t } from "@babel/core"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
// Mapping of React top-level methods that are pure. | ||
// This plugin adds a /*#__PURE__#/ annotation to calls to these methods, | ||
// so that terser and other minifiers can safely remove them during dead | ||
// code elimination. | ||
// See https://reactjs.org/docs/react-api.html | ||
const PURE_CALLS = new Map([ | ||
const PURE_CALLS: [string, Set<string>][] = [ | ||
[ | ||
"react", | ||
[ | ||
new Set([ | ||
"cloneElement", | ||
"createContext", | ||
"createElement", | ||
|
@@ -20,10 +21,10 @@ const PURE_CALLS = new Map([ | |
"isValidElement", | ||
"memo", | ||
"lazy", | ||
], | ||
]), | ||
], | ||
["react-dom", ["createPortal"]], | ||
]); | ||
["react-dom", new Set(["createPortal"])], | ||
]; | ||
|
||
export default declare(api => { | ||
api.assertVersion(7); | ||
|
@@ -40,14 +41,14 @@ export default declare(api => { | |
}; | ||
}); | ||
|
||
function isReactCall(path) { | ||
function isReactCall(path: NodePath<t.CallExpression>) { | ||
// If the callee is not a member expression, then check if it matches | ||
// a named import, e.g. `import {forwardRef} from 'react'`. | ||
if (!t.isMemberExpression(path.node.callee)) { | ||
const callee = path.get("callee"); | ||
const calleePath = path.get("callee"); | ||
if (!calleePath.isMemberExpression()) { | ||
for (const [module, methods] of PURE_CALLS) { | ||
for (const method of methods) { | ||
if (callee.referencesImport(module, method)) { | ||
if (calleePath.referencesImport(module, method)) { | ||
return true; | ||
} | ||
} | ||
|
@@ -60,19 +61,17 @@ function isReactCall(path) { | |
// a default import (`import React from 'react'`) or namespace | ||
// import (`import * as React from 'react'), and check if the | ||
// property matches one of the pure methods. | ||
for (const [module, methods] of PURE_CALLS) { | ||
const object = path.get("callee.object"); | ||
if ( | ||
object.referencesImport(module, "default") || | ||
object.referencesImport(module, "*") | ||
) { | ||
for (const method of methods) { | ||
if (t.isIdentifier(path.node.callee.property, { name: method })) { | ||
return true; | ||
} | ||
const object = calleePath.get("object"); | ||
const callee = calleePath.node; | ||
if (!callee.computed && t.isIdentifier(callee.property)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this computed check is the only real fix, everything else is a minor refactor. |
||
const propertyName = callee.property.name; | ||
for (const [module, methods] of PURE_CALLS) { | ||
if ( | ||
object.referencesImport(module, "default") || | ||
object.referencesImport(module, "*") | ||
) { | ||
return methods.has(propertyName); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
...bel-plugin-transform-react-pure-annotations/test/fixtures/react/invalid-computed/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import React from 'react'; | ||
|
||
var cloneElement, createElement; | ||
React[cloneElement](React[createElement]('div')); |
4 changes: 4 additions & 0 deletions
4
...plugin-transform-react-pure-annotations/test/fixtures/react/invalid-computed/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": ["transform-react-pure-annotations"] | ||
} |
3 changes: 3 additions & 0 deletions
3
...l-plugin-transform-react-pure-annotations/test/fixtures/react/invalid-computed/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from 'react'; | ||
var cloneElement, createElement; | ||
React[cloneElement](React[createElement]('div')); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a map anymore because we never relied on O(1) access, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we always loop through the structure, better just use array for that purpose.