Skip to content

Commit

Permalink
Add support for const type parameters
Browse files Browse the repository at this point in the history
Summary: Add support for `const` type parameters.

Reviewed By: panagosg7

Differential Revision: D67406406

fbshipit-source-id: ee1b0d9238ab1b7cfa3f2d862c3b2115a591e10b
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 19, 2024
1 parent e28fd47 commit 3dc760e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* 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
*/

// $FlowExpectedError[cannot-resolve-module]
import prettierConfig from '../../.prettierrc.json';

import * as prettier from 'prettier';

function format(code: string) {
const options = {
...prettierConfig,
parser: 'hermes',
requirePragma: false,
plugins: [require('../src/index.js')],
};
return prettier.format(code, options);
}

describe(`'const' type parameters`, () => {
test('type', async () => {
expect(
format(`
type T<const X> = X;
`),
).toMatchInlineSnapshot(`
"type T<const X> = X;
"
`);
});

test('function: basic', async () => {
expect(
format(`
function f<const T>(): void {}
`),
).toMatchInlineSnapshot(`
"function f<const T>(): void {}
"
`);
});

test('function: with variance', async () => {
expect(
format(`
function f<const +T>(): void {}
`),
).toMatchInlineSnapshot(`
"function f<const +T>(): void {}
"
`);
});

test('arrow: basic', async () => {
expect(
format(`
<const T>(x: T) => {}
`),
).toMatchInlineSnapshot(`
"<const T>(x: T) => {};
"
`);
});

test('arrow: with variance', async () => {
expect(
format(`
<const +T>(x: T) => {}
`),
).toMatchInlineSnapshot(`
"<const +T>(x: T) => {};
"
`);
});

test('class: basic', async () => {
expect(
format(`
class C<const T>{}
`),
).toMatchInlineSnapshot(`
"class C<const T> {}
"
`);
});

test('class: with variance', async () => {
expect(
format(`
class C<const T>{}
`),
).toMatchInlineSnapshot(`
"class C<const T> {}
"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9033,7 +9033,7 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
}
function printTypeParameter(path, options2, print3) {
const { node, parent } = path;
const parts = [node.type === "TSTypeParameter" && node.const ? "const " : ""];
const parts = [node.const ? "const " : ""];
const name = node.type === "TSTypeParameter" ? print3("name") : node.name;
if (parent.type === "TSMappedType") {
if (parent.readonly) {
Expand Down

0 comments on commit 3dc760e

Please sign in to comment.