Skip to content

Commit 8962bfe

Browse files
committed
[compiler]: use default import for useMemoCache
1 parent f9f17f6 commit 8962bfe

File tree

968 files changed

+1994
-971
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

968 files changed

+1994
-971
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,44 @@ export function updateMemoCacheFunctionImport(
185185
}
186186
}
187187

188+
function generateUniqueName(
189+
program: NodePath<t.Program>,
190+
baseName: string,
191+
): string {
192+
let name = baseName;
193+
let counter = 0;
194+
195+
while (program.scope.hasBinding(name)) {
196+
name = `${baseName}${counter}`;
197+
counter++;
198+
}
199+
return name;
200+
}
201+
188202
function addMemoCacheFunctionImportDeclaration(
189203
program: NodePath<t.Program>,
190204
moduleName: string,
191205
localName: string,
192206
): void {
193-
program.unshiftContainer(
194-
'body',
207+
const importName = generateUniqueName(program, 'c');
208+
209+
program.unshiftContainer('body', [
195210
t.importDeclaration(
196-
[t.importSpecifier(t.identifier(localName), t.identifier('c'))],
211+
[t.importDefaultSpecifier(t.identifier(importName))],
197212
t.stringLiteral(moduleName),
198213
),
199-
);
214+
t.variableDeclaration('const', [
215+
t.variableDeclarator(
216+
t.objectPattern([
217+
t.objectProperty(
218+
t.identifier('c'),
219+
t.identifier(localName),
220+
false,
221+
false,
222+
),
223+
]),
224+
t.identifier(importName),
225+
),
226+
]),
227+
]);
200228
}

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,15 +703,18 @@ function getReactFunctionType(
703703
}
704704

705705
/**
706-
* Returns true if the program contains an `import {c} from "<moduleName>"` declaration,
706+
* Returns true if the program contains an `import pkg from "<moduleName>"; const {c} = pkg` declaration,
707+
* or an `import {c} from "<moduleName>".
707708
* regardless of the local name of the 'c' specifier and the presence of other specifiers
708709
* in the same declaration.
709710
*/
710711
function hasMemoCacheFunctionImport(
711712
program: NodePath<t.Program>,
712713
moduleName: string,
713714
): boolean {
715+
let defaultImportName: string | null = null;
714716
let hasUseMemoCache = false;
717+
715718
program.traverse({
716719
ImportSpecifier(path) {
717720
const imported = path.get('imported');
@@ -729,7 +732,33 @@ function hasMemoCacheFunctionImport(
729732
hasUseMemoCache = true;
730733
}
731734
},
735+
ImportDefaultSpecifier(path) {
736+
if (
737+
path.parentPath.isImportDeclaration() &&
738+
path.parentPath.get('source').node.value === moduleName
739+
) {
740+
defaultImportName = path.node.local.name;
741+
}
742+
},
743+
VariableDeclarator(path) {
744+
if (!defaultImportName) return;
745+
746+
if (
747+
t.isIdentifier(path.node.init) &&
748+
path.node.init.name === defaultImportName &&
749+
t.isObjectPattern(path.node.id)
750+
) {
751+
const properties = path.node.id.properties;
752+
hasUseMemoCache = properties.some(
753+
prop =>
754+
t.isObjectProperty(prop) &&
755+
t.isIdentifier(prop.key) &&
756+
prop.key.name === 'c',
757+
);
758+
}
759+
},
732760
});
761+
733762
return hasUseMemoCache;
734763
}
735764

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver-and-mutate.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const FIXTURE_ENTRYPOINT = {
2727
## Code
2828

2929
```javascript
30-
import { c as _c } from "react/compiler-runtime";
30+
import c from "react/compiler-runtime";
31+
const { c: _c } = c;
3132
import { makeObject_Primitives, mutate } from "shared-runtime";
3233

3334
function Component() {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-capture-in-method-receiver.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function Component() {
1818
## Code
1919

2020
```javascript
21-
import { c as _c } from "react/compiler-runtime";
21+
import c from "react/compiler-runtime";
22+
const { c: _c } = c;
2223
function Component() {
2324
const $ = _c(2);
2425
let t0;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-computed-load.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function component(a) {
1616
## Code
1717

1818
```javascript
19-
import { c as _c } from "react/compiler-runtime";
19+
import c from "react/compiler-runtime";
20+
const { c: _c } = c;
2021
function component(a) {
2122
const $ = _c(2);
2223
let x;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-nested-member-path-mutate.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function component() {
1717
## Code
1818

1919
```javascript
20-
import { c as _c } from "react/compiler-runtime";
20+
import c from "react/compiler-runtime";
21+
const { c: _c } = c;
2122
function component() {
2223
const $ = _c(1);
2324
let x;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-nested-member-path.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const FIXTURE_ENTRYPOINT = {
2222
## Code
2323

2424
```javascript
25-
import { c as _c } from "react/compiler-runtime";
25+
import c from "react/compiler-runtime";
26+
const { c: _c } = c;
2627
function component() {
2728
const $ = _c(1);
2829
let x;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/alias-while.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ function mutate(x, y) {}
2626
## Code
2727

2828
```javascript
29-
import { c as _c } from "react/compiler-runtime";
29+
import c from "react/compiler-runtime";
30+
const { c: _c } = c;
3031
function foo(cond) {
3132
const $ = _c(2);
3233
let a;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scope-starts-within-cond.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export const FIXTURE_ENTRYPOINT = {
2929
## Code
3030

3131
```javascript
32-
import { c as _c } from "react/compiler-runtime";
32+
import c from "react/compiler-runtime";
33+
const { c: _c } = c;
3334
import { mutate } from "shared-runtime";
3435

3536
/**

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-iife-return-modified-later-logical.expect.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const FIXTURE_ENTRYPOINT = {
2222
## Code
2323
2424
```javascript
25-
import { c as _c } from "react/compiler-runtime";
25+
import c from "react/compiler-runtime";
26+
const { c: _c } = c;
2627
import { getNull } from "shared-runtime";
2728

2829
function Component(props) {

0 commit comments

Comments
 (0)