getNameOrValue.js 807 B

12345678910111213141516171819202122232425
  1. import printValue from './printValue.js';
  2. /**
  3. * If node is an Identifier, it returns its name. If it is a literal, it returns
  4. * its value.
  5. */
  6. export default function getNameOrValue(path) {
  7. if (path.isIdentifier()) {
  8. return path.node.name;
  9. }
  10. else if (path.isQualifiedTypeIdentifier() || path.isTSQualifiedName()) {
  11. return printValue(path);
  12. }
  13. else if (path.isStringLiteral() ||
  14. path.isNumericLiteral() ||
  15. path.isBooleanLiteral()) {
  16. return path.node.value;
  17. }
  18. else if (path.isRegExpLiteral()) {
  19. return path.node.pattern;
  20. }
  21. else if (path.isNullLiteral()) {
  22. return null;
  23. }
  24. throw new TypeError(`Argument must be Identifier, Literal, QualifiedTypeIdentifier or TSQualifiedName. Received '${path.node.type}'`);
  25. }