[lint] do not report issues when a matching flow suppression is present

Based on implementation of a similar case in D54776832.
This commit is contained in:
Jan Kassens
2024-03-13 11:40:35 -04:00
parent 56d96ba203
commit b666bd1637
2 changed files with 36 additions and 0 deletions
@@ -36,6 +36,18 @@ const tests: ForgetTestCases = {
}
`,
},
{
name: "Violation with Flow suppression",
code: `
// Valid since error already suppressed with flow.
function useHookWithHook() {
if (cond) {
// $FlowFixMe[react-rule-hook]
useConditionalHook();
}
}
`,
},
{
name: "Basic example with component syntax",
code: normalizeIndent`
@@ -92,6 +92,26 @@ const rule: Rule.RuleModule = {
options.logger?.logEvent("", err);
}
function hasFlowSuppression(
nodeLoc: BabelSourceLocation,
suppression: string
) {
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();
const flowSuppressionRegex = new RegExp(
"\\$FlowFixMe\\[" + suppression + "\\]"
);
for (const commentNode of comments) {
if (
flowSuppressionRegex.test(commentNode.value) &&
commentNode.loc!.end.line === nodeLoc.start.line - 1
) {
return true;
}
}
return false;
}
const babelAST = HermesParser.parse(sourceCode, {
babel: true,
enableExperimentalComponentSyntax: true,
@@ -116,6 +136,10 @@ const rule: Rule.RuleModule = {
if (!isReportableDiagnostic(detail)) {
continue;
}
if (hasFlowSuppression(detail.loc, "react-rule-hook")) {
// If Flow already caught this error, we don't need to report it again.
continue;
}
let suggest: Array<Rule.SuggestionReportDescriptor> = [];
if (Array.isArray(detail.suggestions)) {
for (const suggestion of detail.suggestions) {