@@ -23,6 +23,7 @@ export function parseCell(input, {tag, raw, globals, ...options} = {}) {
23
23
}
24
24
parseReferences ( cell , input , globals ) ;
25
25
parseFeatures ( cell , input ) ;
26
+ parseDeclarations ( cell ) ;
26
27
return cell ;
27
28
}
28
29
@@ -199,29 +200,12 @@ export class CellParser extends Parser {
199
200
: super . toAssignable ( node , isBinding , refDestructuringErrors ) ;
200
201
}
201
202
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" ) {
222
206
this . unexpected ( ) ;
223
- break ;
224
- }
207
+ }
208
+ } ) ;
225
209
}
226
210
checkLocal ( id ) {
227
211
const node = id . id || id ;
@@ -423,3 +407,56 @@ function parseFeatures(cell, input) {
423
407
}
424
408
return cell ;
425
409
}
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
+ }
0 commit comments