Skip to content
Permalink
Browse files
fix: replace annotations when they already exist
  • Loading branch information
gajus committed May 15, 2020
1 parent 68f2395 commit 353dce46a0e2a1f46234a9573772c5e6ddff2788
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
@@ -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);
@@ -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.');
@@ -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,
});
@@ -66,7 +66,7 @@ export default {
options: [
'always',
],
output: '// @flow\na;'
output: '// @flow\na;',
},
{
code: '/* @flow */',

0 comments on commit 353dce4

Please sign in to comment.