Skip to content

Commit

Permalink
feat: add respect to @noflow annotation (#451)
Browse files Browse the repository at this point in the history
* 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 <gajus@gajus.com>
  • Loading branch information
andrejs-sisojevs-accenture and gajus committed Jun 30, 2020
1 parent a91db33 commit e93f1c0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -88,7 +88,7 @@ export default {
recommended,
},
rules: _.mapValues(rules, (rule, key) => {
if (key === 'no-types-missing-file-annotation') {
if (['no-types-missing-file-annotation', 'require-valid-file-annotation'].includes(key)) {
return rule;
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/requireValidFileAnnotation.js
Expand Up @@ -113,7 +113,7 @@ const create = (context) => {
} else {
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
}
} else if (always) {
} else if (always && !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation')) {
context.report({
fix: (fixer) => {
let annotation;
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/checkFlowFileAnnotation.js
@@ -1,8 +1,9 @@
import _ from 'lodash';
import isFlowFile from './isFlowFile';
import isNoFlowFile from './isNoFlowFile';

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

if (!checkThisFile) {
return () => {};
Expand Down
1 change: 1 addition & 0 deletions src/utilities/index.js
Expand Up @@ -9,6 +9,7 @@ export {default as getParameterName} from './getParameterName';
export {default as getTokenAfterParens} from './getTokenAfterParens';
export {default as getTokenBeforeParens} from './getTokenBeforeParens';
export {default as isFlowFile} from './isFlowFile';
export {default as isNoFlowFile} from './isNoFlowFile';
export {default as isFlowFileAnnotation} from './isFlowFileAnnotation';
export {default as iterateFunctionNodes} from './iterateFunctionNodes';
export {default as quoteName} from './quoteName';
Expand Down
21 changes: 21 additions & 0 deletions src/utilities/isNoFlowFile.js
@@ -0,0 +1,21 @@
import isNoFlowFileAnnotation from './isNoFlowFileAnnotation';

/**
* Checks whether a file has an @flow or @noflow annotation.
*
* @param context
* @param [strict] - By default, the function returns true if the file starts with @flow but not if it
* starts by @noflow. When the strict flag is set to false, the function returns true if the flag has @noflow also.
*/

export default (context, strict = true) => {
const comments = context.getAllComments();

if (!comments.length) {
return false;
}

return comments.some((comment) => {
return isNoFlowFileAnnotation(comment.value, strict);
});
};
17 changes: 17 additions & 0 deletions src/utilities/isNoFlowFileAnnotation.js
@@ -0,0 +1,17 @@
import _ from 'lodash';

const FLOW_MATCHER = /^@noflow$/;

export default (comment, strict) => {
// The flow parser splits comments with the following regex to look for the @flow flag.
// See https://github.com/facebook/flow/blob/a96249b93541f2f7bfebd8d62085bf7a75de02f2/src/parsing/docblock.ml#L39
return _.some(comment.split(/[\t\n\r */\\]+/), (commentPart) => {
const match = commentPart.match(FLOW_MATCHER);

if (match === null) {
return false;
}

return !strict || match[0] === '@noflow';
});
};
14 changes: 14 additions & 0 deletions tests/rules/assertions/requireReturnType.js
Expand Up @@ -38,6 +38,14 @@ export default {
},
],
},
{
code: '/* @flow */\n(foo) => { return 1; }',
errors: [
{
message: 'Missing return type annotation.',
},
],
},
{
code: '(foo): undefined => { return; }',
errors: [
Expand Down Expand Up @@ -692,6 +700,12 @@ export default {
},
},
},
{
code: '/* @noflow */\n(foo) => { return 1; }',
options: [
'always',
],
},
{
code: '(foo) => { return undefined; }',
options: [
Expand Down

0 comments on commit e93f1c0

Please sign in to comment.