-
-
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.
feat: add no-types-missing-flow-annotation (#222)
* feat: disallow types in files missing a file annotation * chore: add doc entry * fix: ignore flow files * fix: renamed to avoid flow auto include the file in some setups * chore: clean up, add valid test cases * feat: Allow types in files with an @noflow annotation.
- Loading branch information
Showing
6 changed files
with
127 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### `no-types-missing-file-annotation` | ||
|
||
Disallows Flow type imports, aliases, and annotations in files missing a valid Flow file declaration (or a @noflow annotation). | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/no-types-missing-file-annotation": 2 | ||
} | ||
} | ||
``` | ||
|
||
<!-- assertions noTypesMissingFileAnnotation --> |
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,39 @@ | ||
import {isFlowFile} from '../utilities'; | ||
|
||
/** | ||
* Disallows the use for flow types without a valid file annotation. | ||
* Only checks files without a valid flow annotation. | ||
*/ | ||
|
||
export default (context) => { | ||
// Skip flow files | ||
if (isFlowFile(context, false)) { | ||
return {}; | ||
} | ||
|
||
const reporter = (node, type) => { | ||
context.report({ | ||
data: {type}, | ||
message: 'Type {{type}} require valid Flow declaration.', | ||
node | ||
}); | ||
}; | ||
|
||
return { | ||
ImportDeclaration (node) { | ||
if (node.importKind === 'type') { | ||
reporter(node, 'imports'); | ||
} | ||
if (node.importKind === 'value' && | ||
node.specifiers.some((specifier) => { return specifier.importKind === 'type'; })) { | ||
reporter(node, 'imports'); | ||
} | ||
}, | ||
TypeAlias (node) { | ||
reporter(node, 'aliases'); | ||
}, | ||
TypeAnnotation (node) { | ||
reporter(node, 'annotations'); | ||
} | ||
}; | ||
}; |
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,57 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'const x: number = 42;', | ||
errors: [{ | ||
message: 'Type annotations require valid Flow declaration.' | ||
}] | ||
}, | ||
{ | ||
code: 'type FooType = number;', | ||
errors: [{ | ||
message: 'Type aliases require valid Flow declaration.' | ||
}] | ||
}, | ||
{ | ||
code: 'import type A from "a"', | ||
errors: [{ | ||
message: 'Type imports require valid Flow declaration.' | ||
}] | ||
}, | ||
{ | ||
code: 'import type {A} from "a"', | ||
errors: [{ | ||
message: 'Type imports require valid Flow declaration.' | ||
}] | ||
}, | ||
{ | ||
code: 'import {type A} from "a"', | ||
errors: [{ | ||
message: 'Type imports require valid Flow declaration.' | ||
}] | ||
}, | ||
{ | ||
code: 'function t<T>(): T{}', | ||
errors: [{ | ||
message: 'Type annotations require valid Flow declaration.' | ||
}] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: '// @flow\nconst x: number = 42;' | ||
}, | ||
{ | ||
code: '/* @flow weak */\ntype FooType = number;' | ||
}, | ||
{ | ||
code: '/* @noflow */\ntype FooType = number;' | ||
}, | ||
{ | ||
code: '/* @noflow */\nimport type A from "a"' | ||
}, | ||
{ | ||
code: '/* @noflow */\nimport {type A} from "a"' | ||
} | ||
] | ||
}; |
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