Skip to content

Conversation

@pearmini
Copy link
Collaborator

@pearmini pearmini commented Oct 26, 2025

function add(a, b) {
  return echo(a + b);
}

// Transpile to:
const __cellEcho__ = __getEcho__();
function add(a, b) {
  const echo = __getEcho__() || __cellEcho__;
  return echo(a + b);
}
function add(a, b) {
  return setTimeout(() => echo(a + b));
}

// Transpile to:
const __cellEcho__ = __getEcho__();
function add(a, b) {
  const echo = __getEcho__() || __cellEcho__;
  return setTimeout(() => {
    if(__getEcho__()) { 
      const echo = __getEcho__();
      return echo(a + b) 
    };
    return echo(a + b);
  });
}
function adder(x, y) {
  return () => echo(x + y);
}

// Transpile to:
const __cellEcho__ = __getEcho__();
function adder(x, y) {
  const echo = __getEcho__() || __cellEcho__;
  return () => {
    if(__getEcho__()) { 
      const echo = __getEcho__();
      return echo(a + b);
    }
    return echo(a + b);
  }
}
setTimeout(() => echo(a + b));

// Transpile to:
const __cellEcho__ = __getEcho__();
setTimeout(() => {
  const echo = __getEcho__() || __cellEcho__;
  return echo(a + b);
});
  • clear -> echo.clear()

@vercel
Copy link

vercel bot commented Oct 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
recho-notebook Ready Ready Preview Comment Oct 27, 2025 7:57pm

@pearmini pearmini changed the title Echo in function Proper echo context handling Oct 26, 2025
@pearmini pearmini requested review from chengluyu and Copilot October 26, 2025 19:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements proper echo context handling to ensure that echo calls within functions output to the executing cell rather than the cell where the function was defined. This addresses issues with nested functions, async callbacks, and functions passed as arguments.

Key Changes:

  • Introduces a transpilation system that rewrites function bodies to capture and manage echo context
  • Migrates from standalone clear() function to echo.clear() method for API consistency
  • Adds comprehensive test coverage for various echo usage patterns (functions, classes, recursion, async operations)

Reviewed Changes

Copilot reviewed 31 out of 35 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
runtime/transpile.js New transpiler that rewrites echo calls in functions to handle context properly
runtime/sourcemap.js New source mapping utility for code transformations
runtime/index.js Updated runtime to integrate transpiler and manage echo context with __getEcho__/__setEcho__
test/transile.spec.js New test validating transpiler output for block statements
test/js/*.js New test fixtures for various echo usage patterns
test/output/*.js Expected output files for test cases
app/docs/*.recho.js Updated documentation examples to use echo.clear()
editor/completion.js Updated autocomplete to suggest echo.clear()
runtime/stdlib/index.js Fixed import path for controls
test/stdlib.spec.js Fixed import path for stdlib
package.json Added acorn-walk dependency
LICENCE Added license notice for Observable Notebook Kit
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pearmini pearmini merged commit ec9a13b into main Oct 28, 2025
3 checks passed
@pearmini pearmini deleted the echo-in-function branch October 28, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants