Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/attachScopes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ const blockDeclarations = {
};

const extractors = {
Literal ( names, param ) {
names.push( param.value );
},

Identifier ( names, param ) {
names.push( param.name );
},

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 );
});
},

Expand Down
247 changes: 247 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,253 @@ 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'));
});

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
});

Expand Down