@samitouri / QOS-React / commits / cd3f56bb18

[eslint] Enforce generic array type syntax

Fix and enforce generic array type syntax

Sathya Gunasekaran committed Apr 2, 2024 at 15:31 UTC cd3f56bb18a88562d767d466ecae489fb3c2fb86
15 files changed +53 -41
compiler/packages/babel-plugin-react-forget/.eslintrc.js
+1 -1
@@ -73,7 +73,7 @@ module.exports = {
73 "off",
74 "constructor",
75 ],
76 - "@typescript-eslint/array-type": ["off", "generic"],
76 + "@typescript-eslint/array-type": ["error", { default: "generic" }],
77 "@typescript-eslint/triple-slash-reference": "off",
78 },
79 parser: "@typescript-eslint/parser",
compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts
+16 -8
@@ -43,8 +43,10 @@ module.exports = (useForget: boolean) => {
43 ? [
44 ReactForgetFunctionTransform,
45 {
46 - // Jest hashes the babel config as a cache breaker.
47 - // (see https://github.com/jestjs/jest/blob/v29.6.2/packages/babel-jest/src/index.ts#L84)
46 + /*
47 + * Jest hashes the babel config as a cache breaker.
48 + * (see https://github.com/jestjs/jest/blob/v29.6.2/packages/babel-jest/src/index.ts#L84)
49 + */
50 compilerCacheKey: execSync(
51 "yarn --silent --cwd ../.. hash packages/babel-plugin-react-forget/dist"
52 ).toString(),
@@ -70,8 +72,10 @@ module.exports = (useForget: boolean) => {
72 ) {
73 const arg = path.node.arguments[0];
74 if (arg.type === "StringLiteral") {
73 - // The compiler adds requires of "React", which is expected to be a wrapper
74 - // around the "react" package. For tests, we just rewrite the require.
75 + /*
76 + * The compiler adds requires of "React", which is expected to be a wrapper
77 + * around the "react" package. For tests, we just rewrite the require.
78 + */
79 if (arg.value === "React") {
80 arg.value = "react";
81 }
@@ -90,8 +94,10 @@ module.exports = (useForget: boolean) => {
94 esmodules: true,
95 },
96 } as any);
93 - // typecast needed as DefinitelyTyped does not have updated Babel configs types yet
94 - // (missing passPerPreset and targets).
97 + /*
98 + * typecast needed as DefinitelyTyped does not have updated Babel configs types yet
99 + * (missing passPerPreset and targets).
100 + */
101 }
102
103 return {
@@ -104,8 +110,10 @@ function isReactComponentLike(fn: NodePath<FunctionDeclaration>): boolean {
110 let isReactComponent = false;
111 let hasNoUseForgetDirective = false;
112
107 - // React components start with an upper case letter,
108 - // React hooks start with `use`
113 + /*
114 + * React components start with an upper case letter,
115 + * React hooks start with `use`
116 + */
117 if (
118 fn.node.id == null ||
119 (fn.node.id.name[0].toUpperCase() !== fn.node.id.name[0] &&
compiler/packages/babel-plugin-react-forget/scripts/jest/setupEnvE2E.js
+5 -3
@@ -7,8 +7,10 @@
7
8 const React = require("react");
9
10 -// Our e2e babel transform currently only compiles functions, not programs.
11 -// As a result, our e2e transpiled code does not contain an import for `useMemoCache`
12 -// This is a hack.
10 +/*
11 + * Our e2e babel transform currently only compiles functions, not programs.
12 + * As a result, our e2e transpiled code does not contain an import for `useMemoCache`
13 + * This is a hack.
14 + */
15 React.useMemoCache = React.unstable_useMemoCache;
16 globalThis.useMemoCache = React.unstable_useMemoCache;
compiler/packages/babel-plugin-react-forget/scripts/prettier.js
+4 -2
@@ -7,8 +7,10 @@
7
8 "use strict";
9
10 -// Based on similar script in React
11 -// https://github.com/facebook/react/blob/main/scripts/prettier/index.js
10 +/*
11 + * Based on similar script in React
12 + * https://github.com/facebook/react/blob/main/scripts/prettier/index.js
13 + */
14
15 const chalk = require("chalk");
16 const glob = require("glob");
compiler/packages/babel-plugin-react-forget/src/CompilerError.ts
+2 -2
@@ -98,7 +98,7 @@ export class CompilerErrorDetail {
98 }
99
100 export class CompilerError extends Error {
101 - details: CompilerErrorDetail[] = [];
101 + details: Array<CompilerErrorDetail> = [];
102
103 static invariant(
104 condition: unknown,
@@ -171,7 +171,7 @@ export class CompilerError extends Error {
171 throw errors;
172 }
173
174 - constructor(...args: any[]) {
174 + constructor(...args: Array<any>) {
175 super(...args);
176 this.name = "ReactCompilerError";
177 }
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+6 -6
@@ -35,11 +35,11 @@ import {
35 export type CompilerPass = {
36 opts: PluginOptions;
37 filename: string | null;
38 - comments: (t.CommentBlock | t.CommentLine)[];
38 + comments: Array<t.CommentBlock | t.CommentLine>;
39 };
40
41 function findDirectiveEnablingMemoization(
42 - directives: t.Directive[]
42 + directives: Array<t.Directive>
43 ): t.Directive | null {
44 for (const directive of directives) {
45 const directiveValue = directive.value.value;
@@ -51,7 +51,7 @@ function findDirectiveEnablingMemoization(
51 }
52
53 function findDirectiveDisablingMemoization(
54 - directives: t.Directive[],
54 + directives: Array<t.Directive>,
55 options: PluginOptions
56 ): t.Directive | null {
57 for (const directive of directives) {
@@ -222,7 +222,7 @@ export function compileProgram(
222 );
223 const lintError = suppressionsToCompilerError(suppressions);
224 let hasCriticalError = lintError != null;
225 - const compiledFns: CompileResult[] = [];
225 + const compiledFns: Array<CompileResult> = [];
226
227 const traverseFunction = (fn: BabelFn, pass: CompilerPass): void => {
228 const fnType = getReactFunctionType(fn, pass);
@@ -333,7 +333,7 @@ export function compileProgram(
333 }
334 }
335
336 - const externalFunctions: ExternalFunction[] = [];
336 + const externalFunctions: Array<ExternalFunction> = [];
337 let gating: null | ExternalFunction = null;
338 try {
339 // TODO: check for duplicate import specifiers
@@ -705,7 +705,7 @@ function getFunctionName(
705
706 function checkFunctionReferencedBeforeDeclarationAtTopLevel(
707 program: NodePath<t.Program>,
708 - fns: BabelFn[]
708 + fns: Array<BabelFn>
709 ): CompilerError | null {
710 const fnIds = new Set(
711 fns
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+5 -5
@@ -70,12 +70,12 @@ export function lower(
70 func: NodePath<t.Function>,
71 env: Environment,
72 bindings: Bindings | null = null,
73 - capturedRefs: t.Identifier[] = [],
73 + capturedRefs: Array<t.Identifier> = [],
74 // the outermost function being compiled, in case lower() is called recursively (for lambdas)
75 parent: NodePath<t.Function> | null = null
76 ): Result<HIRFunction, CompilerError> {
77 const builder = new HIRBuilder(env, parent ?? func, bindings, capturedRefs);
78 - const context: Place[] = [];
78 + const context: Array<Place> = [];
79
80 for (const ref of capturedRefs ?? []) {
81 context.push({
@@ -168,7 +168,7 @@ export function lower(
168 }
169 });
170
171 - let directives: string[] = [];
171 + let directives: Array<string> = [];
172 const body = func.get("body");
173 if (body.isExpression()) {
174 const fallthrough = builder.reserve("block");
@@ -730,7 +730,7 @@ function lowerStatement(
730 * Iterate through cases in reverse order, so that previous blocks can fallthrough
731 * to successors
732 */
733 - const cases: Case[] = [];
733 + const cases: Array<Case> = [];
734 let hasDefault = false;
735 for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) {
736 const case_: NodePath<t.SwitchCase> = stmt.get("cases")[ii];
@@ -3827,7 +3827,7 @@ function gatherCapturedDeps(
3827 | t.ObjectMethod
3828 >,
3829 componentScope: Scope
3830 -): { identifiers: t.Identifier[]; refs: Place[] } {
3830 +): { identifiers: Array<t.Identifier>; refs: Array<Place> } {
3831 const capturedIds: Map<t.Identifier, number> = new Map();
3832 const capturedRefs: Set<Place> = new Set();
3833 const seenPaths: Set<string> = new Set();
compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts
+3 -3
@@ -55,7 +55,7 @@ export type ReactiveFunction = {
55 async: boolean;
56 body: ReactiveBlock;
57 env: Environment;
58 - directives: string[];
58 + directives: Array<string>;
59 };
60
61 export type ReactiveScopeBlock = {
@@ -281,7 +281,7 @@ export type HIRFunction = {
281 body: HIR;
282 generator: boolean;
283 async: boolean;
284 - directives: string[];
284 + directives: Array<string>;
285 };
286
287 export type FunctionEffect = {
@@ -423,7 +423,7 @@ export type BranchTerminal = {
423 export type SwitchTerminal = {
424 kind: "switch";
425 test: Place;
426 - cases: Case[];
426 + cases: Array<Case>;
427 fallthrough: BlockId | null;
428 id: InstructionId;
429 loc: SourceLocation;
compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts
+3 -3
@@ -104,7 +104,7 @@ export default class HIRBuilder {
104 #current: WipBlock;
105 #entry: BlockId;
106 #scopes: Array<Scope> = [];
107 - #context: t.Identifier[];
107 + #context: Array<t.Identifier>;
108 #bindings: Bindings;
109 #env: Environment;
110 #exceptionHandlerStack: Array<BlockId> = [];
@@ -115,7 +115,7 @@ export default class HIRBuilder {
115 return this.#env.nextIdentifierId;
116 }
117
118 - get context(): t.Identifier[] {
118 + get context(): Array<t.Identifier> {
119 return this.#context;
120 }
121
@@ -131,7 +131,7 @@ export default class HIRBuilder {
131 env: Environment,
132 parentFunction: NodePath<t.Function>, // the outermost function being compiled
133 bindings: Bindings | null = null,
134 - context: t.Identifier[] | null = null
134 + context: Array<t.Identifier> | null = null
135 ) {
136 this.#env = env;
137 this.#bindings = bindings ?? new Map();
compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts
+1 -1
@@ -117,7 +117,7 @@ function lower(func: HIRFunction): void {
117 function infer(
118 loweredFunc: LoweredFunction,
119 state: IdentifierState,
120 - context: Place[]
120 + context: Array<Place>
121 ): void {
122 const mutations = new Map<string, Effect>();
123 for (const operand of loweredFunc.func.context) {
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+2 -2
@@ -1069,7 +1069,7 @@ function codegenDependency(
1069 return object;
1070 }
1071
1072 -function withLoc<T extends (...args: any[]) => t.Node>(
1072 +function withLoc<T extends (...args: Array<any>) => t.Node>(
1073 fn: T
1074 ): (
1075 loc: SourceLocation | null | undefined,
@@ -1109,7 +1109,7 @@ const createStringLiteral = withLoc(t.stringLiteral);
1109
1110 function createHookGuard(
1111 guard: ExternalFunction,
1112 - stmts: t.Statement[],
1112 + stmts: Array<t.Statement>,
1113 before: GuardKind,
1114 after: GuardKind
1115 ): t.TryStatement {
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CollectReferencedGlobals.ts
+1 -1
@@ -34,7 +34,7 @@ class Visitor extends ReactiveFunctionVisitor<Set<string>> {
34
35 override visitReactiveFunctionValue(
36 _id: InstructionId,
37 - _dependencies: Place[],
37 + _dependencies: Array<Place>,
38 fn: ReactiveFunction,
39 state: Set<string>
40 ): void {
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PromoteUsedTemporaries.ts
+1 -1
@@ -65,7 +65,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
65
66 override visitReactiveFunctionValue(
67 _id: InstructionId,
68 - _dependencies: Place[],
68 + _dependencies: Array<Place>,
69 fn: ReactiveFunction,
70 state: VisitorState
71 ): void {
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/RenameVariables.ts
+1 -1
@@ -104,7 +104,7 @@ class Visitor extends ReactiveFunctionVisitor<Scopes> {
104
105 override visitReactiveFunctionValue(
106 _id: InstructionId,
107 - _dependencies: Place[],
107 + _dependencies: Array<Place>,
108 _fn: ReactiveFunction,
109 _state: Scopes
110 ): void {
compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts
+2 -2
@@ -33,7 +33,7 @@ type IncompletePhi = {
33
34 type State = {
35 defs: Map<Identifier, Identifier>;
36 - incompletePhis: IncompletePhi[];
36 + incompletePhis: Array<IncompletePhi>;
37 };
38
39 class SSABuilder {
@@ -213,7 +213,7 @@ class SSABuilder {
213 }
214
215 print(): void {
216 - const text: string[] = [];
216 + const text: Array<string> = [];
217 for (const [block, state] of this.#states) {
218 text.push(`bb${block.id}:`);
219 for (const [oldId, newId] of state.defs) {