@marimo-team/islands 0.23.12-dev8 → 0.23.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/{chat-ui-BEOvjkmJ.js → chat-ui-CsPewo4h.js} +2 -2
  2. package/dist/{code-visibility-B9yvB9rV.js → code-visibility-BFhOAQbo.js} +714 -707
  3. package/dist/{html-to-image-Di0mtt6O.js → html-to-image-DXwLcQ6l.js} +22 -15
  4. package/dist/main.js +1160 -1027
  5. package/dist/{process-output-BLd4KuwX.js → process-output-C6_e1pT_.js} +1 -1
  6. package/dist/{reveal-component-D6wEWbxH.js → reveal-component-ghVwQgXR.js} +13 -13
  7. package/dist/style.css +1 -1
  8. package/package.json +1 -1
  9. package/src/components/data-table/TableBottomBar.tsx +4 -1
  10. package/src/components/data-table/data-table.tsx +26 -17
  11. package/src/components/data-table/utils.ts +1 -4
  12. package/src/components/editor/actions/useNotebookActions.tsx +4 -4
  13. package/src/components/editor/ai/__tests__/completion-utils.test.ts +48 -2
  14. package/src/components/editor/ai/completion-utils.ts +54 -36
  15. package/src/components/editor/app-container.tsx +3 -1
  16. package/src/components/editor/output/ImageOutput.tsx +12 -3
  17. package/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx +2 -2
  18. package/src/components/home/components.tsx +4 -4
  19. package/src/components/icons/github.tsx +21 -0
  20. package/src/components/icons/youtube.tsx +21 -0
  21. package/src/components/storage/components.tsx +3 -7
  22. package/src/core/codemirror/go-to-definition/__tests__/commands.test.ts +67 -0
  23. package/src/core/codemirror/go-to-definition/__tests__/utils.test.ts +47 -0
  24. package/src/core/codemirror/go-to-definition/commands.ts +47 -30
  25. package/src/core/codemirror/go-to-definition/utils.ts +0 -1
  26. package/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts +54 -0
  27. package/src/core/codemirror/reactive-references/analyzer.ts +44 -35
  28. package/src/core/islands/__tests__/bridge.test.ts +25 -0
  29. package/src/core/islands/__tests__/parse.test.ts +585 -1
  30. package/src/core/islands/__tests__/test-utils.tsx +10 -1
  31. package/src/core/islands/bridge.ts +6 -1
  32. package/src/core/islands/constants.ts +2 -0
  33. package/src/core/islands/parse.ts +290 -13
  34. package/src/plugins/impl/DataTablePlugin.tsx +20 -1
  35. package/src/plugins/impl/__tests__/DataTablePlugin.test.tsx +141 -1
  36. package/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx +54 -4
  37. package/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsx +104 -1
  38. package/src/plugins/impl/anywidget/__tests__/model.test.ts +19 -0
  39. package/src/plugins/impl/anywidget/model.ts +15 -0
  40. package/src/utils/__tests__/records.test.ts +27 -0
  41. package/src/utils/records.ts +12 -0
@@ -307,33 +307,52 @@ function collectMatchingDeclarations(
307
307
  break;
308
308
  }
