Skip to content

Commit e93f1c0

Browse files
andrejs-sisojevs-accenturegajus
andauthored
feat: add respect to @noflow annotation (#451)
* fix: general filter for all rules - should be either @flow file, or `onlyFilesWithFlowAnnotation` should not be enabled and file not marked with @noflow * fix: require-valid-file-annotation tests broken with new `checkFlowFileAnnotation` - respect onlyFilesWithFlowAnnotation == true Co-authored-by: Gajus Kuizinas <[email protected]>
1 parent a91db33 commit e93f1c0

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export default {
8888
recommended,
8989
},
9090
rules: _.mapValues(rules, (rule, key) => {
91-
if (key === 'no-types-missing-file-annotation') {
91+
if (['no-types-missing-file-annotation', 'require-valid-file-annotation'].includes(key)) {
9292
return rule;
9393
}
9494

src/rules/requireValidFileAnnotation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const create = (context) => {
113113
} else {
114114
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
115115
}
116-
} else if (always) {
116+
} else if (always && !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation')) {
117117
context.report({
118118
fix: (fixer) => {
119119
let annotation;

src/utilities/checkFlowFileAnnotation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import _ from 'lodash';
22
import isFlowFile from './isFlowFile';
3+
import isNoFlowFile from './isNoFlowFile';
34

45
export default (cb, context) => {
5-
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context);
6+
const checkThisFile = (!_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') && !isNoFlowFile(context)) || isFlowFile(context); // eslint-disable-line no-extra-parens, max-len
67

78
if (!checkThisFile) {
89
return () => {};

src/utilities/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {default as getParameterName} from './getParameterName';
99
export {default as getTokenAfterParens} from './getTokenAfterParens';
1010
export {default as getTokenBeforeParens} from './getTokenBeforeParens';
1111
export {default as isFlowFile} from './isFlowFile';
12+
export {default as isNoFlowFile} from './isNoFlowFile';
1213
export {default as isFlowFileAnnotation} from './isFlowFileAnnotation';
1314
export {default as iterateFunctionNodes} from './iterateFunctionNodes';
1415
export {default as quoteName} from './quoteName';

src/utilities/isNoFlowFile.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import isNoFlowFileAnnotation from './isNoFlowFileAnnotation';
2+
3+
/**
4+
* Checks whether a file has an @flow or @noflow annotation.
5+
*
6+
* @param context
7+
* @param [strict] - By default, the function returns true if the file starts with @flow but not if it
8+
* starts by @noflow. When the strict flag is set to false, the function returns true if the flag has @noflow also.
9+
*/
10+
11+
export default (context, strict = true) => {
12+
const comments = context.getAllComments();
13+
14+
if (!comments.length) {
15+
return false;
16+
}
17+
18+
return comments.some((comment) => {
19+
return isNoFlowFileAnnotation(comment.value, strict);
20+
});
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import _ from 'lodash';
2+
3+
const FLOW_MATCHER = /^@noflow$/;
4+
5+
export default (comment, strict) => {
6+
// The flow parser splits comments with the following regex to look for the @flow flag.
7+
// See https://github.com/facebook/flow/blob/a96249b93541f2f7bfebd8d62085bf7a75de02f2/src/parsing/docblock.ml#L39
8+
return _.some(comment.split(/[\t\n\r */\\]+/), (commentPart) => {
9+
const match = commentPart.match(FLOW_MATCHER);
10+
11+
if (match === null) {
12+
return false;
13+
}
14+
15+
return !strict || match[0] === '@noflow';
16+
});
17+
};

tests/rules/assertions/requireReturnType.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export default {
3838
},
3939
],
4040
},
41+
{
42+
code: '/* @flow */\n(foo) => { return 1; }',
43+
errors: [
44+
{
45+
message: 'Missing return type annotation.',
46+
},
47+
],
48+
},
4149
{
4250
code: '(foo): undefined => { return; }',
4351
errors: [
@@ -692,6 +700,12 @@ export default {
692700
},
693701
},
694702
},
703+
{
704+
code: '/* @noflow */\n(foo) => { return 1; }',
705+
options: [
706+
'always',
707+
],
708+
},
695709
{
696710
code: '(foo) => { return undefined; }',
697711
options: [

0 commit comments

Comments
 (0)