Skip to content

Commit

Permalink
fix: replace annotations when they already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 15, 2020
1 parent 68f2395 commit 353dce4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
70 changes: 39 additions & 31 deletions src/rules/requireValidFileAnnotation.js
Expand Up @@ -62,26 +62,6 @@ const create = (context) => {
return {
Program (node) {
const firstToken = node.tokens[0];
const addAnnotation = () => {
return (fixer) => {
let annotation;
if (flowStrict) {
annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';
} else {
annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';
}

return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};

const addStrictAnnotation = () => {
return (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';

return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};

const potentialFlowFileAnnotation = _.find(context.getAllComments(), (comment) => {
return looksLikeFlowFileAnnotation(comment.value);
Expand All @@ -92,30 +72,41 @@ const create = (context) => {
context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.');
}
const annotationValue = potentialFlowFileAnnotation.value.trim();

if (isFlowFileAnnotation(annotationValue)) {
if (!isValidAnnotationStyle(potentialFlowFileAnnotation, style)) {
const annotation = style === 'line' ? '// ' + annotationValue : '/* ' + annotationValue + ' */';

context.report({
fix: (fixer) => {
return fixer.replaceTextRange(
[potentialFlowFileAnnotation.range[0], potentialFlowFileAnnotation.range[1]],
[
potentialFlowFileAnnotation.range[0],
potentialFlowFileAnnotation.range[1],
],
annotation,
);
},
message: 'Flow file annotation style must be `' + annotation + '`',
node: potentialFlowFileAnnotation,
});
}
if (!noFlowAnnotation(annotationValue) && flowStrict) {
if (!isFlowStrict(annotationValue)) {
const str = style === 'line' ? '`// @flow strict`' : '`/* @flow strict */`';
context.report({
fix: addStrictAnnotation(),
message: 'Strict Flow file annotation is required, should be ' + str,
node,
});
}

if (!noFlowAnnotation(annotationValue) && flowStrict && !isFlowStrict(annotationValue)) {
const str = style === 'line' ? '`// @flow strict`' : '`/* @flow strict */`';

context.report({
fix: (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow strict' : '/* @flow strict */';

return fixer.replaceTextRange([
potentialFlowFileAnnotation.range[0],
potentialFlowFileAnnotation.range[1],
], annotation);
},
message: 'Strict Flow file annotation is required, should be ' + str,
node,
});
}
} else if (checkAnnotationSpelling(annotationValue)) {
context.report(potentialFlowFileAnnotation, 'Misspelled or malformed Flow file annotation.');
Expand All @@ -124,7 +115,24 @@ const create = (context) => {
}
} else if (always) {
context.report({
fix: addAnnotation(),
fix: (fixer) => {
let annotation;

if (flowStrict) {
annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';
} else {
annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';
}

return fixer
.replaceTextRange(
[
node.range[0],
node.range[0],
],
annotation,
);
},
message: 'Flow file annotation is missing.',
node,
});
Expand Down
2 changes: 1 addition & 1 deletion tests/rules/assertions/requireValidFileAnnotation.js
Expand Up @@ -66,7 +66,7 @@ export default {
options: [
'always',
],
output: '// @flow\na;'
output: '// @flow\na;',
},
{
code: '/* @flow */',
Expand Down

0 comments on commit 353dce4

Please sign in to comment.