Skip to content

Commit

Permalink
implicit display await (#356)
Browse files Browse the repository at this point in the history
* implicit display await

* adopt implicit await
  • Loading branch information
mbostock authored Dec 8, 2023
1 parent 5bdc29e commit 21dc146
Show file tree
Hide file tree
Showing 38 changed files with 71 additions and 71 deletions.
4 changes: 2 additions & 2 deletions docs/lib/duckdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ await db.sql`CREATE TABLE addresses
As an alternative to `db.sql`, there’s also `db.query`:

```js echo
await db.query("SELECT * FROM gaia LIMIT 10")
db.query("SELECT * FROM gaia LIMIT 10")
```

And `db.queryRow`:

```js echo
await db.queryRow("SELECT count() AS count FROM gaia")
db.queryRow("SELECT count() AS count FROM gaia")
```

See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details.
2 changes: 1 addition & 1 deletion docs/lib/mermaid.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import mermaid from "npm:@observablehq/mermaid";
To use in a JavaScript code block:

```js echo
await mermaid`graph TD;
mermaid`graph TD;
A-->B;
A-->C;
B-->D;
Expand Down
4 changes: 2 additions & 2 deletions docs/lib/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ Inputs.table(tracks)
As an alternative to `db.sql`, you can call `db.query`.

```js run=false
await db.query(`SELECT * FROM tracks WHERE Name LIKE $1`, [`%${name}%`])
db.query(`SELECT * FROM tracks WHERE Name LIKE $1`, [`%${name}%`])
```

There’s also `db.queryRow` for just getting a single row.

```js echo
await db.queryRow(`SELECT sqlite_version()`)
db.queryRow(`SELECT sqlite_version()`)
```
4 changes: 2 additions & 2 deletions src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function transpileJavaScript(input: string, options: ParseOptions): Pendi
.map(({name}) => fileReference(name, sourcePath));
const inputs = Array.from(new Set<string>(node.references.map((r) => r.name)));
const implicitDisplay = node.expression && !inputs.includes("display") && !inputs.includes("view");
if (implicitDisplay) inputs.push("display");
if (implicitDisplay) inputs.push("display"), (node.async = true);
if (findImportDeclarations(node).length > 0) node.async = true;
return {
id,
Expand All @@ -89,7 +89,7 @@ export function transpileJavaScript(input: string, options: ParseOptions): Pendi
const output = new Sourcemap(input);
trim(output, input);
if (implicitDisplay) {
output.insertLeft(0, "display((\n");
output.insertLeft(0, "display(await(\n");
output.insertRight(input.length, "\n))");
}
await rewriteImports(output, node, sourcePath, createImportResolver(root, "_import"));
Expand Down
2 changes: 1 addition & 1 deletion test/javascript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe("transpileJavaScript(input, options)", () => {
sourcePath: "index.js",
verbose: false
});
assert.strictEqual(await body(), "(test,display) => {\ndisplay((\ntest\n))\n}");
assert.strictEqual(await body(), "async (test,display) => {\ndisplay(await(\ntest\n))\n}");
});
it("rethrows unexpected errors", () => {
const expected = new Error();
Expand Down
4 changes: 2 additions & 2 deletions test/output/anonymous-class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
class {}
))
}});
4 changes: 2 additions & 2 deletions test/output/anonymous-function.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
function() { return 42; }
))
}});
2 changes: 1 addition & 1 deletion test/output/async-template-literal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["md","promise","display"], body: async (md,promise,display) => {
display((
display(await(
md`${await promise}`
))
}});
2 changes: 1 addition & 1 deletion test/output/await.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["promise","display"], body: async (promise,display) => {
display((
display(await(
await promise
))
}});
4 changes: 2 additions & 2 deletions test/output/bigint-zero.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
0n
))
}});
4 changes: 2 additions & 2 deletions test/output/bigint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["foo","display"], body: (foo,display) => {
display((
define({id: "0", inputs: ["foo","display"], body: async (foo,display) => {
display(await(
foo + 42n
))
}});
2 changes: 1 addition & 1 deletion test/output/block-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n1 + 2\n))\n}"
"body": "async (display) => {\ndisplay(await(\n1 + 2\n))\n}"
}
],
"hash": "ce09e20b956c2d34ace6c261623306829d6b0468cb797a6226dbff737185b7ec"
Expand Down
14 changes: 7 additions & 7 deletions test/output/build/archives/tar.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,43 @@
import {define} from "./_observablehq/client.js";

define({id: "d5134368", inputs: ["FileAttachment","display"], files: [{"name":"./static-tar/file.txt","mimeType":"text/plain","path":"./_file/static-tar/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("static-tar/file.txt").text()
))
}});

define({id: "a0c06958", inputs: ["FileAttachment","display"], files: [{"name":"./static-tgz/file.txt","mimeType":"text/plain","path":"./_file/static-tgz/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("static-tgz/file.txt").text()
))
}});

define({id: "d84cd7fb", inputs: ["FileAttachment","display"], files: [{"name":"./static-tar/does-not-exist.txt","mimeType":"text/plain","path":"./_file/static-tar/does-not-exist.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("static-tar/does-not-exist.txt").text()
))
}});

