Skip to content

Commit 7e25179

Browse files
author
Arthur Ozga
committed
Basic functionality
* pass context as argument in xToNode methods * make sourcefile optional in printer * start consolidating NodeBuilderFlags and TypeFormatFlags
1 parent 5e4b8d6 commit 7e25179

7 files changed

Lines changed: 163 additions & 125 deletions

File tree

src/compiler/checker.ts

Lines changed: 106 additions & 80 deletions
Large diffs are not rendered by default.

src/compiler/emitter.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ namespace ts {
213213
emitLeadingCommentsOfPosition,
214214
} = comments;
215215

216-
let currentSourceFile: SourceFile;
216+
let currentSourceFile: SourceFile | undefined;
217217
let nodeIdToGeneratedName: string[]; // Map of generated names for specific nodes.
218218
let autoGeneratedIdToGeneratedName: string[]; // Map of generated names for temp and loop variables.
219219
let generatedNames: Map<string>; // Set of names generated by the NameGenerator.
@@ -235,7 +235,7 @@ namespace ts {
235235
writeBundle
236236
};
237237

238-
function printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string {
238+
function printNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined): string {
239239
switch (hint) {
240240
case EmitHint.SourceFile:
241241
Debug.assert(isSourceFile(node), "Expected a SourceFile node.");
@@ -265,7 +265,7 @@ namespace ts {
265265
return endPrint();
266266
}
267267

268-
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, output: EmitTextWriter) {
268+
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, output: EmitTextWriter) {
269269
const previousWriter = writer;
270270
setWriter(output);
271271
print(hint, node, sourceFile);
@@ -302,8 +302,10 @@ namespace ts {
302302
return text;
303303
}
304304

305-
function print(hint: EmitHint, node: Node, sourceFile: SourceFile) {
306-
setSourceFile(sourceFile);
305+
function print(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined) {
306+
if (sourceFile) {
307+
setSourceFile(sourceFile);
308+
}
307309
pipelineEmitWithNotification(hint, node);
308310
}
309311

@@ -1101,7 +1103,7 @@ namespace ts {
11011103
function emitPropertyAccessExpression(node: PropertyAccessExpression) {
11021104
let indentBeforeDot = false;
11031105
let indentAfterDot = false;
1104-
if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) {
1106+
if (currentSourceFile && !(getEmitFlags(node) & EmitFlags.NoIndentation)) {
11051107
const dotRangeStart = node.expression.end;
11061108
const dotRangeEnd = skipTrivia(currentSourceFile.text, node.expression.end) + 1;
11071109
const dotToken = <Node>{ kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd };
@@ -2461,7 +2463,7 @@ namespace ts {
24612463

24622464
const firstChild = children[0];
24632465
if (firstChild === undefined) {
2464-
return !rangeIsOnSingleLine(parentNode, currentSourceFile);
2466+
return !(currentSourceFile && rangeIsOnSingleLine(parentNode, currentSourceFile));
24652467
}
24662468
else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(firstChild)) {
24672469
return synthesizedNodeStartsOnNewLine(firstChild, format);
@@ -2487,7 +2489,7 @@ namespace ts {
24872489
return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format);
24882490
}
24892491
else {
2490-
return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile);
2492+
return !(currentSourceFile && rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile));
24912493
}
24922494
}
24932495
else {
@@ -2506,13 +2508,13 @@ namespace ts {
25062508

25072509
const lastChild = lastOrUndefined(children);
25082510
if (lastChild === undefined) {
2509-
return !rangeIsOnSingleLine(parentNode, currentSourceFile);
2511+
return !(currentSourceFile && rangeIsOnSingleLine(parentNode, currentSourceFile));
25102512
}
25112513
else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(lastChild)) {
25122514
return synthesizedNodeStartsOnNewLine(lastChild, format);
25132515
}
25142516
else {
2515-
return !rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile);
2517+
return !(currentSourceFile && rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile));
25162518
}
25172519
}
25182520
else {
@@ -2901,7 +2903,7 @@ namespace ts {
29012903
// Precomputed Formats
29022904
Modifiers = SingleLine | SpaceBetweenSiblings,
29032905
HeritageClauses = SingleLine | SpaceBetweenSiblings,
2904-
TypeLiteralMembers = MultiLine | Indented,
2906+
TypeLiteralMembers = SpaceBetweenBraces | SpaceBetweenSiblings | Indented, // MultiLine | Indented,
29052907
TupleTypeElements = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented,
29062908
UnionTypeConstituents = BarDelimited | SpaceBetweenSiblings | SingleLine,
29072909
IntersectionTypeConstituents = AmpersandDelimited | SpaceBetweenSiblings | SingleLine,

src/compiler/factory.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
/// <reference path="utilities.ts"/>
33

44
namespace ts {
5+
export const nullTransformationContext: TransformationContext = {
6+
enableEmitNotification: noop,
7+
enableSubstitution: noop,
8+
endLexicalEnvironment: () => undefined,
9+
getCompilerOptions: notImplemented,
10+
getEmitHost: notImplemented,
11+
getEmitResolver: notImplemented,
12+
hoistFunctionDeclaration: noop,
13+
hoistVariableDeclaration: noop,
14+
isEmitNotificationEnabled: notImplemented,
15+
isSubstitutionEnabled: notImplemented,
16+
onEmitNode: noop,
17+
onSubstituteNode: notImplemented,
18+
readEmitHelpers: notImplemented,
19+
requestEmitHelper: noop,
20+
resumeLexicalEnvironment: noop,
21+
startLexicalEnvironment: noop,
22+
suspendLexicalEnvironment: noop
23+
};
24+
525
function createSynthesizedNode(kind: SyntaxKind): Node {
626
const node = createNode(kind, -1, -1);
727
node.flags |= NodeFlags.Synthesized;
@@ -64,6 +84,11 @@ namespace ts {
6484
return clone;
6585
}
6686

87+
export function getDeepSynthesizedClone<T extends Node>(node: T | undefined): T {
88+
const clone = visitEachChild(node, getDeepSynthesizedClone, nullTransformationContext, /*nodeVisitor*/ undefined, getSynthesizedClone);
89+
return nodeIsSynthesized(clone) ? clone : getSynthesizedClone(clone);
90+
}
91+
6792
// Literals
6893

6994
export function createLiteral(value: string): StringLiteral;

src/compiler/types.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,8 +2541,10 @@ namespace ts {
25412541
allowQualifedNameInPlaceOfIdentifier = 1 << 1,
25422542
allowTypeParameterInQualifiedName = 1 << 2,
25432543
allowAnonymousIdentifier = 1 << 3,
2544-
allowEmptyUnionOrIntersection = 1 << 4,
2545-
allowEmptyTuple = 1 << 5
2544+
allowEmptyUnionOrIntersection = 1 << 4,
2545+
allowEmptyTuple = 1 << 5,
2546+
suppressAnyReturnType = 1 << 6,
2547+
ignoreErrors = allowThisInObjectLiteral | allowQualifedNameInPlaceOfIdentifier | allowTypeParameterInQualifiedName | allowAnonymousIdentifier | allowEmptyUnionOrIntersection | allowEmptyTuple
25462548
}
25472549

25482550
export interface SymbolDisplayBuilder {
@@ -3470,6 +3472,7 @@ namespace ts {
34703472
export const enum NewLineKind {
34713473
CarriageReturnLineFeed = 0,
34723474
LineFeed = 1,
3475+
None = 2
34733476
}
34743477

34753478
export interface LineAndCharacter {
@@ -4115,7 +4118,7 @@ namespace ts {
41154118
* the identifiers of the source file are used when generating unique names to avoid
41164119
* collisions.
41174120
*/
4118-
printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string;
4121+
printNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined): string;
41194122
/**
41204123
* Prints a source file as-is, without any emit transformations.
41214124
*/
@@ -4124,7 +4127,7 @@ namespace ts {
41244127
* Prints a bundle of source files as-is, without any emit transformations.
41254128
*/
41264129
printBundle(bundle: Bundle): string;
4127-
/*@internal*/ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, writer: EmitTextWriter): void;
4130+
/*@internal*/ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, writer: EmitTextWriter): void;
41284131
/*@internal*/ writeFile(sourceFile: SourceFile, writer: EmitTextWriter): void;
41294132
/*@internal*/ writeBundle(bundle: Bundle, writer: EmitTextWriter): void;
41304133
}

src/compiler/utilities.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,13 +3272,15 @@ namespace ts {
32723272
const carriageReturnLineFeed = "\r\n";
32733273
const lineFeed = "\n";
32743274
export function getNewLineCharacter(options: CompilerOptions | PrinterOptions): string {
3275-
if (options.newLine === NewLineKind.CarriageReturnLineFeed) {
3276-
return carriageReturnLineFeed;
3277-
}
3278-
else if (options.newLine === NewLineKind.LineFeed) {
3279-
return lineFeed;
3280-
}
3281-
else if (sys) {
3275+
switch (options.newLine) {
3276+
case NewLineKind.None:
3277+
return "";
3278+
case NewLineKind.CarriageReturnLineFeed:
3279+
return carriageReturnLineFeed;
3280+
case NewLineKind.LineFeed:
3281+
return lineFeed;
3282+
}
3283+
if (sys) {
32823284
return sys.newLine;
32833285
}
32843286
return carriageReturnLineFeed;

src/services/codefixes/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace ts.codefix {
130130
}
131131

132132
function signatureToMethodDeclaration(signature: Signature, enclosingDeclaration: Node, body?: Block) {
133-
const signatureDeclaration = <MethodDeclaration>checker.signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration);
133+
const signatureDeclaration = <MethodDeclaration>checker.signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, NodeBuilderFlags.suppressAnyReturnType);
134134
if (signatureDeclaration) {
135135
signatureDeclaration.decorators = undefined;
136136
signatureDeclaration.modifiers = modifiers;

src/services/textChanges.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ namespace ts.textChanges {
497497
readonly node: Node;
498498
}
499499

500-
export function getNonformattedText(node: Node, sourceFile: SourceFile, newLine: NewLineKind): NonFormattedText {
501-
const options = { newLine, target: sourceFile.languageVersion };
500+
export function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLine: NewLineKind): NonFormattedText {
501+
const options = { newLine, target: sourceFile && sourceFile.languageVersion };
502502
const writer = new Writer(getNewLineCharacter(options));
503503
const printer = createPrinter(options, writer);
504504
printer.writeNode(EmitHint.Unspecified, node, sourceFile, writer);
@@ -528,26 +528,6 @@ namespace ts.textChanges {
528528
return skipTrivia(s, 0) === s.length;
529529
}
530530

531-
const nullTransformationContext: TransformationContext = {
532-
enableEmitNotification: noop,
533-
enableSubstitution: noop,
534-
endLexicalEnvironment: () => undefined,
535-
getCompilerOptions: notImplemented,
536-
getEmitHost: notImplemented,
537-
getEmitResolver: notImplemented,
538-
hoistFunctionDeclaration: noop,
539-
hoistVariableDeclaration: noop,
540-
isEmitNotificationEnabled: notImplemented,
541-
isSubstitutionEnabled: notImplemented,
542-
onEmitNode: noop,
543-
onSubstituteNode: notImplemented,
544-
readEmitHelpers: notImplemented,
545-
requestEmitHelper: noop,
546-
resumeLexicalEnvironment: noop,
547-
startLexicalEnvironment: noop,
548-
suspendLexicalEnvironment: noop
549-
};
550-
551531
function assignPositionsToNode(node: Node): Node {
552532
const visited = visitEachChild(node, assignPositionsToNode, nullTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
553533
// create proxy node for non synthesized nodes

0 commit comments

Comments
 (0)