Skip to content

Commit 397b7a1

Browse files
zpaogajus
authored andcommitted
feat: parse docblock more robustly for onlyFilesWithFlowAnnotation usage (#404)
Instead of looking for a very specfic string in a part of the docblock, and then checking the entire docblock for "no" (which can show up in copyrights, or just general comments that may be documenting a file), actually see if there's a @noflow comment.
1 parent 714a995 commit 397b7a1

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/utilities/isFlowFile.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ export default (context, strict = true) => {
1515
}
1616

1717
return comments.some((comment) => {
18-
return (
19-
isFlowFileAnnotation(comment.value) &&
20-
!(strict && /no/.test(comment.value))
21-
);
18+
return isFlowFileAnnotation(comment.value, strict);
2219
});
2320
};

src/utilities/isFlowFileAnnotation.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import _ from 'lodash';
22

33
const FLOW_MATCHER = /^@(?:no)?flow$/;
44

5-
export default (comment) => {
5+
export default (comment, strict) => {
66
// eslint-disable-next-line flowtype/require-valid-file-annotation
77
// The flow parser splits comments with the following regex to look for the @flow flag.
88
// See https://github.com/facebook/flow/blob/a96249b93541f2f7bfebd8d62085bf7a75de02f2/src/parsing/docblock.ml#L39
99
return _.some(comment.split(/[ \t\r\n\\*/]+/), (commentPart) => {
10-
return FLOW_MATCHER.test(commentPart);
10+
const match = commentPart.match(FLOW_MATCHER);
11+
12+
if (match === null) {
13+
return false;
14+
}
15+
16+
return !strict || match[0] === '@flow';
1117
});
1218
};
1319

tests/rules/assertions/defineFlowType.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ const VALID_WITH_DEFINE_FLOW_TYPE = [
127127
'\'AType\' is not defined.',
128128
'\'BType\' is not defined.'
129129
]
130+
},
131+
132+
// This tests to ensure we have a robust handling of @flow comments
133+
{
134+
// eslint-disable-next-line no-restricted-syntax
135+
code: `
136+
/**
137+
* Copyright 2019 no corp
138+
* @flow
139+
*/
140+
type Foo = $ReadOnly<{}>`,
141+
errors: [
142+
'\'$ReadOnly\' is not defined.'
143+
],
144+
settings: {
145+
flowtype: {
146+
onlyFilesWithFlowAnnotation: true
147+
}
148+
}
130149
}
131150
];
132151

@@ -211,7 +230,8 @@ export default {
211230
code: subject.code,
212231
rules: {
213232
'no-undef': 2
214-
}
233+
},
234+
settings: subject.settings
215235
};
216236
}),
217237
...VALID_WITH_DEFINE_FLOW_TYPE.map((subject) => {
@@ -223,7 +243,8 @@ export default {
223243
2,
224244
'nofunc'
225245
]
226-
}
246+
},
247+
settings: subject.settings
227248
};
228249
})
229250
]

0 commit comments

Comments
 (0)