babel-plugin-mockable-imports
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -8,2 +8,7 @@ # Changelog | ||
## [1.7.1] - 2020-02-18 | ||
- Handle case where identifier referring to import is renamed by another Babel | ||
plugin after the call to `$imports.$add` is generated [#25](https://github.com/robertknight/babel-plugin-mockable-imports/pull/25) | ||
## [1.7.0] - 2020-02-01 | ||
@@ -10,0 +15,0 @@ |
30
index.js
@@ -202,4 +202,12 @@ "use strict"; | ||
enter(path, state) { | ||
// Set of local identifiers which refer to an import. | ||
state.importIdentifiers = new Set(); | ||
// Map associating local identifiers which refer to an import to the | ||
// alias name which was registered with `$imports.$add`. | ||
// | ||
// Other Babel plugins may rename local identifiers which refer to | ||
// imports, so we need to keep track of what name the identifier had | ||
// at the point when the `$imports.$add` call was generated. | ||
// | ||
// When replacing a reference to such an identifier, this enables us | ||
// to generate the correct `$imports.<alias name>` reference. | ||
state.importIdentifiers = new Map(); | ||
@@ -321,3 +329,3 @@ // Flag to keep track of whether further processing of this file has | ||
// not the assignment. | ||
state.importIdentifiers.add(binding.identifier); | ||
state.importIdentifiers.set(binding.identifier, ident.name); | ||
@@ -352,3 +360,3 @@ // The actual import registration via `$imports.$add` however needs to | ||
imports.forEach(({ alias, source, symbol, value }) => { | ||
state.importIdentifiers.add(value); | ||
state.importIdentifiers.set(value, alias); | ||
path.insertAfter(createAddImportCall(alias, source, symbol, value)); | ||
@@ -395,3 +403,3 @@ }); | ||
state.importIdentifiers.add(spec.local); | ||
state.importIdentifiers.set(spec.local, spec.local.name); | ||
path.insertAfter( | ||
@@ -438,3 +446,6 @@ createAddImportCall(spec.local.name, source, imported, spec.local) | ||
// Replace import reference with `$imports.symbol`. | ||
// Replace import reference with `$imports.<alias>`. Note that it is | ||
// important to use the same alias that was registered with `$imports.$add`, | ||
// even if the identifier was subsequently renamed by other Babel plugins. | ||
const alias = state.importIdentifiers.get(binding.identifier); | ||
if ( | ||
@@ -448,3 +459,3 @@ child.parent.type === "JSXOpeningElement" || | ||
t.jsxIdentifier("$imports"), | ||
t.jsxIdentifier(child.node.name) | ||
t.jsxIdentifier(alias) | ||
) | ||
@@ -454,6 +465,3 @@ ); | ||
child.replaceWith( | ||
t.memberExpression( | ||
t.identifier("$imports"), | ||
t.identifier(child.node.name) | ||
) | ||
t.memberExpression(t.identifier("$imports"), t.identifier(alias)) | ||
); | ||
@@ -460,0 +468,0 @@ } |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "Babel plugin for mocking ES imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
41448
600