Skip to content

Commit a08aa9b

Browse files
committed
fix(compiler): close three gaps in islands shell analysis
1. independent={false} Hydrate was treated as an independent island because the check only looked for the attribute name, not its value. Now the attribute value is inspected: bare independent and independent={true} skip the shell check; independent={false} is correctly reported as ordinary shell work. 2. Non-function exports (export { App }, export default App, export const App = arrow/function) never seeded the analysis roots, so the walk was empty and problems were silently missed. Now VariableDeclaration function inits, re-export specifiers, and default-exported identifiers are resolved. 3. Signal-read detection only matched CallExpression with a bare MemberExpression callee. OptionalCallExpression, OptionalMemberExpression, and TS-wrapped callees (as/satisfies/non-null) were missed. The controlled value/checked literal check had the same unwrap gap. Both now peel wrappers consistently.
1 parent aabf31b commit a08aa9b

2 files changed

Lines changed: 113 additions & 7 deletions

File tree

‎packages/octane/src/compiler/islands-shell.js‎

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ export function analyzeIslandsShell(source, filename, exports = null) {
8383
functions.set(declaration.id.name, declaration);
8484
const name = statement.type === 'ExportDefaultDeclaration' ? 'default' : declaration.id.name;
8585
if (exported && (exports === null || exports.includes(name))) roots.push(declaration);
86+
} else if (exported && declaration?.type === 'VariableDeclaration') {
87+
for (const declarator of declaration.declarations ?? []) {
88+
const init = declarator.init;
89+
if (
90+
declarator.id?.type === 'Identifier' &&
91+
init &&
92+
(init.type === 'FunctionExpression' || init.type === 'ArrowFunctionExpression')
93+
) {
94+
functions.set(declarator.id.name, init);
95+
if (exports === null || exports.includes(declarator.id.name)) roots.push(init);
96+
}
97+
}
98+
}
99+
if (statement.type === 'ExportNamedDeclaration') {
100+
for (const specifier of statement.specifiers ?? []) {
101+
if (specifier.exportKind === 'type') continue;
102+
const exportedName = specifier.exported?.name ?? specifier.exported?.value;
103+
if (exports !== null && !exports.includes(exportedName)) continue;
104+
const fn = functions.get(specifier.local?.name);
105+
if (fn) roots.push(fn);
106+
}
107+
} else if (
108+
statement.type === 'ExportDefaultDeclaration' &&
109+
declaration?.type === 'Identifier'
110+
) {
111+
if (exports === null || exports.includes('default')) {
112+
const fn = functions.get(declaration.name);
113+
if (fn) roots.push(fn);
114+
}
86115
}
87116
}
88117
const problems = [];
@@ -107,7 +136,21 @@ export function analyzeIslandsShell(source, filename, exports = null) {
107136
const attributes = opening.attributes ?? [];
108137
if (tag !== null && octane(tag, 'Hydrate')) {
109138
// An independent island activates on its own; it is not shell work.
110-
if (attributes.some((attribute) => attributeName(attribute) === 'independent')) return;
139+
// Bare `independent` and `independent={true}` are independent;
140+
// `independent={false}` is an ordinary boundary that needs the renderer.
141+
if (
142+
attributes.some((attribute) => {
143+
if (attributeName(attribute) !== 'independent') return false;
144+
if (attribute.value == null) return true;
145+
let expr =
146+
attribute.value.type === 'JSXExpressionContainer'
147+
? attribute.value.expression
148+
: attribute.value;
149+
while (TYPE_WRAPPERS.has(expr?.type)) expr = expr.expression;
150+
return !(expr?.type === 'Literal' && expr.value === false);
151+
})
152+
)
153+
return;
111154
report(node, 'an ordinary <Hydrate> needs the renderer to hydrate its children');
112155
} else if (tag !== null && /^[A-Z]/.test(tag) && !octane(tag, 'Fragment')) {
113156
const local = functions.get(tag);
@@ -137,10 +180,13 @@ export function analyzeIslandsShell(source, filename, exports = null) {
137180
else if (
138181
(name === 'value' || name === 'checked') &&
139182
CONTROLLED.has(tag ?? '') &&
140-
attribute.value?.type === 'JSXExpressionContainer' &&
141-
attribute.value.expression.type !== 'Literal'
142-
)
143-
report(attribute, `a controlled ${JSON.stringify(name)} needs the renderer`);
183+
attribute.value?.type === 'JSXExpressionContainer'
184+
) {
185+
let expr = attribute.value.expression;
186+
while (TYPE_WRAPPERS.has(expr?.type)) expr = expr.expression;
187+
if (expr?.type !== 'Literal')
188+
report(attribute, `a controlled ${JSON.stringify(name)} needs the renderer`);
189+
}
144190
}
145191
} else if (node.type === 'JSXTryExpression' || node.type === 'TryStatement') {
146192
report(node, '@try recovery needs the renderer; move it into an independent island');
@@ -149,11 +195,16 @@ export function analyzeIslandsShell(source, filename, exports = null) {
149195
/\$$/.test(nameOf(node.expression) ?? '')
150196
) {
151197
report(node, 'a signal handle binding needs client code the shell never loads');
152-
} else if (node.type === 'CallExpression') {
198+
} else if (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') {
153199
const callee = node.callee;
154200
const name = nameOf(callee);
201+
let peeled = callee;
202+
while (TYPE_WRAPPERS.has(peeled?.type)) peeled = peeled.expression;
155203
const member =
156-
callee?.type === 'MemberExpression' && !callee.computed ? callee.property.name : null;
204+
(peeled?.type === 'MemberExpression' || peeled?.type === 'OptionalMemberExpression') &&
205+
!peeled.computed
206+
? peeled.property.name
207+
: null;
157208
if (name !== null && /^use(?:[A-Z0-9_]|$)/.test(name))
158209
report(node, `hook ${name}() needs the renderer`);
159210
else if (name !== null && SIGNAL_DECLARATIONS.has(name))

‎packages/octane/tests/compiler/islands-shell.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,59 @@ export function Other() @{ <button onClick={() => {}}>Other</button> }`;
6464
expect(analyzeIslandsShell(source, '/src/Page.tsrx', ['Uses']).problems[0].line).toBe(2);
6565
expect(analyzeIslandsShell(source, '/src/Page.tsrx', null).problems).toHaveLength(2);
6666
});
67+
68+
it('rejects independent={false} Hydrate as ordinary shell work', () => {
69+
const { problems } = analyzeIslandsShell(
70+
shell('<Hydrate independent={false} when={interaction()}><Island /></Hydrate>'),
71+
'/src/Page.tsrx',
72+
);
73+
expect(problems.map((p) => p.message).join('\n')).toMatch(/ordinary <Hydrate>/);
74+
});
75+
76+
it('accepts independent={true} Hydrate', () => {
77+
const { problems } = analyzeIslandsShell(
78+
shell('<Hydrate independent={true} when={interaction()}><Island /></Hydrate>'),
79+
'/src/Page.tsrx',
80+
);
81+
expect(problems).toEqual([]);
82+
});
83+
84+
it('checks exports authored as export { Name }', () => {
85+
const source = `import { Hydrate } from 'octane';
86+
function App() @{ <button onClick={() => {}}>Click</button> }
87+
export { App }`;
88+
const { problems } = analyzeIslandsShell(source, '/src/Page.tsrx', ['App']);
89+
expect(problems.map((p) => p.message).join('\n')).toMatch(/"onClick" needs client code/);
90+
});
91+
92+
it('checks exports authored as export default Identifier', () => {
93+
const source = `import { Hydrate } from 'octane';
94+
function App() @{ <button onClick={() => {}}>Click</button> }
95+
export default App`;
96+
const { problems } = analyzeIslandsShell(source, '/src/Page.tsrx', ['default']);
97+
expect(problems.map((p) => p.message).join('\n')).toMatch(/"onClick" needs client code/);
98+
});
99+
100+
it('checks exports authored as export const Name = arrow', () => {
101+
const source = `import { Hydrate } from 'octane';
102+
export const App = (props) => @{ <button onClick={() => {}}>Click</button> }`;
103+
const { problems } = analyzeIslandsShell(source, '/src/Page.tsrx', ['App']);
104+
expect(problems.map((p) => p.message).join('\n')).toMatch(/"onClick" needs client code/);
105+
});
106+
107+
it('rejects an optional signal read like count$?.get()', () => {
108+
const { problems } = analyzeIslandsShell(
109+
shell('<p>{String(count$?.get()) as string}</p>'),
110+
'/src/Page.tsrx',
111+
);
112+
expect(problems.map((p) => p.message).join('\n')).toMatch(/\.get\(\) read/);
113+
});
114+
115+
it('rejects a signal read through a TS-wrapped callee', () => {
116+
const { problems } = analyzeIslandsShell(
117+
shell('<p>{String((count$ as any).get()) as string}</p>'),
118+
'/src/Page.tsrx',
119+
);
120+
expect(problems.map((p) => p.message).join('\n')).toMatch(/\.get\(\) read/);
121+
});
67122
});

0 commit comments

Comments
 (0)