diff --git a/docs/rules/jsx-pascal-case.md b/docs/rules/jsx-pascal-case.md index 904bdf82ad..0544f45095 100644 --- a/docs/rules/jsx-pascal-case.md +++ b/docs/rules/jsx-pascal-case.md @@ -36,6 +36,18 @@ The following patterns are not considered warnings: ``` +## Rule Options + +```js +... +"jsx-pascal-case": [, { allowAllCaps: , 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. diff --git a/lib/rules/jsx-pascal-case.js b/lib/rules/jsx-pascal-case.js index fde14d9bdf..815262366b 100644 --- a/lib/rules/jsx-pascal-case.js +++ b/lib/rules/jsx-pascal-case.js @@ -11,6 +11,7 @@ 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 @@ -18,6 +19,10 @@ var COMPAT_TAG_REGEX = /^[a-z]|\-/; 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) { @@ -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' @@ -47,3 +54,16 @@ module.exports = function(context) { }; }; + +module.exports.schema = [{ + type: 'object', + properties: { + allowAllCaps: { + type: 'boolean' + }, + ignore: { + type: 'array' + } + }, + additionalProperties: false +}]; diff --git a/tests/lib/rules/jsx-pascal-case.js b/tests/lib/rules/jsx-pascal-case.js index 1e473afabc..187c27a4f9 100644 --- a/tests/lib/rules/jsx-pascal-case.js +++ b/tests/lib/rules/jsx-pascal-case.js @@ -51,10 +51,17 @@ ruleTester.run('jsx-pascal-case', rule, { }, { code: '', parserOptions: parserOptions - }, - { + }, { code: '', parserOptions: parserOptions + }, { + code: '', + parserOptions: parserOptions, + options: [{allowAllCaps: true}] + }, { + code: '', + parserOptions: parserOptions, + options: [{ignore: ['IGNORED']}] }], invalid: [{ @@ -65,5 +72,9 @@ ruleTester.run('jsx-pascal-case', rule, { code: '', parserOptions: parserOptions, errors: [{message: 'Imported JSX component TEST_COMPONENT must be in PascalCase'}] + }, { + code: '', + parserOptions: parserOptions, + errors: [{message: 'Imported JSX component YMCA must be in PascalCase'}] }] });