is-reference.js 1.7 KB

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