Skip to content

Commit

Permalink
Sync prettier v3.0.3 and add support for AsExpression and rendersType…
Browse files Browse the repository at this point in the history
… changes

Summary:
Sync changes from hermes-v2-backport: https://github.com/pieterv/prettier/commits/hermes-v2-backport

This includes:
- Rebase to Prettier v3.0.3 prettier/prettier@d5f3171
- Patch in AsExpression logic from prettier/prettier@675490a
- Update printing logic to support `rendersType` AST structure change.
- Add tweaks to wrap-paren.js logic to ensure renders types and components correctly get wrapped. pieterv/prettier@7c7635c

Reviewed By: SamChou19815

Differential Revision: D50248021

fbshipit-source-id: 097cfe0b435f45c7cf25c03bab0f89a7e2bbf38f
  • Loading branch information
pieterv authored and facebook-github-bot committed Oct 17, 2023
1 parent 9651515 commit 0aac412
Show file tree
Hide file tree
Showing 10 changed files with 936 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import {
parseForSnapshotBabel,
printForSnapshotESTree,
parseForSnapshotESTree,
printForSnapshotBabel,
} from '../__test_utils__/parse';
Expand All @@ -20,6 +21,7 @@ describe('`as` expression', () => {
const code = 'x as number;';

test('ESTree', async () => {
expect(await printForSnapshotESTree(code)).toBe(code.trim());
expect(await parseForSnapshotESTree(code)).toMatchSnapshot();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ describe('ComponentDeclaration', () => {
);
});
});
/* Commenting out these tests for @Pieterv to re-enable after he adds in prettier support
describe('renders maybe type', () => {
const code = `
component Foo() renders? SpecialType {}
Expand Down Expand Up @@ -129,7 +128,6 @@ describe('ComponentDeclaration', () => {
);
});
});
*/

describe('renders type (complex)', () => {
const code = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,60 @@
* @format
*/

import type {AlignmentCase} from '../__test_utils__/alignment-utils';

import {
expectBabelAlignment,
expectEspreeAlignment,
} from '../__test_utils__/alignment-utils';
import {parseForSnapshot} from '../__test_utils__/parse';
printForSnapshotESTree,
parseForSnapshotESTree,
printForSnapshotBabel,
parseForSnapshotBabel,
} from '../__test_utils__/parse';

describe('ComponentTypeAnnotation', () => {
const testCase: AlignmentCase = {
code: `
describe('Basic', () => {
const code = `
type T = component();
`,
espree: {
expectToFail: 'espree-exception',
expectedExceptionMessage: 'Unexpected token T',
},
babel: {
expectToFail: 'babel-exception',
expectedExceptionMessage: 'Unexpected token',
},
};
`;

test('ESTree', async () => {
expect(await parseForSnapshotESTree(code)).toMatchSnapshot();
expect(await printForSnapshotESTree(code)).toBe(code.trim());
});

test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(
`"type T = any;"`,
);
});
});

describe('Union', () => {
const code = `
type T = component() | null;
type T = (component() renders Foo) | null;
`;

test('ESTree', async () => {
expect(await parseForSnapshotESTree(code)).toMatchSnapshot();
expect(await printForSnapshotESTree(code)).toBe(code.trim());
});

test('ESTree', () => {
expect(parseForSnapshot(testCase.code)).toMatchInlineSnapshot(`
{
"body": [
{
"id": {
"name": "T",
"optional": false,
"type": "Identifier",
"typeAnnotation": null,
},
"right": {
"params": [],
"rendersType": null,
"rest": null,
"type": "ComponentTypeAnnotation",
"typeParameters": null,
},
"type": "TypeAlias",
"typeParameters": null,
},
],
"type": "Program",
}
`);
expectEspreeAlignment(testCase);
test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(`
"type T = any | null;
type T = any | null;"
`);
});
});
describe('Without parens union', () => {
const code = `
type T = component() renders Foo | null;
`;

test('Babel', () => {
expect(parseForSnapshot(testCase.code, {babel: true}))
.toMatchInlineSnapshot(`
{
"body": [
{
"id": {
"name": "T",
"type": "Identifier",
},
"right": {
"type": "AnyTypeAnnotation",
},
"type": "TypeAlias",
"typeParameters": null,
},
],
"type": "Program",
}
`);
expectBabelAlignment(testCase);
test('ESTree', async () => {
expect(await printForSnapshotESTree(code)).toBe(
`type T = (component() renders Foo) | null;`,
);
});
});
});
56 changes: 56 additions & 0 deletions tools/hermes-parser/js/hermes-parser/__tests__/MappedType-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 {
printForSnapshotESTree,
parseForSnapshotESTree,
printForSnapshotBabel,
parseForSnapshotBabel,
} from '../__test_utils__/parse';

describe('MappedType', () => {
describe('Basic', () => {
const code = `
type Mapped = {[key in keyof O]: O[key]};
`;

test('ESTree', async () => {
expect(await parseForSnapshotESTree(code)).toMatchSnapshot();
expect(await printForSnapshotESTree(code)).toBe(code.trim());
});

test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(`
"type Mapped = { any
};"
`);
});
});

describe('Union', () => {
const code = `
type Mapped = {[key in keyof (O | Z)]: O[key]};
`;

test('ESTree', async () => {
expect(await parseForSnapshotESTree(code)).toMatchSnapshot();
expect(await printForSnapshotESTree(code)).toBe(code.trim());
});

test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(`
"type Mapped = { any
};"
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ describe('TypeOperator', () => {
describe('renders', () => {
describe('Basic', () => {
const code = `
type T = renders Foo;
type T1 = renders Foo;
type T2 = renders* Foo;
type T3 = renders? Foo;
`;

test('ESTree', async () => {
Expand All @@ -29,14 +31,18 @@ describe('TypeOperator', () => {

test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(
`"type T = any;"`,
);
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(`
"type T1 = any;
type T2 = any;
type T3 = any;"
`);
});
});
describe('Union', () => {
const code = `
type T = renders (Foo | Bar);
type T1 = renders (Foo | Bar);
type T2 = renders* (Foo | Bar);
type T3 = renders? (Foo | Bar);
`;

test('ESTree', async () => {
Expand All @@ -46,9 +52,11 @@ describe('TypeOperator', () => {

test('Babel', async () => {
expect(await parseForSnapshotBabel(code)).toMatchSnapshot();
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(
`"type T = any;"`,
);
expect(await printForSnapshotBabel(code)).toMatchInlineSnapshot(`
"type T1 = any;
type T2 = any;
type T3 = any;"
`);
});
});
describe('Nested Union', () => {
Expand Down
Loading

0 comments on commit 0aac412

Please sign in to comment.