Skip to content

Commit

Permalink
Merge pull request #1157 from henryqdineen/fix-ts-type-literal-comma
Browse files Browse the repository at this point in the history
fix: additional commas in TsTypeLiteral
  • Loading branch information
eventualbuddha authored Aug 3, 2022
2 parents 88df88a + 8e24cf6 commit 52c2ec1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ function genericPrintNoParens(path: any, options: any, print: any) {
]);

case "TSTypeLiteral": {
const memberLines = fromString(",\n").join(path.map(print, "members"));
const memberLines = fromString("\n").join(path.map(print, "members"));

if (memberLines.isEmpty()) {
return fromString("{}", options);
Expand Down
29 changes: 24 additions & 5 deletions test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
"type I = intrinsic;",
"",
"type J = {",
" a: string,",
" a: string",
" b?: number",
"};",
]);
Expand All @@ -60,9 +60,9 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);

check([
"type A<T, U> = {",
' u: "cat",',
" x: number,",
" y: T,",
' u: "cat"',
" x: number",
" y: T",
" z: U",
"};",
]);
Expand All @@ -71,7 +71,7 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
"type F = <T, U>(",
" a: string,",
" b: {",
" y: T,",
" y: T",
" z: U",
" }",
") => void;",
Expand Down Expand Up @@ -383,6 +383,25 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
["namespace Numbers {", " export const five = 5;", "}"].join(eol),
);
});

it("TypeLiteral: unwanted comma", function () {
const code = [
"type A = {",
" x: boolean;",
" y: number;",
" z: string;",
"}",
].join(eol);

const ast = recast.parse(code, { parser });

ast.program.body[0].typeAnnotation.members.pop();

assert.strictEqual(
recast.print(ast).code,
["type A = {", " x: boolean;", " y: number;", "}"].join(eol),
);
});
});

testReprinting(
Expand Down

0 comments on commit 52c2ec1

Please sign in to comment.