pax_global_header00006660000000000000000000000064132370412470014515gustar00rootroot0000000000000052 comment=dd3f228ca9fd40541e98f2f4406069c82fe52f35 get-assigned-identifiers-1.2.0/000077500000000000000000000000001323704124700163725ustar00rootroot00000000000000get-assigned-identifiers-1.2.0/.gitignore000066400000000000000000000010401323704124700203550ustar00rootroot00000000000000# Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules package-lock.json get-assigned-identifiers-1.2.0/.travis.yml000066400000000000000000000001201323704124700204740ustar00rootroot00000000000000language: node_js node_js: - 8 - 6 - 4 cache: directories: - ~/.npm get-assigned-identifiers-1.2.0/CHANGELOG.md000066400000000000000000000005341323704124700202050ustar00rootroot00000000000000# get-destructure-identifiers change log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## 1.2.0 / 2018-02-08 * support object rest destructuring `{...a} = b` ## 1.1.0 / 2017-12-02 * support import declarations ## 1.0.0 / 2017-11-11 * initial release get-assigned-identifiers-1.2.0/LICENSE.md000066400000000000000000000011641323704124700200000ustar00rootroot00000000000000# [Apache License 2.0](https://spdx.org/licenses/Apache-2.0) Copyright 2017 Renée Kooi Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at > http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. get-assigned-identifiers-1.2.0/README.md000066400000000000000000000024661323704124700176610ustar00rootroot00000000000000# get-assigned-identifiers get a list of identifiers that are initialised by a JavaScript AST node. [![npm][npm-image]][npm-url] [![travis][travis-image]][travis-url] [![standard][standard-image]][standard-url] [npm-image]: https://img.shields.io/npm/v/get-assigned-identifiers.svg?style=flat-square [npm-url]: https://www.npmjs.com/package/get-assigned-identifiers [travis-image]: https://img.shields.io/travis/goto-bus-stop/get-assigned-identifiers.svg?style=flat-square [travis-url]: https://travis-ci.org/goto-bus-stop/get-assigned-identifiers [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square [standard-url]: http://npm.im/standard ## Install ``` npm install get-assigned-identifiers ``` ## Usage ```js var getAssignedIdentifiers = require('get-assigned-identifiers') var ast = parse(` var { a, b: [ c,, ...x ], d } = whatever() `) var node = ast.body[0].declarations[0].id getAssignedIdentifiers(node) // → [{ name: 'a' }, { name: 'c' }, { name: 'x' }, { name: 'd' }] ``` ## API ### `getAssignedIdentifiers(node)` Return an array of AST Nodes referencing identifiers that are initialised by the `node`, taking into account destructuring. If `node` is not an identifier or destructuring node, this returns an empty array. ## License [Apache-2.0](LICENSE.md) get-assigned-identifiers-1.2.0/index.js000066400000000000000000000027471323704124700200510ustar00rootroot00000000000000var assert = require('assert') /** * Get a list of all identifiers that are initialised by this (possibly destructuring) * node. * * eg with input: * * var { a: [b, ...c], d } = xyz * * this returns the nodes for 'b', 'c', and 'd' */ module.exports = function getAssignedIdentifiers (node, identifiers) { assert.equal(typeof node, 'object', 'get-assigned-identifiers: node must be object') assert.equal(typeof node.type, 'string', 'get-assigned-identifiers: node must have a type') identifiers = identifiers || [] if (node.type === 'ImportDeclaration') { node.specifiers.forEach(function (el) { getAssignedIdentifiers(el, identifiers) }) } if (node.type === 'ImportDefaultSpecifier' || node.type === 'ImportNamespaceSpecifier' || node.type === 'ImportSpecifier') { node = node.local } if (node.type === 'RestElement') { node = node.argument } if (node.type === 'ArrayPattern') { node.elements.forEach(function (el) { // `el` might be `null` in case of `[x,,y] = whatever` if (el) { getAssignedIdentifiers(el, identifiers) } }) } if (node.type === 'ObjectPattern') { node.properties.forEach(function (prop) { if (prop.type === 'Property') { getAssignedIdentifiers(prop.value, identifiers) } else if (prop.type === 'RestElement') { getAssignedIdentifiers(prop, identifiers) } }) } if (node.type === 'Identifier') { identifiers.push(node) } return identifiers } get-assigned-identifiers-1.2.0/package.json000066400000000000000000000014711323704124700206630ustar00rootroot00000000000000{ "name": "get-assigned-identifiers", "description": "get a list of identifiers that are initialised by a JavaScript AST node.", "version": "1.2.0", "author": "Renée Kooi ", "bugs": { "url": "https://github.com/goto-bus-stop/get-assigned-identifiers/issues" }, "devDependencies": { "acorn": "^5.4.1", "standard": "^10.0.3", "tape": "^4.8.0" }, "homepage": "https://github.com/goto-bus-stop/get-assigned-identifiers", "keywords": [ "ast", "bindings", "destructuring", "identifiers", "javascript", "names", "node" ], "license": "Apache-2.0", "main": "index.js", "repository": { "type": "git", "url": "https://github.com/goto-bus-stop/get-assigned-identifiers.git" }, "scripts": { "test": "standard && tape test/*.js" } } get-assigned-identifiers-1.2.0/test/000077500000000000000000000000001323704124700173515ustar00rootroot00000000000000get-assigned-identifiers-1.2.0/test/index.js000066400000000000000000000062251323704124700210230ustar00rootroot00000000000000var test = require('tape') var assert = require('assert') var parse = require('acorn').parse var getAssignedIdentifiers = require('../') function getName (node) { assert.equal(node.type, 'Identifier', 'Returned node must be an Identifier') return node.name } test('example', function (t) { t.plan(1) var ast = parse(` var { a, b: [ c,, ...x ], d } = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'c', 'x', 'd' ]) }) test('simple identifiers', function (t) { t.plan(1) var ast = parse(` var xyz = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'xyz' ]) }) test('array destructuring', function (t) { t.plan(1) var ast = parse(` var [a, b, c] = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'c' ]) }) test('array destructuring with rest element', function (t) { t.plan(1) var ast = parse(` var [a, b, ...rest] = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'rest' ]) }) test('array destructuring with holes', function (t) { t.plan(1) var ast = parse(` var [a, b,,,,,, boop] = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'boop' ]) }) test('nested array destructuring', function (t) { t.plan(1) var ast = parse(` var [a, [[[b]], ...c], boop] = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'c', 'boop' ]) }) test('object destructuring', function (t) { t.plan(1) var ast = parse(` var {a, b} = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b' ]) }) test('object destructuring with different names', function (t) { t.plan(1) var ast = parse(` var {a: b, b: lol} = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'b', 'lol' ]) }) test('nested object destructuring', function (t) { t.plan(1) var ast = parse(` var {a: {b}, b: lol, c: { d, e: { f: g } }} = whatever() `) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'b', 'lol', 'd', 'g' ]) }) test('object rest destructuring', function (t) { t.plan(1) var ast = parse(` var {a, ...b} = whatever() `, { ecmaVersion: 9 }) var node = ast.body[0].declarations[0].id t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b' ]) }) test('import declarations', function (t) { t.plan(2) var ast = parse(` import x, { y, z as a } from 'module' `, { sourceType: 'module' }) var node = ast.body[0] t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'x', 'y', 'a' ]) ast = parse(` import * as ns from 'module' `, { sourceType: 'module' }) node = ast.body[0] t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'ns' ]) })