Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: jsx-pascal-case allowAllCaps allows underscore #2364

Merged
merged 1 commit into from Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'}]
}]
});