Skip to content

Commit 3291358

Browse files
committed
Use ESLint markVariableAsUsed method instead of our (now useless) custom one (fixes #799)
1 parent ffd8ea7 commit 3291358

File tree

4 files changed

+18
-42
lines changed

4 files changed

+18
-42
lines changed

lib/rules/jsx-uses-react.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
var variableUtil = require('../util/variable');
87
var pragmaUtil = require('../util/pragma');
98

109
// ------------------------------------------------------------------------------
@@ -32,7 +31,7 @@ module.exports = {
3231
return {
3332

3433
JSXOpeningElement: function() {
35-
variableUtil.markVariableAsUsed(context, pragma);
34+
context.markVariableAsUsed(pragma);
3635
},
3736

3837
BlockComment: function(node) {

lib/rules/jsx-uses-vars.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
'use strict';
66

7-
var variableUtil = require('../util/variable');
8-
97
// ------------------------------------------------------------------------------
108
// Rule Definition
119
// ------------------------------------------------------------------------------
@@ -42,7 +40,7 @@ module.exports = {
4240
return;
4341
}
4442

45-
variableUtil.markVariableAsUsed(context, name);
43+
context.markVariableAsUsed(name);
4644
}
4745

4846
};

lib/util/variable.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,6 @@
44
*/
55
'use strict';
66

7-
/**
8-
* Record that a particular variable has been used in code
9-
*
10-
* @param {Object} context The current rule context.
11-
* @param {String} name The name of the variable to mark as used.
12-
* @returns {Boolean} True if the variable was found and marked as used, false if not.
13-
*/
14-
function markVariableAsUsed(context, name) {
15-
var scope = context.getScope();
16-
var variables;
17-
var i;
18-
var len;
19-
var found = false;
20-
21-
// Special Node.js scope means we need to start one level deeper
22-
if (scope.type === 'global') {
23-
while (scope.childScopes.length) {
24-
scope = scope.childScopes[0];
25-
}
26-
}
27-
28-
do {
29-
variables = scope.variables;
30-
for (i = 0, len = variables.length; i < len; i++) {
31-
if (variables[i].name === name) {
32-
variables[i].eslintUsed = true;
33-
found = true;
34-
}
35-
}
36-
scope = scope.upper;
37-
} while (scope);
38-
39-
return found;
40-
}
41-
427
/**
438
* Search a particular variable in a list
449
* @param {Array} variables The variables list.
@@ -88,6 +53,5 @@ function variablesInScope(context) {
8853

8954
module.exports = {
9055
findVariable: findVariable,
91-
variablesInScope: variablesInScope,
92-
markVariableAsUsed: markVariableAsUsed
56+
variablesInScope: variablesInScope
9357
};

tests/lib/rules/jsx-uses-vars.js

+15
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
184184
}],
185185
parser: 'babel-eslint',
186186
parserOptions: parserOptions
187+
}, {
188+
code: '\
189+
/*eslint jsx-uses-vars:1*/\
190+
import {Hello} from \'Hello\';\
191+
function Greetings() {\
192+
const Hello = require(\'Hello\').default;\
193+
return <Hello />;\
194+
}\
195+
Greetings();',
196+
errors: [{
197+
message: '\'Hello\' is defined but never used.',
198+
line: 1
199+
}],
200+
parser: 'babel-eslint',
201+
parserOptions: parserOptions
187202
}
188203
]
189204
});

0 commit comments

Comments
 (0)