diff --git a/tools/hermes-parser/js/prettier-plugin-hermes-parser/__tests__/const-type-param-test.js b/tools/hermes-parser/js/prettier-plugin-hermes-parser/__tests__/const-type-param-test.js new file mode 100644 index 00000000000..41a806da601 --- /dev/null +++ b/tools/hermes-parser/js/prettier-plugin-hermes-parser/__tests__/const-type-param-test.js @@ -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 = X; + `), + ).toMatchInlineSnapshot(` + "type T = X; + " + `); + }); + + test('function: basic', async () => { + expect( + format(` + function f(): void {} + `), + ).toMatchInlineSnapshot(` + "function f(): void {} + " + `); + }); + + test('function: with variance', async () => { + expect( + format(` + function f(): void {} + `), + ).toMatchInlineSnapshot(` + "function f(): void {} + " + `); + }); + + test('arrow: basic', async () => { + expect( + format(` + (x: T) => {} + `), + ).toMatchInlineSnapshot(` + "(x: T) => {}; + " + `); + }); + + test('arrow: with variance', async () => { + expect( + format(` + (x: T) => {} + `), + ).toMatchInlineSnapshot(` + "(x: T) => {}; + " + `); + }); + + test('class: basic', async () => { + expect( + format(` + class C{} + `), + ).toMatchInlineSnapshot(` + "class C {} + " + `); + }); + + test('class: with variance', async () => { + expect( + format(` + class C{} + `), + ).toMatchInlineSnapshot(` + "class C {} + " + `); + }); +}); diff --git a/tools/hermes-parser/js/prettier-plugin-hermes-parser/src/third-party/internal-prettier-v3/plugins/estree.js b/tools/hermes-parser/js/prettier-plugin-hermes-parser/src/third-party/internal-prettier-v3/plugins/estree.js index a4c7796f6ae..79657d8e465 100644 --- a/tools/hermes-parser/js/prettier-plugin-hermes-parser/src/third-party/internal-prettier-v3/plugins/estree.js +++ b/tools/hermes-parser/js/prettier-plugin-hermes-parser/src/third-party/internal-prettier-v3/plugins/estree.js @@ -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) {