From d9b1826b47b1eb0762fef476d31cd309fa3f893a Mon Sep 17 00:00:00 2001 From: Panos Vekris Date: Tue, 17 Dec 2024 18:05:37 -0800 Subject: [PATCH] add support for const type parameter (JS changes) Summary: JS changes for support for [`const` modifier on type parameters](https://github.com/microsoft/TypeScript/pull/51865). E.g. ``` function foo(x: T): [T] {...} ``` Reviewed By: SamChou19815 Differential Revision: D66856337 fbshipit-source-id: 109911abd8400b97a35d20d611b376dd12c46bed --- .../js/flow-api-translator/src/TSDefToFlowDef.js | 1 + tools/hermes-parser/js/hermes-estree/src/types.js | 1 + .../__tests__/__snapshots__/ComponentDeclaration-test.js.snap | 4 ++++ .../__snapshots__/ConditionalTypeAnnotation-test.js.snap | 3 +++ .../__tests__/__snapshots__/HookDeclaration-test.js.snap | 4 ++++ .../__tests__/__snapshots__/MappedType-test.js.snap | 2 ++ .../js/hermes-parser/src/HermesParserNodeDeserializers.js | 1 + .../js/hermes-transform/src/generated/node-types.js | 2 ++ 8 files changed, 18 insertions(+) diff --git a/tools/hermes-parser/js/flow-api-translator/src/TSDefToFlowDef.js b/tools/hermes-parser/js/flow-api-translator/src/TSDefToFlowDef.js index a83b8bef6dc..012d3dae7e3 100644 --- a/tools/hermes-parser/js/flow-api-translator/src/TSDefToFlowDef.js +++ b/tools/hermes-parser/js/flow-api-translator/src/TSDefToFlowDef.js @@ -1884,6 +1884,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => { type: 'TypeAnnotation', typeAnnotation: Transform.TSTypeAnnotation(node.constraint), }), + const: false, default: node.default == null ? null diff --git a/tools/hermes-parser/js/hermes-estree/src/types.js b/tools/hermes-parser/js/hermes-estree/src/types.js index c03239a8698..64c83ad49dc 100644 --- a/tools/hermes-parser/js/hermes-estree/src/types.js +++ b/tools/hermes-parser/js/hermes-estree/src/types.js @@ -1606,6 +1606,7 @@ export interface TypeParameterDeclaration extends BaseNode { export interface TypeParameter extends BaseNode { +type: 'TypeParameter'; +name: string; + +const: boolean; +bound: null | TypeAnnotation; +variance: null | Variance; +default: null | TypeAnnotationType; diff --git a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ComponentDeclaration-test.js.snap b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ComponentDeclaration-test.js.snap index 8eba2105b8b..8b2522c0cb8 100644 --- a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ComponentDeclaration-test.js.snap +++ b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ComponentDeclaration-test.js.snap @@ -4954,6 +4954,7 @@ exports[`ComponentDeclaration type parameters Babel 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "T1", "type": "TypeParameter", @@ -4962,6 +4963,7 @@ exports[`ComponentDeclaration type parameters Babel 1`] = ` }, { "bound": null, + "const": false, "default": null, "name": "T2", "type": "TypeParameter", @@ -5040,6 +5042,7 @@ exports[`ComponentDeclaration type parameters ESTree 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "T1", "type": "TypeParameter", @@ -5048,6 +5051,7 @@ exports[`ComponentDeclaration type parameters ESTree 1`] = ` }, { "bound": null, + "const": false, "default": null, "name": "T2", "type": "TypeParameter", diff --git a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ConditionalTypeAnnotation-test.js.snap b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ConditionalTypeAnnotation-test.js.snap index 3bbc0b7e5aa..f794c7f34a2 100644 --- a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ConditionalTypeAnnotation-test.js.snap +++ b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/ConditionalTypeAnnotation-test.js.snap @@ -16,6 +16,7 @@ exports[`ConditionalTypeAnnotation Basic Babel 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "TKey", "type": "TypeParameter", @@ -114,6 +115,7 @@ exports[`ConditionalTypeAnnotation Basic ESTree 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "TKey", "type": "TypeParameter", @@ -179,6 +181,7 @@ exports[`ConditionalTypeAnnotation InferType ESTree 1`] = ` "bound": { "type": "NumberTypeAnnotation", }, + "const": false, "default": null, "name": "T", "type": "TypeParameter", diff --git a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/HookDeclaration-test.js.snap b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/HookDeclaration-test.js.snap index e48169cf049..c6ddeb4fb6f 100644 --- a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/HookDeclaration-test.js.snap +++ b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/HookDeclaration-test.js.snap @@ -884,6 +884,7 @@ exports[`HookDeclaration type parameters Babel 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "T1", "type": "TypeParameter", @@ -892,6 +893,7 @@ exports[`HookDeclaration type parameters Babel 1`] = ` }, { "bound": null, + "const": false, "default": null, "name": "T2", "type": "TypeParameter", @@ -959,6 +961,7 @@ exports[`HookDeclaration type parameters ESTree 1`] = ` "params": [ { "bound": null, + "const": false, "default": null, "name": "T1", "type": "TypeParameter", @@ -967,6 +970,7 @@ exports[`HookDeclaration type parameters ESTree 1`] = ` }, { "bound": null, + "const": false, "default": null, "name": "T2", "type": "TypeParameter", diff --git a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/MappedType-test.js.snap b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/MappedType-test.js.snap index a59224faaa4..793a8db269d 100644 --- a/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/MappedType-test.js.snap +++ b/tools/hermes-parser/js/hermes-parser/__tests__/__snapshots__/MappedType-test.js.snap @@ -39,6 +39,7 @@ exports[`MappedType Basic ESTree 1`] = ` { "keyTparam": { "bound": null, + "const": false, "default": null, "name": "key", "type": "TypeParameter", @@ -135,6 +136,7 @@ exports[`MappedType Union ESTree 1`] = ` { "keyTparam": { "bound": null, + "const": false, "default": null, "name": "key", "type": "TypeParameter", diff --git a/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js b/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js index 0eb26ecf312..b35f9e8c220 100644 --- a/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js +++ b/tools/hermes-parser/js/hermes-parser/src/HermesParserNodeDeserializers.js @@ -1574,6 +1574,7 @@ function deserializeTypeParameter() { type: 'TypeParameter', loc: this.addEmptyLoc(), name: this.deserializeString(), + const: this.deserializeBoolean(), bound: this.deserializeNode(), variance: this.deserializeNode(), default: this.deserializeNode(), diff --git a/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js b/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js index 3784bbe2022..de8dc99b493 100644 --- a/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js +++ b/tools/hermes-parser/js/hermes-transform/src/generated/node-types.js @@ -1192,6 +1192,7 @@ export type TypeOperatorProps = { export type TypeParameterProps = { +name: TypeParameterType['name'], + +const: TypeParameterType['const'], +bound?: ?MaybeDetachedNode, +variance?: ?MaybeDetachedNode, +default?: ?MaybeDetachedNode, @@ -3685,6 +3686,7 @@ export function TypeParameter(props: { const node = detachedProps((props.parent: $FlowFixMe), { type: 'TypeParameter', name: props.name, + const: props.const, bound: asDetachedNodeForCodeGen(props.bound), variance: asDetachedNodeForCodeGen(props.variance), default: asDetachedNodeForCodeGen(props.default),