From de0492b968d908cb13bc2b9381e00e65c887a456 Mon Sep 17 00:00:00 2001 From: Will Johnson Date: Tue, 5 Apr 2016 15:47:08 -0400 Subject: [PATCH] issue 11: space-after-type-colon does not apply to the return type Given the following configuration: "flowtype/space-after-type-colon": [ 1, "never" ] and a function: function fn (key: string): Object { Running the linter will produce the following: xxx:yy warning There must be no space after "key" parameter type annotation colon flowtype/space-after-type-colon This patch will address this issue and ensure that the return type is also taken into consideration when checking for spaces after the colon. --- src/rules/spaceAfterTypeColon.js | 12 ++++++ tests/rules/assertions/spaceAfterTypeColon.js | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/rules/spaceAfterTypeColon.js b/src/rules/spaceAfterTypeColon.js index d1d5b5d3..069a0aa9 100644 --- a/src/rules/spaceAfterTypeColon.js +++ b/src/rules/spaceAfterTypeColon.js @@ -24,5 +24,17 @@ export default iterateFunctionNodes((context) => { } } }); + + if (functionNode.returnType) { + const spaceAfter = functionNode.returnType.typeAnnotation.start - functionNode.returnType.start - 1; + + if (always && spaceAfter > 1) { + context.report(functionNode, 'There must be 1 space after return type colon.'); + } else if (always && spaceAfter === 0) { + context.report(functionNode, 'There must be a space after return type colon.'); + } else if (!always && spaceAfter > 0) { + context.report(functionNode, 'There must be no space after return type colon.'); + } + } }; }); diff --git a/tests/rules/assertions/spaceAfterTypeColon.js b/tests/rules/assertions/spaceAfterTypeColon.js index 79b727a4..100f015d 100644 --- a/tests/rules/assertions/spaceAfterTypeColon.js +++ b/tests/rules/assertions/spaceAfterTypeColon.js @@ -54,6 +54,37 @@ export default { options: [ 'always' ] + }, + { + code: '():Object => {}', + errors: [ + { + message: 'There must be a space after return type colon.' + } + ], + options: [ + 'always' + ] + }, { + code: '(): Object => {}', + errors: [ + { + message: 'There must be no space after return type colon.' + } + ], + options: [ + 'never' + ] + }, { + code: '(): Object => {}', + errors: [ + { + message: 'There must be 1 space after return type colon.' + } + ], + options: [ + 'always' + ] } ], valid: [ @@ -74,6 +105,17 @@ export default { options: [ 'always' ] + }, + { + code: '():Object => {}', + options: [ + 'never' + ] + }, { + code: '(): Object => {}', + options: [ + 'always' + ] } ] };