Comparing version 1.1.1 to 1.2.0
'use strict'; | ||
const onlyDeclared = ({count}) => count === 1; | ||
const onlyDeclared = ({count, called}) => count === 1 || called; | ||
module.exports = (items) => items.filter(onlyDeclared); | ||
module.exports = (items) => { | ||
return items | ||
.filter(onlyDeclared); | ||
}; | ||
@@ -9,2 +9,4 @@ | ||
const {isCallExpression} = require('@babel/types'); | ||
module.exports = (ast, opts = {}) => { | ||
@@ -23,10 +25,6 @@ const vars = []; | ||
FunctionDeclaration(path) { | ||
initialize({path, returnPath, vars}); | ||
}, | ||
Identifier(path) { | ||
const { | ||
node, | ||
scope, | ||
parent, | ||
} = path; | ||
@@ -36,6 +34,10 @@ | ||
const called = isCallExpression(parent); | ||
inc({ | ||
name, | ||
scope, | ||
path, | ||
vars, | ||
called, | ||
returnPath, | ||
}); | ||
@@ -49,3 +51,3 @@ }, | ||
function getName(node) { | ||
return node.id.name; | ||
return node.name || node.id.name; | ||
} | ||
@@ -90,6 +92,5 @@ | ||
function traverseObjectProps({path, vars}) { | ||
function traverseObjectProps({path, vars, returnPath}) { | ||
const { | ||
node, | ||
scope, | ||
} = path; | ||
@@ -105,4 +106,5 @@ | ||
name, | ||
scope, | ||
path, | ||
vars, | ||
returnPath, | ||
}); | ||
@@ -162,9 +164,21 @@ } | ||
function inc({name, scope, vars}) { | ||
function inc({name, path, vars, called, returnPath}) { | ||
const {scope} = path; | ||
const scopeNumber = getScopeNumber(name, scope); | ||
if (!vars[scopeNumber] || !vars[scopeNumber][name]) | ||
return; | ||
if (!vars[scopeNumber]) | ||
vars[scopeNumber] = {}; | ||
if (!vars[scopeNumber][name]) | ||
initialize({ | ||
path, | ||
scope, | ||
vars, | ||
returnPath, | ||
}); | ||
vars[scopeNumber][name].count += 1; | ||
if (called) | ||
vars[scopeNumber][name].called = called; | ||
} | ||
@@ -171,0 +185,0 @@ |
@@ -21,2 +21,3 @@ 'use strict'; | ||
removeUnused(unused); | ||
const {code} = print(ast); | ||
@@ -23,0 +24,0 @@ |
{ | ||
"name": "putout", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
@@ -25,2 +25,3 @@ "description": "find and remove unused variables", | ||
"@babel/traverse": "^7.1.6", | ||
"@babel/types": "^7.2.0", | ||
"chalk": "^2.4.1", | ||
@@ -27,0 +28,0 @@ "recast": "^0.16.1" |
Sorry, the diff of this file is not supported yet
10017
228
4
+ Added@babel/types@^7.2.0