TSNonNullExpression.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = extractValueFromTSNonNullExpression;
  6. var extractValueFromThisExpression = require('./ThisExpression').default;
  7. /**
  8. * Extractor function for a TSNonNullExpression type value node.
  9. * A TSNonNullExpression is accessing a TypeScript Non-Null Assertion
  10. * Operator !
  11. *
  12. * @param - value - AST Value object with type `TSNonNullExpression`
  13. * @returns - The extracted value converted to correct type
  14. * and maintaing `obj.property` convention.
  15. */
  16. function extractValueFromTSNonNullExpression(value) {
  17. // eslint-disable-next-line global-require
  18. // const getValue = require('.').default;
  19. var errorMessage = 'The prop value with an expression type of TSNonNullExpression could not be resolved. Please file issue to get this fixed immediately.';
  20. // it's just the name
  21. if (value.type === 'Identifier') {
  22. var name = value.name;
  23. return name;
  24. }
  25. if (value.type === 'ThisExpression') {
  26. return extractValueFromThisExpression();
  27. }
  28. // does not contains properties & is not parenthesized
  29. if (value.type === 'TSNonNullExpression' && (!value.extra || value.extra.parenthesized === false)) {
  30. var expression = value.expression;
  31. return extractValueFromTSNonNullExpression(expression) + '!';
  32. }
  33. // does not contains properties & is parenthesized
  34. if (value.type === 'TSNonNullExpression' && value.extra && value.extra.parenthesized === true) {
  35. var _expression = value.expression;
  36. return '(' + extractValueFromTSNonNullExpression(_expression) + '!' + ')';
  37. }
  38. // contains a property & is not parenthesized
  39. if (value.type === 'MemberExpression' && (!value.extra || value.extra.parenthesized === false)) {
  40. return '' + extractValueFromTSNonNullExpression(value.object) + (value.optional ? '?.' : '.') + extractValueFromTSNonNullExpression(value.property);
  41. }
  42. // contains a property & is parenthesized
  43. if (value.type === 'MemberExpression' && value.extra && value.extra.parenthesized === true) {
  44. return '(' + extractValueFromTSNonNullExpression(value.object) + (value.optional ? '?.' : '.') + extractValueFromTSNonNullExpression(value.property) + ')';
  45. }
  46. // try to fail silently, if specs for TSNonNullExpression change
  47. // not throw, only log error. Similar to how it was done previously
  48. if (value.expression) {
  49. var _expression2 = value.expression;
  50. while (_expression2) {
  51. if (_expression2.type === 'Identifier') {
  52. // eslint-disable-next-line no-console
  53. console.error(errorMessage);
  54. return _expression2.name;
  55. }
  56. var _expression3 = _expression2;
  57. _expression2 = _expression3.expression;
  58. }
  59. }
  60. // eslint-disable-next-line no-console
  61. console.error(errorMessage);
  62. return '';
  63. }