File size: 1,246 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# is-reference
Utility for determining whether an AST node is a reference.
`foo` is a reference in these cases:
```js
console.log( foo );
var foo;
function foo () {}
function bar ( foo ) {}
export { foo as x };
```
`foo` is *not* a reference in these cases:
```js
var obj = { foo: 1 };
console.log( obj.foo );
export { x as foo };
```
In all cases, `foo` is an `Identifier` node, but the two kinds must be treated differently for the purposes of scope analysis etc. (The examples are non-exhaustive.)
## Installation
```bash
npm install is-reference
```
## Usage
Example using [Acorn](https://github.com/ternjs/acorn) and [estree-walker](https://github.com/Rich-Harris/estree-walker):
```js
const { parse } = require( 'acorn' );
const { walk } = require( 'estree-walker' );
const isReference = require( 'is-reference' );
const identifiers = [];
const references = [];
const ast = parse( `var a = b.c;` );
walk( ast, {
enter ( node, parent ) {
if ( node.type === 'Identifier' ) identifiers.push( node );
if ( isReference( node, parent ) ) references.push( node );
}
});
identifiers.forEach( node => console.log( node.name ) ); // a, b, c
references.forEach( node => console.log( node.name ) ); // a, b
```
## License
MIT
|