mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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:
+12
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user