UNPKG

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