babel-plugin-danger-remove-unused-import
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -0,1 +1,10 @@ | ||
# [2.0.0](https://github.com/imcuttle/babel-plugin-danger-remove-unused-import/compare/v1.1.2...v2.0.0) (2019-09-09) | ||
### Features | ||
* **core:** use `scope` for more smarter removing ([d6befc6](https://github.com/imcuttle/babel-plugin-danger-remove-unused-import/commit/d6befc6)), closes [#4](https://github.com/imcuttle/babel-plugin-danger-remove-unused-import/issues/4) | ||
## [1.1.2](https://github.com/imcuttle/babel-plugin-danger-remove-unused-import/compare/v1.1.1...v1.1.2) (2019-03-13) | ||
@@ -2,0 +11,0 @@ |
187
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; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
@@ -11,153 +11,54 @@ /** | ||
*/ | ||
var getSpecImport = require('./utils/getSpecImport'); | ||
var match = require('./utils/matchRule'); | ||
var _require = require('../package.json'), | ||
symbol = _require.name; | ||
module.exports = function (babel) { | ||
var t = babel.types; | ||
return { | ||
visitor: { | ||
Program: { | ||
enter: function enter(path) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
opts = _ref.opts; | ||
function warn() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
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]; | ||
} | ||
try { | ||
for (var _iterator = Object.entries(path.scope.bindings)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _step$value = _slicedToArray(_step.value, 2), | ||
name = _step$value[0], | ||
binding = _step$value[1]; | ||
console.log.apply(console, [symbol + ' Log: \n '].concat(args)); | ||
} | ||
var idTraverseObject = { | ||
JSXIdentifier: function JSXIdentifier(path, _ref) { | ||
var runtimeData = _ref.runtimeData; | ||
var parentPath = path.parentPath; | ||
var name = path.node.name; | ||
if (parentPath.isJSXOpeningElement() && parentPath.get('name') === path || parentPath.isJSXMemberExpression() && parentPath.get('object') === path) { | ||
if (runtimeData[name]) { | ||
delete runtimeData[name]; | ||
} | ||
} | ||
}, | ||
Identifier: function Identifier(path, _ref2) { | ||
var runtimeData = _ref2.runtimeData; | ||
var parentPath = path.parentPath, | ||
scope = path.scope; | ||
var name = path.node.name; | ||
// const ID = 'value'; | ||
if (parentPath.isVariableDeclarator() && parentPath.get('id') === path) {} | ||
// const x = { Tabs: 'value' } | ||
else if (parentPath.isObjectProperty() && parentPath.get('key') === path && | ||
// const x = { [Tabs]: 'value' } | ||
!parentPath.node.computed) {} | ||
// { Tabs: 'value' } | ||
else if (parentPath.isLabeledStatement() && parentPath.get('label') === path) {} | ||
// ref.ID | ||
else if (parentPath.isMemberExpression() && parentPath.get('property') === path && parentPath.node.computed === false) {} | ||
// class A { ID() {} } | ||
else if ((parentPath.isClassProperty() || parentPath.isClassMethod()) && parentPath.get('key') === path && | ||
// class A { [ID]() {} } | ||
!parentPath.node.computed) {} else { | ||
// used | ||
if (runtimeData[name]) { | ||
delete runtimeData[name]; | ||
if (binding.kind === 'module') { | ||
if (!binding.referenced) { | ||
var source = binding.path.parentPath.get('source'); | ||
if (t.isStringLiteral(source) && (!opts.ignore || !match(opts.ignore, source.node.value))) { | ||
if (binding.path.node.type === 'ImportSpecifier') { | ||
binding.path.remove(); | ||
} else if (binding.path.parentPath) { | ||
binding.path.parentPath.remove(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
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.hasOwnProperty(name)) { | ||
warn('the declare of ', '`' + name + '`', 'is already existed'); | ||
return; | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
runtimeData[name] = { | ||
parent: path, | ||
children: all, | ||
data: pathData | ||
}; | ||
}); | ||
} | ||
} | ||
}, 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.__removed && parent.remove(); | ||
parent.__removed = true; | ||
} else { | ||
var path = data.path; | ||
!path.__removed && path.remove(); | ||
path.__removed = true; | ||
} | ||
}); | ||
} | ||
module.exports = function (babel) { | ||
return { | ||
pre: function pre(path) { | ||
this.runtimeData = {}; | ||
}, | ||
visitor: { | ||
Program: function Program(path, data) { | ||
// path.skip() | ||
path.traverse(importTraverseObject, { | ||
opts: data.opts, | ||
runtimeData: this.runtimeData | ||
}); | ||
handleRemovePath(this.runtimeData, data.opts); | ||
} | ||
}, | ||
post: function post() { | ||
delete this.runtimeData; | ||
} | ||
}; | ||
}; | ||
// expose internals for use in other plugins | ||
module.exports.importTraverseObject = importTraverseObject; | ||
module.exports.handleRemovePath = handleRemovePath; | ||
}; |
{ | ||
"name": "babel-plugin-danger-remove-unused-import", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
10430
7
81
1