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

Function updateToClass

transforms/class.js:1076–1119  ·  view source on GitHub ↗
(classPath)

Source from the content-addressed store, hash-verified

1074 });
1075
1076 const updateToClass = (classPath) => {
1077 const specPath = ReactUtils.directlyGetCreateClassSpec(classPath);
1078 const name = ReactUtils.directlyGetComponentName(classPath);
1079 const statics = collectStatics(specPath);
1080 const properties = collectNonStaticProperties(specPath);
1081 const comments = getComments(classPath);
1082
1083 const getInitialState = findGetInitialState(specPath);
1084
1085 var path = classPath;
1086
1087 if (
1088 classPath.parentPath &&
1089 classPath.parentPath.value &&
1090 classPath.parentPath.value.type === 'VariableDeclarator'
1091 ) {
1092 // the reason that we need to do this awkward dance here is that
1093 // for things like `var Foo = React.createClass({...})`, we need to
1094 // replace the _entire_ VariableDeclaration with
1095 // `class Foo extends React.Component {...}`.
1096 // it looks scary but since we already know it's a VariableDeclarator
1097 // it's actually safe.
1098 // (VariableDeclaration > declarations > VariableDeclarator > CallExpression)
1099 path = classPath.parentPath.parentPath.parentPath;
1100 }
1101
1102 const staticProperties = createStaticClassProperties(statics);
1103 const baseClassName =
1104 pureRenderMixinPathAndBinding &&
1105 ReactUtils.directlyHasSpecificMixins(classPath, [pureRenderMixinPathAndBinding.binding]) ?
1106 'PureComponent' :
1107 'Component';
1108
1109 j(path).replaceWith(
1110 createESClass(
1111 name,
1112 baseClassName,
1113 staticProperties,
1114 getInitialState,
1115 properties,
1116 comments
1117 )
1118 );
1119 };
1120
1121 const addDisplayName = (displayName, specPath) => {
1122 const props = specPath.properties;

Callers 1

class.jsFile · 0.85

Calls 6

collectStaticsFunction · 0.85
getCommentsFunction · 0.85
findGetInitialStateFunction · 0.85
createESClassFunction · 0.85

Tested by

no test coverage detected