babel-plugin-filter-imports
Advanced tools
Comparing version 0.1.0 to 0.2.0
60
index.js
@@ -1,9 +0,7 @@ | ||
module.exports = function(filteredModules) { | ||
if (!Array.isArray(filteredModules)) { | ||
filteredModules = [filteredModules]; | ||
} | ||
module.exports = function(filteredImports) { | ||
return function(babel) { | ||
var importDeclarationsToRemove; | ||
var shouldRemoveStack; | ||
// A stack of booleans that determine whether an expression statement | ||
// should be removed as it is exited. Expression statements are removed | ||
// when they contain a reference to a filtered imported. | ||
var shouldRemove; | ||
@@ -13,12 +11,6 @@ return new babel.Transformer('babel-plugin-filter-imports', { | ||
enter: function() { | ||
importDeclarationsToRemove = []; | ||
shouldRemoveStack = []; | ||
shouldRemove = []; | ||
}, | ||
exit: function() { | ||
importDeclarationsToRemove.forEach(function(declaration) { | ||
declaration.dangerouslyRemove(); | ||
}); | ||
importDeclarationsToRemove = undefined; | ||
shouldRemoveStack = undefined; | ||
shouldRemove = undefined; | ||
} | ||
@@ -29,6 +21,6 @@ }, | ||
enter: function() { | ||
shouldRemoveStack.push(false); | ||
shouldRemove.push(false); | ||
}, | ||
exit: function() { | ||
if (shouldRemoveStack.pop()) { | ||
if (shouldRemove.pop()) { | ||
this.dangerouslyRemove(); | ||
@@ -39,18 +31,7 @@ } | ||
ImportDeclaration: function(node) { | ||
var name = node.source.value; | ||
if (filteredModules.indexOf(name) !== -1) { | ||
importDeclarationsToRemove.push(this); | ||
} | ||
}, | ||
Identifier: function(node) { | ||
if (shouldRemoveStack.length > 0) { | ||
var identifier = this; | ||
var referencesImport = filteredModules.some(function(name) { | ||
return identifier.referencesImport(name); | ||
}); | ||
if (referencesImport) { | ||
shouldRemoveStack[shouldRemoveStack.length-1] = true; | ||
Identifier: function() { | ||
// Ensure that we're inside of an expression statement. | ||
if (shouldRemove.length > 0) { | ||
if (referencesFilteredImport(this, filteredImports)) { | ||
shouldRemove[shouldRemove.length - 1] = true; | ||
} | ||
@@ -62,1 +43,14 @@ } | ||
}; | ||
function referencesFilteredImport(identifier, filteredImports) { | ||
for (var moduleName in filteredImports) { | ||
var imports = filteredImports[moduleName]; | ||
for (var i = 0; i < imports.length; i++) { | ||
if (identifier.referencesImport(moduleName, imports[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} |
{ | ||
"name": "babel-plugin-filter-imports", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A babel transform for filtering out imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,1 +0,1 @@ | ||
'use strict'; | ||
var _assert = require('assert'); |
@@ -1,1 +0,1 @@ | ||
'use strict'; | ||
var _assert = require('assert'); |
@@ -1,1 +0,3 @@ | ||
'use strict'; | ||
var _assert = require('assert'); | ||
var a = _assert; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
var _assert = require('assert'); | ||
@@ -3,0 +3,0 @@ function foo(bar) { |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
var _assert = require('assert'); | ||
@@ -3,0 +3,0 @@ { |
@@ -14,3 +14,5 @@ var assert = require('assert'); | ||
var result = babel.transformFileSync(fixturePath, { | ||
plugins: [ filterImports(options) ] | ||
blacklist: ['strict'], | ||
modules: 'commonStrict', | ||
plugins: [filterImports(options)] | ||
}); | ||
@@ -23,8 +25,12 @@ | ||
describe('babel-plugin-filter-imports', function() { | ||
testFixture('default', 'assert'); | ||
testFixture('named', 'assert'); | ||
testFixture('namespace', 'assert'); | ||
testFixture('shadowing', 'assert'); | ||
testFixture('multiple-filters', ['assert', 'cloud']); | ||
testFixture('nesting', ['assert']); | ||
testFixture('default', { assert: ['default'] }); | ||
testFixture('named', { assert: ['a'] }); | ||
testFixture('namespace', { assert: ['*'] }); | ||
testFixture('shadowing', { assert: ['default'] }); | ||
testFixture('nesting', { assert: ['default'] }); | ||
testFixture('partial-filter-1', { assert: ['default'], cloud: ['default'] }); | ||
testFixture('partial-filter-2', { assert: ['a', 'c'] }); | ||
testFixture('partial-filter-3', { assert: ['a', 'c'] }); | ||
}); |
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
4695
22
127