define({id: "86bd51aa", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic-tar/file.txt","mimeType":"text/plain","path":"./_file/dynamic-tar/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic-tar/file.txt").text()
))
}});

define({id: "95938c22", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic-tar/does-not-exist.txt","mimeType":"text/plain","path":"./_file/dynamic-tar/does-not-exist.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic-tar/does-not-exist.txt").text()
))
}});

define({id: "7e5740fd", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic-tar-gz/file.txt","mimeType":"text/plain","path":"./_file/dynamic-tar-gz/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic-tar-gz/file.txt").text()
))
}});

define({id: "d0a58efd", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic-tar-gz/does-not-exist.txt","mimeType":"text/plain","path":"./_file/dynamic-tar-gz/does-not-exist.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic-tar-gz/does-not-exist.txt").text()
))
}});
Expand Down
8 changes: 4 additions & 4 deletions test/output/build/archives/zip.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
import {define} from "./_observablehq/client.js";

define({id: "d3b9d0ee", inputs: ["FileAttachment","display"], files: [{"name":"./static/file.txt","mimeType":"text/plain","path":"./_file/static/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("static/file.txt").text()
))
}});

define({id: "bab54217", inputs: ["FileAttachment","display"], files: [{"name":"./static/not-found.txt","mimeType":"text/plain","path":"./_file/static/not-found.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("static/not-found.txt").text()
))
}});

define({id: "11eec300", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic/file.txt","mimeType":"text/plain","path":"./_file/dynamic/file.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic/file.txt").text()
))
}});

