[compiler][be] Fix lint violations in eslint-plugin
ghstack-source-id: 7c11dce833fcf8f46aaa23858ac94a05e870fae8 Pull Request resolved: https://github.com/facebook/react/pull/30335
Mofei Zhang committed
Jul 15, 2024 at 17:44 UTC
6cca9c31847b4a31da0a3a5ceca659e5c642e993
3 files changed
+67
-63
compiler/packages/eslint-plugin-react-compiler/__tests__/ReactCompilerRule-test.ts
+1
-1
@@ -15,7 +15,7 @@ import ReactCompilerRule from "../src/rules/ReactCompilerRule";
15
*/
16
function normalizeIndent(strings: TemplateStringsArray): string {
17
const codeLines = strings[0].split("\n");
18
- const leftPadding = codeLines[1].match(/\s+/)[0];
18
+ const leftPadding = codeLines[1].match(/\s+/)![0];
19
return codeLines.map((line) => line.slice(leftPadding.length)).join("\n");
20
}
21
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts
+65
-61
@@ -6,22 +6,22 @@
6
*/
7
8
import { transformFromAstSync } from "@babel/core";
9
-// @ts-expect-error
9
+// @ts-expect-error: no types available
10
import PluginProposalPrivateMethods from "@babel/plugin-proposal-private-methods";
11
import type { SourceLocation as BabelSourceLocation } from "@babel/types";
12
import BabelPluginReactCompiler, {
13
+ CompilerErrorDetailOptions,
14
CompilerSuggestionOperation,
15
ErrorSeverity,
16
parsePluginOptions,
17
validateEnvironmentConfig,
18
type CompilerError,
18
- type CompilerErrorDetail,
19
type PluginOptions,
20
} from "babel-plugin-react-compiler/src";
21
import type { Rule } from "eslint";
22
import * as HermesParser from "hermes-parser";
23
24
-type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetail, "loc"> & {
24
+type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetailOptions, "loc"> & {
25
loc: BabelSourceLocation;
26
};
27
@@ -40,7 +40,7 @@ const DEFAULT_REPORTABLE_LEVELS = new Set([
40
let reportableLevels = DEFAULT_REPORTABLE_LEVELS;
41
42
function isReportableDiagnostic(
43
- detail: CompilerErrorDetail
43
+ detail: CompilerErrorDetailOptions
44
): detail is CompilerErrorDetailWithLoc {
45
return (
46
reportableLevels.has(detail.severity) &&
@@ -49,6 +49,59 @@ function isReportableDiagnostic(
49
);
50
}
51
52
+function makeSuggestions(
53
+ detail: CompilerErrorDetailOptions
54
+): Array<Rule.SuggestionReportDescriptor> {
55
+ let suggest: Array<Rule.SuggestionReportDescriptor> = [];
56
+ if (Array.isArray(detail.suggestions)) {
57
+ for (const suggestion of detail.suggestions) {
58
+ switch (suggestion.op) {
59
+ case CompilerSuggestionOperation.InsertBefore:
60
+ suggest.push({
61
+ desc: suggestion.description,
62
+ fix(fixer) {
63
+ return fixer.insertTextBeforeRange(
64
+ suggestion.range,
65
+ suggestion.text
66
+ );
67
+ },
68
+ });
69
+ break;
70
+ case CompilerSuggestionOperation.InsertAfter:
71
+ suggest.push({
72
+ desc: suggestion.description,
73
+ fix(fixer) {
74
+ return fixer.insertTextAfterRange(
75
+ suggestion.range,
76
+ suggestion.text
77
+ );
78
+ },
79
+ });
80
+ break;
81
+ case CompilerSuggestionOperation.Replace:
82
+ suggest.push({
83
+ desc: suggestion.description,
84
+ fix(fixer) {
85
+ return fixer.replaceTextRange(suggestion.range, suggestion.text);
86
+ },
87
+ });
88
+ break;
89
+ case CompilerSuggestionOperation.Remove:
90
+ suggest.push({
91
+ desc: suggestion.description,
92
+ fix(fixer) {
93
+ return fixer.removeRange(suggestion.range);
94
+ },
95
+ });
96
+ break;
97
+ default:
98
+ assertExhaustive(suggestion, "Unhandled suggestion operation");
99
+ }
100
+ }
101
+ }
102
+ return suggest;
103
+}
104
+
105
const COMPILER_OPTIONS: Partial<PluginOptions> = {
106
noEmit: true,
107
compilationMode: "infer",
@@ -96,7 +149,7 @@ const rule: Rule.RuleModule = {
149
function hasFlowSuppression(
150
nodeLoc: BabelSourceLocation,
151
suppression: string
99
- ) {
152
+ ): boolean {
153
const sourceCode = context.getSourceCode();
154
const comments = sourceCode.getAllComments();
155
const flowSuppressionRegex = new RegExp(
@@ -122,7 +175,9 @@ const rule: Rule.RuleModule = {
175
sourceType: "unambiguous",
176
plugins: ["typescript", "jsx"],
177
});
125
- } catch {}
178
+ } catch {
179
+ /* empty */
180
+ }
181
} else {
182
try {
183
babelAST = HermesParser.parse(sourceCode, {
@@ -131,7 +186,9 @@ const rule: Rule.RuleModule = {
186
sourceFilename: filename,
187
sourceType: "module",
188
});
134
- } catch {}
189
+ } catch {
190
+ /* empty */
191
+ }
192
}
193
194
if (babelAST != null) {
@@ -158,63 +215,10 @@ const rule: Rule.RuleModule = {
215
// If Flow already caught this error, we don't need to report it again.
216
continue;
217
}
161
- let suggest: Array<Rule.SuggestionReportDescriptor> = [];
162
- if (Array.isArray(detail.suggestions)) {
163
- for (const suggestion of detail.suggestions) {
164
- switch (suggestion.op) {
165
- case CompilerSuggestionOperation.InsertBefore:
166
- suggest.push({
167
- desc: suggestion.description,
168
- fix(fixer) {
169
- return fixer.insertTextBeforeRange(
170
- suggestion.range,
171
- suggestion.text
172
- );
173
- },
174
- });
175
- break;
176
- case CompilerSuggestionOperation.InsertAfter:
177
- suggest.push({
178
- desc: suggestion.description,
179
- fix(fixer) {
180
- return fixer.insertTextAfterRange(
181
- suggestion.range,
182
- suggestion.text
183
- );
184
- },
185
- });
186
- break;
187
- case CompilerSuggestionOperation.Replace:
188
- suggest.push({
189
- desc: suggestion.description,
190
- fix(fixer) {
191
- return fixer.replaceTextRange(
192
- suggestion.range,
193
- suggestion.text
194
- );
195
- },
196
- });
197
- break;
198
- case CompilerSuggestionOperation.Remove:
199
- suggest.push({
200
- desc: suggestion.description,
201
- fix(fixer) {
202
- return fixer.removeRange(suggestion.range);
203
- },
204
- });
205
- break;
206
- default:
207
- assertExhaustive(
208
- suggestion,
209
- "Unhandled suggestion operation"
210
- );
211
- }
212
- }
213
- }
218
context.report({
219
message: detail.reason,
220
loc: detail.loc,
217
- suggest,
221
+ suggest: makeSuggestions(detail),
222
});
223
}
224
} else {
compiler/yarn.lock
+1
-1
@@ -10072,4 +10072,4 @@ zod-validation-error@^3.0.3:
10072
zod@^3.22.4:
10073
version "3.22.4"
10074
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
10075
- integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
10075
+ integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
\ No newline at end of file