Skip to content

Commit

Permalink
Use ESLint markVariableAsUsed method instead of our (now useless) cus…
Browse files Browse the repository at this point in the history
…tom one (fixes #799)
  • Loading branch information
yannickcr committed Sep 12, 2016
1 parent ffd8ea7 commit 3291358
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
3 changes: 1 addition & 2 deletions lib/rules/jsx-uses-react.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

var variableUtil = require('../util/variable');
var pragmaUtil = require('../util/pragma');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -32,7 +31,7 @@ module.exports = {
return {

JSXOpeningElement: function() {
variableUtil.markVariableAsUsed(context, pragma);
context.markVariableAsUsed(pragma);
},

BlockComment: function(node) {
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/jsx-uses-vars.js
Expand Up @@ -4,8 +4,6 @@
*/
'use strict';

var variableUtil = require('../util/variable');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -42,7 +40,7 @@ module.exports = {
return;
}

variableUtil.markVariableAsUsed(context, name);
context.markVariableAsUsed(name);
}

};
Expand Down
38 changes: 1 addition & 37 deletions lib/util/variable.js
Expand Up @@ -4,41 +4,6 @@
*/
'use strict';

/**
* Record that a particular variable has been used in code
*
* @param {Object} context The current rule context.
* @param {String} name The name of the variable to mark as used.
* @returns {Boolean} True if the variable was found and marked as used, false if not.
*/
function markVariableAsUsed(context, name) {
var scope = context.getScope();
var variables;
var i;
var len;
var found = false;

// Special Node.js scope means we need to start one level deeper
if (scope.type === 'global') {
while (scope.childScopes.length) {
scope = scope.childScopes[0];
}
}

do {
variables = scope.variables;
for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
variables[i].eslintUsed = true;
found = true;
}
}
scope = scope.upper;
} while (scope);

return found;
}

/**
* Search a particular variable in a list
* @param {Array} variables The variables list.
Expand Down Expand Up @@ -88,6 +53,5 @@ function variablesInScope(context) {

module.exports = {
findVariable: findVariable,
variablesInScope: variablesInScope,
markVariableAsUsed: markVariableAsUsed
variablesInScope: variablesInScope
};
15 changes: 15 additions & 0 deletions tests/lib/rules/jsx-uses-vars.js
Expand Up @@ -184,6 +184,21 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
}],
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
code: '\
/*eslint jsx-uses-vars:1*/\
import {Hello} from \'Hello\';\
function Greetings() {\
const Hello = require(\'Hello\').default;\
return <Hello />;\
}\
Greetings();',
errors: [{
message: '\'Hello\' is defined but never used.',
line: 1
}],
parser: 'babel-eslint',
parserOptions: parserOptions
}
]
});
Expand Down

0 comments on commit 3291358

Please sign in to comment.