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 1a7dde3cbc..5bfa3c0ade 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -1456,14 +1456,6 @@ function lowerExpression( elements.push({ kind: "Hole", }); - if (builder.environment.config.bailoutOnHoleyArrays) { - builder.errors.push({ - reason: `(BuildHIR::lower) Fix babel holey array backward compatibility.`, - severity: ErrorSeverity.Todo, - loc: expr.node.loc ?? null, - suggestions: null, - }); - } continue; } else if (element.isExpression()) { elements.push(lowerExpressionToTemporary(builder, element)); @@ -3289,15 +3281,6 @@ function lowerAssignment( items.push({ kind: "Hole", }); - - if (builder.environment.config.bailoutOnHoleyArrays) { - builder.errors.push({ - reason: `(BuildHIR::lower) Fix babel holey array backward compatibility.`, - severity: ErrorSeverity.Todo, - loc: lvalue.node.loc ?? null, - suggestions: null, - }); - } continue; } if (element.isRestElement()) { diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index 6c6612f50f..67c1e7d702 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -256,30 +256,6 @@ const EnvironmentConfigSchema = z.object({ // Enable validation of mutable ranges assertValidMutableRanges: z.boolean().default(false), - /* - * - * Instead of handling holey arrays, bail out with a TODO error. - * - * Older versions of babel seem to have inconsistent handling of holey arrays, - * at least when paired with HermesParser. When using these versions, we should - * bail out instead of throwing a Babel validation error. - * - * The babel ast definition for array elements changed from Array - * to Array. Older versions does not expect null in the - * ArrayPattern ast and will throw a validation error. - * - * - HermesParser will parse [, b] into [NodePath, NodePath] - * - Forget will try to preserve this holey array when we codegen back to js - * (e.g. we call a babel builder function arrayPattern([null, identifier])) - * - Babel will fail with `TypeError: Property elements[0] of ArrayPattern - * expected node to be of a type ["PatternLike"] but instead got null` - * - * PR that changed the AST definition - * https://github.com/babel/babel/pull/10917/files#diff-19b555d2f3904c206af406540d9df200b1e16befedb83ff39ebfcbd876f7fa8aL52-R56 - * - */ - bailoutOnHoleyArrays: z.boolean().default(false), - /* * Enable emitting "change variables" which store the result of whether a particular * reactive scope dependency has changed since the scope was last executed. 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 1a3f3ac723..e1e92790b6 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -10,6 +10,7 @@ import { pruneUnusedLValues, pruneUnusedLabels, renameVariables } from "."; import { CompilerError, ErrorSeverity } from "../CompilerError"; import { Environment, EnvironmentConfig, ExternalFunction } from "../HIR"; import { + ArrayPattern, BlockId, GeneratedSource, Identifier, @@ -1827,20 +1828,51 @@ function codegenObjectPropertyKey( } } +function codegenArrayPattern( + cx: Context, + pattern: ArrayPattern +): t.ArrayPattern { + const hasHoles = !pattern.items.every((e) => e.kind !== "Hole"); + if (hasHoles) { + const result = t.arrayPattern([]); + /* + * Older versions of babel have a validation bug fixed by + * https://github.com/babel/babel/pull/10917 + * https://github.com/babel/babel/commit/e7b80a2cb93cf28010207fc3cdd19b4568ca35b9#diff-19b555d2f3904c206af406540d9df200b1e16befedb83ff39ebfcbd876f7fa8aL52 + * + * Link to buggy older version (observe that elements must be PatternLikes here) + * https://github.com/babel/babel/blob/v7.7.4/packages/babel-types/src/definitions/es2015.js#L50-L53 + * + * Link to newer versions with correct validation (observe elements can be PatternLike | null) + * https://github.com/babel/babel/blob/v7.23.0/packages/babel-types/src/definitions/core.ts#L1306-L1311 + */ + for (const item of pattern.items) { + if (item.kind === "Hole") { + result.elements.push(null); + } else { + result.elements.push(codegenLValue(cx, item)); + } + } + return result; + } else { + return t.arrayPattern( + pattern.items.map((item) => { + if (item.kind === "Hole") { + return null; + } + return codegenLValue(cx, item); + }) + ); + } +} + function codegenLValue( cx: Context, pattern: Pattern | Place | SpreadPattern ): t.ArrayPattern | t.ObjectPattern | t.RestElement | t.Identifier { switch (pattern.kind) { case "ArrayPattern": { - return t.arrayPattern( - pattern.items.map((item) => { - if (item.kind === "Hole") { - return null; - } - return codegenLValue(cx, item); - }) - ); + return codegenArrayPattern(cx, pattern); } case "ObjectPattern": { return t.objectPattern( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.expect.md deleted file mode 100644 index 408692a492..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.expect.md +++ /dev/null @@ -1,20 +0,0 @@ - -## Input - -```javascript -// @bailoutOnHoleyArrays - -function Component() { - return [1, , 3]; -} - -``` - - -## Error - -``` -[ReactForget] Todo: (BuildHIR::lower) Fix babel holey array backward compatibility. (4:4) -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.js deleted file mode 100644 index a9d8c23a16..0000000000 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.backwards-compatible-holey-arrays.js +++ /dev/null @@ -1,5 +0,0 @@ -// @bailoutOnHoleyArrays - -function Component() { - return [1, , 3]; -}