309
309
  case "ImportStatement": {
310
+ // The grammar emits one ImportStatement for both `import x [as y]` and
311
+ // `from m import x [as y], ...`. Direct children include the keywords
312
+ // (`from`/`import`/`as`), commas, dots, and every VariableName from the
313
+ // module path AND the import list. We only want the names that actually
314
+ // bind in the current scope: the post-`as` alias if present, otherwise
315
+ // the imported name itself. Names before `import` (the from-path) and
316
+ // the original name when an alias follows it are NOT bindings.
310
317
  const subCursor = node.cursor();
311
318
  subCursor.firstChild();
312
- do {
313
- if (
314
- subCursor.name === "VariableName" &&
315
- state.doc.sliceString(subCursor.from, subCursor.to) === variableName
316
- ) {
317
- addDeclaration(declarations, currentScope, subCursor.from);
319
+ let pastImport = false;
320
+ // Buffer the most recent post-`import` VariableName so we can defer
321
+ // committing it until we know whether `as` follows.
322
+ let pending: { from: number; matches: boolean } | null = null;
323
+ const commit = () => {
324
+ if (pending?.matches) {
325
+ addDeclaration(declarations, currentScope, pending.from);
318
326
  }
319
- } while (subCursor.nextSibling());
320
- break;
321
- }
322
- case "ImportFromStatement": {
323
- const subCursor = node.cursor();
324
- subCursor.firstChild();
325
- let foundImport = false;
327
+ pending = null;
328
+ };
326
329
  do {
327
330
  if (subCursor.name === "import") {
328
- foundImport = true;
329
- } else if (
330
- foundImport &&
331
- subCursor.name === "VariableName" &&
332
- state.doc.sliceString(subCursor.from, subCursor.to) === variableName
333
- ) {
334
- addDeclaration(declarations, currentScope, subCursor.from);
331
+ pastImport = true;
332
+ continue;
333
+ }
334
+ if (!pastImport) {
335
+ continue;
336
+ }
337
+ if (subCursor.name === "as") {
338
+ // Next VariableName is the alias and replaces `pending`.
339
+ pending = null;
340
+ continue;
341
+ }
342
+ if (subCursor.name === "VariableName") {
343
+ // Flush any previous pending name (no `as` followed it).
344
+ commit();
345
+ pending = {
346
+ from: subCursor.from,
347
+ matches:
348
+ state.doc.sliceString(subCursor.from, subCursor.to) ===
349
+ variableName,
350
+ };
351
+ } else if (subCursor.name === ",") {
352
+ commit();
335
353
  }
336
354
  } while (subCursor.nextSibling());
355
+ commit();
337
356
  break;
338
357
  }
339
358
  case "TryStatement":
@@ -410,23 +429,21 @@ function findScopedDefinitionPosition(
410
429
  * @param view The editor view which contains the variable name.
411
430
  * @param variableName The name of the variable to select, if found in the editor.
412
431
  * @param usagePosition The position of the variable usage, if available.
413
- * @param fallbackToFirstMatch Whether to fall back to the first matching
414
- * variable name when no scoped definition is found. Defaults to true.
415
432
  */
416
433
  export function goToVariableDefinition(
417
434
  view: EditorView,
418
435
  variableName: string,
419
436
  usagePosition?: number,
420
- fallbackToFirstMatch = true,
421
437
  ): boolean {
422
438
  const { state } = view;
423
- let from: number | null = null;
424
- if (usagePosition !== undefined) {
425
- from = findScopedDefinitionPosition(state, variableName, usagePosition);
426
- }
427
- if (from === null && fallbackToFirstMatch) {
428
- from = findFirstMatchingVariable(state, variableName);
429
- }
439
+ // When the caller knows the usage position, trust the scoped lookup. Falling
440
+ // back to first-match would defeat the local-vs-cross-cell decision in
441
+ // goToDefinition: if the symbol only appears as a module path in an import,
442
+ // scoped resolution returns null and we want the caller to try other cells.
443
+ const from =
444
+ usagePosition !== undefined
445
+ ? findScopedDefinitionPosition(state, variableName, usagePosition)
446
+ : findFirstMatchingVariable(state, variableName);
430
447
 
431
448
  if (from === null) {
432
449
  return false;
@@ -82,7 +82,6 @@ export function goToDefinition(
82
82
  view,
83
83
  variableName,
84
84
  usagePosition,
85
- false,
86
85
  );
87
86
  if (foundLocally) {
88
87
  return true;
@@ -640,6 +640,60 @@ def run(polars):
640
640
  `);
641
641
  });
642
642
 
643
+ test("set comprehension target shadows outer global", () => {
644
+ // Regression: SCOPE_CREATING_NODES used "SetComprehension" instead of the
645
+ // grammar's "SetComprehensionExpression", so set comprehensions never
646
+ // created a scope and their for-target was treated as reactive.
647
+ expect(runHighlight(["x"], "result = {x for x in range(5)}"))
648
+ .toMatchInlineSnapshot(`
649
+ "
650
+ result = {x for x in range(5)}
651
+ "
652
+ `);
653
+ });
654
+
655
+ test("from-import module path stays reactive", () => {
656
+ // Regression: ImportStatement collected every VariableName child, so the
657
+ // module name in `from m import y` was wrongly treated as a local binding.
658
+ expect(
659
+ runHighlight(
660
+ ["math"],
661
+ `
662
+ def f():
663
+ from math import sin as my_sin
664
+ return math + my_sin(1)`,
665
+ ),
666
+ ).toMatchInlineSnapshot(`
667
+ "
668
+ def f():
669
+ from math import sin as my_sin
670
+ return math + my_sin(1)
671
+ ^^^^
672
+ "
673
+ `);
674
+ });
675
+
676
+ test("from-import: aliased imported name is not a binding", () => {
677
+ // Regression: `sin` in `from math import sin as my_sin` was incorrectly
678
+ // registered as a local binding, hiding genuine reactive uses of `sin`.
679
+ expect(
680
+ runHighlight(
681
+ ["sin"],
682
+ `
683
+ def f():
684
+ from math import sin as my_sin
685
+ return sin + my_sin(1)`,
686
+ ),
687
+ ).toMatchInlineSnapshot(`
688
+ "
689
+ def f():
690
+ from math import sin as my_sin
691
+ return sin + my_sin(1)
692
+ ^^^
693
+ "
694
+ `);
695
+ });
696
+
643
697
  test("lambda inside function with outer global", () => {
644
698
  expect(
645
699
  runHighlight(
@@ -16,7 +16,7 @@ const SCOPE_CREATING_NODES = new Set([
16
16
  "FunctionDefinition",
17
17
  "LambdaExpression",
18
18
  "ArrayComprehensionExpression",
19
- "SetComprehension",
19
+ "SetComprehensionExpression",
20
20
  "DictionaryComprehensionExpression",
21
21
  "ComprehensionExpression",
22
22
  "ClassDefinition",
@@ -152,7 +152,7 @@ export function findReactiveVariables(options: {
152
152
  }
153
153
  case "ArrayComprehensionExpression":
154
154
  case "DictionaryComprehensionExpression":
155
- case "SetComprehension":
155
+ case "SetComprehensionExpression":
156
156
  case "ComprehensionExpression": {
157
157
  // Domprehension variables - look for VariableName or TupleExpression after 'for'
158
158
  const subCursor = node.cursor();
@@ -276,49 +276,52 @@ export function findReactiveVariables(options: {
276
276
  break;
277
277
  }
278
278
  case "ImportStatement": {
279
- // Handle import x
279
+ // The grammar emits a single ImportStatement for both `import x [as y]`
280
+ // and `from m import x [as y], ...`. Direct children mix keywords,
281
+ // module-path names (before `import`), imported names, and aliases.
282
+ // Only post-`import` names that aren't shadowed by a following `as`
283
+ // (and the alias itself when `as` is present) bind in the current
284
+ // scope.
280
285
  const subCursor = node.cursor();
281
286
  subCursor.firstChild();
282
- do {
283
- if (subCursor.name === "VariableName") {
284
- const varName = options.state.doc.sliceString(
285
- subCursor.from,
286
- subCursor.to,
287
- );
288
-
289
- const currentScope =
290
- currentScopeStack[currentScopeStack.length - 1] ?? -1;
291
- if (!allDeclarations.has(currentScope)) {
292
- allDeclarations.set(currentScope, new Set());
293
- }
294
- allDeclarations.get(currentScope)?.add(varName);
287
+ const currentScope =
288
+ currentScopeStack[currentScopeStack.length - 1] ?? -1;
289
+ if (!allDeclarations.has(currentScope)) {
290
+ allDeclarations.set(currentScope, new Set());
291
+ }
292
+ const scope = allDeclarations.get(currentScope);
293
+ let pastImport = false;
294
+ let pending: string | null = null;
295
+ const commit = () => {
296
+ if (pending !== null) {
297
+ scope?.add(pending);
295
298
  }
296
- } while (subCursor.nextSibling());
297
-
298
- break;
299
- }
300
- case "ImportFromStatement": {
301
- // Handle from x import y as z
302
- const subCursor = node.cursor();
303
- subCursor.firstChild();
304
- let foundImport = false;
299
+ pending = null;
300
+ };
305
301
  do {
306
302
  if (subCursor.name === "import") {
307
- foundImport = true;
308
- } else if (foundImport && subCursor.name === "VariableName") {
309
- const varName = options.state.doc.sliceString(
303
+ pastImport = true;
304
+ continue;
305
+ }
306
+ if (!pastImport) {
307
+ continue;
308
+ }
309
+ if (subCursor.name === "as") {
310
+ // Drop the imported name; the next VariableName is the alias.
311
+ pending = null;
312
+ continue;
313
+ }
314
+ if (subCursor.name === "VariableName") {
315
+ commit();
316
+ pending = options.state.doc.sliceString(
310
317
  subCursor.from,
311
318
  subCursor.to,
312
319
  );
313
- // Add to the current innermost scope
314
- const currentScope =
315
- currentScopeStack[currentScopeStack.length - 1] ?? -1;
316
- if (!allDeclarations.has(currentScope)) {
317
- allDeclarations.set(currentScope, new Set());
318
- }
319
- allDeclarations.get(currentScope)?.add(varName);
320
+ } else if (subCursor.name === ",") {
321
+ commit();
320
322
  }
321
323
  } while (subCursor.nextSibling());
324
+ commit();
322
325
 
323
326
  break;
324
327
  }
@@ -424,6 +427,12 @@ export function findReactiveVariables(options: {
424
427
  const nodeName = cursor.name;
425
428
  const nodeStart = cursor.from;
426
429
 
430
+ // Names inside an import statement are module paths, imported names, or
431
+ // aliases — none of them are reactive *uses* of an outer-cell global.
432
+ if (nodeName === "ImportStatement") {
433
+ return;
434
+ }
435
+
427
436
  const isNewScope = SCOPE_CREATING_NODES.has(nodeName);
428
437
 
429
438
  let currentScopeStack = scopeStack;
@@ -12,6 +12,7 @@ import {
12
12
  type Base64String = components["schemas"]["Base64String"];
13
13
  interface TestIslandApp {
14
14
  id: string;
15
+ payloadBacked?: boolean;
15
16
  cells: { code: string; idx: number; output: string }[];
16
17
  }
17
18
  interface TestExportContext {
@@ -142,6 +143,30 @@ describe("IslandsPyodideBridge", () => {
142
143
  });
143
144
  });
144
145
 
146
+ it("should ignore trusted export notebook code for a payload-backed app", async () => {
147
+ const payloadApp = {
148
+ id: "app-1",
149
+ payloadBacked: true,
150
+ cells: [{ code: "x = 1", idx: 0, output: "<div>1</div>" }],
151
+ };
152
+ mockParseMarimoIslandApps.mockReturnValue([payloadApp]);
153
+ mockGetMarimoExportContext.mockReturnValue({
154
+ trusted: true,
155
+ notebookCode: "full notebook should be ignored",
156
+ });
157
+ mockCreateMarimoFile.mockReturnValue("generated payload app");
158
+
159
+ await (
160
+ bridge as unknown as { startSessionsForAllApps(): Promise<void> }
161
+ ).startSessionsForAllApps();
162
+
163
+ expect(mockCreateMarimoFile).toHaveBeenCalledWith(payloadApp);
164
+ expect(mockStartSessionRequest).toHaveBeenCalledWith({
165
+ appId: "app-1",
166
+ code: "generated payload app",
167
+ });
168
+ });
169
+
145
170
  it("should keep synthesized per-app files for multiple reactive apps even when export context exists", async () => {
146
171
  mockParseMarimoIslandApps.mockReturnValue([
147
172
  {