Merge pull request #1013 from lokshunhung/fix/ts-class-prop-definite

Print class property definite assignment assertions
This commit is contained in:
Brian Donovan 2022-04-27 15:56:37 -07:00 committed by GitHub
commit 06eca93f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View file

@ -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"));
}

View file

@ -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);