getParameterName.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  8. var _default = function _default(identifierNode, context) {
  9. if (_lodash["default"].has(identifierNode, 'name')) {
  10. return identifierNode.name;
  11. }
  12. if (_lodash["default"].has(identifierNode, 'left.name')) {
  13. return identifierNode.left.name;
  14. }
  15. if (_lodash["default"].has(identifierNode, 'key.name')) {
  16. return identifierNode.key.name;
  17. }
  18. if (identifierNode.type === 'RestElement') {
  19. return identifierNode.argument.name;
  20. }
  21. if (identifierNode.type === 'ObjectTypeProperty') {
  22. var tokenIndex;
  23. tokenIndex = 0;
  24. if (identifierNode["static"]) {
  25. tokenIndex += 1;
  26. }
  27. if (identifierNode.variance) {
  28. tokenIndex += 1;
  29. }
  30. if (identifierNode.kind === 'set' || identifierNode.kind === 'get') {
  31. tokenIndex += 1;
  32. }
  33. return context.getSourceCode().getFirstToken(identifierNode, tokenIndex).value;
  34. }
  35. if (identifierNode.type === 'ObjectTypeIndexer') {
  36. var _tokenIndex;
  37. _tokenIndex = 0;
  38. if (identifierNode["static"]) {
  39. _tokenIndex += 1;
  40. }
  41. if (identifierNode.variance) {
  42. _tokenIndex += 1;
  43. }
  44. _tokenIndex += 1;
  45. var id = context.getSourceCode().getFirstToken(identifierNode, _tokenIndex);
  46. var colonOrBrace = context.getSourceCode().getTokenAfter(id);
  47. if (colonOrBrace.value === ':') {
  48. return id.value;
  49. }
  50. return null;
  51. }
  52. if (identifierNode.type === 'FunctionTypeParam') {
  53. return context.getSourceCode().getFirstToken(identifierNode).value;
  54. }
  55. if (identifierNode.type === 'ObjectPattern' || identifierNode.type === 'ArrayPattern') {
  56. var text = context.getSourceCode().getText(identifierNode);
  57. if (identifierNode.typeAnnotation) {
  58. return text.replace(context.getSourceCode().getText(identifierNode.typeAnnotation), '').trim();
  59. }
  60. return text;
  61. }
  62. if (_lodash["default"].get(identifierNode, 'left.type') === 'ObjectPattern') {
  63. return context.getSourceCode().getText(identifierNode.left);
  64. }
  65. return null;
  66. };
  67. exports["default"] = _default;
  68. module.exports = exports.default;