Skip to content

Commit ced8785

Browse files
committed
eliminate usage of TypeScript module from services layer
1 parent d225035 commit ced8785

15 files changed

Lines changed: 519 additions & 150 deletions

src/harness/fourslash.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ module FourSlash {
215215
}
216216

217217
public setCancelled(numberOfCalls: number = 0): void {
218-
TypeScript.Debug.assert(numberOfCalls >= 0);
218+
ts.Debug.assert(numberOfCalls >= 0);
219219
this.numberOfCallsBeforeCancellation = numberOfCalls;
220220
}
221221

@@ -239,7 +239,7 @@ module FourSlash {
239239

240240
// This function creates IScriptSnapshot object for testing getPreProcessedFileInfo
241241
// Return object may lack some functionalities for other purposes.
242-
function createScriptSnapShot(sourceText: string): TypeScript.IScriptSnapshot {
242+
function createScriptSnapShot(sourceText: string): ts.IScriptSnapshot {
243243
return {
244244
getText: (start: number, end: number) => {
245245
return sourceText.substr(start, end - start);
@@ -250,8 +250,8 @@ module FourSlash {
250250
getLineStartPositions: () => {
251251
return <number[]>[];
252252
},
253-
getChangeRange: (oldSnapshot: TypeScript.IScriptSnapshot) => {
254-
return <TypeScript.TextChangeRange>undefined;
253+
getChangeRange: (oldSnapshot: ts.IScriptSnapshot) => {
254+
return <ts.TextChangeRange>undefined;
255255
}
256256
};
257257
}
@@ -262,7 +262,7 @@ module FourSlash {
262262
private languageService: ts.LanguageService;
263263

264264
// A reference to the language service's compiler state's compiler instance
265-
private compiler: () => { getSyntaxTree(fileName: string): TypeScript.SyntaxTree; getSourceUnit(fileName: string): TypeScript.SourceUnitSyntax; };
265+
private compiler: () => { getSyntaxTree(fileName: string): ts.SourceFile };
266266

267267
// The current caret position in the active file
268268
public currentCaretPosition = 0;
@@ -403,8 +403,9 @@ module FourSlash {
403403
public goToPosition(pos: number) {
404404
this.currentCaretPosition = pos;
405405

406-
var lineCharPos = TypeScript.LineMap1.fromString(this.getCurrentFileContent()).getLineAndCharacterFromPosition(pos);
407-
this.scenarioActions.push('<MoveCaretToLineAndChar LineNumber="' + (lineCharPos.line() + 1) + '" CharNumber="' + (lineCharPos.character() + 1) + '" />');
406+
var lineStarts = ts.computeLineStarts(this.getCurrentFileContent());
407+
var lineCharPos = ts.getLineAndCharacterOfPosition(lineStarts, pos);
408+
this.scenarioActions.push('<MoveCaretToLineAndChar LineNumber="' + lineCharPos.line + '" CharNumber="' + lineCharPos.character + '" />');
408409
}
409410

410411
public moveCaretRight(count = 1) {
@@ -1017,7 +1018,7 @@ module FourSlash {
10171018

10181019
private alignmentForExtraInfo = 50;
10191020

1020-
private spanInfoToString(pos: number, spanInfo: TypeScript.TextSpan, prefixString: string) {
1021+
private spanInfoToString(pos: number, spanInfo: ts.TextSpan, prefixString: string) {
10211022
var resultString = "SpanInfo: " + JSON.stringify(spanInfo);
10221023
if (spanInfo) {
10231024
var spanString = this.activeFile.content.substr(spanInfo.start(), spanInfo.length());
@@ -1034,7 +1035,7 @@ module FourSlash {
10341035
return resultString;
10351036
}
10361037

1037-
private baselineCurrentFileLocations(getSpanAtPos: (pos: number) => TypeScript.TextSpan): string {
1038+
private baselineCurrentFileLocations(getSpanAtPos: (pos: number) => ts.TextSpan): string {
10381039
var fileLineMap = ts.computeLineStarts(this.activeFile.content);
10391040
var nextLine = 0;
10401041
var resultString = "";
@@ -1748,14 +1749,14 @@ module FourSlash {
17481749

17491750
public verifySemanticClassifications(expected: { classificationType: string; text: string }[]) {
17501751
var actual = this.languageService.getSemanticClassifications(this.activeFile.fileName,
1751-
new TypeScript.TextSpan(0, this.activeFile.content.length));
1752+
new ts.TextSpan(0, this.activeFile.content.length));
17521753

17531754
this.verifyClassifications(expected, actual);
17541755
}
17551756

17561757
public verifySyntacticClassifications(expected: { classificationType: string; text: string }[]) {
17571758
var actual = this.languageService.getSyntacticClassifications(this.activeFile.fileName,
1758-
new TypeScript.TextSpan(0, this.activeFile.content.length));
1759+
new ts.TextSpan(0, this.activeFile.content.length));
17591760

17601761
this.verifyClassifications(expected, actual);
17611762
}
@@ -1789,7 +1790,7 @@ module FourSlash {
17891790
for (var i = 0; i < spans.length; i++) {
17901791
var expectedSpan = spans[i];
17911792
var actualComment = actual[i];
1792-
var actualCommentSpan = new TypeScript.TextSpan(actualComment.position, actualComment.message.length);
1793+
var actualCommentSpan = new ts.TextSpan(actualComment.position, actualComment.message.length);
17931794

17941795
if (expectedSpan.start !== actualCommentSpan.start() || expectedSpan.end !== actualCommentSpan.end()) {
17951796
this.raiseError('verifyOutliningSpans failed - span ' + (i + 1) + ' expected: (' + expectedSpan.start + ',' + expectedSpan.end + '), actual: (' + actualCommentSpan.start() + ',' + actualCommentSpan.end() + ')');

src/harness/harnessLanguageService.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
module Harness.LanguageService {
55
export class ScriptInfo {
66
public version: number = 1;
7-
public editRanges: { length: number; textChangeRange: TypeScript.TextChangeRange; }[] = [];
8-
public lineMap: TypeScript.LineMap = null;
7+
public editRanges: { length: number; textChangeRange: ts.TextChangeRange; }[] = [];
8+
public lineMap: number[] = null;
99

1010
constructor(public fileName: string, public content: string, public isOpen = true) {
1111
this.setContent(content);
1212
}
1313

1414
private setContent(content: string): void {
1515
this.content = content;
16-
this.lineMap = TypeScript.LineMap1.fromString(content);
16+
this.lineMap = ts.computeLineStarts(content);
1717
}
1818

1919
public updateContent(content: string): void {
@@ -32,30 +32,30 @@ module Harness.LanguageService {
3232
// Store edit range + new length of script
3333
this.editRanges.push({
3434
length: this.content.length,
35-
textChangeRange: new TypeScript.TextChangeRange(
36-
TypeScript.TextSpan.fromBounds(minChar, limChar), newText.length)
35+
textChangeRange: new ts.TextChangeRange(
36+
ts.TextSpan.fromBounds(minChar, limChar), newText.length)
3737
});
3838

3939
// Update version #
4040
this.version++;
4141
}
4242

43-
public getTextChangeRangeBetweenVersions(startVersion: number, endVersion: number): TypeScript.TextChangeRange {
43+
public getTextChangeRangeBetweenVersions(startVersion: number, endVersion: number): ts.TextChangeRange {
4444
if (startVersion === endVersion) {
4545
// No edits!
46-
return TypeScript.TextChangeRange.unchanged;
46+
return ts.TextChangeRange.unchanged;
4747
}
4848

4949
var initialEditRangeIndex = this.editRanges.length - (this.version - startVersion);
5050
var lastEditRangeIndex = this.editRanges.length - (this.version - endVersion);
5151

5252
var entries = this.editRanges.slice(initialEditRangeIndex, lastEditRangeIndex);
53-
return TypeScript.TextChangeRange.collapseChangesAcrossMultipleVersions(entries.map(e => e.textChangeRange));
53+
return ts.TextChangeRange.collapseChangesAcrossMultipleVersions(entries.map(e => e.textChangeRange));
5454
}
5555
}
5656

5757
class ScriptSnapshotShim implements ts.ScriptSnapshotShim {
58-
private lineMap: TypeScript.LineMap = null;
58+
private lineMap: number[] = null;
5959
private textSnapshot: string;
6060
private version: number;
6161

@@ -74,10 +74,10 @@ module Harness.LanguageService {
7474

7575
public getLineStartPositions(): string {
7676
if (this.lineMap === null) {
77-
this.lineMap = TypeScript.LineMap1.fromString(this.textSnapshot);
77+
this.lineMap = ts.computeLineStarts(this.textSnapshot);
7878
}
7979

80-
return JSON.stringify(this.lineMap.lineStarts());
80+
return JSON.stringify(this.lineMap);
8181
}
8282

8383
public getChangeRange(oldScript: ts.ScriptSnapshotShim): string {
@@ -108,7 +108,7 @@ module Harness.LanguageService {
108108
public acquireDocument(
109109
fileName: string,
110110
compilationSettings: ts.CompilerOptions,
111-
scriptSnapshot: TypeScript.IScriptSnapshot,
111+
scriptSnapshot: ts.IScriptSnapshot,
112112
version: string,
113113
isOpen: boolean): ts.SourceFile {
114114
return ts.createSourceFile(fileName, scriptSnapshot.getText(0, scriptSnapshot.getLength()), compilationSettings.target, version, isOpen);
@@ -118,10 +118,10 @@ module Harness.LanguageService {
118118
document: ts.SourceFile,
119119
fileName: string,
120120
compilationSettings: ts.CompilerOptions,
121-
scriptSnapshot: TypeScript.IScriptSnapshot,
121+
scriptSnapshot: ts.IScriptSnapshot,
122122
version: string,
123123
isOpen: boolean,
124-
textChangeRange: TypeScript.TextChangeRange
124+
textChangeRange: ts.TextChangeRange
125125
): ts.SourceFile {
126126
return document.update(scriptSnapshot, version, isOpen, textChangeRange);
127127
}
@@ -263,13 +263,13 @@ module Harness.LanguageService {
263263
}
264264

265265
/** Parse file given its source text */
266-
public parseSourceText(fileName: string, sourceText: TypeScript.IScriptSnapshot): TypeScript.SourceUnitSyntax {
267-
return TypeScript.Parser.parse(fileName, TypeScript.SimpleText.fromScriptSnapshot(sourceText), ts.ScriptTarget.Latest, TypeScript.isDTSFile(fileName)).sourceUnit();
266+
public parseSourceText(fileName: string, sourceText: ts.IScriptSnapshot): ts.SourceFile {
267+
return ts.createSourceFile(fileName, sourceText.getText(0, sourceText.getLength()), ts.ScriptTarget.Latest, "1", true);
268268
}
269269

270270
/** Parse a file on disk given its fileName */
271271
public parseFile(fileName: string) {
272-
var sourceText = TypeScript.ScriptSnapshot.fromString(Harness.IO.readFile(fileName));
272+
var sourceText = ts.ScriptSnapshot.fromString(Harness.IO.readFile(fileName));
273273
return this.parseSourceText(fileName, sourceText);
274274
}
275275

@@ -283,22 +283,22 @@ module Harness.LanguageService {
283283
assert.isTrue(line >= 1);
284284
assert.isTrue(col >= 1);
285285

286-
return script.lineMap.getPosition(line - 1, col - 1);
286+
return ts.getPositionFromLineAndCharacter(script.lineMap, line, col);
287287
}
288288

289289
/**
290290
* @param line 0 based index
291291
* @param col 0 based index
292292
*/
293-
public positionToZeroBasedLineCol(fileName: string, position: number): TypeScript.ILineAndCharacter {
293+
public positionToZeroBasedLineCol(fileName: string, position: number): ts.LineAndCharacter {
294294
var script: ScriptInfo = this.fileNameToScript[fileName];
295295
assert.isNotNull(script);
296296

297-
var result = script.lineMap.getLineAndCharacterFromPosition(position);
297+
var result = ts.getLineAndCharacterOfPosition(script.lineMap, position);
298298

299-
assert.isTrue(result.line() >= 0);
300-
assert.isTrue(result.character() >= 0);
301-
return { line: result.line(), character: result.character() };
299+
assert.isTrue(result.line >= 1);
300+
assert.isTrue(result.character >= 1);
301+
return { line: result.line - 1, character: result.character - 1 };
302302
}
303303

304304
/** Verify that applying edits to sourceFileName result in the content of the file baselineFileName */

src/services/breakpoints.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ module ts.BreakpointResolver {
3838
return spanInNode(tokenAtLocation);
3939

4040
function textSpan(startNode: Node, endNode?: Node) {
41-
return TypeScript.TextSpan.fromBounds(startNode.getStart(), (endNode || startNode).getEnd());
41+
return TextSpan.fromBounds(startNode.getStart(), (endNode || startNode).getEnd());
4242
}
4343

44-
function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TypeScript.TextSpan {
44+
function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TextSpan {
4545
if (node && lineOfPosition === sourceFile.getLineAndCharacterFromPosition(node.getStart()).line) {
4646
return spanInNode(node);
4747
}
4848
return spanInNode(otherwiseOnNode);
4949
}
5050

51-
function spanInPreviousNode(node: Node): TypeScript.TextSpan {
51+
function spanInPreviousNode(node: Node): TextSpan {
5252
return spanInNode(findPrecedingToken(node.pos, sourceFile));
5353
}
5454

55-
function spanInNextNode(node: Node): TypeScript.TextSpan {
55+
function spanInNextNode(node: Node): TextSpan {
5656
return spanInNode(findNextToken(node, node.parent));
5757
}
5858

59-
function spanInNode(node: Node): TypeScript.TextSpan {
59+
function spanInNode(node: Node): TextSpan {
6060
if (node) {
6161
if (isExpression(node)) {
6262
if (node.parent.kind === SyntaxKind.DoStatement) {
@@ -256,7 +256,7 @@ module ts.BreakpointResolver {
256256
}
257257
}
258258

259-
function spanInVariableDeclaration(variableDeclaration: VariableDeclaration): TypeScript.TextSpan {
259+
function spanInVariableDeclaration(variableDeclaration: VariableDeclaration): TextSpan {
260260
// If declaration of for in statement, just set the span in parent
261261
if (variableDeclaration.parent.kind === SyntaxKind.ForInStatement) {
262262
return spanInNode(variableDeclaration.parent);
@@ -301,7 +301,7 @@ module ts.BreakpointResolver {
301301
!!(parameter.flags & NodeFlags.Public) || !!(parameter.flags & NodeFlags.Private);
302302
}
303303

304-
function spanInParameterDeclaration(parameter: ParameterDeclaration): TypeScript.TextSpan {
304+
function spanInParameterDeclaration(parameter: ParameterDeclaration): TextSpan {
305305
if (canHaveSpanInParameterDeclaration(parameter)) {
306306
return textSpan(parameter);
307307
}
@@ -324,7 +324,7 @@ module ts.BreakpointResolver {
324324
(functionDeclaration.parent.kind === SyntaxKind.ClassDeclaration && functionDeclaration.kind !== SyntaxKind.Constructor);
325325
}
326326

327-
function spanInFunctionDeclaration(functionDeclaration: FunctionLikeDeclaration): TypeScript.TextSpan {
327+
function spanInFunctionDeclaration(functionDeclaration: FunctionLikeDeclaration): TextSpan {
328328
// No breakpoints in the function signature
329329
if (!functionDeclaration.body) {
330330
return undefined;
@@ -339,7 +339,7 @@ module ts.BreakpointResolver {
339339
return spanInNode(functionDeclaration.body);
340340
}
341341

342-
function spanInFunctionBlock(block: Block): TypeScript.TextSpan {
342+
function spanInFunctionBlock(block: Block): TextSpan {
343343
var nodeForSpanInBlock = block.statements.length ? block.statements[0] : block.getLastToken();
344344
if (canFunctionHaveSpanInWholeDeclaration(<FunctionLikeDeclaration>block.parent)) {
345345
return spanInNodeIfStartsOnSameLine(block.parent, nodeForSpanInBlock);
@@ -348,7 +348,7 @@ module ts.BreakpointResolver {
348348
return spanInNode(nodeForSpanInBlock);
349349
}
350350

351-
function spanInBlock(block: Block): TypeScript.TextSpan {
351+
function spanInBlock(block: Block): TextSpan {
352352
switch (block.parent.kind) {
353353
case SyntaxKind.ModuleDeclaration:
354354
if (getModuleInstanceState(block.parent) !== ModuleInstanceState.Instantiated) {
@@ -370,7 +370,7 @@ module ts.BreakpointResolver {
370370
return spanInNode(block.statements[0]);
371371
}
372372

373-
function spanInForStatement(forStatement: ForStatement): TypeScript.TextSpan {
373+
function spanInForStatement(forStatement: ForStatement): TextSpan {
374374
if (forStatement.declarations) {
375375
return spanInNode(forStatement.declarations[0]);
376376
}
@@ -386,7 +386,7 @@ module ts.BreakpointResolver {
386386
}
387387

388388
// Tokens:
389-
function spanInOpenBraceToken(node: Node): TypeScript.TextSpan {
389+
function spanInOpenBraceToken(node: Node): TextSpan {
390390
switch (node.parent.kind) {
391391
case SyntaxKind.EnumDeclaration:
392392
var enumDeclaration = <EnumDeclaration>node.parent;
@@ -404,7 +404,7 @@ module ts.BreakpointResolver {
404404
return spanInNode(node.parent);
405405
}
406406

407-
function spanInCloseBraceToken(node: Node): TypeScript.TextSpan {
407+
function spanInCloseBraceToken(node: Node): TextSpan {
408408
switch (node.parent.kind) {
409409
case SyntaxKind.ModuleBlock:
410410
// If this is not instantiated module block no bp span
@@ -439,7 +439,7 @@ module ts.BreakpointResolver {
439439
}
440440
}
441441

442-
function spanInOpenParenToken(node: Node): TypeScript.TextSpan {
442+
function spanInOpenParenToken(node: Node): TextSpan {
443443
if (node.parent.kind === SyntaxKind.DoStatement) {
444444
// Go to while keyword and do action instead
445445
return spanInPreviousNode(node);
@@ -449,7 +449,7 @@ module ts.BreakpointResolver {
449449
return spanInNode(node.parent);
450450
}
451451

452-
function spanInCloseParenToken(node: Node): TypeScript.TextSpan {
452+
function spanInCloseParenToken(node: Node): TextSpan {
453453
// Is this close paren token of parameter list, set span in previous token
454454
switch (node.parent.kind) {
455455
case SyntaxKind.FunctionExpression:
@@ -473,7 +473,7 @@ module ts.BreakpointResolver {
473473
return spanInNode(node.parent);
474474
}
475475

476-
function spanInColonToken(node: Node): TypeScript.TextSpan {
476+
function spanInColonToken(node: Node): TextSpan {
477477
// Is this : specifying return annotation of the function declaration
478478
if (isAnyFunction(node.parent) || node.parent.kind === SyntaxKind.PropertyAssignment) {
479479
return spanInPreviousNode(node);
@@ -482,15 +482,15 @@ module ts.BreakpointResolver {
482482
return spanInNode(node.parent);
483483
}
484484

485-
function spanInGreaterThanOrLessThanToken(node: Node): TypeScript.TextSpan {
485+
function spanInGreaterThanOrLessThanToken(node: Node): TextSpan {
486486
if (node.parent.kind === SyntaxKind.TypeAssertion) {
487487
return spanInNode((<TypeAssertion>node.parent).operand);
488488
}
489489

490490
return spanInNode(node.parent);
491491
}
492492

493-
function spanInWhileKeyword(node: Node): TypeScript.TextSpan {
493+
function spanInWhileKeyword(node: Node): TextSpan {
494494
if (node.parent.kind === SyntaxKind.DoStatement) {
495495
// Set span on while expression
496496
return textSpan(node, findNextToken((<DoStatement>node.parent).expression, node.parent));

src/services/formatting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ module ts.formatting {
834834
}
835835

836836
function newTextChange(start: number, len: number, newText: string): TextChange {
837-
return { span: new TypeScript.TextSpan(start, len), newText: newText }
837+
return { span: new TextSpan(start, len), newText: newText }
838838
}
839839

840840
function recordDelete(start: number, len: number) {

0 commit comments

Comments
 (0)