Skip to content

Commit

Permalink
[New] jsx-pascal-case: allowAllCaps option now allows `SCREAMING_…
Browse files Browse the repository at this point in the history
…SNAKE_CASE`
  • Loading branch information
Tyler Swavely authored and ljharb committed Jul 31, 2019
1 parent 5bdc98c commit f6fd9d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
11 changes: 10 additions & 1 deletion docs/rules/jsx-pascal-case.md
Expand Up @@ -46,7 +46,16 @@ The following patterns are **not** considered warnings:

* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
* `allowAllCaps`: optional boolean set to `true` to allow components name in all caps (default to `false`).
* `ignore`: optional array of components name to ignore during validation.
* `ignore`: optional string-array of component names to ignore during validation.

### `allowAllCaps`

The following patterns are **not** considered warnings when `allowAllCaps` is `true`:

```jsx
<ALLOWED />
<TEST_COMPONENT />
```

## When Not To Use It

Expand Down
13 changes: 8 additions & 5 deletions lib/rules/jsx-pascal-case.js
Expand Up @@ -14,7 +14,7 @@ const jsxUtil = require('../util/jsx');
// ------------------------------------------------------------------------------

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

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -65,10 +65,13 @@ module.exports = {
const isIgnored = ignore.indexOf(name) !== -1;

if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
context.report({
node,
message: `Imported JSX component ${name} must be in PascalCase`
});
let message = `Imported JSX component ${name} must be in PascalCase`;

if (allowAllCaps) {
message += ' or SCREAMING_SNAKE_CASE';
}

context.report({node, message});
}
}
};
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/jsx-pascal-case.js
Expand Up @@ -49,6 +49,9 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<YMCA />',
options: [{allowAllCaps: true}]
}, {
code: '<TEST_COMPONENT />',
options: [{allowAllCaps: true}]
}, {
code: '<Modal.Header />'
}, {
Expand All @@ -67,5 +70,17 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<YMCA />',
errors: [{message: 'Imported JSX component YMCA must be in PascalCase'}]
}, {
code: '<_TEST_COMPONENT />',
options: [{allowAllCaps: true}],
errors: [{message: 'Imported JSX component _TEST_COMPONENT must be in PascalCase or SCREAMING_SNAKE_CASE'}]
}, {
code: '<TEST_COMPONENT_ />',
options: [{allowAllCaps: true}],
errors: [{message: 'Imported JSX component TEST_COMPONENT_ must be in PascalCase or SCREAMING_SNAKE_CASE'}]
}, {
code: '<__ />',
options: [{allowAllCaps: true}],
errors: [{message: 'Imported JSX component __ must be in PascalCase or SCREAMING_SNAKE_CASE'}]
}]
});

0 comments on commit f6fd9d4

Please sign in to comment.