Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,25 @@ function addMemoCacheFunctionImportDeclaration(
moduleName: string,
localName: string,
): void {
program.unshiftContainer(
'body',
const importIdentifier = program.scope.generateUidIdentifier('r');

program.unshiftContainer('body', [
t.importDeclaration(
[t.importSpecifier(t.identifier(localName), t.identifier('c'))],
[t.importDefaultSpecifier(t.identifier(importIdentifier.name))],
t.stringLiteral(moduleName),
),
);
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern([
t.objectProperty(
t.identifier('c'),
t.identifier(localName),
false,
false,
),
]),
t.identifier(importIdentifier.name),
),
]),
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,18 @@ function getReactFunctionType(
}

/**
* Returns true if the program contains an `import {c} from "<moduleName>"` declaration,
* Returns true if the program contains an `import pkg from "<moduleName>"; const {c} = pkg` declaration,
* or an `import {c} from "<moduleName>".
* regardless of the local name of the 'c' specifier and the presence of other specifiers
* in the same declaration.
*/
function hasMemoCacheFunctionImport(
program: NodePath<t.Program>,
moduleName: string,
): boolean {
let defaultImportName: string | null = null;
let hasUseMemoCache = false;

program.traverse({
ImportSpecifier(path) {
const imported = path.get('imported');
Expand All @@ -729,7 +732,33 @@ function hasMemoCacheFunctionImport(
hasUseMemoCache = true;
}
},
ImportDefaultSpecifier(path) {
if (
path.parentPath.isImportDeclaration() &&
path.parentPath.get('source').node.value === moduleName
) {
defaultImportName = path.node.local.name;
}
},
VariableDeclarator(path) {
if (!defaultImportName) return;

if (
t.isIdentifier(path.node.init) &&
path.node.init.name === defaultImportName &&
t.isObjectPattern(path.node.id)
) {
const properties = path.node.id.properties;
hasUseMemoCache = properties.some(
prop =>
t.isObjectProperty(prop) &&
t.isIdentifier(prop.key) &&
prop.key.name === 'c',
);
}
},
});

return hasUseMemoCache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { makeObject_Primitives, mutate } from "shared-runtime";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function Component() {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function Component() {
const $ = _c(2);
let t0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function component(a) {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function component(a) {
const $ = _c(2);
let x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function component() {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function component() {
const $ = _c(1);
let x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function component() {
const $ = _c(1);
let x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function mutate(x, y) {}
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function foo(cond) {
const $ = _c(2);
let a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { mutate } from "shared-runtime";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { getNull } from "shared-runtime";

function Component(props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { mutate } from "shared-runtime";
/**
* Fixture showing that it's not sufficient to only align direct scoped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function useFoo(t0) {
const $ = _c(3);
const { cond } = t0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { arrayPush } from "shared-runtime";

function useFoo(t0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { arrayPush, mutate } from "shared-runtime";

function useFoo(t0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { CONST_TRUE, makeObject_Primitives } from "shared-runtime";

function Foo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { Stringify, identity, makeArray, mutate } from "shared-runtime";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; /**
import _r from "react/compiler-runtime";
const { c: _c } = _r; /**
* This is a weird case as data has type `BuiltInMixedReadonly`.
* The only scoped value we currently infer in this program is the
* PropertyLoad `data?.toString`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // bar(props.b) is an allocating expression that produces a primitive, which means
import _r from "react/compiler-runtime";
const { c: _c } = _r; // bar(props.b) is an allocating expression that produces a primitive, which means
// that Forget should memoize it.
// Correctness:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function AllocatingPrimitiveAsDep(props) {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // bar(props.b) is an allocating expression that produces a primitive, which means
import _r from "react/compiler-runtime";
const { c: _c } = _r; // bar(props.b) is an allocating expression that produces a primitive, which means
// that Forget should memoize it.
// Correctness:
// - y depends on either bar(props.b) or bar(props.b) + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validatePreserveExistingMemoizationGuarantees
import { useCallback, useEffect, useState } from "react";

let someGlobal = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useEffect, useState } from "react";

let someGlobal = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useCallback, useEffect, useState } from "react";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useEffect, useState } from "react";

let someGlobal = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useEffect, useState } from "react";

let someGlobal = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useMemo } from "react";

const someGlobal = { value: 0 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
import { useEffect, useState } from "react";

let someGlobal = { value: null };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateRefAccessDuringRender
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validateRefAccessDuringRender
import { useRef } from "react";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateRefAccessDuringRender
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validateRefAccessDuringRender
import { useRef } from "react";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateRefAccessDuringRender
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validateRefAccessDuringRender
import { useRef } from "react";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateRefAccessDuringRender
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validateRefAccessDuringRender
import { useRef } from "react";

function Component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function Component(props) {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function Component(props) {
const $ = _c(1);
const ref = useRef(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import _r from "react/compiler-runtime";
const { c: _c } = _r;
function Component() {
const $ = _c(1);
const onClick = _temp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateRefAccessDuringRender @validateNoSetStateInRender:false
import _r from "react/compiler-runtime";
const { c: _c } = _r; // @validateRefAccessDuringRender @validateNoSetStateInRender:false
import { useCallback, useEffect, useRef, useState } from "react";

function Component() {
Expand Down
Loading
Loading