Skip to content

Commit 6a22de1

Browse files
committed
fix(compiler): unwrap TS receivers in imported member reads
containsImportedMemberRead only accepted a bare Identifier receiver, so mod!.flag and (mod as T).flag missed the hostConditionalPure gate and an unchanged item identity could skip a conditional whose live import still moves.
1 parent 0c15b59 commit 6a22de1

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎packages/octane/src/compiler/compile.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6504,7 +6504,8 @@ function scanComponentBody(root, importedNames) {
65046504

65056505
// A JSX member chain bottoms out at a JSXIdentifier, so the base test below
65066506
// never matches one. Do not reintroduce a JSX-awareness flag: it cannot change
6507-
// the answer.
6507+
// the answer. Unwrap TS/paren wrappers on the receiver so `mod!.flag` and
6508+
// `(mod as T).flag` still count as live imported member reads.
65086509
function containsImportedMemberRead(root, importedNames) {
65096510
let found = false;
65106511
const seen = new WeakSet();
@@ -6517,9 +6518,9 @@ function containsImportedMemberRead(root, importedNames) {
65176518
if (typeof n !== 'object' || seen.has(n)) return;
65186519
seen.add(n);
65196520
if (n.type === 'MemberExpression' || n.type === 'JSXMemberExpression') {
6520-
let object = n;
6521+
let object = unwrapTsExpr(n.object);
65216522
while (object?.type === 'MemberExpression' || object?.type === 'JSXMemberExpression') {
6522-
object = object.object;
6523+
object = unwrapTsExpr(object.object);
65236524
}
65246525
if (object?.type === 'Identifier' && importedNames.has(object.name)) {
65256526
found = true;

‎packages/octane/tests/compiler/for-item-purity.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,45 @@ describe('@for item-body purity with host-only conditional content', () => {
169169
expect(flags[0]! & PURE).toBe(0);
170170
});
171171

172+
it.each<[string, string, string]>([
173+
['a non-null assertion', "import { flags } from './flags';", 'flags!.enabled'],
174+
[
175+
'a type assertion',
176+
"import { flags } from './flags';",
177+
'(flags as { enabled: boolean }).enabled',
178+
],
179+
[
180+
'a satisfies assertion',
181+
"import { flags } from './flags';",
182+
'(flags satisfies { enabled: boolean }).enabled',
183+
],
184+
[
185+
'a type assertion before a further member',
186+
"import { flags } from './flags';",
187+
'(flags as { nested: { enabled: boolean } }).nested.enabled',
188+
],
189+
['a namespace non-null assertion', "import * as flags from './flags';", 'flags!.enabled'],
190+
])('declines when the imported member read uses %s', (_name, prelude, test) => {
191+
// TS wrappers must not hide a live import: the identity-only skip still
192+
// cannot witness `flags.enabled` flipping while `flags` stays fixed.
193+
const flags = appListFlags(
194+
compile(
195+
`
196+
${prelude}
197+
export function App(props) @{
198+
<ul>@for (const item of props.items; key item.id) {
199+
<li>@if (${test}) {<span class="x" />}</li>
200+
}</ul>
201+
}
202+
`,
203+
'App.tsrx',
204+
{ hmr: false, dev: false },
205+
).code,
206+
);
207+
expect(flags).toHaveLength(1);
208+
expect(flags[0]! & PURE).toBe(0);
209+
});
210+
172211
it.each<[string, string, string?]>([
173212
[
174213
'@try',

0 commit comments

Comments
 (0)