Skip to content

Commit f0563ac

Browse files
committed
Printer API unit tests
1 parent 0165b71 commit f0563ac

File tree

11 files changed

+173
-52
lines changed

11 files changed

+173
-52
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ var harnessSources = harnessCoreSources.concat([
269269
"projectErrors.ts",
270270
"matchFiles.ts",
271271
"initializeTSConfig.ts",
272+
"printer.ts",
272273
].map(function (f) {
273274
return path.join(unittestsDirectory, f);
274275
})).concat([

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ namespace ts {
14511451
return compilerOptions.target || ScriptTarget.ES3;
14521452
}
14531453

1454-
export function getEmitModuleKind(compilerOptions: CompilerOptions | PrinterOptions) {
1454+
export function getEmitModuleKind(compilerOptions: CompilerOptions) {
14551455
return typeof compilerOptions.module === "number" ?
14561456
compilerOptions.module :
14571457
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS;

src/compiler/emitter.ts

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ts {
1212
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
1313
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean): EmitResult {
1414
const compilerOptions = host.getCompilerOptions();
15+
const moduleKind = getEmitModuleKind(compilerOptions);
1516
const sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
1617
const emittedFilesList: string[] = compilerOptions.listEmittedFiles ? [] : undefined;
1718
const emitterDiagnostics = createDiagnosticCollection();
@@ -147,6 +148,10 @@ namespace ts {
147148
function emitHelpers(node: Node, writeLines: (text: string) => void) {
148149
let helpersEmitted = false;
149150
const bundle = node.kind === SyntaxKind.Bundle ? <Bundle>node : undefined;
151+
if (bundle && moduleKind === ModuleKind.None) {
152+
return;
153+
}
154+
150155
const numNodes = bundle ? bundle.sourceFiles.length : 1;
151156
for (let i = 0; i < numNodes; i++) {
152157
const currentNode = bundle ? bundle.sourceFiles[i] : node;
@@ -201,7 +206,6 @@ namespace ts {
201206

202207
const newLine = getNewLineCharacter(printerOptions);
203208
const languageVersion = getEmitScriptTarget(printerOptions);
204-
const moduleKind = getEmitModuleKind(printerOptions);
205209
const comments = createCommentWriter(printerOptions, onEmitSourceMapOfPosition);
206210
const {
207211
emitNodeWithComments,
@@ -257,9 +261,7 @@ namespace ts {
257261
function writeBundle(bundle: Bundle, output: EmitTextWriter) {
258262
const previousWriter = writer;
259263
setWriter(output);
260-
if (moduleKind) {
261-
emitHelpersIndirect(bundle);
262-
}
264+
emitHelpersIndirect(bundle);
263265
for (const sourceFile of bundle.sourceFiles) {
264266
print(EmitHint.SourceFile, sourceFile, sourceFile);
265267
}
@@ -280,11 +282,8 @@ namespace ts {
280282
}
281283

282284
function endPrint() {
283-
const text = writer.getText();
284-
if (writer === ownWriter) {
285-
writer.reset();
286-
}
287-
285+
const text = ownWriter.getText();
286+
ownWriter.reset();
288287
return text;
289288
}
290289

@@ -385,6 +384,15 @@ namespace ts {
385384

386385
function pipelineEmitUnspecified(node: Node): void {
387386
const kind = node.kind;
387+
388+
// Reserved words
389+
// Strict mode reserved words
390+
// Contextual keywords
391+
if (isKeyword(kind)) {
392+
writeTokenText(kind);
393+
return;
394+
}
395+
388396
switch (kind) {
389397
// Pseudo-literals
390398
case SyntaxKind.TemplateHead:
@@ -396,46 +404,6 @@ namespace ts {
396404
case SyntaxKind.Identifier:
397405
return emitIdentifier(<Identifier>node);
398406

399-
// Reserved words
400-
case SyntaxKind.ConstKeyword:
401-
case SyntaxKind.DefaultKeyword:
402-
case SyntaxKind.ExportKeyword:
403-
case SyntaxKind.VoidKeyword:
404-
405-
// Strict mode reserved words
406-
case SyntaxKind.PrivateKeyword:
407-
case SyntaxKind.ProtectedKeyword:
408-
case SyntaxKind.PublicKeyword:
409-
case SyntaxKind.StaticKeyword:
410-
411-
// Contextual keywords
412-
case SyntaxKind.AbstractKeyword:
413-
case SyntaxKind.AsKeyword:
414-
case SyntaxKind.AnyKeyword:
415-
case SyntaxKind.AsyncKeyword:
416-
case SyntaxKind.AwaitKeyword:
417-
case SyntaxKind.BooleanKeyword:
418-
case SyntaxKind.ConstructorKeyword:
419-
case SyntaxKind.DeclareKeyword:
420-
case SyntaxKind.GetKeyword:
421-
case SyntaxKind.IsKeyword:
422-
case SyntaxKind.ModuleKeyword:
423-
case SyntaxKind.NamespaceKeyword:
424-
case SyntaxKind.NeverKeyword:
425-
case SyntaxKind.ReadonlyKeyword:
426-
case SyntaxKind.RequireKeyword:
427-
case SyntaxKind.NumberKeyword:
428-
case SyntaxKind.SetKeyword:
429-
case SyntaxKind.StringKeyword:
430-
case SyntaxKind.SymbolKeyword:
431-
case SyntaxKind.TypeKeyword:
432-
case SyntaxKind.UndefinedKeyword:
433-
case SyntaxKind.FromKeyword:
434-
case SyntaxKind.GlobalKeyword:
435-
case SyntaxKind.OfKeyword:
436-
writeTokenText(kind);
437-
return;
438-
439407
// Parse tree nodes
440408

441409
// Names

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3937,7 +3937,6 @@
39373937

39383938
export interface PrinterOptions {
39393939
target?: ScriptTarget;
3940-
module?: ModuleKind;
39413940
removeComments?: boolean;
39423941
newLine?: NewLineKind;
39433942
/*@internal*/ sourceMap?: boolean;

src/harness/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"./unittests/initializeTSConfig.ts",
119119
"./unittests/compileOnSave.ts",
120120
"./unittests/typingsInstaller.ts",
121-
"./unittests/projectErrors.ts"
121+
"./unittests/projectErrors.ts",
122+
"./unittests/printer.ts"
122123
]
123124
}

src/harness/unittests/printer.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/// <reference path="..\..\compiler\emitter.ts" />
2+
/// <reference path="..\harness.ts" />
3+
4+
namespace ts {
5+
describe("PrinterAPI", () => {
6+
function makePrintsCorrectly(prefix: string) {
7+
return function printsCorrectly(name: string, options: PrinterOptions, printCallback: (printer: Printer) => string) {
8+
it(name, () => {
9+
Harness.Baseline.runBaseline(`printerApi/${prefix}.${name}.js`, () =>
10+
printCallback(createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed, ...options })));
11+
});
12+
}
13+
}
14+
15+
describe("printFile", () => {
16+
const printsCorrectly = makePrintsCorrectly("printsFileCorrectly");
17+
const sourceFile = createSourceFile("source.ts", `
18+
interface A<T> {
19+
// comment1
20+
readonly prop?: T;
21+
22+
// comment2
23+
method(): void;
24+
25+
// comment3
26+
new <T>(): A<T>;
27+
28+
// comment4
29+
<T>(): A<T>;
30+
}
31+
32+
// comment5
33+
type B = number | string | object;
34+
type C = A<number> & { x: string; }; // comment6
35+
36+
// comment7
37+
enum E1 {
38+
// comment8
39+
first
40+
}
41+
42+
const enum E2 {
43+
second
44+
}
45+
46+
// comment9
47+
console.log(1 + 2);
48+
`, ScriptTarget.ES2015);
49+
50+
printsCorrectly("default", {}, printer => printer.printFile(sourceFile));
51+
printsCorrectly("removeComments", { removeComments: true }, printer => printer.printFile(sourceFile));
52+
});
53+
54+
describe("printBundle", () => {
55+
const printsCorrectly = makePrintsCorrectly("printsBundleCorrectly");
56+
const bundle = createBundle([
57+
createSourceFile("a.ts", `
58+
/*! [a.ts] */
59+
60+
// comment0
61+
const a = 1;
62+
`, ScriptTarget.ES2015),
63+
createSourceFile("b.ts", `
64+
/*! [b.ts] */
65+
66+
// comment1
67+
const b = 2;
68+
`, ScriptTarget.ES2015)
69+
]);
70+
printsCorrectly("default", {}, printer => printer.printBundle(bundle));
71+
printsCorrectly("removeComments", { removeComments: true }, printer => printer.printBundle(bundle));
72+
});
73+
74+
describe("printNode", () => {
75+
const printsCorrectly = makePrintsCorrectly("printsNodeCorrectly");
76+
const sourceFile = createSourceFile("source.ts", "", ScriptTarget.ES2015);
77+
const syntheticNode = createClassDeclaration(
78+
undefined,
79+
undefined,
80+
/*name*/ createIdentifier("C"),
81+
undefined,
82+
undefined,
83+
createNodeArray([
84+
createProperty(
85+
undefined,
86+
createNodeArray([createToken(SyntaxKind.PublicKeyword)]),
87+
createIdentifier("prop"),
88+
undefined,
89+
undefined,
90+
undefined
91+
)
92+
])
93+
);
94+
printsCorrectly("class", {}, printer => printer.printNode(EmitHint.Unspecified, syntheticNode, sourceFile));
95+
});
96+
});
97+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*! [a.ts] */
2+
// comment0
3+
const a = 1;
4+
/*! [b.ts] */
5+
// comment1
6+
const b = 2;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*! [a.ts] */
2+
const a = 1;
3+
/*! [b.ts] */
4+
const b = 2;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
interface A<T> {
2+
// comment1
3+
readonly prop?: T;
4+
// comment2
5+
method(): void;
6+
// comment3
7+
new <T>(): A<T>;
8+
// comment4
9+
<T>(): A<T>;
10+
}
11+
// comment5
12+
type B = number | string | object;
13+
type C = A<number> & {
14+
x: string;
15+
}; // comment6
16+
// comment7
17+
enum E1 {
18+
// comment8
19+
first
20+
}
21+
const enum E2 {
22+
second
23+
}
24+
// comment9
25+
console.log(1 + 2);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
interface A<T> {
2+
readonly prop?: T;
3+
method(): void;
4+
new <T>(): A<T>;
5+
<T>(): A<T>;
6+
}
7+
type B = number | string | object;
8+
type C = A<number> & {
9+
x: string;
10+
};
11+
enum E1 {
12+
first
13+
}
14+
const enum E2 {
15+
second
16+
}
17+
console.log(1 + 2);

0 commit comments

Comments
 (0)