Skip to content

Commit 2b10611

Browse files
authored
initial revision of infrastructure to produce text changes that uses existing node factory, formatter and printer (microsoft#14441)
initial revision of infrastructure to produce text changes that uses existing node factory, formatter and printer
1 parent ea57fbc commit 2b10611

File tree

118 files changed

+3014
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+3014
-345
lines changed

Jakefile.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ var ts = require("./lib/typescript");
1111

1212
// Variables
1313
var compilerDirectory = "src/compiler/";
14-
var servicesDirectory = "src/services/";
1514
var serverDirectory = "src/server/";
16-
var typingsInstallerDirectory = "src/server/typingsInstaller";
17-
var cancellationTokenDirectory = "src/server/cancellationToken";
18-
var watchGuardDirectory = "src/server/watchGuard";
1915
var harnessDirectory = "src/harness/";
2016
var libraryDirectory = "src/lib/";
2117
var scriptsDirectory = "scripts/";
@@ -131,6 +127,7 @@ var harnessSources = harnessCoreSources.concat([
131127
"matchFiles.ts",
132128
"initializeTSConfig.ts",
133129
"printer.ts",
130+
"textChanges.ts",
134131
"transform.ts",
135132
"customTransforms.ts",
136133
].map(function (f) {

src/compiler/emitter.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ namespace ts {
199199
onEmitHelpers,
200200
onSetSourceFile,
201201
substituteNode,
202+
onBeforeEmitNodeArray,
203+
onAfterEmitNodeArray
202204
} = handlers;
203205

204206
const newLine = getNewLineCharacter(printerOptions);
@@ -631,6 +633,11 @@ namespace ts {
631633
if (isExpression(node)) {
632634
return pipelineEmitExpression(trySubstituteNode(EmitHint.Expression, node));
633635
}
636+
637+
if (isToken(node)) {
638+
writeTokenText(kind);
639+
return;
640+
}
634641
}
635642

636643
function pipelineEmitExpression(node: Node): void {
@@ -1553,6 +1560,10 @@ namespace ts {
15531560
emitSignatureAndBody(node, emitSignatureHead);
15541561
}
15551562

1563+
function emitBlockCallback(_hint: EmitHint, body: Node): void {
1564+
emitBlockFunctionBody(<Block>body);
1565+
}
1566+
15561567
function emitSignatureAndBody(node: FunctionLikeDeclaration, emitSignatureHead: (node: SignatureDeclaration) => void) {
15571568
const body = node.body;
15581569
if (body) {
@@ -1564,12 +1575,22 @@ namespace ts {
15641575

15651576
if (getEmitFlags(node) & EmitFlags.ReuseTempVariableScope) {
15661577
emitSignatureHead(node);
1567-
emitBlockFunctionBody(body);
1578+
if (onEmitNode) {
1579+
onEmitNode(EmitHint.Unspecified, body, emitBlockCallback);
1580+
}
1581+
else {
1582+
emitBlockFunctionBody(body);
1583+
}
15681584
}
15691585
else {
15701586
pushNameGenerationScope();
15711587
emitSignatureHead(node);
1572-
emitBlockFunctionBody(body);
1588+
if (onEmitNode) {
1589+
onEmitNode(EmitHint.Unspecified, body, emitBlockCallback);
1590+
}
1591+
else {
1592+
emitBlockFunctionBody(body);
1593+
}
15731594
popNameGenerationScope();
15741595
}
15751596

@@ -2200,6 +2221,10 @@ namespace ts {
22002221
write(getOpeningBracket(format));
22012222
}
22022223

2224+
if (onBeforeEmitNodeArray) {
2225+
onBeforeEmitNodeArray(children);
2226+
}
2227+
22032228
if (isEmpty) {
22042229
// Write a line terminator if the parent node was multi-line
22052230
if (format & ListFormat.MultiLine) {
@@ -2315,6 +2340,10 @@ namespace ts {
23152340
}
23162341
}
23172342

2343+
if (onAfterEmitNodeArray) {
2344+
onAfterEmitNodeArray(children);
2345+
}
2346+
23182347
if (format & ListFormat.BracketsMask) {
23192348
write(getClosingBracket(format));
23202349
}

src/compiler/factory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,10 @@ namespace ts {
10991099
: node;
11001100
}
11011101

1102+
export function createKeywordTypeNode(kind: KeywordTypeNode["kind"]): KeywordTypeNode {
1103+
return <KeywordTypeNode>createSynthesizedNode(kind);
1104+
}
1105+
11021106
export function createFunctionDeclaration(decorators: Decorator[] | undefined, modifiers: Modifier[] | undefined, asteriskToken: AsteriskToken | undefined, name: string | Identifier | undefined, typeParameters: TypeParameterDeclaration[] | undefined, parameters: ParameterDeclaration[], type: TypeNode | undefined, body: Block | undefined) {
11031107
const node = <FunctionDeclaration>createSynthesizedNode(SyntaxKind.FunctionDeclaration);
11041108
node.decorators = asNodeArray(decorators);

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ namespace ts {
333333
}
334334

335335
/* @internal */
336-
export function getLineStarts(sourceFile: SourceFile): number[] {
336+
export function getLineStarts(sourceFile: SourceFileLike): number[] {
337337
return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text));
338338
}
339339

src/compiler/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,16 @@ namespace ts {
22052205
name: string;
22062206
}
22072207

2208+
/* @internal */
2209+
/**
2210+
* Subset of properties from SourceFile that are used in multiple utility functions
2211+
*/
2212+
export interface SourceFileLike {
2213+
readonly text: string;
2214+
lineMap: number[];
2215+
}
2216+
2217+
22082218
// Source files are declarations when they are external modules.
22092219
export interface SourceFile extends Declaration {
22102220
kind: SyntaxKind.SourceFile;
@@ -4132,6 +4142,8 @@ namespace ts {
41324142
/*@internal*/ onEmitSourceMapOfPosition?: (pos: number) => void;
41334143
/*@internal*/ onEmitHelpers?: (node: Node, writeLines: (text: string) => void) => void;
41344144
/*@internal*/ onSetSourceFile?: (node: SourceFile) => void;
4145+
/*@internal*/ onBeforeEmitNodeArray?: (nodes: NodeArray<any>) => void;
4146+
/*@internal*/ onAfterEmitNodeArray?: (nodes: NodeArray<any>) => void;
41354147
}
41364148

41374149
export interface PrinterOptions {

src/compiler/utilities.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ namespace ts {
184184
return false;
185185
}
186186

187-
export function getStartPositionOfLine(line: number, sourceFile: SourceFile): number {
187+
export function getStartPositionOfLine(line: number, sourceFile: SourceFileLike): number {
188188
Debug.assert(line >= 0);
189189
return getLineStarts(sourceFile)[line];
190190
}
@@ -204,7 +204,7 @@ namespace ts {
204204
return value !== undefined;
205205
}
206206

207-
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
207+
export function getEndLinePosition(line: number, sourceFile: SourceFileLike): number {
208208
Debug.assert(line >= 0);
209209
const lineStarts = getLineStarts(sourceFile);
210210

@@ -255,7 +255,11 @@ namespace ts {
255255
return !nodeIsMissing(node);
256256
}
257257

258-
export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile, includeJsDoc?: boolean): number {
258+
export function isToken(n: Node): boolean {
259+
return n.kind >= SyntaxKind.FirstToken && n.kind <= SyntaxKind.LastToken;
260+
}
261+
262+
export function getTokenPosOfNode(node: Node, sourceFile?: SourceFileLike, includeJsDoc?: boolean): number {
259263
// With nodes that have no width (i.e. 'Missing' nodes), we actually *don't*
260264
// want to skip trivia because this will launch us forward to the next token.
261265
if (nodeIsMissing(node)) {
@@ -289,7 +293,7 @@ namespace ts {
289293
return node.kind >= SyntaxKind.FirstJSDocTagNode && node.kind <= SyntaxKind.LastJSDocTagNode;
290294
}
291295

292-
export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFile): number {
296+
export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFileLike): number {
293297
if (nodeIsMissing(node) || !node.decorators) {
294298
return getTokenPosOfNode(node, sourceFile);
295299
}
@@ -2491,7 +2495,7 @@ namespace ts {
24912495
return indentStrings[1].length;
24922496
}
24932497

2494-
export function createTextWriter(newLine: String): EmitTextWriter {
2498+
export function createTextWriter(newLine: string): EmitTextWriter {
24952499
let output: string;
24962500
let indent: number;
24972501
let lineStart: boolean;

0 commit comments

Comments
 (0)