asg017 / unofficial-observablehq-compiler

An unofficial compiler for Observable notebook syntax
https://www.npmjs.com/package/@alex.garcia/unofficial-observablehq-compiler
115 stars 22 forks source link

mutable syntax issue #35

Open a10k opened 3 years ago

a10k commented 3 years ago

The below .ojs markup works perfectly on observable, the parser library is able to parse it correctly as a block cell too, but in the new interpreter this fails.

mutable x = []
{
  mutable x.push(1);
  mutable x = mutable x;
}

Console error when it fails

VM130:3 Uncaught (in promise) SyntaxError: Invalid or unexpected token
    at new Function (<anonymous>)
    at createRegularCellDefinition (index.js:formatted:7648)
    at Interpreter.cell (index.js:formatted:7737)
    at Interpreter.module (index.js:formatted:7694)
    at HTMLDivElement.eval (observablehq-214:8)
    at worker.8767064650b2824c13f8f3e3a6bb3bd4accb75c98d9277e71e09afe4bedf282d.js:2
asg017 commented 3 years ago

Yup, can confirm this is a bug with this compiler, thanks for the report! It's super hard to catch edge cases like these.

Pretty sure it's got to do with this part of the codebase, will take a closer look

mootari commented 2 years ago

Parser AST for the example (without the first line):

```javascript { "type": "Cell", "start": 0, "end": 49, "id": null, "body": { "type": "BlockStatement", "start": 0, "end": 49, "body": [ { "type": "ExpressionStatement", "start": 4, "end": 22, "expression": { "type": "CallExpression", "start": 4, "end": 21, "callee": { "type": "MemberExpression", "start": 4, "end": 18, "object": { "type": "MutableExpression", "start": 4, "end": 13, "id": { "type": "Identifier", "start": 12, "end": 13, "name": "x" } }, "property": { "type": "Identifier", "start": 14, "end": 18, "name": "push" }, "computed": false, "optional": false }, "arguments": [ { "type": "Literal", "start": 19, "end": 20, "value": 1, "raw": "1" } ], "optional": false } }, { "type": "ExpressionStatement", "start": 25, "end": 47, "expression": { "type": "AssignmentExpression", "start": 25, "end": 46, "operator": "=", "left": { "type": "MutableExpression", "start": 25, "end": 34, "id": { "type": "Identifier", "start": 33, "end": 34, "name": "x" } }, "right": { "type": "MutableExpression", "start": 37, "end": 46, "id": { "type": "Identifier", "start": 45, "end": 46, "name": "x" } } } } ] }, "async": false, "generator": false, "references": [ { "type": "MutableExpression", "start": 4, "end": 13, "id": { "type": "Identifier", "start": 12, "end": 13, "name": "x" } }, { "type": "MutableExpression", "start": 25, "end": 34, "id": { "type": "Identifier", "start": 33, "end": 34, "name": "x" } }, { "type": "MutableExpression", "start": 37, "end": 46, "id": { "type": "Identifier", "start": 45, "end": 46, "name": "x" } } ], "fileAttachments": {}, "databaseClients": {}, "secrets": {} } ```