-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathjsx-no-undef.js
162 lines (152 loc) · 4.16 KB
/
jsx-no-undef.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @fileoverview Tests for jsx-no-undef
* @author Yannick Croissant
*/
'use strict';
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
const semver = require('semver');
const eslintPkg = require('eslint/package.json');
const RuleTester = require('../../helpers/ruleTester');
const getRuleDefiner = require('../../helpers/getRuleDefiner');
const getESLintCoreRule = require('../../helpers/getESLintCoreRule');
const rule = require('../../../lib/rules/jsx-no-undef');
const parsers = require('../../helpers/parsers');
const parserOptions = {
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
jsxPragma: null,
};
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions });
// In ESLint >= 9, it isn't possible to redefine a core rule, but it isn't necessary anyway since the core rules are certainly already available in the RuleTester/Linter
if (semver.major(eslintPkg.version) < 9) {
const ruleDefiner = getRuleDefiner(ruleTester);
ruleDefiner.defineRule('no-undef', getESLintCoreRule('no-undef'));
}
ruleTester.run('jsx-no-undef', rule, {
valid: parsers.all([
{
code: '/*eslint no-undef:1*/ var React, App; React.render(<App />);',
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<img />);',
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<x-gif />);',
},
{
code: '/*eslint no-undef:1*/ var React, app; React.render(<app.Foo />);',
},
{
code: '/*eslint no-undef:1*/ var React, app; React.render(<app.foo.Bar />);',
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<Apppp:Foo />);',
features: ['jsx namespace'],
},
{
code: `
/*eslint no-undef:1*/
var React;
class Hello extends React.Component {
render() {
return <this.props.tag />
}
}
`,
},
{
code: 'var React; React.render(<Text />);',
globals: {
Text: true,
},
features: ['no-babel'], // TODO: FIXME: remove `no-babel` and fix
},
{
code: `
import Text from "cool-module";
const TextWrapper = function (props) {
return (
<Text />
);
};
`,
parserOptions: Object.assign({ sourceType: 'module' }, parserOptions),
options: [{ allowGlobals: false }],
},
].map(parsers.disableNewTS)),
invalid: parsers.all([
{
code: '/*eslint no-undef:1*/ var React; React.render(<App />);',
errors: [
{
messageId: 'undefined',
data: { identifier: 'App' },
},
],
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<Appp.Foo />);',
errors: [
{
messageId: 'undefined',
data: { identifier: 'Appp' },
},
],
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<appp.Foo />);',
errors: [
{
messageId: 'undefined',
data: { identifier: 'appp' },
},
],
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<appp.foo.Bar />);',
errors: [
{
messageId: 'undefined',
data: { identifier: 'appp' },
},
],
},
{
code: `
const TextWrapper = function (props) {
return (
<Text />
);
};
export default TextWrapper;
`,
parserOptions: Object.assign({ sourceType: 'module' }, parserOptions),
errors: [
{
messageId: 'undefined',
data: { identifier: 'Text' },
},
],
options: [{ allowGlobals: false }],
globals: {
Text: true,
},
},
{
code: '/*eslint no-undef:1*/ var React; React.render(<Foo />);',
errors: [
{
messageId: 'undefined',
data: { identifier: 'Foo' },
},
],
},
].map(parsers.disableNewTS)),
});