Skip to content

Commit

Permalink
fix: use babel-ts parser for prettier formatting (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jan 5, 2023
1 parent 36d6163 commit 8fd8227
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/plugin/generate-types-from-abstract-syntax-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const generateTypesFromAbstractSyntaxTree = (filePath: string, prettierCo
const interfaceExists = t.isTSModuleDeclaration(lastNode) && lastNode.global && lastNode.declare;
const newNodes = interfaceExists ? currentNodes.slice(0, currentNodes.length - 1) : currentNodes;
const { code: existingCode } = generate(t.program(newNodes), { retainLines: true });
const formattedExistingCode = format(existingCode, { parser: 'babel', ...prettierConfig });
const formattedExistingCode = format(existingCode, { parser: 'babel-ts', ...prettierConfig });
const { code: newCode } = generate(t.program(newInterface));
return `${formattedExistingCode}\n${newCode}\n`;
};
Expand Down
34 changes: 29 additions & 5 deletions test/generate-types-from-abstract-syntax-tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const prettierConfig = {
};

describe('generateTypesFromAbstractSyntaxTree', () => {
it('should generate types when types do not exist', async () => {
it('should generate types when types do not exist', () => {
(readFileSync as jest.Mock).mockReturnValue(`// some comment
export function functionExampleOneInput(input1: string) {
Expand Down Expand Up @@ -85,7 +85,7 @@ declare global {
`);
});

it('should generate types when types exist', async () => {
it('should generate types when types exist', () => {
(readFileSync as jest.Mock).mockReturnValue(`// some comment
export function functionExampleOneInput(input1: string) {
Expand Down Expand Up @@ -154,7 +154,7 @@ declare global {
`);
});

it('should preserve formatting', async () => {
it('should preserve formatting', () => {
(readFileSync as jest.Mock).mockReturnValue(`// some comment
export function functionExampleOneInput(input1: string) {
Expand Down Expand Up @@ -227,7 +227,7 @@ declare global {
`);
});

it('should generate a special type for scoped commands', async () => {
it('should generate a special type for scoped commands', () => {
(readFileSync as jest.Mock).mockReturnValue(`// some comment
export function functionExampleScoped(
Expand Down Expand Up @@ -257,7 +257,31 @@ declare global {
`);
});

it('should throw descriptive error for object-destructured input', async () => {
it('should handle generic types properly', () => {
(readFileSync as jest.Mock).mockReturnValue(`// some comment
export function functionExampleGenericType(input1: string) {
cy.log<GenericType>('Here is a generic type!');
}
`);
const result = generateTypesFromAbstractSyntaxTree(filePath, prettierConfig);
expect(result).toEqual(`// some comment
export function functionExampleGenericType(input1: string) {
cy.log<GenericType>('Here is a generic type!');
}
declare global {
namespace Cypress {
interface Chainable {
functionExampleGenericType(input1: string): Chainable;
}
}
}
`);
});

it('should throw descriptive error for object-destructured input', () => {
(readFileSync as jest.Mock).mockReturnValue(`
export const objectDestructureExample = ({ input1, input2 }: { input1: string; input2: string }) => {
Expand Down

0 comments on commit 8fd8227

Please sign in to comment.