jsx-uses-vars.js
1.49 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
52
53
54
55
56
57
58
59
60
/**
* @fileoverview Prevent variables used in JSX to be marked as unused
* @author Yannick Croissant
*/
'use strict';
const docsUrl = require('../util/docsUrl');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const isTagNameRe = /^[a-z]/;
const isTagName = (name) => isTagNameRe.test(name);
module.exports = {
// eslint-disable-next-line eslint-plugin/prefer-message-ids -- https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues/292
meta: {
docs: {
description: 'Disallow variables used in JSX to be incorrectly 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) {
// <Foo:Bar>
return;
}
if (node.name.name) {
// <Foo>
name = node.name.name;
// Exclude lowercase tag names like <div>
if (isTagName(name)) {
return;
}
} 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);
},
};
},
};