From 653f960cff49d28e2d02569e8e6469aa61af1a2c Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Thu, 5 Oct 2017 08:45:36 +1000 Subject: [PATCH 1/3] fix: sortKeys auto-fix works with property variance --- src/rules/sortKeys.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rules/sortKeys.js b/src/rules/sortKeys.js index da82ed63..0b48c6bb 100644 --- a/src/rules/sortKeys.js +++ b/src/rules/sortKeys.js @@ -65,12 +65,17 @@ const isValidOrders = { } }; +const variances = { + minus: '-', + plus: '+' +}; + const generateOrderedList = (context, sort, properties) => { return properties.map((property) => { const name = getParameterName(property, context); const value = context.getSourceCode().getText(property.value); - return [name + (property.optional ? '?' : ''), value]; + return [(variances[property.variance] || '') + name + (property.optional ? '?' : ''), value]; }) .sort((first, second) => { return sort(first[0], second[0]) ? -1 : 1; }) .map((item) => { return item[0] + ': ' + item[1]; }); From a7e0b5ca5164a7164ffbd8f7fc555e33b410aa42 Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Thu, 5 Oct 2017 09:15:53 +1000 Subject: [PATCH 2/3] fix: sortKeys auto-fix works with nested object property types --- src/rules/sortKeys.js | 56 +++++++++++++++---------- tests/rules/assertions/sortKeys.js | 65 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 22 deletions(-) diff --git a/src/rules/sortKeys.js b/src/rules/sortKeys.js index 0b48c6bb..9f3f6273 100644 --- a/src/rules/sortKeys.js +++ b/src/rules/sortKeys.js @@ -73,7 +73,13 @@ const variances = { const generateOrderedList = (context, sort, properties) => { return properties.map((property) => { const name = getParameterName(property, context); - const value = context.getSourceCode().getText(property.value); + let value; + + if (property.value.type === 'ObjectTypeAnnotation') { + value = generateFix(property.value, context, sort); // eslint-disable-line no-use-before-define + } else { + value = context.getSourceCode().getText(property.value); + } return [(variances[property.variance] || '') + name + (property.optional ? '?' : ''), value]; }) @@ -81,6 +87,32 @@ const generateOrderedList = (context, sort, properties) => { .map((item) => { return item[0] + ': ' + item[1]; }); }; +const generateFix = (node, context, sort) => { + // this could be done much more cleanly in ESLint >=4 + // as we can apply multiple fixes. That also means we can + // maintain code style in a much nicer way + let nodeText; + const newTypes = generateOrderedList(context, sort, node.properties); + const source = context.getSourceCode(node); + + const originalSubstring = source.getText(node); + + nodeText = originalSubstring; + + node.properties.forEach((property, index) => { + const subString = source.getText(property); + const addComma = subString[subString.length - 1] === ','; + + nodeText = nodeText.replace(subString, '$' + index + (addComma ? ',' : '')); + }); + + newTypes.forEach((item, index) => { + nodeText = nodeText.replace('$' + index, item); + }); + + return nodeText; +}; + const create = (context) => { const order = _.get(context, ['options', 0], 'asc'); const {natural, caseSensitive} = _.get(context, ['options', 1], defaults); @@ -113,27 +145,7 @@ const create = (context) => { order }, fix (fixer) { - // this could be done much more cleanly in ESLint >=4 - // as we can apply multiple fixes. That also means we can - // maintain code style in a much nicer way - let nodeText; - const newTypes = generateOrderedList(context, isValidOrder, node.properties); - const source = context.getSourceCode(node); - - const originalSubstring = source.getText(node); - - nodeText = originalSubstring; - - node.properties.forEach((property, index) => { - const subString = source.getText(property); - const addComma = subString[subString.length - 1] === ','; - - nodeText = nodeText.replace(subString, '$' + index + (addComma ? ',' : '')); - }); - - newTypes.forEach((item, index) => { - nodeText = nodeText.replace('$' + index, item); - }); + const nodeText = generateFix(node, context, isValidOrder); return fixer.replaceText(node, nodeText); }, diff --git a/tests/rules/assertions/sortKeys.js b/tests/rules/assertions/sortKeys.js index 33062b02..e81c9657 100644 --- a/tests/rules/assertions/sortKeys.js +++ b/tests/rules/assertions/sortKeys.js @@ -166,6 +166,71 @@ export default { c: number, } ` + }, + { + code: ` + type FooType = { + c: { + z: number, + x: string, + y: boolean, + }, + a: number | string | boolean, + b: (param: string) => number, + } + `, + errors: [ + {message: 'Expected type annotations to be in ascending order. "x" should be before "z".'}, + {message: 'Expected type annotations to be in ascending order. "a" should be before "c".'} + ], + output: ` + type FooType = { + a: number | string | boolean, + b: (param: string) => number, + c: { + x: string, + y: boolean, + z: number, + }, + } + ` + }, + { + code: ` + type FooType = { + c: { + z: { + j: string, + l: number, + k: boolean, + }, + x: string, + y: boolean, + }, + a: number | string | boolean, + b: (param: string) => number, + } + `, + errors: [ + {message: 'Expected type annotations to be in ascending order. "k" should be before "l".'}, + {message: 'Expected type annotations to be in ascending order. "x" should be before "z".'}, + {message: 'Expected type annotations to be in ascending order. "a" should be before "c".'} + ], + output: ` + type FooType = { + a: number | string | boolean, + b: (param: string) => number, + c: { + x: string, + y: boolean, + z: { + j: string, + k: boolean, + l: number, + }, + }, + } + ` } /* eslint-enable no-restricted-syntax */ ], From 5f7eb1a7fcb0f60bf75a9a53ff3ef2959743b181 Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Thu, 5 Oct 2017 09:16:34 +1000 Subject: [PATCH 3/3] docs: update the sort-keys readme source with the fixable line --- .README/rules/sort-keys.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.README/rules/sort-keys.md b/.README/rules/sort-keys.md index a083bb62..824dd48e 100644 --- a/.README/rules/sort-keys.md +++ b/.README/rules/sort-keys.md @@ -1,5 +1,7 @@ ### `sort-keys` +_The `--fix` option on the command line automatically fixes problems reported by this rule._ + Enforces sorting of Object annotations. This rule mirrors ESlint's [sort-keys](http://eslint.org/docs/rules/sort-keys) rule.