Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler][be] Fix lint violations in eslint-plugin #30335

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[compiler][be] Fix lint rules in eslint-plugin
[ghstack-poisoned]
  • Loading branch information
mofeiZ committed Jul 15, 2024
commit 7f159a5df93ce3cd6e28eec16e3a47d5f712852a
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 @@ -13,7 +13,7 @@
"dependencies": {
"@babel/core": "^7.24.4",
"@babel/parser": "^7.24.4",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-transform-private-methods": "^7.24.7",
"hermes-parser": "^0.20.1",
"zod": "^3.22.4",
"zod-validation-error": "^3.0.3"
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
import PluginProposalPrivateMethods from "@babel/plugin-proposal-private-methods";
// @ts-expect-error: no types available
import PluginProposalPrivateMethods from "@babel/plugin-transform-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
Loading