Skip to content

Commit

Permalink
Add allowAllCaps and ignore options to jsx-pascal-case (fixes #575)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed May 9, 2016
1 parent 4e82b10 commit ed21047
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/rules/jsx-pascal-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ The following patterns are not considered warnings:
<CSSTransitionGroup />
```

## Rule Options

```js
...
"jsx-pascal-case": [<enabled>, { allowAllCaps: <allowAllCaps>, ignore: <ignore> }]
...
```

* `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.

## When Not To Use It

If you are not using JSX.
22 changes: 21 additions & 1 deletion lib/rules/jsx-pascal-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

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

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {

var configuration = context.options[0] || {};
var allowAllCaps = configuration.allowAllCaps || false;
var ignore = configuration.ignore || [];

return {
JSXOpeningElement: function(node) {
switch (node.name.type) {
Expand All @@ -36,8 +41,10 @@ module.exports = function(context) {

var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
var isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(node.name);
var isIgnored = ignore.indexOf(node.name) !== -1;

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

};

module.exports.schema = [{
type: 'object',
properties: {
allowAllCaps: {
type: 'boolean'
},
ignore: {
type: 'array'
}
},
additionalProperties: false
}];
15 changes: 13 additions & 2 deletions tests/lib/rules/jsx-pascal-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<T3stComp0nent />',
parserOptions: parserOptions
},
{
}, {
code: '<T />',
parserOptions: parserOptions
}, {
code: '<YMCA />',
parserOptions: parserOptions,
options: [{allowAllCaps: true}]
}, {
code: '<IGNORED />',
parserOptions: parserOptions,
options: [{ignore: ['IGNORED']}]
}],

invalid: [{
Expand All @@ -65,5 +72,9 @@ ruleTester.run('jsx-pascal-case', rule, {
code: '<TEST_COMPONENT />',
parserOptions: parserOptions,
errors: [{message: 'Imported JSX component TEST_COMPONENT must be in PascalCase'}]
}, {
code: '<YMCA />',
parserOptions: parserOptions,
errors: [{message: 'Imported JSX component YMCA must be in PascalCase'}]
}]
});

0 comments on commit ed21047

Please sign in to comment.