normalizeClassDefinition.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { classProperty, inheritsComments } from '@babel/types';
  2. import getMemberExpressionRoot from '../utils/getMemberExpressionRoot.js';
  3. import getMembers from '../utils/getMembers.js';
  4. import { visitors } from '@babel/traverse';
  5. import { ignore } from './traverse.js';
  6. const explodedVisitors = visitors.explode({
  7. Function: { enter: ignore },
  8. Class: { enter: ignore },
  9. Loop: { enter: ignore },
  10. AssignmentExpression(path, state) {
  11. const left = path.get('left');
  12. if (left.isMemberExpression()) {
  13. const first = getMemberExpressionRoot(left);
  14. if (first.isIdentifier({ name: state.variableName })) {
  15. const [member] = getMembers(left);
  16. if (member &&
  17. !member.path.has('computed') &&
  18. !member.path.isPrivateName()) {
  19. const property = classProperty(member.path.node, path.node.right, null, null, false, true);
  20. inheritsComments(property, path.node);
  21. if (path.parentPath.isExpressionStatement()) {
  22. inheritsComments(property, path.parentPath.node);
  23. }
  24. state.classDefinition.get('body').unshiftContainer('body', property);
  25. path.skip();
  26. path.remove();
  27. }
  28. }
  29. }
  30. else {
  31. path.skip();
  32. }
  33. },
  34. });
  35. /**
  36. * Given a class definition (i.e. `class` declaration or expression), this
  37. * function "normalizes" the definition, by looking for assignments of static
  38. * properties and converting them to ClassProperties.
  39. *
  40. * Example:
  41. *
  42. * class MyComponent extends React.Component {
  43. * // ...
  44. * }
  45. * MyComponent.propTypes = { ... };
  46. *
  47. * is converted to
  48. *
  49. * class MyComponent extends React.Component {
  50. * // ...
  51. * static propTypes = { ... };
  52. * }
  53. */
  54. export default function normalizeClassDefinition(classDefinition) {
  55. let variableName;
  56. if (classDefinition.isClassDeclaration()) {
  57. // Class declarations may not have an id, e.g.: `export default class extends React.Component {}`
  58. if (classDefinition.node.id) {
  59. variableName = classDefinition.node.id.name;
  60. }
  61. }
  62. else if (classDefinition.isClassExpression()) {
  63. let parentPath = classDefinition.parentPath;
  64. while (parentPath &&
  65. parentPath.node !== classDefinition.scope.block &&
  66. !parentPath.isBlockStatement()) {
  67. if (parentPath.isVariableDeclarator()) {
  68. const idPath = parentPath.get('id');
  69. if (idPath.isIdentifier()) {
  70. variableName = idPath.node.name;
  71. break;
  72. }
  73. }
  74. else if (parentPath.isAssignmentExpression()) {
  75. const leftPath = parentPath.get('left');
  76. if (leftPath.isIdentifier()) {
  77. variableName = leftPath.node.name;
  78. break;
  79. }
  80. }
  81. parentPath = parentPath.parentPath;
  82. }
  83. }
  84. if (!variableName) {
  85. return;
  86. }
  87. const state = {
  88. variableName,
  89. classDefinition,
  90. };
  91. classDefinition.parentPath.scope.path.traverse(explodedVisitors, state);
  92. }