Skip to content

Commit

Permalink
[compiler][be] Fix lint violations in eslint-plugin
Browse files Browse the repository at this point in the history
ghstack-source-id: 7c11dce833fcf8f46aaa23858ac94a05e870fae8
Pull Request resolved: facebook#30335
  • Loading branch information
mofeiZ committed Jul 15, 2024
1 parent 1d5a208 commit 6cca9c3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ReactCompilerRule from "../src/rules/ReactCompilerRule";
*/
function normalizeIndent(strings: TemplateStringsArray): string {
const codeLines = strings[0].split("\n");
const leftPadding = codeLines[1].match(/\s+/)[0];
const leftPadding = codeLines[1].match(/\s+/)![0];
return codeLines.map((line) => line.slice(leftPadding.length)).join("\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
*/

import { transformFromAstSync } from "@babel/core";
// @ts-expect-error
// @ts-expect-error: no types available
import PluginProposalPrivateMethods from "@babel/plugin-proposal-private-methods";
import type { SourceLocation as BabelSourceLocation } from "@babel/types";
import BabelPluginReactCompiler, {
CompilerErrorDetailOptions,
CompilerSuggestionOperation,
ErrorSeverity,
parsePluginOptions,
validateEnvironmentConfig,
type CompilerError,
type CompilerErrorDetail,
type PluginOptions,
} from "babel-plugin-react-compiler/src";
import type { Rule } from "eslint";
import * as HermesParser from "hermes-parser";

type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetail, "loc"> & {
type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetailOptions, "loc"> & {
loc: BabelSourceLocation;
};

Expand All @@ -40,7 +40,7 @@ const DEFAULT_REPORTABLE_LEVELS = new Set([
let reportableLevels = DEFAULT_REPORTABLE_LEVELS;

function isReportableDiagnostic(
detail: CompilerErrorDetail
detail: CompilerErrorDetailOptions
): detail is CompilerErrorDetailWithLoc {
return (
reportableLevels.has(detail.severity) &&
Expand All @@ -49,6 +49,59 @@ function isReportableDiagnostic(
);
}

function makeSuggestions(
detail: CompilerErrorDetailOptions
): Array<Rule.SuggestionReportDescriptor> {
let suggest: Array<Rule.SuggestionReportDescriptor> = [];
if (Array.isArray(detail.suggestions)) {
for (const suggestion of detail.suggestions) {
switch (suggestion.op) {
case CompilerSuggestionOperation.InsertBefore:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.insertTextBeforeRange(
suggestion.range,
suggestion.text
);
},
});
break;
case CompilerSuggestionOperation.InsertAfter:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.insertTextAfterRange(
suggestion.range,
suggestion.text
);
},
});
break;
case CompilerSuggestionOperation.Replace:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.replaceTextRange(suggestion.range, suggestion.text);
},
});
break;
case CompilerSuggestionOperation.Remove:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.removeRange(suggestion.range);
},
});
break;
default:
assertExhaustive(suggestion, "Unhandled suggestion operation");
}
}
}
return suggest;
}

const COMPILER_OPTIONS: Partial<PluginOptions> = {
noEmit: true,
compilationMode: "infer",
Expand Down Expand Up @@ -96,7 +149,7 @@ const rule: Rule.RuleModule = {
function hasFlowSuppression(
nodeLoc: BabelSourceLocation,
suppression: string
) {
): boolean {
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();
const flowSuppressionRegex = new RegExp(
Expand All @@ -122,7 +175,9 @@ const rule: Rule.RuleModule = {
sourceType: "unambiguous",
plugins: ["typescript", "jsx"],
});
} catch {}
} catch {
/* empty */
}
} else {
try {
babelAST = HermesParser.parse(sourceCode, {
Expand All @@ -131,7 +186,9 @@ const rule: Rule.RuleModule = {
sourceFilename: filename,
sourceType: "module",
});
} catch {}
} catch {
/* empty */
}
}

if (babelAST != null) {
Expand All @@ -158,63 +215,10 @@ const rule: Rule.RuleModule = {
// 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) {
switch (suggestion.op) {
case CompilerSuggestionOperation.InsertBefore:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.insertTextBeforeRange(
suggestion.range,
suggestion.text
);
},
});
break;
case CompilerSuggestionOperation.InsertAfter:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.insertTextAfterRange(
suggestion.range,
suggestion.text
);
},
});
break;
case CompilerSuggestionOperation.Replace:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.replaceTextRange(
suggestion.range,
suggestion.text
);
},
});
break;
case CompilerSuggestionOperation.Remove:
suggest.push({
desc: suggestion.description,
fix(fixer) {
return fixer.removeRange(suggestion.range);
},
});
break;
default:
assertExhaustive(
suggestion,
"Unhandled suggestion operation"
);
}
}
}
context.report({
message: detail.reason,
loc: detail.loc,
suggest,
suggest: makeSuggestions(detail),
});
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10072,4 +10072,4 @@ zod-validation-error@^3.0.3:
zod@^3.22.4:
version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==

0 comments on commit 6cca9c3

Please sign in to comment.