[eslint] Enforce generic array type syntax

Fix and enforce generic array type syntax
This commit is contained in:
Sathya Gunasekaran
2024-04-02 15:31:25 +01:00
parent 1de6c5aea2
commit cd3f56bb18
15 changed files with 53 additions and 41 deletions
@@ -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",
@@ -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<FunctionDeclaration>): 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] &&
@@ -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;
@@ -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");
@@ -98,7 +98,7 @@ export class CompilerErrorDetail {
}
export class CompilerError extends Error {
details: CompilerErrorDetail[] = [];
details: Array<CompilerErrorDetail> = [];
static invariant(
condition: unknown,
@@ -171,7 +171,7 @@ export class CompilerError extends Error {
throw errors;
}
constructor(...args: any[]) {
constructor(...args: Array<any>) {
super(...args);
this.name = "ReactCompilerError";
}
@@ -35,11 +35,11 @@ import {
export type CompilerPass = {
opts: PluginOptions;
filename: string | null;
comments: (t.CommentBlock | t.CommentLine)[];
comments: Array<t.CommentBlock | t.CommentLine>;
};
function findDirectiveEnablingMemoization(
directives: t.Directive[]
directives: Array<t.Directive>
): 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<t.Directive>,
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<CompileResult> = [];
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<ExternalFunction> = [];
let gating: null | ExternalFunction = null;
try {
// TODO: check for duplicate import specifiers
@@ -705,7 +705,7 @@ function getFunctionName(
function checkFunctionReferencedBeforeDeclarationAtTopLevel(
program: NodePath<t.Program>,
fns: BabelFn[]
fns: Array<BabelFn>
): CompilerError | null {
const fnIds = new Set(
fns
@@ -70,12 +70,12 @@ export function lower(
func: NodePath<t.Function>,
env: Environment,
bindings: Bindings | null = null,
capturedRefs: t.Identifier[] = [],
capturedRefs: Array<t.Identifier> = [],
// the outermost function being compiled, in case lower() is called recursively (for lambdas)
parent: NodePath<t.Function> | null = null
): Result<HIRFunction, CompilerError> {
const builder = new HIRBuilder(env, parent ?? func, bindings, capturedRefs);
const context: Place[] = [];
const context: Array<Place> = [];
for (const ref of capturedRefs ?? []) {
context.push({
@@ -168,7 +168,7 @@ export function lower(
}
});
let directives: string[] = [];
let directives: Array<string> = [];
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<Case> = [];
let hasDefault = false;
for (let ii = stmt.get("cases").length - 1; ii >= 0; ii--) {
const case_: NodePath<t.SwitchCase> = stmt.get("cases")[ii];
@@ -3827,7 +3827,7 @@ function gatherCapturedDeps(
| t.ObjectMethod
>,
componentScope: Scope
): { identifiers: t.Identifier[]; refs: Place[] } {
): { identifiers: Array<t.Identifier>; refs: Array<Place> } {
const capturedIds: Map<t.Identifier, number> = new Map();
const capturedRefs: Set<Place> = new Set();
const seenPaths: Set<string> = new Set();
@@ -55,7 +55,7 @@ export type ReactiveFunction = {
async: boolean;
body: ReactiveBlock;
env: Environment;
directives: string[];
directives: Array<string>;
};
export type ReactiveScopeBlock = {
@@ -281,7 +281,7 @@ export type HIRFunction = {
body: HIR;
generator: boolean;
async: boolean;
directives: string[];
directives: Array<string>;
};
export type FunctionEffect = {
@@ -423,7 +423,7 @@ export type BranchTerminal = {
export type SwitchTerminal = {
kind: "switch";
test: Place;
cases: Case[];
cases: Array<Case>;
fallthrough: BlockId | null;
id: InstructionId;
loc: SourceLocation;
@@ -104,7 +104,7 @@ export default class HIRBuilder {
#current: WipBlock;
#entry: BlockId;
#scopes: Array<Scope> = [];
#context: t.Identifier[];
#context: Array<t.Identifier>;
#bindings: Bindings;
#env: Environment;
#exceptionHandlerStack: Array<BlockId> = [];
@@ -115,7 +115,7 @@ export default class HIRBuilder {
return this.#env.nextIdentifierId;
}
get context(): t.Identifier[] {
get context(): Array<t.Identifier> {
return this.#context;
}
@@ -131,7 +131,7 @@ export default class HIRBuilder {
env: Environment,
parentFunction: NodePath<t.Function>, // the outermost function being compiled
bindings: Bindings | null = null,
context: t.Identifier[] | null = null
context: Array<t.Identifier> | null = null
) {
this.#env = env;
this.#bindings = bindings ?? new Map();
@@ -117,7 +117,7 @@ function lower(func: HIRFunction): void {
function infer(
loweredFunc: LoweredFunction,
state: IdentifierState,
context: Place[]
context: Array<Place>
): void {
const mutations = new Map<string, Effect>();
for (const operand of loweredFunc.func.context) {
@@ -1069,7 +1069,7 @@ function codegenDependency(
return object;
}
function withLoc<T extends (...args: any[]) => t.Node>(
function withLoc<T extends (...args: Array<any>) => 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<t.Statement>,
before: GuardKind,
after: GuardKind
): t.TryStatement {
@@ -34,7 +34,7 @@ class Visitor extends ReactiveFunctionVisitor<Set<string>> {
override visitReactiveFunctionValue(
_id: InstructionId,
_dependencies: Place[],
_dependencies: Array<Place>,
fn: ReactiveFunction,
state: Set<string>
): void {
@@ -65,7 +65,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
override visitReactiveFunctionValue(
_id: InstructionId,
_dependencies: Place[],
_dependencies: Array<Place>,
fn: ReactiveFunction,
state: VisitorState
): void {
@@ -104,7 +104,7 @@ class Visitor extends ReactiveFunctionVisitor<Scopes> {
override visitReactiveFunctionValue(
_id: InstructionId,
_dependencies: Place[],
_dependencies: Array<Place>,
_fn: ReactiveFunction,
_state: Scopes
): void {
@@ -33,7 +33,7 @@ type IncompletePhi = {
type State = {
defs: Map<Identifier, Identifier>;
incompletePhis: IncompletePhi[];
incompletePhis: Array<IncompletePhi>;
};
class SSABuilder {
@@ -213,7 +213,7 @@ class SSABuilder {
}
print(): void {
const text: string[] = [];
const text: Array<string> = [];
for (const [block, state] of this.#states) {
text.push(`bb${block.id}:`);
for (const [oldId, newId] of state.defs) {