get-property-name.js 863 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const {getStaticValue} = require('eslint-utils');
  3. /**
  4. Get the property value of a `MemberExpression` node.
  5. @param {Node} node - The `MemberExpression` node.
  6. @param {Scope} [scope] - The scope to start finding the variable. Optional. If this scope was given, it tries to resolve identifier references which are in the given node as much as possible.
  7. */
  8. function getPropertyName(node, scope) {
  9. const {type, property, computed} = node;
  10. /* istanbul ignore next */
  11. if (type !== 'MemberExpression') {
  12. return;
  13. }
  14. if (!computed) {
  15. if (property.type === 'Identifier') {
  16. return property.name;
  17. }
  18. /* istanbul ignore next: It could be `PrivateIdentifier`(ESTree) or `PrivateName`(Babel) when it's in `class` */
  19. return;
  20. }
  21. const result = getStaticValue(property, scope);
  22. return result && result.value;
  23. }
  24. module.exports = getPropertyName;