From 9410fff5f0e74632d1657aa33903477173014834 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Wed, 2 Dec 2026 16:27:07 -0800 Subject: [PATCH] Spell `[Symbol.iterator]` instead of `@@iterator` in `HTMLCollection` and `NodeList` (#60501) Summary: Flow is moving `Symbol.iterator` from the string literal `'@iterator'` to a `unique symbol` (matching TypeScript), so a member named `@iterator` names a string property rather than the iterator protocol. Spell `[Symbol.iterator](): IteratorObject` instead of `@iterator(): IteratorObject` in `HTMLCollection.js.flow` and `NodeList.js.flow` (matching their runtime implementations in `HTMLCollection.js` and `NodeList.js`). Requires `flow-api-translator` with computed `[Symbol.*]` key support so `build-types` can translate `[Symbol.iterator]` to TypeScript declarations. Changelog: [Internal] Differential Revision: D135780451 --- .../HTMLCollection.js.flow | 2 +- .../dom/oldstylecollections/NodeList.js.flow | 2 +- .../encodeComputedSymbolKeys-test.js | 70 ++++++++++++++++++ .../flow/encodeComputedSymbolKeys.js | 59 +++++++++++++++ .../decodeComputedSymbolKeys-test.js | 51 +++++++++++++ .../typescript/decodeComputedSymbolKeys.js | 71 +++++++++++++++++++ .../js-api/build-types/translateSourceFile.js | 2 + 7 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 scripts/js-api/build-types/transforms/flow/__tests__/encodeComputedSymbolKeys-test.js create mode 100644 scripts/js-api/build-types/transforms/flow/encodeComputedSymbolKeys.js create mode 100644 scripts/js-api/build-types/transforms/typescript/__tests__/decodeComputedSymbolKeys-test.js create mode 100644 scripts/js-api/build-types/transforms/typescript/decodeComputedSymbolKeys.js diff --git a/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow b/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow index 2b56c4ccba58..77ba8bd5f5dd 100644 --- a/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow +++ b/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow @@ -21,7 +21,7 @@ declare export default class HTMLCollection readonly length: number; item(index: number): T | null; namedItem(name: string): T | null; - @@iterator(): IteratorObject; + [Symbol.iterator](): IteratorObject; } declare export function createHTMLCollection( diff --git a/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow b/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow index e00b9945c47a..568c53a8c80f 100644 --- a/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow +++ b/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow @@ -23,7 +23,7 @@ declare export default class NodeList implements Iterable, ArrayLike { ): void; keys(): IteratorObject; values(): IteratorObject; - @@iterator(): IteratorObject; + [Symbol.iterator](): IteratorObject; } declare export function createNodeList( diff --git a/scripts/js-api/build-types/transforms/flow/__tests__/encodeComputedSymbolKeys-test.js b/scripts/js-api/build-types/transforms/flow/__tests__/encodeComputedSymbolKeys-test.js new file mode 100644 index 000000000000..a5638b1146e2 --- /dev/null +++ b/scripts/js-api/build-types/transforms/flow/__tests__/encodeComputedSymbolKeys-test.js @@ -0,0 +1,70 @@ +/** + * 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 + */ + +const translateSourceFile = require('../../../translateSourceFile'); +const encodeComputedSymbolKeys = require('../encodeComputedSymbolKeys'); +const {parse, print} = require('flow-transform'); + +const prettierOptions = {parser: 'babel'}; + +async function translate(code: string): Promise { + const parsed = await parse(code); + const result = await encodeComputedSymbolKeys(parsed); + return print(result.ast, result.mutatedCode, prettierOptions); +} + +describe('encodeComputedSymbolKeys', () => { + test('encodes computed [Symbol.*] members on declare class, interface, and object types', async () => { + const code = ` +declare export default class Foo { + [Symbol.iterator](): IteratorObject; + static get [Symbol.species](): typeof Foo; + readonly [Symbol.toStringTag]: string; +} +export interface Bar { + [Symbol.asyncIterator](): AsyncIterator; + readonly [Symbol.toStringTag]?: string; +} +export type Baz = { + [Symbol.dispose](): void, +}; +`; + const result = await translate(code); + expect(result).toContain('$$SYMBOL_KEY__iterator$$(): IteratorObject;'); + expect(result).toContain( + 'static get $$SYMBOL_KEY__species$$(): typeof Foo;', + ); + expect(result).toContain('readonly $$SYMBOL_KEY__toStringTag$$: string;'); + expect(result).toContain( + '$$SYMBOL_KEY__asyncIterator$$(): AsyncIterator;', + ); + expect(result).toContain('readonly $$SYMBOL_KEY__toStringTag$$?: string;'); + expect(result).toContain('$$SYMBOL_KEY__dispose$$(): void,'); + }); + + test('round-trips through translateSourceFile to TypeScript [Symbol.*] keys', async () => { + const code = ` +declare export default class Collection implements Iterable { + [index: number]: T; + readonly length: number; + [Symbol.iterator](): IteratorObject; + static get [Symbol.species](): typeof Collection; + readonly [Symbol.toStringTag]: string; +} +`; + const {result} = await translateSourceFile(code, 'Collection.js.flow'); + expect(result).toContain('[Symbol.iterator](): IteratorObject;'); + expect(result).toContain( + 'static get [Symbol.species](): typeof Collection;', + ); + expect(result).toContain('readonly [Symbol.toStringTag]: string;'); + expect(result).not.toContain('$$SYMBOL_KEY__'); + }); +}); diff --git a/scripts/js-api/build-types/transforms/flow/encodeComputedSymbolKeys.js b/scripts/js-api/build-types/transforms/flow/encodeComputedSymbolKeys.js new file mode 100644 index 000000000000..7af24314c9f9 --- /dev/null +++ b/scripts/js-api/build-types/transforms/flow/encodeComputedSymbolKeys.js @@ -0,0 +1,59 @@ +/** + * 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 + */ + +import type {TransformVisitor} from 'flow-transform'; +import type {ParseResult} from 'flow-transform/dist/transform/parse'; +import type {TransformASTResult} from 'flow-transform/dist/transform/transformAST'; + +const {t} = require('flow-transform'); +const {transformAST} = require('flow-transform/dist/transform/transformAST'); + +const SYMBOL_KEY_PREFIX = '$$SYMBOL_KEY__'; +const SYMBOL_KEY_SUFFIX = '$$'; + +const visitors: TransformVisitor = context => ({ + ObjectTypeProperty(node): void { + if ( + node.computed !== true || + node.key.type !== 'MemberExpression' || + node.key.computed !== false || + node.key.object.type !== 'Identifier' || + node.key.object.name !== 'Symbol' || + node.key.property.type !== 'Identifier' + ) { + return; + } + + const encodedKey = t.Identifier({ + name: `${SYMBOL_KEY_PREFIX}${node.key.property.name}${SYMBOL_KEY_SUFFIX}`, + }); + + context.modifyNodeInPlace(node, { + computed: undefined, + key: encodedKey, + }); + }, +}); + +/** + * Stopgap pre-transform for `flow-api-translator` versions that do not yet + * support computed `[Symbol.*]` keys on object/interface/class type members. + * + * Encodes `[Symbol.]` as a placeholder identifier that passes through + * `flow-api-translator` untouched and is restored to `[Symbol.]` by + * `transforms/typescript/decodeComputedSymbolKeys`. + */ +async function encodeComputedSymbolKeys( + source: ParseResult, +): Promise { + return transformAST(source, visitors); +} + +module.exports = encodeComputedSymbolKeys; diff --git a/scripts/js-api/build-types/transforms/typescript/__tests__/decodeComputedSymbolKeys-test.js b/scripts/js-api/build-types/transforms/typescript/__tests__/decodeComputedSymbolKeys-test.js new file mode 100644 index 000000000000..c572ae083a64 --- /dev/null +++ b/scripts/js-api/build-types/transforms/typescript/__tests__/decodeComputedSymbolKeys-test.js @@ -0,0 +1,51 @@ +/** + * 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 + */ + +const decodeComputedSymbolKeys = require('../decodeComputedSymbolKeys'); +const babel = require('@babel/core'); + +async function translate(code: string): Promise { + const result = await babel.transformAsync(code, { + plugins: ['@babel/plugin-syntax-typescript', decodeComputedSymbolKeys], + }); + + return result.code; +} + +describe('decodeComputedSymbolKeys', () => { + test('restores placeholder identifiers to computed [Symbol.*] keys', async () => { + const code = ` +declare class Foo { + $$SYMBOL_KEY__iterator$$(): IteratorObject; + static get $$SYMBOL_KEY__species$$(): typeof Foo; + readonly $$SYMBOL_KEY__toStringTag$$: string; +} +interface Bar { + $$SYMBOL_KEY__asyncIterator$$(): AsyncIterator; + readonly $$SYMBOL_KEY__toStringTag$$?: string | undefined; +} +type Baz = { + $$SYMBOL_KEY__dispose$$(): void; + regular(): void; +}; +`; + const result = await translate(code); + expect(result).toContain('[Symbol.iterator](): IteratorObject;'); + expect(result).toContain('static get [Symbol.species](): typeof Foo;'); + expect(result).toContain('readonly [Symbol.toStringTag]: string;'); + expect(result).toContain('[Symbol.asyncIterator](): AsyncIterator;'); + expect(result).toContain( + 'readonly [Symbol.toStringTag]?: string | undefined;', + ); + expect(result).toContain('[Symbol.dispose](): void;'); + expect(result).toContain('regular(): void;'); + expect(result).not.toContain('$$SYMBOL_KEY__'); + }); +}); diff --git a/scripts/js-api/build-types/transforms/typescript/decodeComputedSymbolKeys.js b/scripts/js-api/build-types/transforms/typescript/decodeComputedSymbolKeys.js new file mode 100644 index 000000000000..284e9134a8e7 --- /dev/null +++ b/scripts/js-api/build-types/transforms/typescript/decodeComputedSymbolKeys.js @@ -0,0 +1,71 @@ +/** + * 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 + */ + +import type {PluginObj} from '@babel/core'; +import type { + ClassMethod, + ClassProperty, + TSDeclareMethod, + TSMethodSignature, + TSPropertySignature, +} from '@babel/types'; + +import * as t from '@babel/types'; + +const SYMBOL_KEY_PATTERN = /^\$\$SYMBOL_KEY__([A-Za-z_$][A-Za-z0-9_$]*)\$\$$/; + +function decodeSymbolKey( + node: + | TSDeclareMethod + | TSMethodSignature + | TSPropertySignature + | ClassMethod + | ClassProperty, +): void { + if (node.computed === true || !t.isIdentifier(node.key)) { + return; + } + + const match = SYMBOL_KEY_PATTERN.exec(node.key.name); + if (match == null) { + return; + } + + node.computed = true; + node.key = t.memberExpression(t.identifier('Symbol'), t.identifier(match[1])); +} + +/** + * Stopgap post-transform paired with `transforms/flow/encodeComputedSymbolKeys`. + * + * Restores placeholder identifiers emitted for `[Symbol.]` back into + * computed `Symbol.` member expressions in the generated TypeScript AST. + */ +const visitor: PluginObj = { + visitor: { + TSDeclareMethod(path) { + decodeSymbolKey(path.node); + }, + TSMethodSignature(path) { + decodeSymbolKey(path.node); + }, + TSPropertySignature(path) { + decodeSymbolKey(path.node); + }, + ClassMethod(path) { + decodeSymbolKey(path.node); + }, + ClassProperty(path) { + decodeSymbolKey(path.node); + }, + }, +}; + +module.exports = visitor; diff --git a/scripts/js-api/build-types/translateSourceFile.js b/scripts/js-api/build-types/translateSourceFile.js index 7a7cb5bbf65a..043c15f24d19 100644 --- a/scripts/js-api/build-types/translateSourceFile.js +++ b/scripts/js-api/build-types/translateSourceFile.js @@ -29,8 +29,10 @@ const preTransforms: Array = [ require('./transforms/flow/replaceNullablePropertiesWithUndefined'), require('./transforms/flow/reattachDocComments'), require('./transforms/flow/ensureNoUnprefixedProps'), + require('./transforms/flow/encodeComputedSymbolKeys'), ]; const postTransforms = (filePath: string): Array> => [ + require('./transforms/typescript/decodeComputedSymbolKeys'), require('./transforms/typescript/convertTypeAliasesToInterfaces'), require('./transforms/typescript/ensureUndefinedOnOptionalMembers'), require('./transforms/typescript/replaceProtectedConstructors'),