forked from gajus/eslint-plugin-flowtype
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add newline-after-flow-annotation rule (gajus#312)
- Loading branch information
Showing
6 changed files
with
148 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,25 @@ | ||
### `newline-after-flow-annotation` | ||
|
||
This rule requires an empty line after the Flow annotation. | ||
|
||
#### Options | ||
|
||
The rule has a string option: | ||
|
||
* `"always"` (default): Enforces that `@flow` annotations be followed by an empty line, separated by newline (LF) | ||
* `"always-windows"`: Identical to "always", but will use a CRLF when autofixing | ||
* `"never"`: Enforces that `@flow` annotations are not followed by empty lines | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/newline-after-flow-annotation": [ | ||
2, | ||
"always" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
|
||
<!-- assertions newlineAfterFlowAnnotation --> |
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,76 @@ | ||
import _ from 'lodash'; | ||
|
||
const looksLikeFlowFileAnnotation = (comment) => { | ||
return /@(?:no)?flo/i.test(comment); | ||
}; | ||
|
||
const schema = [ | ||
{ | ||
enum: ['always', 'always-windows', 'never'], | ||
type: 'string' | ||
} | ||
]; | ||
|
||
const create = (context) => { | ||
const mode = context.options[0]; | ||
const never = mode === 'never'; | ||
|
||
const newline = mode === 'always-windows' ? '\r\n' : '\n'; | ||
|
||
return { | ||
Program (node) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const potentialFlowFileAnnotation = _.find( | ||
context.getAllComments(), | ||
(comment) => { | ||
return looksLikeFlowFileAnnotation(comment.value); | ||
} | ||
); | ||
|
||
if (potentialFlowFileAnnotation) { | ||
const line = potentialFlowFileAnnotation.loc.end.line; | ||
const nextLineIsEmpty = sourceCode.lines[line] === ''; | ||
|
||
if (!never && !nextLineIsEmpty) { | ||
context.report({ | ||
fix: (fixer) => { | ||
return fixer.insertTextAfter( | ||
potentialFlowFileAnnotation, | ||
newline | ||
); | ||
}, | ||
message: 'Expected newline after flow annotation', | ||
node | ||
}); | ||
} | ||
|
||
if (never && nextLineIsEmpty) { | ||
context.report({ | ||
fix: (fixer) => { | ||
const lineBreak = sourceCode.text[potentialFlowFileAnnotation.end]; | ||
|
||
return fixer.replaceTextRange( | ||
[ | ||
potentialFlowFileAnnotation.end, | ||
potentialFlowFileAnnotation.end + ( | ||
lineBreak === '\r' ? 2 : 1 | ||
) | ||
], | ||
'' | ||
); | ||
}, | ||
message: 'Expected no newline after flow annotation', | ||
node | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; | ||
|
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,42 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: '// @flow\nimport Foo from \'./foo\';', | ||
errors: [{message: 'Expected newline after flow annotation'}], | ||
output: '// @flow\n\nimport Foo from \'./foo\';' | ||
}, | ||
{ | ||
code: '// @flow\nimport Foo from \'./foo\';', | ||
errors: [{message: 'Expected newline after flow annotation'}], | ||
options: ['always'], | ||
output: '// @flow\n\nimport Foo from \'./foo\';' | ||
}, | ||
{ | ||
code: '// @flow\r\nimport Foo from \'./foo\';', | ||
errors: [{message: 'Expected newline after flow annotation'}], | ||
options: ['always-windows'], | ||
output: '// @flow\r\n\r\nimport Foo from \'./foo\';' | ||
}, | ||
{ | ||
code: '// @flow\n\n', | ||
errors: [{message: 'Expected no newline after flow annotation'}], | ||
options: ['never'], | ||
output: '// @flow\n' | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: '// @flow\n\nimport Foo from \'./foo\';', | ||
options: ['always'] | ||
}, | ||
{ | ||
code: '// @flow\r\n\r\nimport Foo from \'./foo\';', | ||
options: ['always-windows'] | ||
}, | ||
{ | ||
code: '// @flow\nimport Foo from \'./foo\';', | ||
options: ['never'] | ||
} | ||
] | ||
}; | ||
|
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