From e71ab9bc6af4a42c88d83b4a9ce4e55ee15b3067 Mon Sep 17 00:00:00 2001 From: LS Hung <61226932+lokshunhung@users.noreply.github.com> Date: Sat, 11 Dec 2021 21:35:05 +0800 Subject: [PATCH] Test printing definite class property --- lib/printer.ts | 4 ++++ test/printer.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/printer.ts b/lib/printer.ts index a53f1e76..d862c5c2 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -1449,6 +1449,10 @@ function genericPrintNoParens(path: any, options: any, print: any) { parts.push("?"); } + if (n.definite) { + parts.push("!"); + } + if (n.typeAnnotation) { parts.push(path.call(print, "typeAnnotation")); } diff --git a/test/printer.ts b/test/printer.ts index ef969a45..0278a06c 100644 --- a/test/printer.ts +++ b/test/printer.ts @@ -1206,6 +1206,33 @@ describe("printer", function () { assert.strictEqual(pretty, code); }); + it("prints 'definite' ClassProperty correctly", function () { + const code = ["class A {", " foo!: string;", "}"].join(eol); + + const ast = b.program([ + b.classDeclaration( + b.identifier("A"), + b.classBody([ + Object.assign( + b.classProperty( + b.identifier("foo"), + null, + b.typeAnnotation(b.stringTypeAnnotation()), + ), + { definite: true }, + ), + ]), + ), + ]); + + const printer = new Printer({ + tabWidth: 2, + }); + + const pretty = printer.printGenerically(ast).code; + assert.strictEqual(pretty, code); + }); + it("prints static ClassProperty correctly", function () { const code = ["class A {", " static foo = Bar;", "}"].join(eol);