UNPKG

1.76 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global = global || self, global.isReference = factory());
5}(this, (function () { 'use strict';
6
7 function isReference(node, parent) {
8 if (node.type === 'MemberExpression') {
9 return !node.computed && isReference(node.object, node);
10 }
11 if (node.type === 'Identifier') {
12 if (!parent)
13 return true;
14 switch (parent.type) {
15 // disregard `bar` in `foo.bar`
16 case 'MemberExpression': return parent.computed || node === parent.object;
17 // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
18 case 'MethodDefinition': return parent.computed;
19 // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
20 case 'FieldDefinition': return parent.computed || node === parent.value;
21 // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
22 case 'Property': return parent.computed || node === parent.value;
23 // disregard the `bar` in `export { foo as bar }` or
24 // the foo in `import { foo as bar }`
25 case 'ExportSpecifier':
26 case 'ImportSpecifier': return node === parent.local;
27 // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
28 case 'LabeledStatement':
29 case 'BreakStatement':
30 case 'ContinueStatement': return false;
31 default: return true;
32 }
33 }
34 return false;
35 }
36
37 return isReference;
38
39})));