Skip to content

Commit

Permalink
add support for const type parameter (JS changes)
Browse files Browse the repository at this point in the history
Summary:
JS changes for support for [`const` modifier on type parameters](microsoft/TypeScript#51865). E.g.
```
function foo<const T>(x: T): [T] {...}
```

Reviewed By: SamChou19815

Differential Revision: D66856337

fbshipit-source-id: 109911abd8400b97a35d20d611b376dd12c46bed
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Dec 18, 2024
1 parent 5ce24bf commit d9b1826
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
type: 'TypeAnnotation',
typeAnnotation: Transform.TSTypeAnnotation(node.constraint),
}),
const: false,
default:
node.default == null
? null
Expand Down
1 change: 1 addition & 0 deletions tools/hermes-parser/js/hermes-estree/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4954,6 +4954,7 @@ exports[`ComponentDeclaration type parameters Babel 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "T1",
"type": "TypeParameter",
Expand All @@ -4962,6 +4963,7 @@ exports[`ComponentDeclaration type parameters Babel 1`] = `
},
{
"bound": null,
"const": false,
"default": null,
"name": "T2",
"type": "TypeParameter",
Expand Down Expand Up @@ -5040,6 +5042,7 @@ exports[`ComponentDeclaration type parameters ESTree 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "T1",
"type": "TypeParameter",
Expand All @@ -5048,6 +5051,7 @@ exports[`ComponentDeclaration type parameters ESTree 1`] = `
},
{
"bound": null,
"const": false,
"default": null,
"name": "T2",
"type": "TypeParameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`ConditionalTypeAnnotation Basic Babel 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "TKey",
"type": "TypeParameter",
Expand Down Expand Up @@ -114,6 +115,7 @@ exports[`ConditionalTypeAnnotation Basic ESTree 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "TKey",
"type": "TypeParameter",
Expand Down Expand Up @@ -179,6 +181,7 @@ exports[`ConditionalTypeAnnotation InferType ESTree 1`] = `
"bound": {
"type": "NumberTypeAnnotation",
},
"const": false,
"default": null,
"name": "T",
"type": "TypeParameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ exports[`HookDeclaration type parameters Babel 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "T1",
"type": "TypeParameter",
Expand All @@ -892,6 +893,7 @@ exports[`HookDeclaration type parameters Babel 1`] = `
},
{
"bound": null,
"const": false,
"default": null,
"name": "T2",
"type": "TypeParameter",
Expand Down Expand Up @@ -959,6 +961,7 @@ exports[`HookDeclaration type parameters ESTree 1`] = `
"params": [
{
"bound": null,
"const": false,
"default": null,
"name": "T1",
"type": "TypeParameter",
Expand All @@ -967,6 +970,7 @@ exports[`HookDeclaration type parameters ESTree 1`] = `
},
{
"bound": null,
"const": false,
"default": null,
"name": "T2",
"type": "TypeParameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ exports[`MappedType Basic ESTree 1`] = `
{
"keyTparam": {
"bound": null,
"const": false,
"default": null,
"name": "key",
"type": "TypeParameter",
Expand Down Expand Up @@ -135,6 +136,7 @@ exports[`MappedType Union ESTree 1`] = `
{
"keyTparam": {
"bound": null,
"const": false,
"default": null,
"name": "key",
"type": "TypeParameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,7 @@ export type TypeOperatorProps = {

export type TypeParameterProps = {
+name: TypeParameterType['name'],
+const: TypeParameterType['const'],
+bound?: ?MaybeDetachedNode<TypeParameterType['bound']>,
+variance?: ?MaybeDetachedNode<TypeParameterType['variance']>,
+default?: ?MaybeDetachedNode<TypeParameterType['default']>,
Expand Down Expand Up @@ -3685,6 +3686,7 @@ export function TypeParameter(props: {
const node = detachedProps<TypeParameterType>((props.parent: $FlowFixMe), {
type: 'TypeParameter',
name: props.name,
const: props.const,
bound: asDetachedNodeForCodeGen(props.bound),
variance: asDetachedNodeForCodeGen(props.variance),
default: asDetachedNodeForCodeGen(props.default),
Expand Down

0 comments on commit d9b1826

Please sign in to comment.