Skip to content

Commit 1e17259

Browse files
committed
Fix jsx-pascal-case to stop checking lower cased components (fixes #329)
1 parent d55b866 commit 1e17259

File tree

3 files changed

+28
-74
lines changed

3 files changed

+28
-74
lines changed

docs/rules/jsx-pascal-case.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@
22

33
Enforces coding style that user-defined JSX components are defined and referenced in PascalCase.
44

5+
Note that since React's JSX uses the upper vs. lower case convention to distinguish between local component classes and HTML tags this rule will not warn on components that stats with a lower case letter.
6+
57
## Rule Details
68

79
The following patterns are considered warnings:
810

911
```js
10-
<testComponent />
12+
<Test_component />
1113
```
1214

1315
```js
14-
<testComponent>
15-
<div />
16-
</testComponent>
16+
<TEST_COMPONENT />
1717
```
1818

19-
```js
20-
<test_component />
21-
```
19+
The following patterns are not considered warnings:
2220

2321
```js
24-
<YELLING />
22+
<div />
2523
```
2624

27-
The following patterns are not considered warnings:
28-
2925
```js
3026
<TestComponent />
3127
```

lib/rules/jsx-pascal-case.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
'use strict';
77

8-
var variableUtil = require('../util/variable');
9-
108
// ------------------------------------------------------------------------------
119
// Constants
1210
// ------------------------------------------------------------------------------
1311

1412
var PASCAL_CASE_REGEX = /^[A-Z0-9]+[a-z0-9]+(?:[A-Z0-9]+[a-z0-9]*)*$/;
13+
var COMPAT_TAG_REGEX = /^[a-z]|\-/;
1514

1615
// ------------------------------------------------------------------------------
1716
// Rule Definition
@@ -21,8 +20,6 @@ module.exports = function(context) {
2120

2221
return {
2322
JSXOpeningElement: function(node) {
24-
var variables = variableUtil.variablesInScope(context);
25-
2623
switch (node.name.type) {
2724
case 'JSXIdentifier':
2825
node = node.name;
@@ -37,10 +34,10 @@ module.exports = function(context) {
3734
break;
3835
}
3936

40-
var isImportedVariable = variableUtil.findVariable(variables, node.name);
4137
var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
38+
var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
4239

43-
if (isImportedVariable && !isPascalCase) {
40+
if (!isPascalCase && !isCompatTag) {
4441
context.report(node, 'Imported JSX component ' + node.name + ' must be in PascalCase');
4542
}
4643
}

tests/lib/rules/jsx-pascal-case.js

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,102 +18,63 @@ var RuleTester = require('eslint').RuleTester;
1818
var ruleTester = new RuleTester();
1919
ruleTester.run('jsx-pascal-case', rule, {
2020
valid: [{
21-
code: [
22-
'var TestComponent;',
23-
'<TestComponent />'
24-
].join('\n'),
21+
code: '<testComponent />',
2522
ecmaFeatures: {
2623
jsx: true
2724
}
2825
}, {
29-
code: [
30-
'var CSSTransitionGroup;',
31-
'<CSSTransitionGroup />'
32-
].join('\n'),
26+
code: '<test_component />',
3327
ecmaFeatures: {
3428
jsx: true
3529
}
3630
}, {
37-
code: [
38-
'var BetterThanCSS;',
39-
'<BetterThanCSS />'
40-
].join('\n'),
31+
code: '<TestComponent />',
4132
ecmaFeatures: {
4233
jsx: true
4334
}
4435
}, {
45-
code: [
46-
'var TestComponent;',
47-
'<TestComponent>',
48-
' <div />',
49-
'</TestComponent>'
50-
].join('\n'),
36+
code: '<CSSTransitionGroup />',
5137
ecmaFeatures: {
5238
jsx: true
5339
}
5440
}, {
55-
code: [
56-
'var Test1Component;',
57-
'<Test1Component />'
58-
].join('\n'),
41+
code: '<BetterThanCSS />',
5942
ecmaFeatures: {
6043
jsx: true
6144
}
6245
}, {
63-
code: [
64-
'var TestComponent1;',
65-
'<TestComponent1 />'
66-
].join('\n'),
46+
code: '<TestComponent><div /></TestComponent>',
6747
ecmaFeatures: {
6848
jsx: true
6949
}
7050
}, {
71-
code: [
72-
'var T3stComp0nent;',
73-
'<T3stComp0nent />'
74-
].join('\n'),
51+
code: '<Test1Component />',
7552
ecmaFeatures: {
7653
jsx: true
7754
}
78-
}],
79-
80-
invalid: [{
81-
code: [
82-
'var testComponent;',
83-
'<testComponent />'
84-
].join('\n'),
55+
}, {
56+
code: '<TestComponent1 />',
8557
ecmaFeatures: {
8658
jsx: true
87-
},
88-
errors: [{message: 'Imported JSX component testComponent must be in PascalCase'}]
59+
}
8960
}, {
90-
code: [
91-
'var test_component;',
92-
'<test_component />'
93-
].join('\n'),
61+
code: '<T3stComp0nent />',
9462
ecmaFeatures: {
9563
jsx: true
96-
},
97-
errors: [{message: 'Imported JSX component test_component must be in PascalCase'}]
98-
}, {
99-
code: [
100-
'var YELLING;',
101-
'<YELLING />'
102-
].join('\n'),
64+
}
65+
}],
66+
67+
invalid: [{
68+
code: '<Test_component />',
10369
ecmaFeatures: {
10470
jsx: true
10571
},
106-
errors: [{message: 'Imported JSX component YELLING must be in PascalCase'}]
72+
errors: [{message: 'Imported JSX component Test_component must be in PascalCase'}]
10773
}, {
108-
code: [
109-
'var testComponent;',
110-
'<testComponent>',
111-
' <div />',
112-
'</testComponent>'
113-
].join('\n'),
74+
code: '<TEST_COMPONENT />',
11475
ecmaFeatures: {
11576
jsx: true
11677
},
117-
errors: [{message: 'Imported JSX component testComponent must be in PascalCase'}]
78+
errors: [{message: 'Imported JSX component TEST_COMPONENT must be in PascalCase'}]
11879
}]
11980
});

0 commit comments

Comments
 (0)