Skip to content

Commit

Permalink
feat: allow line break to satisfy trailing space rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Harvey committed Nov 17, 2016
1 parent 8f355c2 commit 29a17a8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
24 changes: 23 additions & 1 deletion .README/rules/space-after-type-colon.md
Expand Up @@ -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
}
]
}
}

<!-- assertions spaceAfterTypeColon -->
4 changes: 3 additions & 1 deletion 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'
});
};
13 changes: 11 additions & 2 deletions src/rules/typeColonSpacing/reporter.js
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/rules/assertions/spaceAfterTypeColon.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -113,6 +118,12 @@ const ARROW_FUNCTION_PARAMS = {
{
code: '(i?:number) => {}',
options: ['never']
},
{
code: '(foo:\n { a: string, b: number }) => {}',
options: ['always', {
allowLineBreak: true
}]
}
]
};
Expand Down

0 comments on commit 29a17a8

Please sign in to comment.