From 12ae1814d0b2b0c455feb53521a36655c6e393de Mon Sep 17 00:00:00 2001 From: Edoardo Cavazza Date: Fri, 9 Mar 2018 08:37:45 +0100 Subject: [PATCH 1/3] fix: handle Literal types as ObjectPattern keys #25 --- src/attachScopes.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/attachScopes.js b/src/attachScopes.js index d2d1409..8536151 100644 --- a/src/attachScopes.js +++ b/src/attachScopes.js @@ -6,6 +6,10 @@ const blockDeclarations = { }; const extractors = { + Literal ( names, param ) { + names.push( param.value ); + }, + Identifier ( names, param ) { names.push( param.name ); }, From 1563df739320f5d3453f233b4b1886c78074695c Mon Sep 17 00:00:00 2001 From: Edoardo Cavazza Date: Fri, 20 Apr 2018 18:13:17 +0200 Subject: [PATCH 2/3] test: add test for destructuring assignment #25 --- src/attachScopes.js | 2 +- test/test.js | 129 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/src/attachScopes.js b/src/attachScopes.js index 8536151..67d6c29 100644 --- a/src/attachScopes.js +++ b/src/attachScopes.js @@ -16,7 +16,7 @@ const extractors = { ObjectPattern ( names, param ) { param.properties.forEach( prop => { - extractors[ prop.key.type ]( names, prop.key ); + extractors[ (prop.value || prop.key).type ]( names, prop.value || prop.key ); }); }, diff --git a/test/test.js b/test/test.js index a87a2b9..21c7056 100644 --- a/test/test.js +++ b/test/test.js @@ -177,6 +177,135 @@ describe( 'rollup-pluginutils', function () { assert.ok( scope.contains( 'bar' ) ); }); + it('adds named declarators from a deconstructed declaration', function() { + var ast = { + "type": "Program", + "start": 0, + "end": 13, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 42, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 41, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 15, + "properties": [ + { + "type": "Property", + "start": 6, + "end": 10, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Literal", + "start": 6, + "end": 7, + "value": 1, + "raw": "1" + }, + "value": { + "type": "Identifier", + "start": 9, + "end": 10, + "name": "a" + }, + "kind": "init" + }, + { + "type": "Property", + "start": 12, + "end": 13, + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "b" + }, + "kind": "init", + "value": { + "type": "Identifier", + "start": 12, + "end": 13, + "name": "b" + } + } + ]}, + "init": { + "type": "ObjectExpression", + "start": 18, + "end": 41, + "properties": [ + { + "type": "Property", + "start": 22, + "end": 28, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Literal", + "start": 22, + "end": 23, + "value": 1, + "raw": "1" + }, + "value": { + "type": "Literal", + "start": 25, + "end": 28, + "value": "a", + "raw": "'a'" + }, + "kind": "init" + }, + { + "type": "Property", + "start": 32, + "end": 38, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 33, + "name": "b" + }, + "value": { + "type": "Literal", + "start": 35, + "end": 38, + "value": "b", + "raw": "'b'" + }, + "kind": "init" + } + ] + } + } + ], + "kind": "var" + } + ], + "sourceType": "module" + }; + + var scope = attachScopes(ast, 'scope'); + assert.ok(scope.contains('a')); + assert.ok(scope.contains('b')); + }); + // TODO more tests }); From ca7358f4f415e7e3381081cff245285f46e9028d Mon Sep 17 00:00:00 2001 From: Edoardo Cavazza Date: Fri, 4 May 2018 09:37:53 +0200 Subject: [PATCH 3/3] test: add nested case for nested declarators #25 --- test/test.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/test/test.js b/test/test.js index 21c7056..bebb113 100644 --- a/test/test.js +++ b/test/test.js @@ -306,6 +306,124 @@ describe( 'rollup-pluginutils', function () { assert.ok(scope.contains('b')); }); + it('adds nested declarators from a deconstructed declaration', function() { + var ast = { + "type": "Program", + "start": 0, + "end": 40, + "body": [{ + "type": "VariableDeclaration", + "start": 0, + "end": 40, + "declarations": [{ + "type": "VariableDeclarator", + "start": 4, + "end": 39, + "id": { + "type": "ObjectPattern", + "start": 4, + "end": 19, + "properties": [{ + "type": "Property", + "start": 6, + "end": 17, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 6, + "end": 7, + "name": "a" + }, + "value": { + "type": "ObjectPattern", + "start": 9, + "end": 17, + "properties": [{ + "type": "Property", + "start": 11, + "end": 15, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 11, + "end": 12, + "name": "b" + }, + "value": { + "type": "Identifier", + "start": 14, + "end": 15, + "name": "c" + }, + "kind": "init" + }] + }, + "kind": "init" + }] + }, + "init": { + "type": "ObjectExpression", + "start": 22, + "end": 39, + "properties": [{ + "type": "Property", + "start": 24, + "end": 37, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 24, + "end": 25, + "name": "a" + }, + "value": { + "type": "ObjectExpression", + "start": 27, + "end": 37, + "properties": [{ + "type": "Property", + "start": 29, + "end": 35, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 30, + "name": "b" + }, + "value": { + "type": "Literal", + "start": 32, + "end": 35, + "value": "b", + "raw": "'b'" + }, + "kind": "init" + }] + }, + "kind": "init" + }] + } + }], + "kind": "let" + }], + "sourceType": "module" + }; + + var scope = attachScopes(ast, 'scope'); + assert.ok(!scope.contains('a')); + assert.ok(!scope.contains('b')); + assert.ok(scope.contains('c')); + }); + // TODO more tests });