babel-plugin-danger-remove-unused-import
Advanced tools
Comparing version 1.0.4 to 1.0.5
177
lib/index.js
'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
/** | ||
@@ -10,3 +12,2 @@ * @file: index | ||
var getSpecImport = require('./utils/getSpecImport'); | ||
var matchRule = require('./utils/matchRule'); | ||
@@ -21,89 +22,113 @@ var _require = require('../package.json'), | ||
console.warn.apply(console, [symbol + ' Warn...\n'].concat(args)); | ||
console.warn.apply(console, [symbol + ' Warn: \n '].concat(args)); | ||
} | ||
function log() { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
module.exports = function (babel) { | ||
return { | ||
pre: function pre(path) { | ||
this.runtimeData = {}; | ||
}, | ||
console.log.apply(console, [symbol + ' Log: \n '].concat(args)); | ||
} | ||
visitor: { | ||
ImportDeclaration: function ImportDeclaration(path, data) { | ||
var _this = this; | ||
var idTraverseObject = { | ||
Identifier: function Identifier(path, _ref) { | ||
var runtimeData = _ref.runtimeData; | ||
var parentPath = path.parentPath; | ||
var name = path.node.name; | ||
// const ID = 'value'; | ||
data.opts.ignores = []; | ||
var locals = getSpecImport(path, { withPath: true, ignore: data.opts.ignore }); | ||
if (locals) { | ||
locals.forEach(function (pathData, index, all) { | ||
var name = pathData.name; | ||
// already existed | ||
if (_this.runtimeData[name]) { | ||
warn('the declare of ', '`' + name + '`', 'is already existed'); | ||
return; | ||
if (parentPath.isVariableDeclarator() && parentPath.get('id') === path) {} | ||
// { Tabs: 'value' } | ||
else if (parentPath.isLabeledStatement() && parentPath.get('label') === path) {} | ||
// ref.ID | ||
else if (parentPath.isMemberExpression() && parentPath.get('property') === path) {} | ||
// class A { ID() {} } | ||
else if ((parentPath.isClassProperty() || parentPath.isClassMethod()) && parentPath.get('key') === path) {} else { | ||
// used | ||
if (runtimeData[name]) { | ||
delete runtimeData[name]; | ||
} | ||
} | ||
} | ||
}; | ||
_this.runtimeData[name] = { | ||
parent: path, | ||
children: all, | ||
data: pathData | ||
}; | ||
}); | ||
var importTraverseObject = _extends({ | ||
ImportDeclaration: function ImportDeclaration(path, data) { | ||
var _data$opts = data.opts, | ||
opts = _data$opts === undefined ? {} : _data$opts, | ||
runtimeData = data.runtimeData; | ||
path.skip(); | ||
var locals = getSpecImport(path, { withPath: true, ignore: opts.ignore }); | ||
if (locals) { | ||
locals.forEach(function (pathData, index, all) { | ||
var name = pathData.name; | ||
// already existed | ||
if (runtimeData[name]) { | ||
warn('the declare of ', '`' + name + '`', 'is already existed'); | ||
return; | ||
} | ||
}, | ||
Identifier: function Identifier(path, data) { | ||
var parentPath = path.parentPath; | ||
var name = path.node.name; | ||
// const ID = 'value'; | ||
runtimeData[name] = { | ||
parent: path, | ||
children: all, | ||
data: pathData | ||
}; | ||
}); | ||
if (parentPath.isVariableDeclarator() && parentPath.get('id') === path) {} | ||
// { Tabs: 'value' } | ||
else if (parentPath.isLabeledStatement() && parentPath.get('label') === path) {} | ||
// ref.ID | ||
else if (parentPath.isMemberExpression() && parentPath.get('property') === path) {} | ||
// class A { ID() {} } | ||
else if ((parentPath.isClassProperty() || parentPath.isClassMethod()) && parentPath.get('key') === path) {} else { | ||
// used | ||
if (this.runtimeData[name]) { | ||
delete this.runtimeData[name]; | ||
} | ||
} | ||
} | ||
path.skip(); | ||
} | ||
} | ||
}, idTraverseObject); | ||
function handleRemovePath(runtimeData) { | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _opts$verbose = opts.verbose, | ||
verbose = _opts$verbose === undefined ? false : _opts$verbose; | ||
/* | ||
{ | ||
parent: path, | ||
children: [ { path, name } ], | ||
data: { path, name } | ||
} | ||
*/ | ||
var allNames = Object.keys(runtimeData); | ||
verbose && log('unused-import-list', allNames); | ||
allNames.forEach(function (name) { | ||
var _runtimeData$name = runtimeData[name], | ||
children = _runtimeData$name.children, | ||
data = _runtimeData$name.data, | ||
parent = _runtimeData$name.parent; | ||
var childNames = children.map(function (x) { | ||
return x.name; | ||
}); | ||
// every imported identifier is unused | ||
if (childNames.every(function (cName) { | ||
return allNames.includes(cName); | ||
})) { | ||
parent.remove(); | ||
} else { | ||
data.path.remove(); | ||
} | ||
}); | ||
} | ||
module.exports = function (babel) { | ||
return { | ||
pre: function pre(path) { | ||
this.runtimeData = {}; | ||
}, | ||
post: function post(path) { | ||
var _this2 = this; | ||
/* | ||
{ | ||
parent: path, | ||
children: [ { path, name } ], | ||
data: { path, name } | ||
} | ||
*/ | ||
var allNames = Object.keys(this.runtimeData); | ||
// warn(allNames) | ||
allNames.forEach(function (name) { | ||
var _runtimeData$name = _this2.runtimeData[name], | ||
children = _runtimeData$name.children, | ||
data = _runtimeData$name.data, | ||
parent = _runtimeData$name.parent; | ||
var childNames = children.map(function (x) { | ||
return x.name; | ||
visitor: { | ||
Program: function Program(path, data) { | ||
path.traverse(importTraverseObject, { | ||
opts: data.opts, | ||
runtimeData: this.runtimeData | ||
}); | ||
// every imported identifier is unused | ||
if (childNames.every(function (cName) { | ||
return allNames.includes(cName); | ||
})) { | ||
!parent.__removed && parent.remove(); | ||
parent.__removed = true; | ||
} else { | ||
!data.path.__removed && data.path.remove(); | ||
data.path.__removed = true; | ||
} | ||
}); | ||
handleRemovePath(this.runtimeData, data.opts); | ||
} | ||
}, | ||
post: function post() { | ||
delete this.runtimeData; | ||
@@ -110,0 +135,0 @@ } |
{ | ||
"name": "babel-plugin-danger-remove-unused-import", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "", | ||
@@ -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
9261
243