define({id: "ee2310f3", inputs: ["FileAttachment","display"], files: [{"name":"./dynamic/not-found.txt","mimeType":"text/plain","path":"./_file/dynamic/not-found.txt"}], body: async (FileAttachment,display) => {
display((
display(await(
await FileAttachment("dynamic/not-found.txt").text()
))
}});
Expand Down
8 changes: 4 additions & 4 deletions test/output/build/files/files.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

import {define} from "./_observablehq/client.js";

define({id: "10037545", inputs: ["FileAttachment","display"], files: [{"name":"./file-top.csv","mimeType":"text/csv","path":"./_file/file-top.csv"}], body: (FileAttachment,display) => {
display((
define({id: "10037545", inputs: ["FileAttachment","display"], files: [{"name":"./file-top.csv","mimeType":"text/csv","path":"./_file/file-top.csv"}], body: async (FileAttachment,display) => {
display(await(
FileAttachment("file-top.csv")
))
}});

define({id: "453a8147", inputs: ["FileAttachment","display"], files: [{"name":"./subsection/file-sub.csv","mimeType":"text/csv","path":"./_file/subsection/file-sub.csv"}], body: (FileAttachment,display) => {
display((
define({id: "453a8147", inputs: ["FileAttachment","display"], files: [{"name":"./subsection/file-sub.csv","mimeType":"text/csv","path":"./_file/subsection/file-sub.csv"}], body: async (FileAttachment,display) => {
display(await(
FileAttachment("subsection/file-sub.csv")
))
}});
Expand Down
8 changes: 4 additions & 4 deletions test/output/build/files/subsection/subfiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

import {define} from "../_observablehq/client.js";

define({id: "ef9a31ef", inputs: ["FileAttachment","display"], files: [{"name":"../file-top.csv","mimeType":"text/csv","path":"../_file/file-top.csv"}], body: (FileAttachment,display) => {
display((
define({id: "ef9a31ef", inputs: ["FileAttachment","display"], files: [{"name":"../file-top.csv","mimeType":"text/csv","path":"../_file/file-top.csv"}], body: async (FileAttachment,display) => {
display(await(
FileAttachment("../file-top.csv")
))
}});

define({id: "834ecf9f", inputs: ["FileAttachment","display"], files: [{"name":"./file-sub.csv","mimeType":"text/csv","path":"../_file/subsection/file-sub.csv"}], body: (FileAttachment,display) => {
display((
define({id: "834ecf9f", inputs: ["FileAttachment","display"], files: [{"name":"./file-sub.csv","mimeType":"text/csv","path":"../_file/subsection/file-sub.csv"}], body: async (FileAttachment,display) => {
display(await(
FileAttachment("file-sub.csv")
))
}});
Expand Down
4 changes: 2 additions & 2 deletions test/output/build/missing-file/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import {define} from "./_observablehq/client.js";

define({id: "5760fd93", inputs: ["FileAttachment","display"], files: [{"name":"./does-not-exist.txt","mimeType":"text/plain","path":"./_file/does-not-exist.txt"}], body: (FileAttachment,display) => {
display((
define({id: "5760fd93", inputs: ["FileAttachment","display"], files: [{"name":"./does-not-exist.txt","mimeType":"text/plain","path":"./_file/does-not-exist.txt"}], body: async (FileAttachment,display) => {
display(await(
FileAttachment("does-not-exist.txt")
))
}});
Expand Down
4 changes: 2 additions & 2 deletions test/output/build/missing-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import {define} from "./_observablehq/client.js";

define({id: "e0627979", inputs: ["display"], body: (display) => {
display((
define({id: "e0627979", inputs: ["display"], body: async (display) => {
display(await(
import("./_import/does-not-exist.js?sha=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
))
}});
Expand Down
4 changes: 2 additions & 2 deletions test/output/build/multi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
return {f1};
}});

define({id: "db071921", inputs: ["Input","f1","display"], body: (Input,f1,display) => {
display((
define({id: "db071921", inputs: ["Input","f1","display"], body: async (Input,f1,display) => {
display(await(
Input.table(f1)
))
}});
Expand Down
4 changes: 2 additions & 2 deletions test/output/comma-expression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
1, 2
))
}});
2 changes: 1 addition & 1 deletion test/output/dollar-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n1 + 2\n))\n}"
"body": "async (display) => {\ndisplay(await(\n1 + 2\n))\n}"
}
],
"hash": "6cea6f4fe1a149eea7dd2d373a27aad913532be30cc3164decd82e8239f5dc5e"
Expand Down
2 changes: 1 addition & 1 deletion test/output/dot-graphviz.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dot",
"display"
],
"body": "(dot,display) => {\ndisplay((\ndot`digraph D {\n\n A [shape=diamond]\n B [shape=box]\n C [shape=circle]\n\n A -> B [style=dashed]\n A -> C\n A -> D [penwidth=5, arrowhead=none]\n\n}\n`\n))\n}"
"body": "async (dot,display) => {\ndisplay(await(\ndot`digraph D {\n\n A [shape=diamond]\n B [shape=box]\n C [shape=circle]\n\n A -> B [style=dashed]\n A -> C\n A -> D [penwidth=5, arrowhead=none]\n\n}\n`\n))\n}"
}
],
"hash": "3a62af050e650638e3aa41d5a6f413d07e30ee63a74a8c5eb9b7e4c217b9a7bc"
Expand Down
2 changes: 1 addition & 1 deletion test/output/double-quote-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n\"}'\"\n))\n}"
"body": "async (display) => {\ndisplay(await(\n\"}'\"\n))\n}"
}
],
"hash": "f00c09574547ac66c34c0d87676f1aab13f25e02204f433edb07359daf6042a4"
Expand Down
2 changes: 1 addition & 1 deletion test/output/embedded-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n1 + 2\n))\n}"
"body": "async (display) => {\ndisplay(await(\n1 + 2\n))\n}"
}
],
"hash": "a9e5ef0e43af5ca3669ebca7ace93a58f4f06b4192067a770f7be3cf1edfbfb6"
Expand Down
4 changes: 2 additions & 2 deletions test/output/expression-leading-comment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
// Hello, comment.
42
))
Expand Down
4 changes: 2 additions & 2 deletions test/output/expression-trailing-comment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
42 // Hello, comment.
))
}});
4 changes: 2 additions & 2 deletions test/output/expression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
1 + 2
))
}});
2 changes: 1 addition & 1 deletion test/output/heading-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n1 + 2\n))\n}"
"body": "async (display) => {\ndisplay(await(\n1 + 2\n))\n}"
}
],
"hash": "8ec3a8e125394f6357cd56e3b191d738b4645a0c97cd403fe19fcff17a5a2127"
Expand Down
4 changes: 2 additions & 2 deletions test/output/identifier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["foo","display"], body: (foo,display) => {
display((
define({id: "0", inputs: ["foo","display"], body: async (foo,display) => {
display(await(
foo
))
}});
2 changes: 1 addition & 1 deletion test/output/inline-expression.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"display"
],
"inline": true,
"body": "(display) => {\ndisplay((\n1 + 2\n))\n}"
"body": "async (display) => {\ndisplay(await(\n1 + 2\n))\n}"
}
],
"hash": "609842fc8b06be8d6b1ca6ce90c67c4384f6078adc49b9f887065560eababf65"
Expand Down
2 changes: 1 addition & 1 deletion test/output/local-fetch.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"path": "./_file/local-fetch.md"
}
],
"body": "(FileAttachment,display) => {\ndisplay((\nFileAttachment(\"./local-fetch.md\")\n))\n}"
"body": "async (FileAttachment,display) => {\ndisplay(await(\nFileAttachment(\"./local-fetch.md\")\n))\n}"
}
],
"hash": "3f31c6717790dfd2fb68fb8bf9c11b4fcb44a2014ab382c1a8bbbf711a99857e"
Expand Down
2 changes: 1 addition & 1 deletion test/output/mermaid.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"mermaid",
"display"
],
"body": "async (mermaid,display) => {\ndisplay((\nawait mermaid`graph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n`\n))\n}"
"body": "async (mermaid,display) => {\ndisplay(await(\nawait mermaid`graph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n`\n))\n}"
}
],
"hash": "6d9b9a614504f0decca01c59af8f6bae96982a675312166fedc028078dedf093"
Expand Down
4 changes: 2 additions & 2 deletions test/output/object-literal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], body: (display) => {
display((
define({id: "0", inputs: ["display"], body: async (display) => {
display(await(
{a: 1}
))
}});
Loading

0 comments on commit 21dc146

Please sign in to comment.