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

Function createConstructor

transforms/class.js:568–615  ·  view source on GitHub ↗
(getInitialState)

Source from the content-addressed store, hash-verified

566 };
567
568 const createConstructor = (getInitialState) => {
569 const initialStateAST = j(getInitialState);
570 let hasContextAccess = false;
571
572 if (
573 initialStateAST.find(j.MemberExpression, { // has `this.context` access
574 object: {type: 'ThisExpression'},
575 property: {type: 'Identifier', name: 'context'},
576 }).size() ||
577 initialStateAST.find(j.CallExpression, { // a direct method call `this.x()`
578 callee: {
579 type: 'MemberExpression',
580 object: {type: 'ThisExpression'},
581 },
582 }).size() ||
583 initialStateAST.find(j.MemberExpression, { // `this` is referenced alone
584 object: {type: 'ThisExpression'},
585 }).size() !== initialStateAST.find(j.ThisExpression).size()
586 ) {
587 hasContextAccess = true;
588 }
589
590 updatePropsAndContextAccess(getInitialState);
591 const constructorArgs = createConstructorArgs(hasContextAccess);
592
593 return [
594 createMethodDefinition({
595 key: j.identifier('constructor'),
596 value: j.functionExpression(
597 null,
598 constructorArgs,
599 j.blockStatement(
600 [].concat(
601 [
602 j.expressionStatement(
603 j.callExpression(
604 j.identifier('super'),
605 constructorArgs
606 )
607 ),
608 ],
609 inlineGetInitialState(getInitialState)
610 )
611 )
612 ),
613 }),
614 ];
615 };
616
617 const createArrowFunctionExpression = fn => {
618 const arrowFunc = j.arrowFunctionExpression(

Callers 1

createESClassFunction · 0.85

Calls 4

createConstructorArgsFunction · 0.85
createMethodDefinitionFunction · 0.85
inlineGetInitialStateFunction · 0.85

Tested by

no test coverage detected