From 340b93ce983d08dd1daa6f553faeefa762b49562 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 25 Nov 2016 12:10:18 -0800 Subject: [PATCH] jsx-sort-props noSortAlphabetically option Closes #541 Closes #786 --- docs/rules/jsx-sort-props.md | 11 ++++++++++- lib/rules/jsx-sort-props.js | 7 ++++++- tests/lib/rules/jsx-sort-props.js | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/rules/jsx-sort-props.md b/docs/rules/jsx-sort-props.md index 2fb8ca56fc..63b5dd6e1e 100644 --- a/docs/rules/jsx-sort-props.md +++ b/docs/rules/jsx-sort-props.md @@ -27,7 +27,8 @@ The following patterns are considered okay and do not cause warnings: "callbacksLast": , "shorthandFirst": , "shorthandLast": , - "ignoreCase": + "ignoreCase": , + "noSortAlphabetically": }] ... ``` @@ -66,6 +67,14 @@ When `true`, short hand props must be listed after all other props (unless `call ``` +### `noSortAlphabetically` + +When `true`, alphabetical order is not enforced: + +```js + +``` + ## When not to use This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing props isn't a part of your coding standards, then you can leave this rule off. diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index afd7e85ad6..335675bfe0 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -40,6 +40,10 @@ module.exports = { }, ignoreCase: { type: 'boolean' + }, + // Whether alphabetical sorting should be enforced + noSortAlphabetically: { + type: 'boolean' } }, additionalProperties: false @@ -53,6 +57,7 @@ module.exports = { var callbacksLast = configuration.callbacksLast || false; var shorthandFirst = configuration.shorthandFirst || false; var shorthandLast = configuration.shorthandLast || false; + var noSortAlphabetically = configuration.noSortAlphabetically || false; return { JSXOpeningElement: function(node) { @@ -114,7 +119,7 @@ module.exports = { } } - if (currentPropName < previousPropName) { + if (!noSortAlphabetically && currentPropName < previousPropName) { context.report({ node: decl, message: 'Props should be sorted alphabetically' diff --git a/tests/lib/rules/jsx-sort-props.js b/tests/lib/rules/jsx-sort-props.js index 53503863e3..1e3366419f 100644 --- a/tests/lib/rules/jsx-sort-props.js +++ b/tests/lib/rules/jsx-sort-props.js @@ -57,6 +57,12 @@ var shorthandAndCallbackLastArgs = [{ var ignoreCaseArgs = [{ ignoreCase: true }]; +var noSortAlphabeticallyArgs = [{ + noSortAlphabetically: true +}]; +var sortAlphabeticallyArgs = [{ + noSortAlphabetically: false +}]; ruleTester.run('jsx-sort-props', rule, { valid: [ @@ -84,7 +90,10 @@ ruleTester.run('jsx-sort-props', rule, { code: ';', options: shorthandAndCallbackLastArgs, parserOptions: parserOptions - } + }, + // noSortAlphabetically + {code: ';', options: noSortAlphabeticallyArgs, parserOptions: parserOptions}, + {code: ';', options: noSortAlphabeticallyArgs, parserOptions: parserOptions} ], invalid: [ {code: ';', errors: [expectedError], parserOptions: parserOptions}, @@ -122,6 +131,7 @@ ruleTester.run('jsx-sort-props', rule, { errors: [shorthandAndCallbackLastArgs], options: shorthandLastArgs, parserOptions: parserOptions - } + }, + {code: ';', errors: [expectedError], options: sortAlphabeticallyArgs, parserOptions: parserOptions} ] });