Skip to content

Commit 716309a

Browse files
committed
expose declaration nodes
1 parent a364003 commit 716309a

File tree

119 files changed

+679
-117
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

+679
-117
lines changed

src/parse.js

+38
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

@@ -423,3 +424,40 @@ function parseFeatures(cell, input) {
423424
}
424425
return cell;
425426
}
427+
428+
// Find declarations: things that this cell defines.
429+
function parseDeclarations(cell) {
430+
if (!cell.body) {
431+
cell.declarations = [];
432+
} else if (cell.body.type === "ImportDeclaration") {
433+
cell.declarations = cell.body.specifiers.map(s => s.local);
434+
} else if (!cell.id) {
435+
cell.declarations = [];
436+
} else {
437+
switch (cell.id.type) {
438+
case "Identifier":
439+
case "ViewExpression":
440+
case "MutableExpression":
441+
cell.declarations = [cell.id];
442+
break;
443+
case "ArrayPattern":
444+
case "ObjectPattern":
445+
cell.declarations = [];
446+
declarePattern(cell.id, node => cell.declarations.push(node));
447+
break;
448+
default: throw new Error(`unexpected identifier: ${cell.id.type}`);
449+
}
450+
}
451+
}
452+
453+
function declarePattern(node, callback) {
454+
switch (node.type) {
455+
case "Identifier": callback(node); break;
456+
case "ObjectPattern": node.properties.forEach(node => declarePattern(node, callback)); break;
457+
case "ArrayPattern": node.elements.forEach(node => node && declarePattern(node, callback)); break;
458+
case "Property": declarePattern(node.value, callback); break;
459+
case "RestElement": declarePattern(node.argument, callback); break;
460+
case "AssignmentPattern": declarePattern(node.left, callback); break;
461+
default: throw new Error(`illegal declaration: ${node.type}`);
462+
}
463+
}

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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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-yield.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
],
6262
"fileAttachments": [],
6363
"databaseClients": [],
64-
"secrets": []
64+
"secrets": [],
65+
"declarations": []
6566
}

test/output/backslash-after-dollar-markdown.md.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
"references": [],
5353
"fileAttachments": [],
5454
"databaseClients": [],
55-
"secrets": []
55+
"secrets": [],
56+
"declarations": []
5657
}

test/output/backslash-before-dollar-markdown.md.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
"references": [],
5353
"fileAttachments": [],
5454
"databaseClients": [],
55-
"secrets": []
55+
"secrets": [],
56+
"declarations": []
5657
}

test/output/backslash-markdown.md.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
"references": [],
5353
"fileAttachments": [],
5454
"databaseClients": [],
55-
"secrets": []
55+
"secrets": [],
56+
"declarations": []
5657
}

test/output/bare-dynamic-import.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
"references": [],
2121
"fileAttachments": [],
2222
"databaseClients": [],
23-
"secrets": []
23+
"secrets": [],
24+
"declarations": []
2425
}

test/output/basic.db.sql.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,6 @@
119119
],
120120
"fileAttachments": [],
121121
"databaseClients": [],
122-
"secrets": []
122+
"secrets": [],
123+
"declarations": []
123124
}

test/output/basic.html.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@
8989
],
9090
"fileAttachments": [],
9191
"databaseClients": [],
92-
"secrets": []
92+
"secrets": [],
93+
"declarations": []
9394
}

test/output/basic.sql.json

+2-1
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/basic.tex.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@
110110
],
111111
"fileAttachments": [],
112112
"databaseClients": [],
113-
"secrets": []
113+
"secrets": [],
114+
"declarations": []
114115
}

test/output/bigint-zero.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"references": [],
1717
"fileAttachments": [],
1818
"databaseClients": [],
19-
"secrets": []
19+
"secrets": [],
20+
"declarations": []
2021
}

test/output/bigint.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
],
3636
"fileAttachments": [],
3737
"databaseClients": [],
38-
"secrets": []
38+
"secrets": [],
39+
"declarations": []
3940
}

test/output/binary-expression.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
],
3535
"fileAttachments": [],
3636
"databaseClients": [],
37-
"secrets": []
37+
"secrets": [],
38+
"declarations": []
3839
}

test/output/block-leading-comment.js.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
"references": [],
3333
"fileAttachments": [],
3434
"databaseClients": [],
35-
"secrets": []
35+
"secrets": [],
36+
"declarations": [
37+
{
38+
"type": "Identifier",
39+
"start": 21,
40+
"end": 24,
41+
"name": "foo"
42+
}
43+
]
3644
}

test/output/block-trailing-comment.js.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
"references": [],
3333
"fileAttachments": [],
3434
"databaseClients": [],
35-
"secrets": []
35+
"secrets": [],
36+
"declarations": [
37+
{
38+
"type": "Identifier",
39+
"start": 0,
40+
"end": 3,
41+
"name": "foo"
42+
}
43+
]
3644
}

test/output/catch-clause.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,6 @@
125125
],
126126
"fileAttachments": [],
127127
"databaseClients": [],
128-
"secrets": []
128+
"secrets": [],
129+
"declarations": []
129130
}

test/output/comment.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"references": [],
1010
"fileAttachments": [],
1111
"databaseClients": [],
12-
"secrets": []
12+
"secrets": [],
13+
"declarations": []
1314
}

test/output/computed-property-name.js.json

+2-1
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/database-client.js.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@
7272
]
7373
]
7474
],
75-
"secrets": []
75+
"secrets": [],
76+
"declarations": []
7677
}

test/output/destructure-array-literal.js.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,19 @@
5959
"references": [],
6060
"fileAttachments": [],
6161
"databaseClients": [],
62-
"secrets": []
62+
"secrets": [],
63+
"declarations": [
64+
{
65+
"type": "Identifier",
66+
"start": 2,
67+
"end": 5,
68+
"name": "foo"
69+
},
70+
{
71+
"type": "Identifier",
72+
"start": 10,
73+
"end": 13,
74+
"name": "bar"
75+
}
76+
]
6377
}

test/output/destructure-block.js.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,19 @@
119119
"references": [],
120120
"fileAttachments": [],
121121
"databaseClients": [],
122-
"secrets": []
122+
"secrets": [],
123+
"declarations": [
124+
{
125+
"type": "Identifier",
126+
"start": 2,
127+
"end": 5,
128+
"name": "foo"
129+
},
130+
{
131+
"type": "Identifier",
132+
"start": 7,
133+
"end": 10,
134+
"name": "bar"
135+
}
136+
]
123137
}

test/output/destructure-default-value-reference.js.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,19 @@
7070
],
7171
"fileAttachments": [],
7272
"databaseClients": [],
73-
"secrets": []
73+
"secrets": [],
74+
"declarations": [
75+
{
76+
"type": "Identifier",
77+
"start": 2,
78+
"end": 3,
79+
"name": "x"
80+
},
81+
{
82+
"type": "Identifier",
83+
"start": 5,
84+
"end": 6,
85+
"name": "y"
86+
}
87+
]
7488
}

test/output/destructure-default-value-secret.js.json

+14
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,19 @@
8383
}
8484
]
8585
]
86+
],
87+
"declarations": [
88+
{
89+
"type": "Identifier",
90+
"start": 2,
91+
"end": 3,
92+
"name": "x"
93+
},
94+
{
95+
"type": "Identifier",
96+
"start": 5,
97+
"end": 6,
98+
"name": "y"
99+
}
86100
]
87101
}

0 commit comments

Comments
 (0)