Skip to content

Commit

Permalink
feat: parse docblock more robustly for onlyFilesWithFlowAnnotation us…
Browse files Browse the repository at this point in the history
…age (#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.
  • Loading branch information
zpao authored and gajus committed May 8, 2019
1 parent 714a995 commit 397b7a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/utilities/isFlowFile.js
Expand Up @@ -15,9 +15,6 @@ export default (context, strict = true) => {
}

return comments.some((comment) => {
return (
isFlowFileAnnotation(comment.value) &&
!(strict && /no/.test(comment.value))
);
return isFlowFileAnnotation(comment.value, strict);
});
};
10 changes: 8 additions & 2 deletions src/utilities/isFlowFileAnnotation.js
Expand Up @@ -2,12 +2,18 @@ import _ from 'lodash';

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

export default (comment) => {
export default (comment, strict) => {
// eslint-disable-next-line flowtype/require-valid-file-annotation
// 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\r\n\\*/]+/), (commentPart) => {
return FLOW_MATCHER.test(commentPart);
const match = commentPart.match(FLOW_MATCHER);

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

return !strict || match[0] === '@flow';
});
};

25 changes: 23 additions & 2 deletions tests/rules/assertions/defineFlowType.js
Expand Up @@ -127,6 +127,25 @@ const VALID_WITH_DEFINE_FLOW_TYPE = [
'\'AType\' is not defined.',
'\'BType\' is not defined.'
]
},

// This tests to ensure we have a robust handling of @flow comments
{
// eslint-disable-next-line no-restricted-syntax
code: `
/**
* Copyright 2019 no corp
* @flow
*/
type Foo = $ReadOnly<{}>`,
errors: [
'\'$ReadOnly\' is not defined.'
],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
];

Expand Down Expand Up @@ -211,7 +230,8 @@ export default {
code: subject.code,
rules: {
'no-undef': 2
}
},
settings: subject.settings
};
}),
...VALID_WITH_DEFINE_FLOW_TYPE.map((subject) => {
Expand All @@ -223,7 +243,8 @@ export default {
2,
'nofunc'
]
}
},
settings: subject.settings
};
})
]
Expand Down

0 comments on commit 397b7a1

Please sign in to comment.