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

Function repositionStateProperty

transforms/class.js:891–925  ·  view source on GitHub ↗
(initialStateProperty, propertiesAndMethods)

Source from the content-addressed store, hash-verified

889
890 // to ensure that our property initializers' evaluation order is safe
891 const repositionStateProperty = (initialStateProperty, propertiesAndMethods) => {
892 const initialStateCollection = j(initialStateProperty);
893 const thisCount = initialStateCollection.find(j.ThisExpression).size();
894 const safeThisMemberCount = initialStateCollection.find(j.MemberExpression, {
895 object: {
896 type: 'ThisExpression',
897 },
898 property: {
899 type: 'Identifier',
900 name: 'props',
901 },
902 }).size() + initialStateCollection.find(j.MemberExpression, {
903 object: {
904 type: 'ThisExpression',
905 },
906 property: {
907 type: 'Identifier',
908 name: 'context',
909 },
910 }).size();
911
912 if (thisCount === safeThisMemberCount) {
913 return initialStateProperty.concat(propertiesAndMethods);
914 }
915
916 const result = [].concat(propertiesAndMethods);
917 let lastPropPosition = result.length - 1;
918
919 while (lastPropPosition >= 0 && result[lastPropPosition].kind === 'method') {
920 lastPropPosition--;
921 }
922
923 result.splice(lastPropPosition + 1, 0, initialStateProperty[0]);
924 return result;
925 };
926
927 // if there's no `getInitialState` or the `getInitialState` function is simple
928 // (i.e., it's just a return statement) then we don't need a constructor.

Callers 1

createESClassFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected