From 7e1e4c010e0070d6b2d38fc26be581543deae6ce Mon Sep 17 00:00:00 2001 From: kahless Date: Fri, 16 Mar 2018 19:37:00 +0300 Subject: [PATCH] Recognize destructured assignment to a new name (fixes #485) --- lib/__tests__/findUndefinedIdentifiers-test.js | 8 ++++++++ lib/visitIdentifierNodes.js | 5 ++++- package.json | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/__tests__/findUndefinedIdentifiers-test.js b/lib/__tests__/findUndefinedIdentifiers-test.js index deb92f24..1f83e42c 100644 --- a/lib/__tests__/findUndefinedIdentifiers-test.js +++ b/lib/__tests__/findUndefinedIdentifiers-test.js @@ -25,6 +25,14 @@ it('recognizes destructured assignment', () => { `))).toEqual(new Set([])); }); +it('recognizes destructured assignment to a new name', () => { + expect(findUndefinedIdentifiers(parse(` + import Foo from 'foo'; + const { bar: scar } = Foo; + scar(); + `))).toEqual(new Set([])); +}); + it('knows about scope', () => { expect(findUndefinedIdentifiers(parse(` () => { diff --git a/lib/visitIdentifierNodes.js b/lib/visitIdentifierNodes.js index b0436b10..91c8eb4e 100644 --- a/lib/visitIdentifierNodes.js +++ b/lib/visitIdentifierNodes.js @@ -56,7 +56,10 @@ function normalizeNode(node, context) { const isAssignment = KEYS_USED_FOR_ASSIGNMENT.has(key) || (key === 'key' && parent.parent.type === 'ObjectPattern') || (key === 'left' && parent.type === 'AssignmentPattern') || - (key === 'elements' && parent.type === 'ArrayPattern'); + (key === 'elements' && parent.type === 'ArrayPattern') || + (key === 'value' && + parent.parent.type === 'ObjectPattern' && + parent.parent.parent.type === 'VariableDeclarator'); if (isAssignment) { context.definedInScope.add(node.name); } diff --git a/package.json b/package.json index 4f22e3c8..6d96ad04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "import-js", - "version": "2.10.0", + "version": "2.10.1", "description": "Simplifies importing JavaScript modules", "bin": { "importjs": "./bin/importjs.js",