MCPcopy Index your code
hub / github.com/reactjs/react-codemod / createESClass

Function createESClass

transforms/class.js:937–1014  ·  view source on GitHub ↗
(
    name,
    baseClassName,
    staticProperties,
    getInitialState,
    rawProperties,
    comments
  )

Source from the content-addressed store, hash-verified

935 // 2. there's a direct method call `this.x()`, or
936 // 3. `this` is referenced alone
937 const createESClass = (
938 name,
939 baseClassName,
940 staticProperties,
941 getInitialState,
942 rawProperties,
943 comments
944 ) => {
945 const initialStateProperty = [];
946 let maybeConstructor = [];
947 let maybeFlowStateAnnotation = []; // we only need this when we do `this.state = ...`
948
949 if (isInitialStateLiftable(getInitialState)) {
950 if (getInitialState) {
951 initialStateProperty.push(convertInitialStateToClassProperty(getInitialState));
952 }
953 } else {
954 maybeConstructor = createConstructor(getInitialState);
955 if (shouldTransformFlow) {
956 let stateType = j.typeAnnotation(
957 j.existsTypeAnnotation()
958 );
959
960 if (getInitialState.value.returnType) {
961 stateType = getInitialState.value.returnType;
962 }
963
964 maybeFlowStateAnnotation.push(j.classProperty(
965 j.identifier('state'),
966 null,
967 stateType,
968 false
969 ));
970 }
971 }
972
973 const propertiesAndMethods = rawProperties.map(prop => {
974 if (isPrimPropertyWithTypeAnnotation(prop)) {
975 return createClassPropertyWithType(prop);
976 } else if (isPrimProperty(prop)) {
977 return createClassProperty(prop);
978 } else if (AUTOBIND_IGNORE_KEYS.hasOwnProperty(prop.key.name)) {
979 return createMethodDefinition(prop);
980 }
981
982 return createArrowProperty(prop);
983 });
984
985 const flowPropsAnnotation = shouldTransformFlow ?
986 createFlowAnnotationsFromPropTypesProperties(
987 staticProperties.find((path) => path.key.name === 'propTypes')
988 ) :
989 [];
990
991 let finalStaticProperties = staticProperties;
992
993 if (shouldTransformFlow && options['remove-runtime-proptypes']) {
994 finalStaticProperties = staticProperties.filter((prop) => prop.key.name !== 'propTypes');

Callers 1

updateToClassFunction · 0.85

Calls 12

isInitialStateLiftableFunction · 0.85
createConstructorFunction · 0.85
isPrimPropertyFunction · 0.85
createClassPropertyFunction · 0.85
createMethodDefinitionFunction · 0.85
repositionStatePropertyFunction · 0.85
createArrowPropertyFunction · 0.70
withCommentsFunction · 0.70

Tested by

no test coverage detected