Skip to content

Commit 96cf8d0

Browse files
committed
Test printing definite class property definite
1 parent 074f299 commit 96cf8d0

File tree

1 file changed

+72
-41
lines changed

1 file changed

+72
-41
lines changed

test/printer.ts

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,33 @@ describe("printer", function () {
11901190
assert.strictEqual(pretty, code);
11911191
});
11921192

1193+
it("prints 'definite' ClassProperty correctly", function () {
1194+
const code = ["class A {", " foo!: string;", "}"].join(eol);
1195+
1196+
const ast = b.program([
1197+
b.classDeclaration(
1198+
b.identifier("A"),
1199+
b.classBody([
1200+
Object.assign(
1201+
b.classProperty(
1202+
b.identifier("foo"),
1203+
null,
1204+
b.typeAnnotation(b.stringTypeAnnotation()),
1205+
),
1206+
{ definite: true },
1207+
),
1208+
]),
1209+
),
1210+
]);
1211+
1212+
const printer = new Printer({
1213+
tabWidth: 2,
1214+
});
1215+
1216+
const pretty = printer.printGenerically(ast).code;
1217+
assert.strictEqual(pretty, code);
1218+
});
1219+
11931220
it("prints static ClassProperty correctly", function () {
11941221
const code = ["class A {", " static foo = Bar;", "}"].join(eol);
11951222

@@ -1671,12 +1698,8 @@ describe("printer", function () {
16711698

16721699
it("prints chained expression elements", function () {
16731700
const node = b.chainExpression(
1674-
b.memberExpression(
1675-
b.identifier("foo"),
1676-
b.identifier("bar"),
1677-
false
1678-
),
1679-
)
1701+
b.memberExpression(b.identifier("foo"), b.identifier("bar"), false),
1702+
);
16801703

16811704
assert.strictEqual(recast.print(node).code, "foo.bar");
16821705
});
@@ -1687,9 +1710,9 @@ describe("printer", function () {
16871710
b.identifier("foo"),
16881711
b.identifier("bar"),
16891712
false,
1690-
true
1713+
true,
16911714
),
1692-
)
1715+
);
16931716

16941717
assert.strictEqual(recast.print(node).code, "foo?.bar");
16951718
});
@@ -2192,23 +2215,29 @@ describe("printer", function () {
21922215
});
21932216

21942217
const emptyBlockReprinted = printer.print(ast).code;
2195-
assert.strictEqual(emptyBlockReprinted, [
2196-
"class A {",
2197-
" static a = 1;",
2198-
" static #b = 2;",
2199-
"", // Empty line preserved because of conservative printer.print reprinting.
2200-
" static {}",
2201-
"}",
2202-
].join(eol));
2218+
assert.strictEqual(
2219+
emptyBlockReprinted,
2220+
[
2221+
"class A {",
2222+
" static a = 1;",
2223+
" static #b = 2;",
2224+
"", // Empty line preserved because of conservative printer.print reprinting.
2225+
" static {}",
2226+
"}",
2227+
].join(eol),
2228+
);
22032229

22042230
const emptyBlockPrettyPrinted = printer.printGenerically(ast).code;
2205-
assert.strictEqual(emptyBlockPrettyPrinted, [
2206-
"class A {",
2207-
" static a = 1;",
2208-
" static #b = 2;",
2209-
" static {}",
2210-
"}",
2211-
].join(eol));
2231+
assert.strictEqual(
2232+
emptyBlockPrettyPrinted,
2233+
[
2234+
"class A {",
2235+
" static a = 1;",
2236+
" static #b = 2;",
2237+
" static {}",
2238+
"}",
2239+
].join(eol),
2240+
);
22122241
});
22132242

22142243
it("can pretty-print ImportAttribute syntax", function () {
@@ -2223,10 +2252,10 @@ describe("printer", function () {
22232252
'import * as noAssertions from "./module";',
22242253
'import * as emptyAssert from "./module";',
22252254
'import json from "./module" assert { type: "json" };',
2226-
'',
2255+
"",
22272256
'import * as ns from "./module" assert {',
22282257
' type: "reallyLongStringLiteralThatShouldTriggerReflowOntoMultipleLines"',
2229-
'};',
2258+
"};",
22302259
].join(eol);
22312260

22322261
const printer = new Printer({
@@ -2254,12 +2283,15 @@ describe("printer", function () {
22542283
});
22552284

22562285
const reprinted = printer.print(ast).code;
2257-
assert.strictEqual(reprinted, [
2258-
'import * as noAssertions from "./module";',
2259-
'import * as emptyAssert from "./module" assert {};',
2260-
'import json from "./module" assert { type: "json" };',
2261-
'import * as ns from "./module" assert { type: "shorter" }',
2262-
].join(eol));
2286+
assert.strictEqual(
2287+
reprinted,
2288+
[
2289+
'import * as noAssertions from "./module";',
2290+
'import * as emptyAssert from "./module" assert {};',
2291+
'import json from "./module" assert { type: "json" };',
2292+
'import * as ns from "./module" assert { type: "shorter" }',
2293+
].join(eol),
2294+
);
22632295
});
22642296

22652297
it("can pretty-print RecordExpression syntax", function () {
@@ -2316,8 +2348,8 @@ describe("printer", function () {
23162348
it("can pretty-print ModuleExpression syntax", function () {
23172349
const code = [
23182350
'import { log } from "logger";',
2319-
'export const url = import.meta.url;',
2320-
'log(url);',
2351+
"export const url = import.meta.url;",
2352+
"log(url);",
23212353
].join(eol);
23222354

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

2335-
const reprinted = printer.print(
2336-
b.moduleExpression(ast.program)
2337-
).code;
2338-
assert.strictEqual(reprinted, [
2339-
"module {",
2340-
...code.split(eol).map(line => " " + line),
2341-
"}",
2342-
].join(eol));
2367+
const reprinted = printer.print(b.moduleExpression(ast.program)).code;
2368+
assert.strictEqual(
2369+
reprinted,
2370+
["module {", ...code.split(eol).map((line) => " " + line), "}"].join(
2371+
eol,
2372+
),
2373+
);
23432374
});
23442375
});

0 commit comments

Comments
 (0)