accessors.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isSupportedAccessor = exports.isStringNode = exports.isIdentifier = exports.getStringValue = exports.getAccessorValue = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. /**
  8. * Checks if the given `node` is a `StringLiteral`.
  9. *
  10. * If a `value` is provided & the `node` is a `StringLiteral`,
  11. * the `value` will be compared to that of the `StringLiteral`.
  12. *
  13. * @param {Node} node
  14. * @param {V} [value]
  15. *
  16. * @return {node is StringLiteral<V>}
  17. *
  18. * @template V
  19. */
  20. const isStringLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.Literal && typeof node.value === 'string' && (value === undefined || node.value === value);
  21. /**
  22. * Checks if the given `node` is a `TemplateLiteral`.
  23. *
  24. * Complex `TemplateLiteral`s are not considered specific, and so will return `false`.
  25. *
  26. * If a `value` is provided & the `node` is a `TemplateLiteral`,
  27. * the `value` will be compared to that of the `TemplateLiteral`.
  28. *
  29. * @param {Node} node
  30. * @param {V} [value]
  31. *
  32. * @return {node is TemplateLiteral<V>}
  33. *
  34. * @template V
  35. */
  36. const isTemplateLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1 && ( // bail out if not simple
  37. value === undefined || node.quasis[0].value.raw === value);
  38. /**
  39. * Checks if the given `node` is a {@link StringNode}.
  40. *
  41. * @param {Node} node
  42. * @param {V} [specifics]
  43. *
  44. * @return {node is StringNode}
  45. *
  46. * @template V
  47. */
  48. const isStringNode = (node, specifics) => isStringLiteral(node, specifics) || isTemplateLiteral(node, specifics);
  49. /**
  50. * Gets the value of the given `StringNode`.
  51. *
  52. * If the `node` is a `TemplateLiteral`, the `raw` value is used;
  53. * otherwise, `value` is returned instead.
  54. *
  55. * @param {StringNode<S>} node
  56. *
  57. * @return {S}
  58. *
  59. * @template S
  60. */
  61. exports.isStringNode = isStringNode;
  62. const getStringValue = node => isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;
  63. /**
  64. * An `Identifier` with a known `name` value - i.e `expect`.
  65. */
  66. exports.getStringValue = getStringValue;
  67. /**
  68. * Checks if the given `node` is an `Identifier`.
  69. *
  70. * If a `name` is provided, & the `node` is an `Identifier`,
  71. * the `name` will be compared to that of the `identifier`.
  72. *
  73. * @param {Node} node
  74. * @param {V} [name]
  75. *
  76. * @return {node is KnownIdentifier<Name>}
  77. *
  78. * @template V
  79. */
  80. const isIdentifier = (node, name) => node.type === _utils.AST_NODE_TYPES.Identifier && (name === undefined || node.name === name);
  81. /**
  82. * Checks if the given `node` is a "supported accessor".
  83. *
  84. * This means that it's a node can be used to access properties,
  85. * and who's "value" can be statically determined.
  86. *
  87. * `MemberExpression` nodes most commonly contain accessors,
  88. * but it's possible for other nodes to contain them.
  89. *
  90. * If a `value` is provided & the `node` is an `AccessorNode`,
  91. * the `value` will be compared to that of the `AccessorNode`.
  92. *
  93. * Note that `value` here refers to the normalised value.
  94. * The property that holds the value is not always called `name`.
  95. *
  96. * @param {Node} node
  97. * @param {V} [value]
  98. *
  99. * @return {node is AccessorNode<V>}
  100. *
  101. * @template V
  102. */
  103. exports.isIdentifier = isIdentifier;
  104. const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStringNode(node, value);
  105. /**
  106. * Gets the value of the given `AccessorNode`,
  107. * account for the different node types.
  108. *
  109. * @param {AccessorNode<S>} accessor
  110. *
  111. * @return {S}
  112. *
  113. * @template S
  114. */
  115. exports.isSupportedAccessor = isSupportedAccessor;
  116. const getAccessorValue = accessor => accessor.type === _utils.AST_NODE_TYPES.Identifier ? accessor.name : getStringValue(accessor);
  117. exports.getAccessorValue = getAccessorValue;