From ddbfa1b8714f0e9c3e1f46f271e78232abaf4c50 Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Thu, 12 Nov 2026 21:13:13 -0800 Subject: [PATCH] Upgrade the Flow parser tooling to 0.340.0 Summary: @public Upgrade `flow-parser`, `flow-estree`, `flow-eslint`, `flow-transform` and `flow-api-translator` to 0.340.0, and adapt the code generators, codemods, lint rules and parser adapters that read the ESTree output those releases produce. Four shapes moved since the versions in use, and each one silently changed behavior rather than failing loudly: - Generic type annotations carry their arguments in `typeArguments` instead of `typeParameters`, so consumers that still read the old field dropped every type argument. Readers now take the canonical field whenever the node carries it, including when it is `null`, and fall back to the previous field otherwise, so the same source keeps working with the older releases that remain installed elsewhere. - Enum declarations expose a single `EnumBody` carrying an explicit type instead of one node type per value kind. Enum readers accept both and resolve the value kind the same way, so the supported enums and the existing rejection of boolean and symbol enums are unchanged. - `JSXAttribute.name` widened to also allow a namespaced name, so transforms that compare an attribute name against a list now match only plain identifiers and leave namespaced attributes alone, which is what they already did at runtime. - Object type properties, array elements, import phases and several other fields are now nullable or newly required. Readers guard them and report the existing unsupported-input errors instead of failing on a missing property, and import declarations are built from constant parser-owned templates so one source satisfies both the old and the new builder. The lint rules, the declaration generators and the `jscodeshift`/recast boundary are adapted the same way, and the published type mirrors for the lint and transform packages are refreshed. Changelog: [Internal] Reviewed By: SamChou19815, panagosg7 Differential Revision: D131489493 --- package.json | 8 +-- packages/metro-babel-register/package.json | 2 +- packages/metro-babel-transformer/package.json | 2 +- packages/metro/package.json | 2 +- scripts/__tests__/declaration-input-test.js | 45 ++++++++++++++ scripts/generateTypeScriptDefinitions.js | 7 ++- yarn.lock | 60 +++++++++---------- 7 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 scripts/__tests__/declaration-input-test.js diff --git a/package.json b/package.json index 5e861482ed..c41da2c4c3 100644 --- a/package.json +++ b/package.json @@ -34,11 +34,11 @@ "eslint-plugin-lint": "^1.0.0", "eslint-plugin-react": "^7.37.5", "eslint-plugin-relay": "^1.8.3", - "flow-api-translator": "0.336.0", + "flow-api-translator": "0.340.0", "flow-bin": "^0.340.0", - "flow-eslint": "0.336.0", - "flow-estree": "0.336.0", - "flow-parser": "0.336.0", + "flow-eslint": "0.340.0", + "flow-estree": "0.340.0", + "flow-parser": "0.340.0", "invariant": "^2.2.4", "istanbul-lib-coverage": "3.0.0", "istanbul-lib-report": "3.0.0", diff --git a/packages/metro-babel-register/package.json b/packages/metro-babel-register/package.json index 28ecdcc6a1..1bed4396e0 100644 --- a/packages/metro-babel-register/package.json +++ b/packages/metro-babel-register/package.json @@ -21,7 +21,7 @@ "@babel/preset-typescript": "^7.24.7", "@babel/register": "^7.24.6", "babel-plugin-replace-ts-export-assignment": "^0.0.2", - "flow-parser": "0.336.0", + "flow-parser": "0.340.0", "babel-plugin-transform-flow-enums": "^0.0.2", "escape-string-regexp": "^1.0.5", "flow-enums-runtime": "^0.0.6" diff --git a/packages/metro-babel-transformer/package.json b/packages/metro-babel-transformer/package.json index 2475bc1782..b2ad297b32 100644 --- a/packages/metro-babel-transformer/package.json +++ b/packages/metro-babel-transformer/package.json @@ -25,7 +25,7 @@ "dependencies": { "@babel/core": "^7.25.2", "flow-enums-runtime": "^0.0.6", - "flow-parser": "0.336.0", + "flow-parser": "0.340.0", "metro-cache-key": "0.88.0", "nullthrows": "^1.1.1" }, diff --git a/packages/metro/package.json b/packages/metro/package.json index 03ea4aacb4..2efac1209c 100644 --- a/packages/metro/package.json +++ b/packages/metro/package.json @@ -31,7 +31,7 @@ "debug": "^4.4.0", "error-stack-parser": "^2.0.6", "flow-enums-runtime": "^0.0.6", - "flow-parser": "0.336.0", + "flow-parser": "0.340.0", "invariant": "^2.2.4", "jest-worker": "^29.7.0", "jsc-safe-url": "^0.2.2", diff --git a/scripts/__tests__/declaration-input-test.js b/scripts/__tests__/declaration-input-test.js new file mode 100644 index 0000000000..9a6f0752f5 --- /dev/null +++ b/scripts/__tests__/declaration-input-test.js @@ -0,0 +1,45 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import {expandFlowCommentTypes} from '../generateTypeScriptDefinitions'; +import {translateFlowToFlowDef} from 'flow-api-translator'; + +test('declaration input removes an executable hashbang without moving lines', () => { + const source = + '#!/usr/bin/env node\r\n/** @flow */\r\nexport const value: string = "ok";'; + expect(expandFlowCommentTypes(source)).toBe( + '\r\n/** @flow */\r\nexport const value: string = "ok";', + ); +}); + +test('preserves hashbang text in ordinary strings', () => { + const source = 'export const value: string = "#!/usr/bin/env node";'; + expect(expandFlowCommentTypes(source)).toBe(source); +}); + +test('expands comment types after removing a hashbang', () => { + const source = '#!/usr/bin/env node\nconst value /*: string */ = "ok";'; + expect(expandFlowCommentTypes(source)).toBe( + '\nconst value : string = "ok";', + ); +}); + +test('translates a typed CLI into declarations with the published parser', async () => { + const source = + '#!/usr/bin/env node\n/** @flow */\nexport const value: string = "ok";'; + const result = await translateFlowToFlowDef( + expandFlowCommentTypes(source), + {}, + {}, + ); + expect(result).not.toContain('#!'); + expect(result).toContain('value: string'); +}); diff --git a/scripts/generateTypeScriptDefinitions.js b/scripts/generateTypeScriptDefinitions.js index 0a8d24d67b..6888a0fb28 100644 --- a/scripts/generateTypeScriptDefinitions.js +++ b/scripts/generateTypeScriptDefinitions.js @@ -315,9 +315,10 @@ export async function generateTsDefsForJsGlobs( // https://flow.org/en/docs/types/comments/: // `/*: T */` -> `: T` // `/*:: code */` and `/*flow-include code */` -> `code` -function expandFlowCommentTypes(source: string): string { +export function expandFlowCommentTypes(source: string): string { + const declarationSource = source.replace(/^#![^\r\n]*/, ''); // Splice from the end so earlier comment ranges stay valid. - return parse(source, {flow: 'all'}).comments.reduceRight( + return parse(declarationSource, {flow: 'all'}).comments.reduceRight( (code, {type, value, range: [start, end]}) => { const marker = type === 'Block' ? /^(::|flow-include|:)/.exec(value)?.[1] : null; @@ -331,7 +332,7 @@ function expandFlowCommentTypes(source: string): string { code.slice(end) ); }, - source, + declarationSource, ); } diff --git a/yarn.lock b/yarn.lock index 3e17732d90..d0cbe72d12 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3379,19 +3379,19 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== -flow-api-translator@0.336.0: - version "0.336.0" - resolved "https://registry.yarnpkg.com/flow-api-translator/-/flow-api-translator-0.336.0.tgz#588c4672c6a32f7fcb00064314b7ad1551e728a5" - integrity sha512-0odorqlErVwnXLuFpQdpRwlUNaqklR2H2tN8uRmlV/Usb82k0S++ReH3afKL7BhUevi4xMsBa02vd38Lf8RznQ== +flow-api-translator@0.340.0: + version "0.340.0" + resolved "https://registry.yarnpkg.com/flow-api-translator/-/flow-api-translator-0.340.0.tgz#c064bd1b86d0f68432d3aff620a3e4fc1e8543b5" + integrity sha512-scSfD46icbYxjzN4a0zER1Jc++po4TXeIKwCKQlKGjE7QfYNKpdf3KoN8hjnAH1HsVjM14gDYglYKqQepZ+AiQ== dependencies: "@babel/code-frame" "^7.16.0" "@typescript-eslint/parser" "8.38.0" "@typescript-eslint/visitor-keys" "8.38.0" flow-enums-runtime "^0.0.6" - flow-eslint "0.336.0" - flow-estree "0.336.0" - flow-parser "0.336.0" - flow-transform "0.336.0" + flow-eslint "0.340.0" + flow-estree "0.340.0" + flow-parser "0.340.0" + flow-transform "0.340.0" typescript "5.3.2" flow-bin@^0.340.0: @@ -3404,38 +3404,38 @@ flow-enums-runtime@^0.0.6: resolved "https://registry.yarnpkg.com/flow-enums-runtime/-/flow-enums-runtime-0.0.6.tgz#5bb0cd1b0a3e471330f4d109039b7eba5cb3e787" integrity sha512-3PYnM29RFXwvAN6Pc/scUfkI7RwhQ/xqyLUyPNlXUp9S40zI8nup9tUSrTLSVnWGBN38FNiGWbwZOB6uR4OGdw== -flow-eslint@0.336.0: - version "0.336.0" - resolved "https://registry.yarnpkg.com/flow-eslint/-/flow-eslint-0.336.0.tgz#2a9e807c85f41f8ba2329e787fd84bc67c411f6d" - integrity sha512-HWsiMD2mC3HbVwPKX74BxyX+ZjgiJi3BYDOG/FofbpLce/Eg1wftSuGEyvWprYpgKOnTEe/7o32IdLdTv25LaQ== +flow-eslint@0.340.0: + version "0.340.0" + resolved "https://registry.yarnpkg.com/flow-eslint/-/flow-eslint-0.340.0.tgz#dbca65729dd9d671ae06ff935b6a0897f22f8d9f" + integrity sha512-Ms9ViJJtdv5dMH3M4i6jD3j+YTg0DyNN8rzLQrBKHLcsxH0zFbv4uajpJK9SgLmw2RKaBuuwRXKdDgfsfanWKQ== dependencies: esrecurse "^4.3.0" - flow-estree "0.336.0" - flow-parser "0.336.0" + flow-estree "0.340.0" + flow-parser "0.340.0" -flow-estree@0.336.0: - version "0.336.0" - resolved "https://registry.yarnpkg.com/flow-estree/-/flow-estree-0.336.0.tgz#e9e90c68ac69b078b2d167588e47a11e5d240d86" - integrity sha512-0VaGWHJourzh88otU5bVJ4Z/oIsD+R6yaeR0Q8t8dezsKQI/RLi5m3fNzkiLYLhJ+YwM3JRSKlOqGplOI5Yo3w== +flow-estree@0.340.0: + version "0.340.0" + resolved "https://registry.yarnpkg.com/flow-estree/-/flow-estree-0.340.0.tgz#e86e368f5f33f4f5cc8871a18d3f79122569ebc2" + integrity sha512-LpYdzPWHsfDAc5nNmVrUPLo8UedFkJqogIxNyVO19tG7qBhvNwjs+Oo4TfWB+2Qa4arFevu3vdbxR5tfWps53g== -flow-parser@0.*, flow-parser@0.336.0: - version "0.336.0" - resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.336.0.tgz#5c34f8a97edfa241d3e71c7a1dcd99a56b1cc2bc" - integrity sha512-O8QvQf/B3rBF+eagcc9/WMAjfJoaKckdgSd8PT/qvMIIe4ZjVKrSqV5SDnRFAKY0VrE8r5C4ADYgvYhmwkWADA== +flow-parser@0.*, flow-parser@0.340.0: + version "0.340.0" + resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.340.0.tgz#7e1790634772a462612cc3d3347e2979ca4261af" + integrity sha512-zB1DgZcBVes/hHj/upWKLhukHAIkj+6g2o0yVnxCxS1Wyi/YLX513w8XpFLJ7/LUrbSE7CIHJAEZ94smHujt9Q== dependencies: - flow-estree "0.336.0" + flow-estree "0.340.0" -flow-transform@0.336.0: - version "0.336.0" - resolved "https://registry.yarnpkg.com/flow-transform/-/flow-transform-0.336.0.tgz#61fd6c761b63c412b67c707dcd81333d24e5fa8d" - integrity sha512-KQEt9y581jhxaUOM80sPbssa/t6bEnwGz0x/xfwmxXv8a69bs6wW83/1Roq1h9qoJfwvSTncWk3IlrpdBVBXXQ== +flow-transform@0.340.0: + version "0.340.0" + resolved "https://registry.yarnpkg.com/flow-transform/-/flow-transform-0.340.0.tgz#ae9fa16ba916378d25806606fa7073c921652e29" + integrity sha512-JC7YcbERLI0XV0D3BkAXCFkZktq0EILxXRvJsWoHjdAZAZe2hIS1DGOmhbRyFti6crskGD/ui8w8nLvM/SoPxg== dependencies: "@babel/code-frame" "^7.16.0" esquery "^1.4.0" flow-enums-runtime "^0.0.6" - flow-eslint "0.336.0" - flow-estree "0.336.0" - flow-parser "0.336.0" + flow-eslint "0.340.0" + flow-estree "0.340.0" + flow-parser "0.340.0" string-width "4.2.3" for-each@^0.3.3, for-each@^0.3.5: