Skip to content

Commit 98467c8

Browse files
committed
expose declaration nodes
1 parent a364003 commit 98467c8

File tree

119 files changed

+700
-139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+700
-139
lines changed

src/parse.js

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function parseCell(input, {tag, raw, globals, ...options} = {}) {
2323
}
2424
parseReferences(cell, input, globals);
2525
parseFeatures(cell, input);
26+
parseDeclarations(cell);
2627
return cell;
2728
}
2829

@@ -199,29 +200,12 @@ export class CellParser extends Parser {
199200
: super.toAssignable(node, isBinding, refDestructuringErrors);
200201
}
201202
checkCellDeclaration(node) {
202-
switch (node.type) {
203-
case "Identifier":
204-
break;
205-
case "ObjectPattern":
206-
for (const p of node.properties) this.checkCellDeclaration(p);
207-
break;
208-
case "ArrayPattern":
209-
for (const e of node.elements) e && this.checkCellDeclaration(e);
210-
break;
211-
case "Property":
212-
this.checkCellDeclaration(node.value);
213-
break;
214-
case "RestElement":
215-
this.checkCellDeclaration(node.argument);
216-
break;
217-
case "AssignmentPattern":
218-
this.checkCellDeclaration(node.left);
219-
break;
220-
default:
221-
// Don’t allow destructuring into viewof or mutable declarations.
203+
// Don’t allow destructuring into viewof or mutable declarations.
204+
declarePattern(node, id => {
205+
if (id.type !== "Identifier") {
222206
this.unexpected();
223-
break;
224-
}
207+
}
208+
});
225209
}
226210
checkLocal(id) {
227211
const node = id.id || id;
@@ -423,3 +407,56 @@ function parseFeatures(cell, input) {
423407
}
424408
return cell;
425409
}
410+
411+
// Find declarations: things that this cell defines.
412+
function parseDeclarations(cell) {
413+
if (!cell.body) {
414+
cell.declarations = [];
415+
} else if (cell.body.type === "ImportDeclaration") {
416+
cell.declarations = cell.body.specifiers.map(s => s.local);
417+
} else if (!cell.id) {
418+
cell.declarations = [];
419+
} else {
420+
switch (cell.id.type) {
421+
case "Identifier":
422+
case "ViewExpression":
423+
case "MutableExpression":
424+
cell.declarations = [cell.id];
425+
break;
426+
case "ArrayPattern":
427+
case "ObjectPattern":
428+
cell.declarations = [];
429+
declarePattern(cell.id, node => cell.declarations.push(node));
430+
break;
431+
default:
432+
throw new Error(`unexpected identifier: ${cell.id.type}`);
433+
}
434+
}
435+
}
436+
437+
function declarePattern(node, callback) {
438+
switch (node.type) {
439+
case "Identifier":
440+
case "ViewExpression":
441+
case "MutableExpression":
442+
callback(node);
443+
break;
444+
case "ObjectPattern":
445+
node.properties.forEach(node => declarePattern(node, callback));
446+
break;
447+
case "ArrayPattern":
448+
node.elements.forEach(node => node && declarePattern(node, callback));
449+
break;
450+
case "Property":
451+
declarePattern(node.value, callback);
452+
break;
453+
case "RestElement":
454+
declarePattern(node.argument, callback);
455+
break;
456+
case "AssignmentPattern":
457+
declarePattern(node.left, callback);
458+
break;
459+
default:
460+
throw new Error(`unexpected declaration: ${node.type}`);
461+
}
462+
}

test/output/anonymous-block-cell.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"references": [],
2828
"fileAttachments": [],
2929
"databaseClients": [],
30-
"secrets": []
30+
"secrets": [],
31+
"declarations": []
3132
}

test/output/anonymous-expression-cell.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"references": [],
1616
"fileAttachments": [],
1717
"databaseClients": [],
18-
"secrets": []
18+
"secrets": [],
19+
"declarations": []
1920
}

test/output/anonymous-function.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@
3737
"references": [],
3838
"fileAttachments": [],
3939
"databaseClients": [],
40-
"secrets": []
40+
"secrets": [],
41+
"declarations": []
4142
}

test/output/await-block-cell.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
],
3939
"fileAttachments": [],
4040
"databaseClients": [],
41-
"secrets": []
41+
"secrets": [],
42+
"declarations": []
4243
}

test/output/await-in-arrow-function-expression.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
],
4949
"fileAttachments": [],
5050
"databaseClients": [],
51-
"secrets": []
51+
"secrets": [],
52+
"declarations": []
5253
}

test/output/await-in-arrow-function.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@
7474
],
7575
"fileAttachments": [],
7676
"databaseClients": [],
77-
"secrets": []
77+
"secrets": [],
78+
"declarations": []
7879
}

test/output/await-in-class.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@
8888
],
8989
"fileAttachments": [],
9090
"databaseClients": [],
91-
"secrets": []
91+
"secrets": [],
92+
"declarations": []
9293
}

test/output/await-in-function.js.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@
6060
],
6161
"fileAttachments": [],
6262
"databaseClients": [],
63-
"secrets": []
63+
"secrets": [],
64+
"declarations": []
6465
}

test/output/await-in-markdown.md.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@
8888
],
8989
"fileAttachments": [],
9090
"databaseClients": [],
91-
"secrets": []
91+
"secrets": [],
92+
"declarations": []
9293
}

0 commit comments

Comments
 (0)