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 authored Apr 27, 2022
1 parent 5fddfec commit 6e0685c
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 @@ -2424,12 +2424,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 @@ -213,23 +213,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 @@ -257,17 +257,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 @@ -307,6 +307,68 @@ const nodeMajorVersion = parseInt(process.versions.node, 10);
"type Alpha = Color.a;",
]);
});

it("InterfaceBody: 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("InterfaceBody: duplicate semicolon: a lot of 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 6e0685c

Please sign in to comment.