-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Support method-call version of macro functions
Adds fixtures for `macro.namespace(...)` style invocations which we use internally in some cases instead of just `macro(...)`. I tried every example i could think of that could possibly break it (including basing one off of another fixture where we hit an invariant related due to a temporary being emitted for a method call), and they all worked. I just had to fix an existing bug where we early return in some cases instead of continuing, which is a holdover from when this pass was originally written as a ReactiveFunction visitor. ghstack-source-id: c01f45b3ef6f42b6d1f1ff0508aea258000e0fce Pull Request resolved: #29899
- Loading branch information
1 parent
92219ff
commit dbc5f36
Showing
6 changed files
with
233 additions
and
5 deletions.
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
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
100 changes: 100 additions & 0 deletions
100
.../fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.expect.md
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,100 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) | ||
import { identity } from "shared-runtime"; | ||
|
||
const DARK = "dark"; | ||
|
||
function Component() { | ||
const theme = useTheme(); | ||
return ( | ||
<div | ||
className={cx.foo({ | ||
"styles/light": true, | ||
"styles/dark": identity([theme.getTheme()]), | ||
})} | ||
/> | ||
); | ||
} | ||
|
||
function cx(obj) { | ||
const classes = []; | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (value) { | ||
classes.push(key); | ||
} | ||
} | ||
return classes.join(" "); | ||
} | ||
|
||
function useTheme() { | ||
return { | ||
getTheme() { | ||
return DARK; | ||
}, | ||
}; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @compilationMode(infer) @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) | ||
import { identity } from "shared-runtime"; | ||
|
||
const DARK = "dark"; | ||
|
||
function Component() { | ||
const $ = _c(2); | ||
const theme = useTheme(); | ||
|
||
const t0 = cx.foo({ | ||
"styles/light": true, | ||
"styles/dark": identity([theme.getTheme()]), | ||
}); | ||
let t1; | ||
if ($[0] !== t0) { | ||
t1 = <div className={t0} />; | ||
$[0] = t0; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} | ||
|
||
function cx(obj) { | ||
const classes = []; | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (value) { | ||
classes.push(key); | ||
} | ||
} | ||
return classes.join(" "); | ||
} | ||
|
||
function useTheme() { | ||
return { | ||
getTheme() { | ||
return DARK; | ||
}, | ||
}; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) cx.foo is not a function |
39 changes: 39 additions & 0 deletions
39
...ler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.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,39 @@ | ||
// @compilationMode(infer) @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) | ||
import { identity } from "shared-runtime"; | ||
|
||
const DARK = "dark"; | ||
|
||
function Component() { | ||
const theme = useTheme(); | ||
return ( | ||
<div | ||
className={cx.foo({ | ||
"styles/light": true, | ||
"styles/dark": identity([theme.getTheme()]), | ||
})} | ||
/> | ||
); | ||
} | ||
|
||
function cx(obj) { | ||
const classes = []; | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (value) { | ||
classes.push(key); | ||
} | ||
} | ||
return classes.join(" "); | ||
} | ||
|
||
function useTheme() { | ||
return { | ||
getTheme() { | ||
return DARK; | ||
}, | ||
}; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
68 changes: 68 additions & 0 deletions
68
.../src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.expect.md
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,68 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { makeArray } from "shared-runtime"; | ||
|
||
function Component() { | ||
const items = makeArray("foo", "bar", "", null, "baz", false, "merp"); | ||
const classname = cx.namespace(...items.filter(isNonEmptyString)); | ||
return <div className={classname}>Ok</div>; | ||
} | ||
|
||
function isNonEmptyString(s) { | ||
return typeof s === "string" && s.trim().length !== 0; | ||
} | ||
|
||
const cx = { | ||
namespace(...items) { | ||
return items.join(" "); | ||
}, | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { makeArray } from "shared-runtime"; | ||
|
||
function Component() { | ||
const $ = _c(1); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
const items = makeArray("foo", "bar", "", null, "baz", false, "merp"); | ||
const classname = cx.namespace(...items.filter(isNonEmptyString)); | ||
t0 = <div className={classname}>Ok</div>; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
return t0; | ||
} | ||
|
||
function isNonEmptyString(s) { | ||
return typeof s === "string" && s.trim().length !== 0; | ||
} | ||
|
||
const cx = { | ||
namespace(...items) { | ||
return items.join(" "); | ||
}, | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div class="foo bar baz merp">Ok</div> |
22 changes: 22 additions & 0 deletions
22
...in-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.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,22 @@ | ||
import { makeArray } from "shared-runtime"; | ||
|
||
function Component() { | ||
const items = makeArray("foo", "bar", "", null, "baz", false, "merp"); | ||
const classname = cx.namespace(...items.filter(isNonEmptyString)); | ||
return <div className={classname}>Ok</div>; | ||
} | ||
|
||
function isNonEmptyString(s) { | ||
return typeof s === "string" && s.trim().length !== 0; | ||
} | ||
|
||
const cx = { | ||
namespace(...items) { | ||
return items.join(" "); | ||
}, | ||
}; | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |