From cd3f56bb18a88562d767d466ecae489fb3c2fb86 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Tue, 2 Apr 2024 15:31:25 +0100 Subject: [PATCH] [eslint] Enforce generic array type syntax Fix and enforce generic array type syntax --- .../babel-plugin-react-forget/.eslintrc.js | 2 +- .../scripts/jest/makeTransform.ts | 24 ++++++++++++------- .../scripts/jest/setupEnvE2E.js | 8 ++++--- .../scripts/prettier.js | 6 +++-- .../src/CompilerError.ts | 4 ++-- .../src/Entrypoint/Program.ts | 12 +++++----- .../src/HIR/BuildHIR.ts | 10 ++++---- .../babel-plugin-react-forget/src/HIR/HIR.ts | 6 ++--- .../src/HIR/HIRBuilder.ts | 6 ++--- .../src/Inference/AnalyseFunctions.ts | 2 +- .../ReactiveScopes/CodegenReactiveFunction.ts | 4 ++-- .../CollectReferencedGlobals.ts | 2 +- .../ReactiveScopes/PromoteUsedTemporaries.ts | 2 +- .../src/ReactiveScopes/RenameVariables.ts | 2 +- .../src/SSA/EnterSSA.ts | 4 ++-- 15 files changed, 53 insertions(+), 41 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/.eslintrc.js b/compiler/packages/babel-plugin-react-forget/.eslintrc.js index f0362087d9..b56cb97bce 100644 --- a/compiler/packages/babel-plugin-react-forget/.eslintrc.js +++ b/compiler/packages/babel-plugin-react-forget/.eslintrc.js @@ -73,7 +73,7 @@ module.exports = { "off", "constructor", ], - "@typescript-eslint/array-type": ["off", "generic"], + "@typescript-eslint/array-type": ["error", { default: "generic" }], "@typescript-eslint/triple-slash-reference": "off", }, parser: "@typescript-eslint/parser", diff --git a/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts b/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts index b32acb8174..5a7c71babd 100644 --- a/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts +++ b/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts @@ -43,8 +43,10 @@ module.exports = (useForget: boolean) => { ? [ ReactForgetFunctionTransform, { - // Jest hashes the babel config as a cache breaker. - // (see https://github.com/jestjs/jest/blob/v29.6.2/packages/babel-jest/src/index.ts#L84) + /* + * Jest hashes the babel config as a cache breaker. + * (see https://github.com/jestjs/jest/blob/v29.6.2/packages/babel-jest/src/index.ts#L84) + */ compilerCacheKey: execSync( "yarn --silent --cwd ../.. hash packages/babel-plugin-react-forget/dist" ).toString(), @@ -70,8 +72,10 @@ module.exports = (useForget: boolean) => { ) { const arg = path.node.arguments[0]; if (arg.type === "StringLiteral") { - // The compiler adds requires of "React", which is expected to be a wrapper - // around the "react" package. For tests, we just rewrite the require. + /* + * The compiler adds requires of "React", which is expected to be a wrapper + * around the "react" package. For tests, we just rewrite the require. + */ if (arg.value === "React") { arg.value = "react"; } @@ -90,8 +94,10 @@ module.exports = (useForget: boolean) => { esmodules: true, }, } as any); - // typecast needed as DefinitelyTyped does not have updated Babel configs types yet - // (missing passPerPreset and targets). + /* + * typecast needed as DefinitelyTyped does not have updated Babel configs types yet + * (missing passPerPreset and targets). + */ } return { @@ -104,8 +110,10 @@ function isReactComponentLike(fn: NodePath): boolean { let isReactComponent = false; let hasNoUseForgetDirective = false; - // React components start with an upper case letter, - // React hooks start with `use` + /* + * React components start with an upper case letter, + * React hooks start with `use` + */ if ( fn.node.id == null || (fn.node.id.name[0].toUpperCase() !== fn.node.id.name[0] && diff --git a/compiler/packages/babel-plugin-react-forget/scripts/jest/setupEnvE2E.js b/compiler/packages/babel-plugin-react-forget/scripts/jest/setupEnvE2E.js index 7f0ff80e6c..0236b4c1f7 100644 --- a/compiler/packages/babel-plugin-react-forget/scripts/jest/setupEnvE2E.js +++ b/compiler/packages/babel-plugin-react-forget/scripts/jest/setupEnvE2E.js @@ -7,8 +7,10 @@ const React = require("react"); -// Our e2e babel transform currently only compiles functions, not programs. -// As a result, our e2e transpiled code does not contain an import for `useMemoCache` -// This is a hack. +/* + * Our e2e babel transform currently only compiles functions, not programs. + * As a result, our e2e transpiled code does not contain an import for `useMemoCache` + * This is a hack. + */ React.useMemoCache = React.unstable_useMemoCache; globalThis.useMemoCache = React.unstable_useMemoCache; diff --git a/compiler/packages/babel-plugin-react-forget/scripts/prettier.js b/compiler/packages/babel-plugin-react-forget/scripts/prettier.js index 47d68fae77..4a17fb2eca 100644 --- a/compiler/packages/babel-plugin-react-forget/scripts/prettier.js +++ b/compiler/packages/babel-plugin-react-forget/scripts/prettier.js @@ -7,8 +7,10 @@ "use strict"; -// Based on similar script in React -// https://github.com/facebook/react/blob/main/scripts/prettier/index.js +/* + * Based on similar script in React + * https://github.com/facebook/react/blob/main/scripts/prettier/index.js + */ const chalk = require("chalk"); const glob = require("glob"); diff --git a/compiler/packages/babel-plugin-react-forget/src/CompilerError.ts b/compiler/packages/babel-plugin-react-forget/src/CompilerError.ts index 198d3a34b7..e15a11973b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/CompilerError.ts +++ b/compiler/packages/babel-plugin-react-forget/src/CompilerError.ts @@ -98,7 +98,7 @@ export class CompilerErrorDetail { } export class CompilerError extends Error { - details: CompilerErrorDetail[] = []; + details: Array = []; static invariant( condition: unknown, @@ -171,7 +171,7 @@ export class CompilerError extends Error { throw errors; } - constructor(...args: any[]) { + constructor(...args: Array) { super(...args); this.name = "ReactCompilerError"; } diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts index 441ae7e77a..f5020b559c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -35,11 +35,11 @@ import { export type CompilerPass = { opts: PluginOptions; filename: string | null; - comments: (t.CommentBlock | t.CommentLine)[]; + comments: Array; }; function findDirectiveEnablingMemoization( - directives: t.Directive[] + directives: Array ): t.Directive | null { for (const directive of directives) { const directiveValue = directive.value.value; @@ -51,7 +51,7 @@ function findDirectiveEnablingMemoization( } function findDirectiveDisablingMemoization( - directives: t.Directive[], + directives: Array, options: PluginOptions ): t.Directive | null { for (const directive of directives) { @@ -222,7 +222,7 @@ export function compileProgram( ); const lintError = suppressionsToCompilerError(suppressions); let hasCriticalError = lintError != null; - const compiledFns: CompileResult[] = []; + const compiledFns: Array = []; const traverseFunction = (fn: BabelFn, pass: CompilerPass): void => { const fnType = getReactFunctionType(fn, pass); @@ -333,7 +333,7 @@ export function compileProgram( } } - const externalFunctions: ExternalFunction[] = []; + const externalFunctions: Array = []; let gating: null | ExternalFunction = null; try { // TODO: check for duplicate import specifiers @@ -705,7 +705,7 @@ function getFunctionName( function checkFunctionReferencedBeforeDeclarationAtTopLevel( program: NodePath, - fns: BabelFn[] + fns: Array ): CompilerError | null { const fnIds = new Set( fns diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 642736e32e..5101f362b8 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -70,12 +70,12 @@ export function lower( func: NodePath, env: Environment, bindings: Bindings | null = null, - capturedRefs: t.Identifier[] = [], + capturedRefs: Array = [], // the outermost function being compiled, in case lower() is called recursively (for lambdas) parent: NodePath | null = null ): Result { const builder = new HIRBuilder(env, parent ?? func, bindings, capturedRefs); - const context: Place[] = []; + const context: Array = []; for (const ref of capturedRefs ?? []) { context.push({ @@ -168,7 +168,7 @@ export function lower( } }); - let directives: string[] = []; + let directives: Array = []; const body = func.get("body"); if (body.isExpression()) { const fallthrough = builder.reserve("block"); @@ -730,7 +730,7 @@ function lowerStatement( * Iterate through cases in reverse order, so that previous blocks can fallthrough * to successors */ - const cases: Case[] = []; + const cases: Array = []; let hasDefault = false; for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) { const case_: NodePath = stmt.get("cases")[ii]; @@ -3827,7 +3827,7 @@ function gatherCapturedDeps( | t.ObjectMethod >, componentScope: Scope -): { identifiers: t.Identifier[]; refs: Place[] } { +): { identifiers: Array; refs: Array } { const capturedIds: Map = new Map(); const capturedRefs: Set = new Set(); const seenPaths: Set = new Set(); diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts index 69ab35fc12..007ddfe6e4 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts @@ -55,7 +55,7 @@ export type ReactiveFunction = { async: boolean; body: ReactiveBlock; env: Environment; - directives: string[]; + directives: Array; }; export type ReactiveScopeBlock = { @@ -281,7 +281,7 @@ export type HIRFunction = { body: HIR; generator: boolean; async: boolean; - directives: string[]; + directives: Array; }; export type FunctionEffect = { @@ -423,7 +423,7 @@ export type BranchTerminal = { export type SwitchTerminal = { kind: "switch"; test: Place; - cases: Case[]; + cases: Array; fallthrough: BlockId | null; id: InstructionId; loc: SourceLocation; diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts index d70eda0887..966b778fb2 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/HIRBuilder.ts @@ -104,7 +104,7 @@ export default class HIRBuilder { #current: WipBlock; #entry: BlockId; #scopes: Array = []; - #context: t.Identifier[]; + #context: Array; #bindings: Bindings; #env: Environment; #exceptionHandlerStack: Array = []; @@ -115,7 +115,7 @@ export default class HIRBuilder { return this.#env.nextIdentifierId; } - get context(): t.Identifier[] { + get context(): Array { return this.#context; } @@ -131,7 +131,7 @@ export default class HIRBuilder { env: Environment, parentFunction: NodePath, // the outermost function being compiled bindings: Bindings | null = null, - context: t.Identifier[] | null = null + context: Array | null = null ) { this.#env = env; this.#bindings = bindings ?? new Map(); diff --git a/compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts b/compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts index 011c8147eb..abcab6773e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Inference/AnalyseFunctions.ts @@ -117,7 +117,7 @@ function lower(func: HIRFunction): void { function infer( loweredFunc: LoweredFunction, state: IdentifierState, - context: Place[] + context: Array ): void { const mutations = new Map(); for (const operand of loweredFunc.func.context) { diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 31d13943bd..11296c1059 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -1069,7 +1069,7 @@ function codegenDependency( return object; } -function withLoc t.Node>( +function withLoc) => t.Node>( fn: T ): ( loc: SourceLocation | null | undefined, @@ -1109,7 +1109,7 @@ const createStringLiteral = withLoc(t.stringLiteral); function createHookGuard( guard: ExternalFunction, - stmts: t.Statement[], + stmts: Array, before: GuardKind, after: GuardKind ): t.TryStatement { diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CollectReferencedGlobals.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CollectReferencedGlobals.ts index 58494c031d..3ca0d54fd9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CollectReferencedGlobals.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CollectReferencedGlobals.ts @@ -34,7 +34,7 @@ class Visitor extends ReactiveFunctionVisitor> { override visitReactiveFunctionValue( _id: InstructionId, - _dependencies: Place[], + _dependencies: Array, fn: ReactiveFunction, state: Set ): void { diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PromoteUsedTemporaries.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PromoteUsedTemporaries.ts index a810af95da..7af588c544 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PromoteUsedTemporaries.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PromoteUsedTemporaries.ts @@ -65,7 +65,7 @@ class Visitor extends ReactiveFunctionVisitor { override visitReactiveFunctionValue( _id: InstructionId, - _dependencies: Place[], + _dependencies: Array, fn: ReactiveFunction, state: VisitorState ): void { diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/RenameVariables.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/RenameVariables.ts index cb0df0500e..f8f690b3cd 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/RenameVariables.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/RenameVariables.ts @@ -104,7 +104,7 @@ class Visitor extends ReactiveFunctionVisitor { override visitReactiveFunctionValue( _id: InstructionId, - _dependencies: Place[], + _dependencies: Array, _fn: ReactiveFunction, _state: Scopes ): void { diff --git a/compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts b/compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts index 5067088ed7..1a0c26089b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts +++ b/compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts @@ -33,7 +33,7 @@ type IncompletePhi = { type State = { defs: Map; - incompletePhis: IncompletePhi[]; + incompletePhis: Array; }; class SSABuilder { @@ -213,7 +213,7 @@ class SSABuilder { } print(): void { - const text: string[] = []; + const text: Array = []; for (const [block, state] of this.#states) { text.push(`bb${block.id}:`); for (const [oldId, newId] of state.defs) {