Skip to content

Commit f6fd9d4

Browse files
Tyler Swavelyljharb
Tyler Swavely
authored andcommitted
[New] jsx-pascal-case: allowAllCaps option now allows SCREAMING_SNAKE_CASE
1 parent 5bdc98c commit f6fd9d4

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

docs/rules/jsx-pascal-case.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ The following patterns are **not** considered warnings:
4646

4747
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
4848
* `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.
49+
* `ignore`: optional string-array of component names to ignore during validation.
50+
51+
### `allowAllCaps`
52+
53+
The following patterns are **not** considered warnings when `allowAllCaps` is `true`:
54+
55+
```jsx
56+
<ALLOWED />
57+
<TEST_COMPONENT />
58+
```
5059

5160
## When Not To Use It
5261

lib/rules/jsx-pascal-case.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const jsxUtil = require('../util/jsx');
1414
// ------------------------------------------------------------------------------
1515

1616
const PASCAL_CASE_REGEX = /^([A-Z0-9]|[A-Z0-9]+[a-z0-9]+(?:[A-Z0-9]+[a-z0-9]*)*)$/;
17-
const ALL_CAPS_TAG_REGEX = /^[A-Z0-9]+$/;
17+
const ALL_CAPS_TAG_REGEX = /^[A-Z0-9]+([A-Z0-9_]*[A-Z0-9]+)?$/;
1818

1919
// ------------------------------------------------------------------------------
2020
// Rule Definition
@@ -65,10 +65,13 @@ module.exports = {
6565
const isIgnored = ignore.indexOf(name) !== -1;
6666

6767
if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
68-
context.report({
69-
node,
70-
message: `Imported JSX component ${name} must be in PascalCase`
71-
});
68+
let message = `Imported JSX component ${name} must be in PascalCase`;
69+
70+
if (allowAllCaps) {
71+
message += ' or SCREAMING_SNAKE_CASE';
72+
}
73+
74+
context.report({node, message});
7275
}
7376
}
7477
};

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ ruleTester.run('jsx-pascal-case', rule, {
4949
}, {
5050
code: '<YMCA />',
5151
options: [{allowAllCaps: true}]
52+
}, {
53+
code: '<TEST_COMPONENT />',
54+
options: [{allowAllCaps: true}]
5255
}, {
5356
code: '<Modal.Header />'
5457
}, {
@@ -67,5 +70,17 @@ ruleTester.run('jsx-pascal-case', rule, {
6770
}, {
6871
code: '<YMCA />',
6972
errors: [{message: 'Imported JSX component YMCA must be in PascalCase'}]
73+
}, {
74+
code: '<_TEST_COMPONENT />',
75+
options: [{allowAllCaps: true}],
76+
errors: [{message: 'Imported JSX component _TEST_COMPONENT must be in PascalCase or SCREAMING_SNAKE_CASE'}]
77+
}, {
78+
code: '<TEST_COMPONENT_ />',
79+
options: [{allowAllCaps: true}],
80+
errors: [{message: 'Imported JSX component TEST_COMPONENT_ must be in PascalCase or SCREAMING_SNAKE_CASE'}]
81+
}, {
82+
code: '<__ />',
83+
options: [{allowAllCaps: true}],
84+
errors: [{message: 'Imported JSX component __ must be in PascalCase or SCREAMING_SNAKE_CASE'}]
7085
}]
7186
});

0 commit comments

Comments
 (0)