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

fix: do not mark computed React[...] methods as pure #14528

Merged
merged 1 commit into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions packages/babel-plugin-transform-react-pure-annotations/src/index.ts
@@ -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>][] = [
Copy link
Member

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?

Copy link
Contributor Author

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.

[
"react",
[
new Set([
"cloneElement",
"createContext",
"createElement",
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand All @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}

Expand Down
@@ -0,0 +1,4 @@
import React from 'react';

var cloneElement, createElement;
React[cloneElement](React[createElement]('div'));
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["transform-react-pure-annotations"]
}
@@ -0,0 +1,3 @@
import React from 'react';
var cloneElement, createElement;
React[cloneElement](React[createElement]('div'));