Skip to content

Commit d6eb735

Browse files
[compiler] Update for Zod v3/v4 compatibility (facebook#34717)
Partial redo of facebook#34710. The changes there tried to use `z.function(args, return)` to be compatible across Zod v3 and v4, but Zod 4's function API has completely changed. Instead, I've updated to just use `z.any()` where we expect a function, and manually validate that it's a function before we call the value. We already have validation of the return type (also using Zod). Co-authored-by: kolvian <[email protected]>
1 parent 71753ac commit d6eb735

File tree

8 files changed

+31
-50
lines changed

8 files changed

+31
-50
lines changed

compiler/packages/babel-plugin-react-compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
"react-dom": "0.0.0-experimental-4beb1fd8-20241118",
5353
"ts-jest": "^29.1.1",
5454
"ts-node": "^10.9.2",
55-
"zod": "^3.22.4",
56-
"zod-validation-error": "^2.1.0"
55+
"zod": "^3.22.4 || ^4.0.0",
56+
"zod-validation-error": "^3.0.3 || ^4.0.0"
5757
},
5858
"resolutions": {
5959
"./**/@babel/parser": "7.7.4",

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export const EnvironmentConfigSchema = z.object({
159159
* A function that, given the name of a module, can optionally return a description
160160
* of that module's type signature.
161161
*/
162-
moduleTypeProvider: z.nullable(z.function().args(z.string())).default(null),
162+
moduleTypeProvider: z.nullable(z.any()).default(null),
163163

164164
/**
165165
* A list of functions which the application compiles as macros, where
@@ -249,7 +249,7 @@ export const EnvironmentConfigSchema = z.object({
249249
* Allows specifying a function that can populate HIR with type information from
250250
* Flow
251251
*/
252-
flowTypeProvider: z.nullable(z.function().args(z.string())).default(null),
252+
flowTypeProvider: z.nullable(z.any()).default(null),
253253

254254
/**
255255
* Enables inference of optional dependency chains. Without this flag
@@ -906,6 +906,12 @@ export class Environment {
906906
if (moduleTypeProvider == null) {
907907
return null;
908908
}
909+
if (typeof moduleTypeProvider !== 'function') {
910+
CompilerError.throwInvalidConfig({
911+
reason: `Expected a function for \`moduleTypeProvider\``,
912+
loc,
913+
});
914+
}
909915
const unparsedModuleConfig = moduleTypeProvider(moduleName);
910916
if (unparsedModuleConfig != null) {
911917
const parsedModuleConfig = TypeSchema.safeParse(unparsedModuleConfig);

compiler/packages/babel-plugin-react-compiler/src/__tests__/envConfig-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('parseConfigPragma()', () => {
2020
validateHooksUsage: 1,
2121
} as any);
2222
}).toThrowErrorMatchingInlineSnapshot(
23-
`"Error: Could not validate environment config. Update React Compiler config to fix the error. Validation error: Expected boolean, received number at "validateHooksUsage"."`,
23+
`"Error: Could not validate environment config. Update React Compiler config to fix the error. Validation error: Invalid input: expected boolean, received number at "validateHooksUsage"."`,
2424
);
2525
});
2626

@@ -38,7 +38,7 @@ describe('parseConfigPragma()', () => {
3838
],
3939
} as any);
4040
}).toThrowErrorMatchingInlineSnapshot(
41-
`"Error: Could not validate environment config. Update React Compiler config to fix the error. Validation error: autodepsIndex must be > 0 at "inferEffectDependencies[0].autodepsIndex"."`,
41+
`"Error: Could not validate environment config. Update React Compiler config to fix the error. Validation error: AutodepsIndex must be > 0 at "inferEffectDependencies[0].autodepsIndex"."`,
4242
);
4343
});
4444

compiler/packages/eslint-plugin-react-compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"@babel/core": "^7.24.4",
1616
"@babel/parser": "^7.24.4",
1717
"hermes-parser": "^0.25.1",
18-
"zod": "^3.22.4",
19-
"zod-validation-error": "^3.0.3"
18+
"zod": "^3.22.4 || ^4.0.0",
19+
"zod-validation-error": "^3.0.3 || ^4.0.0"
2020
},
2121
"devDependencies": {
2222
"@babel/preset-env": "^7.22.4",

compiler/packages/react-compiler-healthcheck/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"fast-glob": "^3.3.2",
1818
"ora": "5.4.1",
1919
"yargs": "^17.7.2",
20-
"zod": "^3.22.4",
21-
"zod-validation-error": "^3.0.3"
20+
"zod": "^3.22.4 || ^4.0.0",
21+
"zod-validation-error": "^3.0.3 || ^4.0.0"
2222
},
2323
"devDependencies": {},
2424
"engines": {

compiler/packages/react-mcp-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"html-to-text": "^9.0.5",
2525
"prettier": "^3.3.3",
2626
"puppeteer": "^24.7.2",
27-
"zod": "^3.23.8"
27+
"zod": "^3.22.4 || ^4.0.0"
2828
},
2929
"devDependencies": {
3030
"@types/html-to-text": "^9.0.4",

compiler/yarn.lock

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10486,16 +10486,7 @@ string-length@^4.0.1:
1048610486
char-regex "^1.0.2"
1048710487
strip-ansi "^6.0.0"
1048810488

10489-
"string-width-cjs@npm:string-width@^4.2.0":
10490-
version "4.2.3"
10491-
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
10492-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
10493-
dependencies:
10494-
emoji-regex "^8.0.0"
10495-
is-fullwidth-code-point "^3.0.0"
10496-
strip-ansi "^6.0.1"
10497-
10498-
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
10489+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
1049910490
version "4.2.3"
1050010491
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
1050110492
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -10568,14 +10559,7 @@ string_decoder@~1.1.1:
1056810559
dependencies:
1056910560
safe-buffer "~5.1.0"
1057010561

10571-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
10572-
version "6.0.1"
10573-
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
10574-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
10575-
dependencies:
10576-
ansi-regex "^5.0.1"
10577-
10578-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
10562+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
1057910563
version "6.0.1"
1058010564
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
1058110565
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -11352,7 +11336,7 @@ workerpool@^6.5.1:
1135211336
resolved "https://registry.npmjs.org/workerpool/-/workerpool-6.5.1.tgz"
1135311337
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==
1135411338

11355-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
11339+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
1135611340
version "7.0.0"
1135711341
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
1135811342
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -11370,15 +11354,6 @@ wrap-ansi@^6.2.0:
1137011354
string-width "^4.1.0"
1137111355
strip-ansi "^6.0.0"
1137211356

11373-
wrap-ansi@^7.0.0:
11374-
version "7.0.0"
11375-
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
11376-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
11377-
dependencies:
11378-
ansi-styles "^4.0.0"
11379-
string-width "^4.1.0"
11380-
strip-ansi "^6.0.0"
11381-
1138211357
wrap-ansi@^8.1.0:
1138311358
version "8.1.0"
1138411359
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz"
@@ -11530,17 +11505,17 @@ zod-to-json-schema@^3.24.1:
1153011505
resolved "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.24.5.tgz"
1153111506
integrity sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g==
1153211507

11533-
zod-validation-error@^2.1.0:
11534-
version "2.1.0"
11535-
resolved "https://registry.npmjs.org/zod-validation-error/-/zod-validation-error-2.1.0.tgz"
11536-
integrity sha512-VJh93e2wb4c3tWtGgTa0OF/dTt/zoPCPzXq4V11ZjxmEAFaPi/Zss1xIZdEB5RD8GD00U0/iVXgqkF77RV7pdQ==
11508+
"zod-validation-error@^3.0.3 || ^4.0.0":
11509+
version "4.0.2"
11510+
resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-4.0.2.tgz#bc605eba49ce0fcd598c127fee1c236be3f22918"
11511+
integrity sha512-Q6/nZLe6jxuU80qb/4uJ4t5v2VEZ44lzQjPDhYJNztRQ4wyWc6VF3D3Kb/fAuPetZQnhS3hnajCf9CsWesghLQ==
1153711512

11538-
zod-validation-error@^3.0.3:
11539-
version "3.0.3"
11540-
resolved "https://registry.npmjs.org/zod-validation-error/-/zod-validation-error-3.0.3.tgz"
11541-
integrity sha512-cETTrcMq3Ze58vhdR0zD37uJm/694I6mAxcf/ei5bl89cC++fBNxrC2z8lkFze/8hVMPwrbtrwXHR2LB50fpHw==
11513+
"zod@^3.22.4 || ^4.0.0":
11514+
version "4.1.11"
11515+
resolved "https://registry.yarnpkg.com/zod/-/zod-4.1.11.tgz#4aab62f76cfd45e6c6166519ba31b2ea019f75f5"
11516+
integrity sha512-WPsqwxITS2tzx1bzhIKsEs19ABD5vmCVa4xBo2tq/SrV4RNZtfws1EnCWQXM6yh8bD08a1idvkB5MZSBiZsjwg==
1154211517

11543-
zod@^3.22.4, zod@^3.23.8, zod@^3.24.1:
11518+
zod@^3.23.8, zod@^3.24.1:
1154411519
version "3.24.3"
1154511520
resolved "https://registry.npmjs.org/zod/-/zod-3.24.3.tgz"
1154611521
integrity sha512-HhY1oqzWCQWuUqvBFnsyrtZRhyPeR7SUGv+C4+MsisMuVfSPx8HpwWqH8tRahSlt6M3PiFAcoeFhZAqIXTxoSg==

packages/eslint-plugin-react-hooks/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"dependencies": {
4242
"@babel/core": "^7.24.4",
4343
"@babel/parser": "^7.24.4",
44-
"zod": "^3.22.4",
45-
"zod-validation-error": "^3.0.3"
44+
"zod": "^3.22.4 || ^4.0.0",
45+
"zod-validation-error": "^3.0.3 || ^4.0.0"
4646
},
4747
"devDependencies": {
4848
"@babel/eslint-parser": "^7.11.4",

0 commit comments

Comments
 (0)