(name, path, knownProperties, uses)
| 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); |
no test coverage detected