[lint] do not report issues when a matching flow suppression is present
Based on implementation of a similar case in D54776832.
Jan Kassens committed
Mar 13, 2024 at 11:40 UTC
b666bd163759df5afa8bcadc2c14f87f0d294a49
2 files changed
+36
compiler/packages/eslint-plugin-react-compiler/__tests__/ReactForgetDiagnostics-test.ts
+12
@@ -36,6 +36,18 @@ const tests: ForgetTestCases = {
36
}
37
`,
38
},
39
+ {
40
+ name: "Violation with Flow suppression",
41
+ code: `
42
+ // Valid since error already suppressed with flow.
43
+ function useHookWithHook() {
44
+ if (cond) {
45
+ // $FlowFixMe[react-rule-hook]
46
+ useConditionalHook();
47
+ }
48
+ }
49
+ `,
50
+ },
51
{
52
name: "Basic example with component syntax",
53
code: normalizeIndent`
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactForgetDiagnostics.ts
+24
@@ -92,6 +92,26 @@ const rule: Rule.RuleModule = {
92
options.logger?.logEvent("", err);
93
}
94
95
+ function hasFlowSuppression(
96
+ nodeLoc: BabelSourceLocation,
97
+ suppression: string
98
+ ) {
99
+ const sourceCode = context.getSourceCode();
100
+ const comments = sourceCode.getAllComments();
101
+ const flowSuppressionRegex = new RegExp(
102
+ "\\$FlowFixMe\\[" + suppression + "\\]"
103
+ );
104
+ for (const commentNode of comments) {
105
+ if (
106
+ flowSuppressionRegex.test(commentNode.value) &&
107
+ commentNode.loc!.end.line === nodeLoc.start.line - 1
108
+ ) {
109
+ return true;
110
+ }
111
+ }
112
+ return false;
113
+ }
114
+
115
const babelAST = HermesParser.parse(sourceCode, {
116
babel: true,
117
enableExperimentalComponentSyntax: true,
@@ -116,6 +136,10 @@ const rule: Rule.RuleModule = {
136
if (!isReportableDiagnostic(detail)) {
137
continue;
138
}
139
+ if (hasFlowSuppression(detail.loc, "react-rule-hook")) {
140
+ // If Flow already caught this error, we don't need to report it again.
141
+ continue;
142
+ }
143
let suggest: Array<Rule.SuggestionReportDescriptor> = [];
144
if (Array.isArray(detail.suggestions)) {
145
for (const suggestion of detail.suggestions) {