Skip to content

Commit ed21047

Browse files
committed
Add allowAllCaps and ignore options to jsx-pascal-case (fixes #575)
1 parent 4e82b10 commit ed21047

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

docs/rules/jsx-pascal-case.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ The following patterns are not considered warnings:
3636
<CSSTransitionGroup />
3737
```
3838

39+
## Rule Options
40+
41+
```js
42+
...
43+
"jsx-pascal-case": [<enabled>, { allowAllCaps: <allowAllCaps>, ignore: <ignore> }]
44+
...
45+
```
46+
47+
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
48+
* `allowAllCaps`: optional boolean set to `true` to allow components name in all caps (default to `false`).
49+
* `ignore`: optional array of components name to ignore during validation.
50+
3951
## When Not To Use It
4052

4153
If you are not using JSX.

lib/rules/jsx-pascal-case.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111

1212
var PASCAL_CASE_REGEX = /^([A-Z0-9]|[A-Z0-9]+[a-z0-9]+(?:[A-Z0-9]+[a-z0-9]*)*)$/;
1313
var COMPAT_TAG_REGEX = /^[a-z]|\-/;
14+
var ALL_CAPS_TAG_REGEX = /^[A-Z0-9]+$/;
1415

1516
// ------------------------------------------------------------------------------
1617
// Rule Definition
1718
// ------------------------------------------------------------------------------
1819

1920
module.exports = function(context) {
2021

22+
var configuration = context.options[0] || {};
23+
var allowAllCaps = configuration.allowAllCaps || false;
24+
var ignore = configuration.ignore || [];
25+
2126
return {
2227
JSXOpeningElement: function(node) {
2328
switch (node.name.type) {
@@ -36,8 +41,10 @@ module.exports = function(context) {
3641

3742
var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
3843
var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
44+
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(node.name);
45+
var isIgnored = ignore.indexOf(node.name) !== -1;
3946

40-
if (!isPascalCase && !isCompatTag) {
47+
if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
4148
context.report({
4249
node: node,
4350
message: 'Imported JSX component ' + node.name + ' must be in PascalCase'
@@ -47,3 +54,16 @@ module.exports = function(context) {
4754
};
4855

4956
};
57+
58+
module.exports.schema = [{
59+
type: 'object',
60+
properties: {
61+
allowAllCaps: {
62+
type: 'boolean'
63+
},
64+
ignore: {
65+
type: 'array'
66+
}
67+
},
68+
additionalProperties: false
69+
}];

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,17 @@ ruleTester.run('jsx-pascal-case', rule, {
5151
}, {
5252
code: '<T3stComp0nent />',
5353
parserOptions: parserOptions
54-
},
55-
{
54+
}, {
5655
code: '<T />',
5756
parserOptions: parserOptions
57+
}, {
58+
code: '<YMCA />',
59+
parserOptions: parserOptions,
60+
options: [{allowAllCaps: true}]
61+
}, {
62+
code: '<IGNORED />',
63+
parserOptions: parserOptions,
64+
options: [{ignore: ['IGNORED']}]
5865
}],
5966

6067
invalid: [{
@@ -65,5 +72,9 @@ ruleTester.run('jsx-pascal-case', rule, {
6572
code: '<TEST_COMPONENT />',
6673
parserOptions: parserOptions,
6774
errors: [{message: 'Imported JSX component TEST_COMPONENT must be in PascalCase'}]
75+
}, {
76+
code: '<YMCA />',
77+
parserOptions: parserOptions,
78+
errors: [{message: 'Imported JSX component YMCA must be in PascalCase'}]
6879
}]
6980
});

0 commit comments

Comments
 (0)