From c0f45bedb0b439b162cfe7589c30543c517b4e9e Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Mon, 19 Mar 2018 12:42:30 +0100 Subject: [PATCH] fix(scopes): Support Function and Class Declarations without id These declarations are used in combination with default exports ``` export default function () {}; ``` --- src/attachScopes.js | 2 +- test/test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/attachScopes.js b/src/attachScopes.js index d2d1409..84c96e6 100644 --- a/src/attachScopes.js +++ b/src/attachScopes.js @@ -61,7 +61,7 @@ class Scope { // it's a `var` or function node, and this // is a block scope, so we need to go up this.parent.addDeclaration( node, isBlockDeclaration, isVar ); - } else { + } else if ( node.id ) { extractNames( node.id ).forEach( name => { this.declarations[ name ] = true; }); diff --git a/test/test.js b/test/test.js index a87a2b9..6195657 100644 --- a/test/test.js +++ b/test/test.js @@ -177,6 +177,41 @@ describe( 'rollup-pluginutils', function () { assert.ok( scope.contains( 'bar' ) ); }); + it( 'supports FunctionDeclarations without id', function () { + var ast = { + "type": "Program", + "start": 0, + "end": 33, + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 0, + "end": 32, + "declaration": { + "type": "FunctionDeclaration", + "start": 15, + "end": 32, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 26, + "end": 32, + "body": [] + } + } + } + ], + "sourceType": "module" + }; + + var scope = attachScopes( ast, 'scope' ); + // does not throw + }); + // TODO more tests });