Initial commit

This commit is contained in:
Spencer Pincott
2024-07-15 22:20:13 -04:00
commit 97737ca1ae
16618 changed files with 934131 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
language: node_js
node_js:
- 8
- 6
- 4
cache:
directories:
- ~/.npm

View File

@@ -0,0 +1,17 @@
# 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

View File

@@ -0,0 +1,15 @@
# [Apache License 2.0](https://spdx.org/licenses/Apache-2.0)
Copyright 2017 Renée Kooi <renee@kooi.me>
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.

View File

@@ -0,0 +1,45 @@
# 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)

View File

@@ -0,0 +1,57 @@
var 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
}

View File

@@ -0,0 +1,33 @@
{
"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 <renee@kooi.me>",
"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"
}
}

View File

@@ -0,0 +1,122 @@
var 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' ])
})