Skip to content

Commit 910a11b

Browse files
authored
Optimize error messages throw by observable transpile function (#159)
* Optimize error messages throwed by observable transpile function * Remove unused parameter
1 parent bfad641 commit 910a11b

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

runtime/index.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export function createRuntime(initialCode) {
7070
let code = initialCode;
7171
let prevCode = null;
7272
let isRunning = false;
73-
let noSyntaxError = false;
7473

7574
const runtime = new Runtime(BUILTINS);
7675
const main = runtime.module();
@@ -129,9 +128,10 @@ export function createRuntime(initialCode) {
129128
if (isRunning) rerun(code);
130129
},
131130
rejected(error) {
132-
console.error(error);
131+
const e = state.syntaxError || error;
132+
console.error(e);
133133
clear(state);
134-
echo(state, error);
134+
echo(state, e);
135135
},
136136
};
137137
}
@@ -149,16 +149,11 @@ export function createRuntime(initialCode) {
149149
}
150150
}
151151

152-
function transpile(cell, code) {
152+
function transpile(cell) {
153153
try {
154154
return transpileJavaScript(cell);
155155
} catch (error) {
156-
console.error(error);
157-
const changes = removeChanges(code);
158-
const errorMsg = formatError(error) + "\n";
159-
changes.push({from: 0, insert: errorMsg});
160-
dispatch(changes);
161-
return null;
156+
return {body: cell, inputs: [], outputs: [], error};
162157
}
163158
}
164159

@@ -202,25 +197,19 @@ export function createRuntime(initialCode) {
202197
}
203198

204199
function rerun(code) {
205-
// If the code is the same as the pervious one, and the previous code has no syntax error,
206-
// there is no need to to update the position of blocks. So skip the diffing and just
207-
// refresh the outputs.
208-
if (code === prevCode && noSyntaxError) return refresh(code);
200+
if (code === prevCode) return refresh(code);
209201

210202
prevCode = code;
211203
isRunning = true;
212-
noSyntaxError = false;
213204

214205
const nodes = split(code);
215206
if (!nodes) return;
216207

217208
for (const node of nodes) {
218209
const cell = code.slice(node.start, node.end);
219-
const transpiled = transpile(cell, code);
210+
const transpiled = transpile(cell);
220211
node.transpiled = transpiled;
221212
}
222-
if (nodes.some((n) => !n.transpiled)) return;
223-
noSyntaxError = true;
224213

225214
const groups = group(nodes, (n) => code.slice(n.start, n.end));
226215
const enter = [];
@@ -265,9 +254,9 @@ export function createRuntime(initialCode) {
265254
// @ref https://github.com/observablehq/notebook-kit/blob/02914e034fd21a50ebcdca08df57ef5773864125/src/runtime/define.ts#L33
266255
for (const node of enter) {
267256
const vid = uid();
268-
const state = {values: [], variables: [], error: null, doc: false};
257+
const {inputs, body, outputs, error = null} = node.transpiled;
258+
const state = {values: [], variables: [], error: null, syntaxError: error, doc: false};
269259
node.state = state;
270-
const {inputs, body, outputs} = node.transpiled;
271260
const v = main.variable(observer(state), {shadow: {}});
272261
if (inputs.includes("echo")) {
273262
state.doc = true;

test/js/syntax-error2.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export const syntaxError2 = `let a = 1; a = 2;`;
1+
export const syntaxError2 = `let a = 1;
2+
3+
a = 2;
4+
5+
echo(1 + 1);`;

test/output/syntaxError2.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
let a = 1;
2+
13
//✗ [SyntaxError: Assignment to external variable 'a' (1:0)]
2-
let a = 1; a = 2;
4+
a = 2;
5+
6+
//➜ 2
7+
echo(1 + 1);

0 commit comments

Comments
 (0)