Comparing version 3.1.0 to 3.1.1
@@ -72,3 +72,3 @@ { | ||
}, | ||
"version": "3.1.0" | ||
"version": "3.1.1" | ||
} |
@@ -151,4 +151,7 @@ import * as t from 'babel-types'; | ||
let collectedDefaultImportNames = []; | ||
let firstUnsafeLocation = getFirstUnsafeLocation(path, ['require', ...safeFunctionIdentifiers]); | ||
body.forEach(statement => rewriteAsImport(statement, module, firstUnsafeLocation)); | ||
body.forEach(statement => | ||
rewriteAsImport(statement, module, firstUnsafeLocation, collectedDefaultImportNames)); | ||
removeDefaultAccesses(path, module, collectedDefaultImportNames); | ||
} | ||
@@ -651,3 +654,9 @@ | ||
function rewriteAsImport(path: Path, module: Module, firstUnsafeLocation: number): boolean { | ||
/** | ||
* Rewrite this potential import statement, considering various import styles. | ||
* Any new default import names are added to collectedDefaultImportNames. | ||
*/ | ||
function rewriteAsImport( | ||
path: Path, module: Module, firstUnsafeLocation: number, | ||
collectedDefaultImportNames: Array<string>): boolean { | ||
if (isDeclaredName(path.scope, 'require')) { | ||
@@ -662,3 +671,3 @@ return false; | ||
return ( | ||
rewriteSingleExportRequire(path, module) || | ||
rewriteSingleExportRequire(path, module, collectedDefaultImportNames) || | ||
rewriteNamedExportRequire(path, module) || | ||
@@ -670,3 +679,12 @@ rewriteDeconstructedImportRequire(path, module) || | ||
function rewriteSingleExportRequire(path: Path, module: Module): boolean { | ||
/** | ||
* Convert | ||
* let a = require('b'); | ||
* to | ||
* import a from 'b'; | ||
* | ||
* Any imported names are added to the collectedDefaultImportNames parameter. | ||
*/ | ||
function rewriteSingleExportRequire( | ||
path: Path, module: Module, collectedDefaultImportNames: Array<string>): boolean { | ||
let { node } = path; | ||
@@ -727,5 +745,12 @@ | ||
collectedDefaultImportNames.push(...extractableDeclarations.map(d => d.id.name)); | ||
return true; | ||
} | ||
/** | ||
* Convert | ||
* let a = require('b').c; | ||
* to | ||
* import { c as a } from 'b'; | ||
*/ | ||
function rewriteNamedExportRequire(path: Path, module: Module): boolean { | ||
@@ -772,2 +797,8 @@ let declaration = extractSingleDeclaration(path.node); | ||
/** | ||
* Convert | ||
* let { a } = require('b'); | ||
* to | ||
* import { a } from 'b'; | ||
*/ | ||
function rewriteDeconstructedImportRequire(path: Path, module: Module): boolean { | ||
@@ -816,2 +847,8 @@ let declaration = extractSingleDeclaration(path.node); | ||
/** | ||
* Convert | ||
* require('a'); | ||
* to | ||
* import 'a'; | ||
*/ | ||
function rewriteSideEffectRequire(path: Path, module: Module): boolean { | ||
@@ -876,2 +913,29 @@ let { node } = path; | ||
/** | ||
* Remove `.default` accesses on names that are known to be default imports. | ||
* For example, if `let a = require('a');` became `import a from 'a';`, then | ||
* any usage of `a.default` should change to just `a`. | ||
* | ||
* Note that this isn't 100% correct, and being fully correct here is | ||
* undecidable, but it seems good enough for real-world cases. | ||
*/ | ||
function removeDefaultAccesses(programPath: Path, module: Module, defaultImportNames: Array<string>) { | ||
programPath.traverse({ | ||
MemberExpression(path: Path) { | ||
let {object, property, computed} = path.node; | ||
if (computed) { | ||
return; | ||
} | ||
if (!computed && | ||
t.isIdentifier(object) && | ||
defaultImportNames.indexOf(object.name) !== -1 && | ||
t.isIdentifier(property) && | ||
property.name === 'default') { | ||
let dotToken = findToken('.', module.tokensInRange(object.end, path.node.end)); | ||
module.magicString.remove(dotToken.token.start, property.end); | ||
} | ||
} | ||
}) | ||
} | ||
type ImportMetadata = { | ||
@@ -878,0 +942,0 @@ type: string, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
269252
7947