Skip to content

Commit

Permalink
expose declaration nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 28, 2021
1 parent a364003 commit 98467c8
Show file tree
Hide file tree
Showing 119 changed files with 700 additions and 139 deletions.
81 changes: 59 additions & 22 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function parseCell(input, {tag, raw, globals, ...options} = {}) {
}
parseReferences(cell, input, globals);
parseFeatures(cell, input);
parseDeclarations(cell);
return cell;
}

Expand Down Expand Up @@ -199,29 +200,12 @@ export class CellParser extends Parser {
: super.toAssignable(node, isBinding, refDestructuringErrors);
}
checkCellDeclaration(node) {
switch (node.type) {
case "Identifier":
break;
case "ObjectPattern":
for (const p of node.properties) this.checkCellDeclaration(p);
break;
case "ArrayPattern":
for (const e of node.elements) e && this.checkCellDeclaration(e);
break;
case "Property":
this.checkCellDeclaration(node.value);
break;
case "RestElement":
this.checkCellDeclaration(node.argument);
break;
case "AssignmentPattern":
this.checkCellDeclaration(node.left);
break;
default:
// Don’t allow destructuring into viewof or mutable declarations.
// Don’t allow destructuring into viewof or mutable declarations.
declarePattern(node, id => {
if (id.type !== "Identifier") {
this.unexpected();
break;
}
}
});
}
checkLocal(id) {
const node = id.id || id;
Expand Down Expand Up @@ -423,3 +407,56 @@ function parseFeatures(cell, input) {
}
return cell;
}

// Find declarations: things that this cell defines.
function parseDeclarations(cell) {
if (!cell.body) {
cell.declarations = [];
} else if (cell.body.type === "ImportDeclaration") {
cell.declarations = cell.body.specifiers.map(s => s.local);
} else if (!cell.id) {
cell.declarations = [];
} else {
switch (cell.id.type) {
case "Identifier":
case "ViewExpression":
case "MutableExpression":
cell.declarations = [cell.id];
break;
case "ArrayPattern":
case "ObjectPattern":
cell.declarations = [];
declarePattern(cell.id, node => cell.declarations.push(node));
break;
default:
throw new Error(`unexpected identifier: ${cell.id.type}`);
}
}
}

function declarePattern(node, callback) {
switch (node.type) {
case "Identifier":
case "ViewExpression":
case "MutableExpression":
callback(node);
break;
case "ObjectPattern":
node.properties.forEach(node => declarePattern(node, callback));
break;
case "ArrayPattern":
node.elements.forEach(node => node && declarePattern(node, callback));
break;
case "Property":
declarePattern(node.value, callback);
break;
case "RestElement":
declarePattern(node.argument, callback);
break;
case "AssignmentPattern":
declarePattern(node.left, callback);
break;
default:
throw new Error(`unexpected declaration: ${node.type}`);
}
}
3 changes: 2 additions & 1 deletion test/output/anonymous-block-cell.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/anonymous-expression-cell.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/anonymous-function.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-block-cell.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-in-arrow-function-expression.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-in-arrow-function.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-in-class.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-in-function.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-in-markdown.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/await-yield.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/backslash-after-dollar-markdown.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/backslash-before-dollar-markdown.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/backslash-markdown.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/bare-dynamic-import.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/basic.db.sql.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/basic.html.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/basic.sql.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/basic.tex.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/bigint-zero.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/bigint.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/binary-expression.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
10 changes: 9 additions & 1 deletion test/output/block-leading-comment.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": [
{
"type": "Identifier",
"start": 21,
"end": 24,
"name": "foo"
}
]
}
10 changes: 9 additions & 1 deletion test/output/block-trailing-comment.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": [
{
"type": "Identifier",
"start": 0,
"end": 3,
"name": "foo"
}
]
}
3 changes: 2 additions & 1 deletion test/output/catch-clause.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/comment.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/computed-property-name.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": []
}
3 changes: 2 additions & 1 deletion test/output/database-client.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
]
]
],
"secrets": []
"secrets": [],
"declarations": []
}
16 changes: 15 additions & 1 deletion test/output/destructure-array-literal.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,19 @@
"references": [],
"fileAttachments": [],
"databaseClients": [],
"secrets": []
"secrets": [],
"declarations": [
{
"type": "Identifier",
"start": 2,
"end": 5,
"name": "foo"
},
{
"type": "Identifier",
"start": 10,
"end": 13,
"name": "bar"
}
]
}
Loading

0 comments on commit 98467c8

Please sign in to comment.