Skip to content

Commit 626277c

Browse files
committed
Merge pull request #1700 from Microsoft/noMethodsOnSourceFile
Move function typed properties from the SourceFile to a dedicated functions
2 parents 02fbc07 + 45defa8 commit 626277c

19 files changed

+804
-679
lines changed

src/compiler/emitter.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module ts {
134134
}
135135

136136
function getLineOfLocalPosition(currentSourceFile: SourceFile, pos: number) {
137-
return currentSourceFile.getLineAndCharacterFromPosition(pos).line;
137+
return getLineAndCharacterOfPosition(currentSourceFile, pos).line;
138138
}
139139

140140
function emitNewLineBeforeLeadingComments(currentSourceFile: SourceFile, writer: EmitTextWriter, node: TextRange, leadingComments: CommentRange[]) {
@@ -169,16 +169,16 @@ module ts {
169169

170170
function writeCommentRange(currentSourceFile: SourceFile, writer: EmitTextWriter, comment: CommentRange, newLine: string){
171171
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
172-
var firstCommentLineAndCharacter = currentSourceFile.getLineAndCharacterFromPosition(comment.pos);
173-
var lastLine = currentSourceFile.getLineStarts().length;
172+
var firstCommentLineAndCharacter = getLineAndCharacterOfPosition(currentSourceFile, comment.pos);
173+
var lastLine = getLineStarts(currentSourceFile).length;
174174
var firstCommentLineIndent: number;
175175
for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) {
176-
var nextLineStart = currentLine === lastLine ? (comment.end + 1) : currentSourceFile.getPositionFromLineAndCharacter(currentLine + 1, /*character*/1);
176+
var nextLineStart = currentLine === lastLine ? (comment.end + 1) : getPositionFromLineAndCharacter(currentSourceFile, currentLine + 1, /*character*/1);
177177

178178
if (pos !== comment.pos) {
179179
// If we are not emitting first line, we need to write the spaces to adjust the alignment
180180
if (firstCommentLineIndent === undefined) {
181-
firstCommentLineIndent = calculateIndent(currentSourceFile.getPositionFromLineAndCharacter(firstCommentLineAndCharacter.line, /*character*/1),
181+
firstCommentLineIndent = calculateIndent(getPositionFromLineAndCharacter(currentSourceFile, firstCommentLineAndCharacter.line, /*character*/1),
182182
comment.pos);
183183
}
184184

@@ -1660,7 +1660,7 @@ module ts {
16601660
}
16611661

16621662
function recordSourceMapSpan(pos: number) {
1663-
var sourceLinePos = currentSourceFile.getLineAndCharacterFromPosition(pos);
1663+
var sourceLinePos = getLineAndCharacterOfPosition(currentSourceFile, pos);
16641664
var emittedLine = writer.getLine();
16651665
var emittedColumn = writer.getColumn();
16661666

src/compiler/parser.ts

+401-422
Large diffs are not rendered by default.

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ module ts {
245245
else {
246246
files.push(file);
247247
}
248-
forEach(file.getSyntacticDiagnostics(), e => {
248+
forEach(getSyntacticDiagnostics(file), e => {
249249
errors.push(e);
250250
});
251251
}

src/compiler/scanner.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,20 @@ module ts {
278278
return result;
279279
}
280280

281-
export function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
282-
Debug.assert(line > 0 && line <= lineStarts.length );
281+
export function getPositionFromLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
282+
return computePositionFromLineAndCharacter(getLineStarts(sourceFile), line, character);
283+
}
284+
285+
export function computePositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
286+
Debug.assert(line > 0 && line <= lineStarts.length);
283287
return lineStarts[line - 1] + character - 1;
284288
}
285289

286-
export function getLineAndCharacterOfPosition(lineStarts: number[], position: number) {
290+
export function getLineStarts(sourceFile: SourceFile): number[] {
291+
return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text));
292+
}
293+
294+
export function computeLineAndCharacterOfPosition(lineStarts: number[], position: number) {
287295
var lineNumber = binarySearch(lineStarts, position);
288296
if (lineNumber < 0) {
289297
// If the actual position was not found,
@@ -298,9 +306,8 @@ module ts {
298306
};
299307
}
300308

301-
export function positionToLineAndCharacter(text: string, pos: number) {
302-
var lineStarts = computeLineStarts(text);
303-
return getLineAndCharacterOfPosition(lineStarts, pos);
309+
export function getLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter {
310+
return computeLineAndCharacterOfPosition(getLineStarts(sourceFile), position);
304311
}
305312

306313
var hasOwnProperty = Object.prototype.hasOwnProperty;

src/compiler/tsc.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module ts {
7272
function countLines(program: Program): number {
7373
var count = 0;
7474
forEach(program.getSourceFiles(), file => {
75-
count += file.getLineAndCharacterFromPosition(file.end).line;
75+
count += getLineAndCharacterOfPosition(file, file.end).line;
7676
});
7777
return count;
7878
}
@@ -86,7 +86,7 @@ module ts {
8686
var output = "";
8787

8888
if (diagnostic.file) {
89-
var loc = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
89+
var loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
9090

9191
output += diagnostic.file.filename + "(" + loc.line + "," + loc.character + "): ";
9292
}

src/compiler/types.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -887,21 +887,6 @@ module ts {
887887
filename: string;
888888
text: string;
889889

890-
getLineAndCharacterFromPosition(position: number): LineAndCharacter;
891-
getPositionFromLineAndCharacter(line: number, character: number): number;
892-
getLineStarts(): number[];
893-
894-
// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
895-
// indicates what changed between the 'text' that this SourceFile has and the 'newText'.
896-
// The SourceFile will be created with the compiler attempting to reuse as many nodes from
897-
// this file as possible.
898-
//
899-
// Note: this function mutates nodes from this SourceFile. That means any existing nodes
900-
// from this SourceFile that are being held onto may change as a result (including
901-
// becoming detached from any SourceFile). It is recommended that this SourceFile not
902-
// be used once 'update' is called on it.
903-
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
904-
905890
amdDependencies: string[];
906891
amdModuleName: string;
907892
referencedFiles: FileReference[];
@@ -913,19 +898,26 @@ module ts {
913898
// missing tokens, or tokens it didn't know how to deal with).
914899
parseDiagnostics: Diagnostic[];
915900

916-
// Returns all syntactic diagnostics (i.e. the reference, parser and grammar diagnostics).
917-
getSyntacticDiagnostics(): Diagnostic[];
901+
//getSyntacticDiagnostics(): Diagnostic[];
918902

919903
// File level diagnostics reported by the binder.
920904
semanticDiagnostics: Diagnostic[];
921905

906+
// Returns all syntactic diagnostics (i.e. the reference, parser and grammar diagnostics).
907+
// This field should never be used directly, use getSyntacticDiagnostics function instead.
908+
syntacticDiagnostics: Diagnostic[];
909+
922910
hasNoDefaultLib: boolean;
923911
externalModuleIndicator: Node; // The first node that causes this file to be an external module
924912
nodeCount: number;
925913
identifierCount: number;
926914
symbolCount: number;
927915
languageVersion: ScriptTarget;
928916
identifiers: Map<string>;
917+
918+
// Stores a line map for the file.
919+
// This field should never be used directly to obtain line map, use getLineMap function instead.
920+
lineMap: number[];
929921
}
930922

931923
export interface ScriptReferenceHost {

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module ts {
109109
// This is a useful function for debugging purposes.
110110
export function nodePosToString(node: Node): string {
111111
var file = getSourceFileOfNode(node);
112-
var loc = file.getLineAndCharacterFromPosition(node.pos);
112+
var loc = getLineAndCharacterOfPosition(file, node.pos);
113113
return file.filename + "(" + loc.line + "," + loc.character + ")";
114114
}
115115

src/harness/fourslash.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ module FourSlash {
388388
this.currentCaretPosition = pos;
389389

390390
var lineStarts = ts.computeLineStarts(this.getCurrentFileContent());
391-
var lineCharPos = ts.getLineAndCharacterOfPosition(lineStarts, pos);
391+
var lineCharPos = ts.computeLineAndCharacterOfPosition(lineStarts, pos);
392392
this.scenarioActions.push('<MoveCaretToLineAndChar LineNumber="' + lineCharPos.line + '" CharNumber="' + lineCharPos.character + '" />');
393393
}
394394

@@ -1393,15 +1393,15 @@ module FourSlash {
13931393
var incrementalSourceFile = this.languageService.getSourceFile(this.activeFile.fileName);
13941394
Utils.assertInvariants(incrementalSourceFile, /*parent:*/ undefined);
13951395

1396-
var incrementalSyntaxDiagnostics = incrementalSourceFile.getSyntacticDiagnostics();
1396+
var incrementalSyntaxDiagnostics = ts.getSyntacticDiagnostics(incrementalSourceFile);
13971397

13981398
// Check syntactic structure
13991399
var snapshot = this.languageServiceShimHost.getScriptSnapshot(this.activeFile.fileName);
14001400
var content = snapshot.getText(0, snapshot.getLength());
14011401

14021402
var referenceSourceFile = ts.createLanguageServiceSourceFile(
14031403
this.activeFile.fileName, createScriptSnapShot(content), ts.ScriptTarget.Latest, /*version:*/ "0", /*setNodeParents:*/ false);
1404-
var referenceSyntaxDiagnostics = referenceSourceFile.getSyntacticDiagnostics();
1404+
var referenceSyntaxDiagnostics = ts.getSyntacticDiagnostics(referenceSourceFile);
14051405

14061406
Utils.assertDiagnosticsEquals(incrementalSyntaxDiagnostics, referenceSyntaxDiagnostics);
14071407
Utils.assertStructuralEquals(incrementalSourceFile, referenceSourceFile);

src/harness/harnessLanguageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ module Harness.LanguageService {
286286
assert.isTrue(line >= 1);
287287
assert.isTrue(col >= 1);
288288

289-
return ts.getPositionFromLineAndCharacter(script.lineMap, line, col);
289+
return ts.computePositionFromLineAndCharacter(script.lineMap, line, col);
290290
}
291291

292292
/**
@@ -297,7 +297,7 @@ module Harness.LanguageService {
297297
var script: ScriptInfo = this.fileNameToScript[fileName];
298298
assert.isNotNull(script);
299299

300-
var result = ts.getLineAndCharacterOfPosition(script.lineMap, position);
300+
var result = ts.computeLineAndCharacterOfPosition(script.lineMap, position);
301301

302302
assert.isTrue(result.line >= 1);
303303
assert.isTrue(result.character >= 1);

src/services/services.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ module ts {
6161
scriptSnapshot: IScriptSnapshot;
6262
nameTable: Map<string>;
6363
getNamedDeclarations(): Declaration[];
64+
getLineAndCharacterFromPosition(pos: number): LineAndCharacter;
65+
getLineStarts(): number[];
66+
getPositionFromLineAndCharacter(line: number, character: number): number;
67+
getSyntacticDiagnostics(): Diagnostic[];
68+
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
6469
}
6570

6671
/**
@@ -716,22 +721,16 @@ module ts {
716721
public filename: string;
717722
public text: string;
718723
public scriptSnapshot: IScriptSnapshot;
724+
public lineMap: number[];
719725

720726
public statements: NodeArray<Statement>;
721727
public endOfFileToken: Node;
722728

723-
// These methods will have their implementation provided by the implementation the
724-
// compiler actually exports off of SourceFile.
725-
public getLineAndCharacterFromPosition: (position: number) => LineAndCharacter;
726-
public getPositionFromLineAndCharacter: (line: number, character: number) => number;
727-
public getLineStarts: () => number[];
728-
public getSyntacticDiagnostics: () => Diagnostic[];
729-
public update: (newText: string, textChangeRange: TextChangeRange) => SourceFile;
730-
731729
public amdDependencies: string[];
732730
public amdModuleName: string;
733731
public referencedFiles: FileReference[];
734732

733+
public syntacticDiagnostics: Diagnostic[];
735734
public referenceDiagnostics: Diagnostic[];
736735
public parseDiagnostics: Diagnostic[];
737736
public semanticDiagnostics: Diagnostic[];
@@ -748,6 +747,26 @@ module ts {
748747

749748
private namedDeclarations: Declaration[];
750749

750+
public getSyntacticDiagnostics(): Diagnostic[]{
751+
return getSyntacticDiagnostics(this);
752+
}
753+
754+
public update(newText: string, textChangeRange: TextChangeRange): SourceFile {
755+
return updateSourceFile(this, newText, textChangeRange);
756+
}
757+
758+
public getLineAndCharacterFromPosition(position: number): LineAndCharacter {
759+
return getLineAndCharacterOfPosition(this, position);
760+
}
761+
762+
public getLineStarts(): number[] {
763+
return getLineStarts(this);
764+
}
765+
766+
public getPositionFromLineAndCharacter(line: number, character: number): number {
767+
return getPositionFromLineAndCharacter(this, line, character);
768+
}
769+
751770
public getNamedDeclarations() {
752771
if (!this.namedDeclarations) {
753772
var sourceFile = this;
@@ -1633,7 +1652,7 @@ module ts {
16331652
if (version !== sourceFile.version) {
16341653
// Once incremental parsing is ready, then just call into this function.
16351654
if (!disableIncrementalParsing) {
1636-
var newSourceFile = sourceFile.update(scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange);
1655+
var newSourceFile = updateSourceFile(sourceFile, scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange);
16371656
setSourceFileFields(newSourceFile, scriptSnapshot, version);
16381657
// after incremental parsing nameTable might not be up-to-date
16391658
// drop it so it can be lazily recreated later

tests/baselines/reference/APISample_compile.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -725,24 +725,21 @@ declare module "typescript" {
725725
endOfFileToken: Node;
726726
filename: string;
727727
text: string;
728-
getLineAndCharacterFromPosition(position: number): LineAndCharacter;
729-
getPositionFromLineAndCharacter(line: number, character: number): number;
730-
getLineStarts(): number[];
731-
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
732728
amdDependencies: string[];
733729
amdModuleName: string;
734730
referencedFiles: FileReference[];
735731
referenceDiagnostics: Diagnostic[];
736732
parseDiagnostics: Diagnostic[];
737-
getSyntacticDiagnostics(): Diagnostic[];
738733
semanticDiagnostics: Diagnostic[];
734+
syntacticDiagnostics: Diagnostic[];
739735
hasNoDefaultLib: boolean;
740736
externalModuleIndicator: Node;
741737
nodeCount: number;
742738
identifierCount: number;
743739
symbolCount: number;
744740
languageVersion: ScriptTarget;
745741
identifiers: Map<string>;
742+
lineMap: number[];
746743
}
747744
interface ScriptReferenceHost {
748745
getCompilerOptions(): CompilerOptions;
@@ -1393,15 +1390,14 @@ declare module "typescript" {
13931390
}
13941391
function tokenToString(t: SyntaxKind): string;
13951392
function computeLineStarts(text: string): number[];
1396-
function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number;
1397-
function getLineAndCharacterOfPosition(lineStarts: number[], position: number): {
1398-
line: number;
1399-
character: number;
1400-
};
1401-
function positionToLineAndCharacter(text: string, pos: number): {
1393+
function getPositionFromLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number;
1394+
function computePositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number;
1395+
function getLineStarts(sourceFile: SourceFile): number[];
1396+
function computeLineAndCharacterOfPosition(lineStarts: number[], position: number): {
14021397
line: number;
14031398
character: number;
14041399
};
1400+
function getLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter;
14051401
function isWhiteSpace(ch: number): boolean;
14061402
function isLineBreak(ch: number): boolean;
14071403
function isOctalDigit(ch: number): boolean;
@@ -1417,6 +1413,8 @@ declare module "typescript" {
14171413
function createNode(kind: SyntaxKind): Node;
14181414
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
14191415
function modifierToFlag(token: SyntaxKind): NodeFlags;
1416+
function getSyntacticDiagnostics(sourceFile: SourceFile): Diagnostic[];
1417+
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
14201418
function isEvalOrArgumentsIdentifier(node: Node): boolean;
14211419
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
14221420
function isLeftHandSideExpression(expr: Expression): boolean;
@@ -1476,6 +1474,11 @@ declare module "typescript" {
14761474
scriptSnapshot: IScriptSnapshot;
14771475
nameTable: Map<string>;
14781476
getNamedDeclarations(): Declaration[];
1477+
getLineAndCharacterFromPosition(pos: number): LineAndCharacter;
1478+
getLineStarts(): number[];
1479+
getPositionFromLineAndCharacter(line: number, character: number): number;
1480+
getSyntacticDiagnostics(): Diagnostic[];
1481+
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
14791482
}
14801483
/**
14811484
* Represents an immutable snapshot of a script at a specified time.Once acquired, the

0 commit comments

Comments
 (0)