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

Function emitImport

transforms/react-to-react-dom.js:355–385  ·  view source on GitHub ↗
(name, path, knownProperties, uses)

Source from the content-addressed store, hash-verified

353 }
354
355 function emitImport(name, path, knownProperties, uses) {
356 const usedProperties = coreImportDeclaration.value.specifiers.filter(
357 sp => sp.type === j.ImportSpecifier.name
358 ).filter(sp =>
359 sp.imported.type === 'Identifier' && knownProperties.indexOf(sp.imported.name) !== -1
360 ).map(sp => sp.imported.name);
361
362 const importDeclaration = findImportPath(name, path);
363 const specifiers = [];
364
365 // if ReactDOM needs to be in scope and it's not declared, or we're going to
366 // replace its declaration to add import specifiers...
367 if (uses > 0 && (!domAlreadyDeclared || importDeclaration.length > 0)) {
368 specifiers.push(j.importDefaultSpecifier(j.identifier(name)));
369 }
370 if (usedProperties.length > 0) {
371 j(coreImportDeclaration).find(j.ImportSpecifier)
372 .filter(p => usedProperties.indexOf(p.value.local.name) !== -1)
373 .remove();
374 specifiers.push(...usedProperties.map(prop =>
375 j.importSpecifier(j.identifier(prop))
376 ));
377 }
378 if (specifiers.length > 0) {
379 if (importDeclaration.length > 0) {
380 importDeclaration.replaceWith(j.importDeclaration(specifiers, j.literal(path)));
381 } else {
382 coreImportDeclaration.insertAfter(j.importDeclaration(specifiers, j.literal(path)));
383 }
384 }
385 }
386
387 emitImport('ReactDOM', domModuleName, DOM_PROPERTIES, domUses);
388 emitImport('ReactDOMServer', domServerModuleName, DOM_SERVER_PROPERTIES, domServerUses);

Callers 1

Calls 1

findImportPathFunction · 0.85

Tested by

no test coverage detected