diff --git a/.README/rules/space-after-type-colon.md b/.README/rules/space-after-type-colon.md index 5b54edcb..7c187fa0 100644 --- a/.README/rules/space-after-type-colon.md +++ b/.README/rules/space-after-type-colon.md @@ -4,6 +4,28 @@ _The `--fix` option on the command line automatically fixes problems reported by Enforces consistent spacing after the type annotation colon. -This rule takes one argument. If it is `'always'` then a problem is raised when there is no space after the type annotation colon. If it is `'never'` then a problem is raised when there is a space after the type annotation colon. The default value is `'always'`. +#### Options + +This rule has a string argument. + +* `"always"` (default): Require a space after the type annotation colon (e.g. foo: BarType). +* `"never"`: Require no spaces after the type annotation colon (e.g. foo:BarType). + +This rule has an option object. + +* `"allowLineBreak"` - Allow a line break to count as a space following the annotation colon. + * `"true"`: Enable + * `"false"`: Disable + +{ + "rules": { + "flowtype/space-after-type-colon": [ + 2, + "always", { + "allowLineBreak": false + } + ] + } +} diff --git a/src/rules/spaceAfterTypeColon.js b/src/rules/spaceAfterTypeColon.js index d93b0c8a..c1d5314f 100644 --- a/src/rules/spaceAfterTypeColon.js +++ b/src/rules/spaceAfterTypeColon.js @@ -1,7 +1,9 @@ +import _ from 'lodash'; import makeSpacing from './typeColonSpacing'; export default (context) => { return makeSpacing('after', context, { - always: (context.options[0] || 'always') === 'always' + allowLineBreak: _.get(context, ['options', '1', 'allowLineBreak'], false), + always: _.get(context, ['options', '0'], 'always') === 'always' }); }; diff --git a/src/rules/typeColonSpacing/reporter.js b/src/rules/typeColonSpacing/reporter.js index 5e175123..49b186ad 100644 --- a/src/rules/typeColonSpacing/reporter.js +++ b/src/rules/typeColonSpacing/reporter.js @@ -10,15 +10,24 @@ const getSpaces = (direction, colon, context) => { } }; -export default (direction, context, {always}) => { +export default (direction, context, {always, allowLineBreak}) => { return ({colon, node, name = '', type = 'type annotation'}) => { - const spaces = getSpaces(direction, colon, context); + let spaces; + const data = { direction, name, type }; + const charAfter = context.getSourceCode().getText(colon, 0, 1).slice(1); + + if (allowLineBreak && (charAfter === '\n' || charAfter === '\r\n')) { + spaces = 1; + } else { + spaces = getSpaces(direction, colon, context); + } + if (always && spaces > 1) { context.report({ data, diff --git a/tests/rules/assertions/spaceAfterTypeColon.js b/tests/rules/assertions/spaceAfterTypeColon.js index 6ddd35de..13976583 100644 --- a/tests/rules/assertions/spaceAfterTypeColon.js +++ b/tests/rules/assertions/spaceAfterTypeColon.js @@ -67,6 +67,11 @@ const ARROW_FUNCTION_PARAMS = { errors: [{message: 'There must be no space after "i" parameter type annotation colon.'}], options: ['never'], output: '(i?:number) => {}' + }, + { + code: '(foo:\n { a: string, b: number }) => {}', + errors: [{message: 'There must be 1 space after "foo" parameter type annotation colon.'}], + output: '(foo: { a: string, b: number }) => {}' } ], valid: [ @@ -113,6 +118,12 @@ const ARROW_FUNCTION_PARAMS = { { code: '(i?:number) => {}', options: ['never'] + }, + { + code: '(foo:\n { a: string, b: number }) => {}', + options: ['always', { + allowLineBreak: true + }] } ] };