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

Function fallbackToCreateClassModule

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

Source from the content-addressed store, hash-verified

1136 };
1137
1138 const fallbackToCreateClassModule = (classPath) => {
1139 const comments = getComments(classPath);
1140 const specPath = ReactUtils.directlyGetCreateClassSpec(classPath);
1141
1142 if (!NO_DISPLAY_NAME) {
1143 if (specPath) {
1144 // Add a displayName property to the spec object
1145 let path = classPath;
1146 let displayName;
1147 while (path && displayName === undefined) {
1148 switch (path.node.type) {
1149 case 'ExportDefaultDeclaration':
1150 displayName = basename(file.path, extname(file.path));
1151 if (displayName === 'index') {
1152 // ./{module name}/index.js
1153 displayName = basename(dirname(file.path));
1154 }
1155 break;
1156 case 'VariableDeclarator':
1157 displayName = path.node.id.name;
1158 break;
1159 case 'AssignmentExpression':
1160 displayName = path.node.left.name;
1161 break;
1162 case 'Property':
1163 displayName = path.node.key.name;
1164 break;
1165 case 'Statement':
1166 displayName = null;
1167 break;
1168 }
1169 path = path.parent;
1170 }
1171 if (displayName) {
1172 addDisplayName(displayName, specPath);
1173 }
1174 }
1175 }
1176
1177 withComments(
1178 j(classPath).replaceWith(
1179 specPath
1180 ? j.callExpression(j.identifier(CREATE_CLASS_VARIABLE_NAME), [specPath])
1181 : j.callExpression(j.identifier(CREATE_CLASS_VARIABLE_NAME), classPath.value.arguments)
1182 ),
1183 {comments}
1184 );
1185 };
1186
1187 if (
1188 options['explicit-require'] === false || ReactUtils.hasReact(root)

Callers 1

class.jsFile · 0.85

Calls 3

getCommentsFunction · 0.85
addDisplayNameFunction · 0.85
withCommentsFunction · 0.70

Tested by

no test coverage detected