babel-plugin-mockable-imports
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -8,2 +8,7 @@ # Changelog | ||
## [1.3.0] - 2019-04-11 | ||
- Support CommonJS imports with separate variable declaration and | ||
initialization (#5) | ||
## [1.2.0] - 2019-04-11 | ||
@@ -10,0 +15,0 @@ |
56
index.js
@@ -23,3 +23,3 @@ 'use strict'; | ||
*/ | ||
function commomJSRequireSource(node) { | ||
function commonJSRequireSource(node) { | ||
const args = node.arguments; | ||
@@ -113,3 +113,3 @@ if ( | ||
} | ||
const source = commomJSRequireSource(init); | ||
const source = commonJSRequireSource(init); | ||
if (!source) { | ||
@@ -242,6 +242,54 @@ return; | ||
AssignmentExpression(path, state) { | ||
if (!isCommonJSExportAssignment(path)) { | ||
if (state.aborted) { | ||
return false; | ||
} | ||
// Skip assignments that are not at the top level. | ||
if (path.parentPath.parent.type !== 'Program') { | ||
return; | ||
} | ||
state.hasCommonJSExportAssignment = true; | ||
// Track whether a `module.exports =` assignment was seen at the top-level | ||
// of the file. | ||
if (isCommonJSExportAssignment(path)) { | ||
state.hasCommonJSExportAssignment = true; | ||
} | ||
// Handle Common JS imports where the variable declaration and | ||
// initialization are separate. ie. `var foo; foo = require("./foo")`. | ||
// | ||
// Currently there is no support for destructuring here or anything on | ||
// the right side of the assignment other than the `require` call. | ||
if (!t.isIdentifier(path.node.left) || !t.isCallExpression(path.node.right)) { | ||
return; | ||
} | ||
const ident = path.node.left; | ||
const callExpr = path.node.right; | ||
const source = commonJSRequireSource(callExpr); | ||
if (!source) { | ||
return; | ||
} | ||
// Look up the original identifier node that introduced this binding. | ||
const binding = path.scope.getBinding(ident.name, /* noGlobal */ true); | ||
if (!binding) { | ||
return; | ||
} | ||
// Register the import. Note that this needs to refer to the identifier | ||
// in the `var` declaration, *not* this assignment. This is because | ||
// when looking up the binding when processing later references to the | ||
// identifier, the binding will refer back to the `var` declaration, | ||
// not the assignment. | ||
state.importMeta.set(binding.identifier, { | ||
symbol: '<CJS>', | ||
source, | ||
value: binding.identifier, | ||
}); | ||
// The actual import registration via `$imports.$add` however needs to | ||
// be placed after the assignment. | ||
path.insertAfter( | ||
createAddImportCall(ident.name, source, '<CJS>', ident) | ||
); | ||
}, | ||
@@ -248,0 +296,0 @@ |
{ | ||
"name": "babel-plugin-mockable-imports", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Babel plugin for mocking ES imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -243,3 +243,11 @@ # babel-plugin-mockable-imports | ||
- Is tied to Node and Browserify | ||
- Works only with `require` calls, rather than handling `import` declarations | ||
"natively". This can cause issues such as Babel's transformation of `import` | ||
breaking proxyquireify's ability to recognize `proxyquire` calls. | ||
Additionally because this plugin adds metadata to modules about their imports, | ||
it can provide helpful warnings at runtime if a mock is provided which doesn't | ||
match the imported symbols, eg. due to an unnecessary mock or a typo in the | ||
module path or symbol name. | ||
There is another Babel plugin, | ||
@@ -246,0 +254,0 @@ [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire) which |
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
31682
512
269