Skip to content

Commit

Permalink
fix: additional semicolon in TSInterfaceBody
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Feb 23, 2021
1 parent 7126994 commit 184ad76
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
5 changes: 2 additions & 3 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2378,12 +2378,11 @@ function genericPrintNoParens(path: any, options: any, print: any) {
]);

case "TSInterfaceBody": {
const lines = fromString(";\n").join(path.map(print, "body"));
const lines = fromString("\n").join(path.map(print, "body"));
if (lines.isEmpty()) {
return fromString("{}", options);
}

return concat(["{\n", lines.indent(options.tabWidth), ";", "\n}"]);
return concat(["{\n", lines.indent(options.tabWidth), "\n}"]);
}

case "TSImportType":
Expand Down
92 changes: 77 additions & 15 deletions test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,23 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);

check([
"interface LabelledContainer<T> {",
" label: string;",
" content: T;",
" option?: boolean;",
" readonly x: number;",
" [index: number]: string;",
" [propName: string]: any;",
" readonly [index: number]: string;",
" (source: string, subString: string): boolean;",
" (start: number): string;",
" reset(): void;",
" a(c: (this: void, e: E) => void): void;",
" label: string",
" content: T",
" option?: boolean",
" readonly x: number",
" [index: number]: string",
" [propName: string]: any",
" readonly [index: number]: string",
" (source: string, subString: string): boolean",
" (start: number): string",
" reset(): void",
" a(c: (this: void, e: E) => void): void",
"}",
]);

check([
"interface Square<T, U> extends Shape<T, U>, Visible<T, U> {",
" sideLength: number;",
" sideLength: number",
"}",
]);

Expand Down Expand Up @@ -260,17 +260,17 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
"}",
]);

check(["export interface S {", " i(s: string): boolean;", "}"]);
check(["export interface S {", " i(s: string): boolean", "}"]);

check([
"namespace Validation {",
" export interface S {",
" i(j: string): boolean;",
" i(j: string): boolean",
" }",
"}",
]);

check(["export interface S {", " i(j: string): boolean;", "}"]);
check(["export interface S {", " i(j: string): boolean", "}"]);

check(["declare namespace D3 {", " export const f: number = 2;", "}"]);

Expand Down Expand Up @@ -338,6 +338,68 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
check("(() => {}) as void");
check("(function () {} as void)");
});

it("IntefaceBody: duplicate semicolon", function () {
const code = [
"interface Hello {",
" 'hello': any;",
" 'hello': string;",
"}",
].join(eol);

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

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

assert.strictEqual(
recast.print(ast).code,
[
"interface Hello {",
" 'hello': any;",
"}",
].join(eol),
);
});

it("IntefaceBody: duplicate semicolon: a lot properties", function () {
const code = [
"interface LabelledContainer<T> {",
" label: string;",
" content: T;",
" option?: boolean;",
" readonly x: number;",
" [index: number]: string;",
" [propName: string]: any;",
" readonly [index: number]: string;",
" (source: string, subString: string): boolean;",
" (start: number): string;",
" reset(): void;",
" a(c: (this: void, e: E) => void): void;",
"}",
].join(eol);

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

ast.program.body[0].body.body.shift();

assert.strictEqual(
recast.print(ast).code,
[
"interface LabelledContainer<T> {",
" content: T;",
" option?: boolean;",
" readonly x: number;",
" [index: number]: string;",
" [propName: string]: any;",
" readonly [index: number]: string;",
" (source: string, subString: string): boolean;",
" (start: number): string;",
" reset(): void;",
" a(c: (this: void, e: E) => void): void;",
"}",
].join(eol),
);
});
});

testReprinting(
Expand Down

0 comments on commit 184ad76

Please sign in to comment.