-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'genericSpacing' of https://github.com/danharper/eslint-…
…plugin-flowtype into danharper-genericSpacing
- Loading branch information
Showing
7 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### `generic-spacing` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Enforces consistent spacing within generic type annotation parameters. | ||
|
||
This rule takes one argument. If it is `'never'` then a problem is raised when there is a space surrounding the generic type parameters. If it is `'always'` then a problem is raised when there is no space surrounding the generic type parameters. | ||
|
||
The default value is `'never'`. | ||
|
||
<!-- assertions genericSpacing --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import {spacingFixers} from './../utilities'; | ||
|
||
export default (context) => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const never = (context.options[0] || 'never') === 'never'; | ||
|
||
return { | ||
GenericTypeAnnotation (node) { | ||
const types = node.typeParameters; | ||
|
||
// Promise<foo> | ||
// ^^^^^^^^^^^^ GenericTypeAnnotation (with typeParameters) | ||
// ^^^ GenericTypeAnnotation (without typeParameters) | ||
if (!types) { | ||
return; | ||
} | ||
|
||
const [opener, firstInnerToken] = sourceCode.getFirstTokens(types, 2); | ||
const [lastInnerToken, closer] = sourceCode.getLastTokens(types, 2); | ||
|
||
const spacesBefore = firstInnerToken.start - opener.end; | ||
const spacesAfter = closer.start - lastInnerToken.end; | ||
|
||
if (never) { | ||
if (spacesBefore) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.stripSpacesAfter(opener, spacesBefore), | ||
message: 'There must be no space at start of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} | ||
|
||
if (spacesAfter) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.stripSpacesAfter(lastInnerToken, spacesAfter), | ||
message: 'There must be no space at end of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} | ||
} else { | ||
if (spacesBefore > 1) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.stripSpacesAfter(opener, spacesBefore - 1), | ||
message: 'There must be one space at start of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} else if (spacesBefore === 0) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.addSpaceAfter(opener), | ||
message: 'There must be a space at start of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} | ||
|
||
if (spacesAfter > 1) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.stripSpacesAfter(lastInnerToken, spacesAfter - 1), | ||
message: 'There must be one space at end of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} else if (spacesAfter === 0) { | ||
context.report({ | ||
data: {name: node.id.name}, | ||
fix: spacingFixers.addSpaceAfter(lastInnerToken), | ||
message: 'There must be a space at end of "{{name}}" generic type annotation', | ||
node: types | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
export default { | ||
invalid: [ | ||
// Never | ||
|
||
{ | ||
code: 'type X = Promise< string>', | ||
errors: [{message: 'There must be no space at start of "Promise" generic type annotation'}], | ||
output: 'type X = Promise<string>' | ||
}, | ||
{ | ||
code: 'type X = Promise< string>', | ||
errors: [{message: 'There must be no space at start of "Promise" generic type annotation'}], | ||
options: ['never'], | ||
output: 'type X = Promise<string>' | ||
}, | ||
{ | ||
code: 'type X = FooBar<string >', | ||
errors: [{message: 'There must be no space at end of "FooBar" generic type annotation'}], | ||
output: 'type X = FooBar<string>' | ||
}, | ||
{ | ||
code: 'type X = Promise< string >', | ||
errors: [ | ||
{message: 'There must be no space at start of "Promise" generic type annotation'}, | ||
{message: 'There must be no space at end of "Promise" generic type annotation'} | ||
], | ||
output: 'type X = Promise<string>' | ||
}, | ||
{ | ||
code: 'type X = Promise< (foo), bar, (((baz))) >', | ||
errors: [ | ||
{message: 'There must be no space at start of "Promise" generic type annotation'}, | ||
{message: 'There must be no space at end of "Promise" generic type annotation'} | ||
], | ||
output: 'type X = Promise<(foo), bar, (((baz)))>' | ||
}, | ||
|
||
// Always (given no space) | ||
|
||
{ | ||
code: 'type X = Promise<string >', | ||
errors: [{message: 'There must be a space at start of "Promise" generic type annotation'}], | ||
options: ['always'], | ||
output: 'type X = Promise< string >' | ||
}, | ||
{ | ||
code: 'type X = FooBar< string>', | ||
errors: [{message: 'There must be a space at end of "FooBar" generic type annotation'}], | ||
options: ['always'], | ||
output: 'type X = FooBar< string >' | ||
}, | ||
{ | ||
code: 'type X = Promise<string>', | ||
errors: [ | ||
{message: 'There must be a space at start of "Promise" generic type annotation'}, | ||
{message: 'There must be a space at end of "Promise" generic type annotation'} | ||
], | ||
options: ['always'], | ||
output: 'type X = Promise< string >' | ||
}, | ||
{ | ||
code: 'type X = Promise<(foo), bar, (((baz)))>', | ||
errors: [ | ||
{message: 'There must be a space at start of "Promise" generic type annotation'}, | ||
{message: 'There must be a space at end of "Promise" generic type annotation'} | ||
], | ||
options: ['always'], | ||
output: 'type X = Promise< (foo), bar, (((baz))) >' | ||
}, | ||
|
||
// Always (given too many spaces) | ||
|
||
{ | ||
code: 'type X = FooBar< string >', | ||
errors: [{message: 'There must be one space at start of "FooBar" generic type annotation'}], | ||
options: ['always'], | ||
output: 'type X = FooBar< string >' | ||
}, | ||
{ | ||
code: 'type X = FooBar< string >', | ||
errors: [{message: 'There must be one space at end of "FooBar" generic type annotation'}], | ||
options: ['always'], | ||
output: 'type X = FooBar< string >' | ||
}, | ||
{ | ||
code: 'type X = Promise< (foo), bar, (((baz))) >', | ||
errors: [ | ||
{message: 'There must be one space at start of "Promise" generic type annotation'}, | ||
{message: 'There must be one space at end of "Promise" generic type annotation'} | ||
], | ||
options: ['always'], | ||
output: 'type X = Promise< (foo), bar, (((baz))) >' | ||
} | ||
], | ||
valid: [ | ||
// Never | ||
|
||
{code: 'type X = Promise<string>'}, | ||
{code: 'type X = Promise<(string)>'}, | ||
{code: 'type X = Promise<(foo), bar, (((baz)))>'}, | ||
|
||
// Always | ||
|
||
{ | ||
code: 'type X = Promise< string >', | ||
options: ['always'] | ||
}, | ||
{ | ||
code: 'type X = Promise< (string) >', | ||
options: ['always'] | ||
}, | ||
{ | ||
code: 'type X = Promise< (foo), bar, (((baz))) >', | ||
options: ['always'] | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters