jsx-uses-vars.js
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @fileoverview Prevent variables used in JSX to be marked as unused
* @author Yannick Croissant
*/
'use strict';
const docsUrl = require('../util/docsUrl');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Prevent variables used in JSX to be marked as unused',
category: 'Best Practices',
recommended: true,
url: docsUrl('jsx-uses-vars')
},
schema: []
},
create(context) {
return {
JSXOpeningElement(node) {
let name;
if (node.name.namespace && node.name.namespace.name) {
// <Foo:Bar>
name = node.name.namespace.name;
} else if (node.name.name) {
// <Foo>
name = node.name.name;
} else if (node.name.object) {
// <Foo...Bar>
let parent = node.name.object;
while (parent.object) {
parent = parent.object;
}
name = parent.name;
} else {
return;
}
context.markVariableAsUsed(name);
}
};
}
};