@@ -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+ }
0 commit comments