Skip to content

Commit

Permalink
Test printing definite class property definite
Browse files Browse the repository at this point in the history
  • Loading branch information
lokshunhung committed Dec 11, 2021
1 parent 074f299 commit 96cf8d0
Showing 1 changed file with 72 additions and 41 deletions.
113 changes: 72 additions & 41 deletions test/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,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);

Expand Down Expand Up @@ -1671,12 +1698,8 @@ describe("printer", function () {

it("prints chained expression elements", function () {
const node = b.chainExpression(
b.memberExpression(
b.identifier("foo"),
b.identifier("bar"),
false
),
)
b.memberExpression(b.identifier("foo"), b.identifier("bar"), false),
);

assert.strictEqual(recast.print(node).code, "foo.bar");
});
Expand All @@ -1687,9 +1710,9 @@ describe("printer", function () {
b.identifier("foo"),
b.identifier("bar"),
false,
true
true,
),
)
);

assert.strictEqual(recast.print(node).code, "foo?.bar");
});
Expand Down Expand Up @@ -2192,23 +2215,29 @@ describe("printer", function () {
});

const emptyBlockReprinted = printer.print(ast).code;
assert.strictEqual(emptyBlockReprinted, [
"class A {",
" static a = 1;",
" static #b = 2;",
"", // Empty line preserved because of conservative printer.print reprinting.
" static {}",
"}",
].join(eol));
assert.strictEqual(
emptyBlockReprinted,
[
"class A {",
" static a = 1;",
" static #b = 2;",
"", // Empty line preserved because of conservative printer.print reprinting.
" static {}",
"}",
].join(eol),
);

const emptyBlockPrettyPrinted = printer.printGenerically(ast).code;
assert.strictEqual(emptyBlockPrettyPrinted, [
"class A {",
" static a = 1;",
" static #b = 2;",
" static {}",
"}",
].join(eol));
assert.strictEqual(
emptyBlockPrettyPrinted,
[
"class A {",
" static a = 1;",
" static #b = 2;",
" static {}",
"}",
].join(eol),
);
});

it("can pretty-print ImportAttribute syntax", function () {
Expand All @@ -2223,10 +2252,10 @@ describe("printer", function () {
'import * as noAssertions from "./module";',
'import * as emptyAssert from "./module";',
'import json from "./module" assert { type: "json" };',
'',
"",
'import * as ns from "./module" assert {',
' type: "reallyLongStringLiteralThatShouldTriggerReflowOntoMultipleLines"',
'};',
"};",
].join(eol);

const printer = new Printer({
Expand Down Expand Up @@ -2254,12 +2283,15 @@ describe("printer", function () {
});

const reprinted = printer.print(ast).code;
assert.strictEqual(reprinted, [
'import * as noAssertions from "./module";',
'import * as emptyAssert from "./module" assert {};',
'import json from "./module" assert { type: "json" };',
'import * as ns from "./module" assert { type: "shorter" }',
].join(eol));
assert.strictEqual(
reprinted,
[
'import * as noAssertions from "./module";',
'import * as emptyAssert from "./module" assert {};',
'import json from "./module" assert { type: "json" };',
'import * as ns from "./module" assert { type: "shorter" }',
].join(eol),
);
});

it("can pretty-print RecordExpression syntax", function () {
Expand Down Expand Up @@ -2316,8 +2348,8 @@ describe("printer", function () {
it("can pretty-print ModuleExpression syntax", function () {
const code = [
'import { log } from "logger";',
'export const url = import.meta.url;',
'log(url);',
"export const url = import.meta.url;",
"log(url);",
].join(eol);

const ast = parse(code, {
Expand All @@ -2332,13 +2364,12 @@ describe("printer", function () {
const pretty = printer.printGenerically(ast).code;
assert.strictEqual(pretty, code);

const reprinted = printer.print(
b.moduleExpression(ast.program)
).code;
assert.strictEqual(reprinted, [
"module {",
...code.split(eol).map(line => " " + line),
"}",
].join(eol));
const reprinted = printer.print(b.moduleExpression(ast.program)).code;
assert.strictEqual(
reprinted,
["module {", ...code.split(eol).map((line) => " " + line), "}"].join(
eol,
),
);
});
});

0 comments on commit 96cf8d0

Please sign in to comment.