From b666bd163759df5afa8bcadc2c14f87f0d294a49 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Wed, 13 Mar 2024 11:40:35 -0400 Subject: [PATCH] [lint] do not report issues when a matching flow suppression is present Based on implementation of a similar case in D54776832. --- .../__tests__/ReactForgetDiagnostics-test.ts | 12 ++++++++++ .../src/rules/ReactForgetDiagnostics.ts | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/compiler/packages/eslint-plugin-react-compiler/__tests__/ReactForgetDiagnostics-test.ts b/compiler/packages/eslint-plugin-react-compiler/__tests__/ReactForgetDiagnostics-test.ts index 8de211758f..643548603b 100644 --- a/compiler/packages/eslint-plugin-react-compiler/__tests__/ReactForgetDiagnostics-test.ts +++ b/compiler/packages/eslint-plugin-react-compiler/__tests__/ReactForgetDiagnostics-test.ts @@ -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` diff --git a/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactForgetDiagnostics.ts b/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactForgetDiagnostics.ts index 75515c0b41..134d43fc9e 100644 --- a/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactForgetDiagnostics.ts +++ b/compiler/packages/eslint-plugin-react-compiler/src/rules/ReactForgetDiagnostics.ts @@ -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 = []; if (Array.isArray(detail.suggestions)) { for (const suggestion of detail.suggestions) {