Skip to content

Commit

Permalink
fix: @noflow support for strict (#415)
Browse files Browse the repository at this point in the history
* Fix @noflow support for strict

* Update regex

* docs: generate docs
  • Loading branch information
xunuoi authored and gajus committed Jun 24, 2019
1 parent e36b920 commit 9b37c35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3162,6 +3162,9 @@ a;
// Options: ["always",{"annotationStyle":"line"}]
// @flow

// Options: ["always",{"annotationStyle":"line","strict":true}]
// @noflow

// Options: ["always",{"annotationStyle":"line","strict":true}]
// @flow strict

Expand Down
18 changes: 11 additions & 7 deletions src/rules/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const checkAnnotationSpelling = (comment) => {
};

const isFlowStrict = (comment) => {
return /@flow\sstrict\b/.test(comment);
return /^@flow\sstrict\b/.test(comment);
};

const noFlowAnnotation = (comment) => {
return /^@noflow\b/.test(comment);
};

const schema = [
Expand Down Expand Up @@ -87,15 +91,15 @@ const create = (context) => {
if (firstToken && firstToken.start < potentialFlowFileAnnotation.start) {
context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.');
}

if (isFlowFileAnnotation(potentialFlowFileAnnotation.value.trim())) {
const annotationValue = potentialFlowFileAnnotation.value.trim();
if (isFlowFileAnnotation(annotationValue)) {
if (!isValidAnnotationStyle(potentialFlowFileAnnotation, style)) {
const str = style === 'line' ? '`// ' + potentialFlowFileAnnotation.value.trim() + '`' : '`/* ' + potentialFlowFileAnnotation.value.trim() + ' */`';
const str = style === 'line' ? '`// ' + annotationValue + '`' : '`/* ' + annotationValue + ' */`';

context.report(potentialFlowFileAnnotation, 'Flow file annotation style must be ' + str);
}
if (flowStrict) {
if (!isFlowStrict(potentialFlowFileAnnotation.value.trim())) {
if (!noFlowAnnotation(annotationValue) && flowStrict) {
if (!isFlowStrict(annotationValue)) {
const str = style === 'line' ? '`// @flow strict`' : '`/* @flow strict */`';
context.report({
fix: addStrictAnnotation(),
Expand All @@ -104,7 +108,7 @@ const create = (context) => {
});
}
}
} else if (checkAnnotationSpelling(potentialFlowFileAnnotation.value.trim())) {
} else if (checkAnnotationSpelling(annotationValue)) {
context.report(potentialFlowFileAnnotation, 'Misspelled or malformed Flow file annotation.');
} else {
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
Expand Down
10 changes: 10 additions & 0 deletions tests/rules/assertions/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ export default {
}
]
},
{
code: '// @noflow',
options: [
'always',
{
annotationStyle: 'line',
strict: true
}
]
},
{
code: '// @flow strict',
options: [
Expand Down

0 comments on commit 9b37c35

Please sign in to comment.