Skip to content

Commit

Permalink
Merge pull request #1700 from Microsoft/noMethodsOnSourceFile
Browse files Browse the repository at this point in the history
Move function typed properties from the SourceFile to a dedicated functions
  • Loading branch information
vladima committed Feb 3, 2015
2 parents 02fbc07 + 45defa8 commit 626277c
Show file tree
Hide file tree
Showing 19 changed files with 804 additions and 679 deletions.
12 changes: 6 additions & 6 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module ts {
}

function getLineOfLocalPosition(currentSourceFile: SourceFile, pos: number) {
return currentSourceFile.getLineAndCharacterFromPosition(pos).line;
return getLineAndCharacterOfPosition(currentSourceFile, pos).line;
}

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

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

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

Expand Down Expand Up @@ -1660,7 +1660,7 @@ module ts {
}

function recordSourceMapSpan(pos: number) {
var sourceLinePos = currentSourceFile.getLineAndCharacterFromPosition(pos);
var sourceLinePos = getLineAndCharacterOfPosition(currentSourceFile, pos);
var emittedLine = writer.getLine();
var emittedColumn = writer.getColumn();

Expand Down
823 changes: 401 additions & 422 deletions src/compiler/parser.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ module ts {
else {
files.push(file);
}
forEach(file.getSyntacticDiagnostics(), e => {
forEach(getSyntacticDiagnostics(file), e => {
errors.push(e);
});
}
Expand Down
19 changes: 13 additions & 6 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,20 @@ module ts {
return result;
}

export function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
Debug.assert(line > 0 && line <= lineStarts.length );
export function getPositionFromLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
return computePositionFromLineAndCharacter(getLineStarts(sourceFile), line, character);
}

export function computePositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
Debug.assert(line > 0 && line <= lineStarts.length);
return lineStarts[line - 1] + character - 1;
}

export function getLineAndCharacterOfPosition(lineStarts: number[], position: number) {
export function getLineStarts(sourceFile: SourceFile): number[] {
return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text));
}

export function computeLineAndCharacterOfPosition(lineStarts: number[], position: number) {
var lineNumber = binarySearch(lineStarts, position);
if (lineNumber < 0) {
// If the actual position was not found,
Expand All @@ -298,9 +306,8 @@ module ts {
};
}

export function positionToLineAndCharacter(text: string, pos: number) {
var lineStarts = computeLineStarts(text);
return getLineAndCharacterOfPosition(lineStarts, pos);
export function getLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter {
return computeLineAndCharacterOfPosition(getLineStarts(sourceFile), position);
}

var hasOwnProperty = Object.prototype.hasOwnProperty;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module ts {
function countLines(program: Program): number {
var count = 0;
forEach(program.getSourceFiles(), file => {
count += file.getLineAndCharacterFromPosition(file.end).line;
count += getLineAndCharacterOfPosition(file, file.end).line;
});
return count;
}
Expand All @@ -86,7 +86,7 @@ module ts {
var output = "";

if (diagnostic.file) {
var loc = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
var loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);

output += diagnostic.file.filename + "(" + loc.line + "," + loc.character + "): ";
}
Expand Down
26 changes: 9 additions & 17 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,21 +887,6 @@ module ts {
filename: string;
text: string;

getLineAndCharacterFromPosition(position: number): LineAndCharacter;
getPositionFromLineAndCharacter(line: number, character: number): number;
getLineStarts(): number[];

// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
// indicates what changed between the 'text' that this SourceFile has and the 'newText'.
// The SourceFile will be created with the compiler attempting to reuse as many nodes from
// this file as possible.
//
// Note: this function mutates nodes from this SourceFile. That means any existing nodes
// from this SourceFile that are being held onto may change as a result (including
// becoming detached from any SourceFile). It is recommended that this SourceFile not
// be used once 'update' is called on it.
update(newText: string, textChangeRange: TextChangeRange): SourceFile;

amdDependencies: string[];
amdModuleName: string;
referencedFiles: FileReference[];
Expand All @@ -913,19 +898,26 @@ module ts {
// missing tokens, or tokens it didn't know how to deal with).
parseDiagnostics: Diagnostic[];

// Returns all syntactic diagnostics (i.e. the reference, parser and grammar diagnostics).
getSyntacticDiagnostics(): Diagnostic[];
//getSyntacticDiagnostics(): Diagnostic[];

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

// Returns all syntactic diagnostics (i.e. the reference, parser and grammar diagnostics).
// This field should never be used directly, use getSyntacticDiagnostics function instead.
syntacticDiagnostics: Diagnostic[];

hasNoDefaultLib: boolean;
externalModuleIndicator: Node; // The first node that causes this file to be an external module
nodeCount: number;
identifierCount: number;
symbolCount: number;
languageVersion: ScriptTarget;
identifiers: Map<string>;

// Stores a line map for the file.
// This field should never be used directly to obtain line map, use getLineMap function instead.
lineMap: number[];
}

export interface ScriptReferenceHost {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module ts {
// This is a useful function for debugging purposes.
export function nodePosToString(node: Node): string {
var file = getSourceFileOfNode(node);
var loc = file.getLineAndCharacterFromPosition(node.pos);
var loc = getLineAndCharacterOfPosition(file, node.pos);
return file.filename + "(" + loc.line + "," + loc.character + ")";
}

Expand Down
6 changes: 3 additions & 3 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ module FourSlash {
this.currentCaretPosition = pos;

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

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

var incrementalSyntaxDiagnostics = incrementalSourceFile.getSyntacticDiagnostics();
var incrementalSyntaxDiagnostics = ts.getSyntacticDiagnostics(incrementalSourceFile);

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

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

Utils.assertDiagnosticsEquals(incrementalSyntaxDiagnostics, referenceSyntaxDiagnostics);
Utils.assertStructuralEquals(incrementalSourceFile, referenceSourceFile);
Expand Down
4 changes: 2 additions & 2 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ module Harness.LanguageService {
assert.isTrue(line >= 1);
assert.isTrue(col >= 1);

return ts.getPositionFromLineAndCharacter(script.lineMap, line, col);
return ts.computePositionFromLineAndCharacter(script.lineMap, line, col);
}

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

var result = ts.getLineAndCharacterOfPosition(script.lineMap, position);
var result = ts.computeLineAndCharacterOfPosition(script.lineMap, position);

assert.isTrue(result.line >= 1);
assert.isTrue(result.character >= 1);
Expand Down
37 changes: 28 additions & 9 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ module ts {
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterFromPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionFromLineAndCharacter(line: number, character: number): number;
getSyntacticDiagnostics(): Diagnostic[];
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
}

/**
Expand Down Expand Up @@ -716,22 +721,16 @@ module ts {
public filename: string;
public text: string;
public scriptSnapshot: IScriptSnapshot;
public lineMap: number[];

public statements: NodeArray<Statement>;
public endOfFileToken: Node;

// These methods will have their implementation provided by the implementation the
// compiler actually exports off of SourceFile.
public getLineAndCharacterFromPosition: (position: number) => LineAndCharacter;
public getPositionFromLineAndCharacter: (line: number, character: number) => number;
public getLineStarts: () => number[];
public getSyntacticDiagnostics: () => Diagnostic[];
public update: (newText: string, textChangeRange: TextChangeRange) => SourceFile;

public amdDependencies: string[];
public amdModuleName: string;
public referencedFiles: FileReference[];

public syntacticDiagnostics: Diagnostic[];
public referenceDiagnostics: Diagnostic[];
public parseDiagnostics: Diagnostic[];
public semanticDiagnostics: Diagnostic[];
Expand All @@ -748,6 +747,26 @@ module ts {

private namedDeclarations: Declaration[];

public getSyntacticDiagnostics(): Diagnostic[]{
return getSyntacticDiagnostics(this);
}

public update(newText: string, textChangeRange: TextChangeRange): SourceFile {
return updateSourceFile(this, newText, textChangeRange);
}

public getLineAndCharacterFromPosition(position: number): LineAndCharacter {
return getLineAndCharacterOfPosition(this, position);
}

public getLineStarts(): number[] {
return getLineStarts(this);
}

public getPositionFromLineAndCharacter(line: number, character: number): number {
return getPositionFromLineAndCharacter(this, line, character);
}

public getNamedDeclarations() {
if (!this.namedDeclarations) {
var sourceFile = this;
Expand Down Expand Up @@ -1633,7 +1652,7 @@ module ts {
if (version !== sourceFile.version) {
// Once incremental parsing is ready, then just call into this function.
if (!disableIncrementalParsing) {
var newSourceFile = sourceFile.update(scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange);
var newSourceFile = updateSourceFile(sourceFile, scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange);
setSourceFileFields(newSourceFile, scriptSnapshot, version);
// after incremental parsing nameTable might not be up-to-date
// drop it so it can be lazily recreated later
Expand Down
25 changes: 14 additions & 11 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,24 +725,21 @@ declare module "typescript" {
endOfFileToken: Node;
filename: string;
text: string;
getLineAndCharacterFromPosition(position: number): LineAndCharacter;
getPositionFromLineAndCharacter(line: number, character: number): number;
getLineStarts(): number[];
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
amdDependencies: string[];
amdModuleName: string;
referencedFiles: FileReference[];
referenceDiagnostics: Diagnostic[];
parseDiagnostics: Diagnostic[];
getSyntacticDiagnostics(): Diagnostic[];
semanticDiagnostics: Diagnostic[];
syntacticDiagnostics: Diagnostic[];
hasNoDefaultLib: boolean;
externalModuleIndicator: Node;
nodeCount: number;
identifierCount: number;
symbolCount: number;
languageVersion: ScriptTarget;
identifiers: Map<string>;
lineMap: number[];
}
interface ScriptReferenceHost {
getCompilerOptions(): CompilerOptions;
Expand Down Expand Up @@ -1393,15 +1390,14 @@ declare module "typescript" {
}
function tokenToString(t: SyntaxKind): string;
function computeLineStarts(text: string): number[];
function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number;
function getLineAndCharacterOfPosition(lineStarts: number[], position: number): {
line: number;
character: number;
};
function positionToLineAndCharacter(text: string, pos: number): {
function getPositionFromLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number;
function computePositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number;
function getLineStarts(sourceFile: SourceFile): number[];
function computeLineAndCharacterOfPosition(lineStarts: number[], position: number): {
line: number;
character: number;
};
function getLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter;
function isWhiteSpace(ch: number): boolean;
function isLineBreak(ch: number): boolean;
function isOctalDigit(ch: number): boolean;
Expand All @@ -1417,6 +1413,8 @@ declare module "typescript" {
function createNode(kind: SyntaxKind): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function modifierToFlag(token: SyntaxKind): NodeFlags;
function getSyntacticDiagnostics(sourceFile: SourceFile): Diagnostic[];
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
function isEvalOrArgumentsIdentifier(node: Node): boolean;
function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
function isLeftHandSideExpression(expr: Expression): boolean;
Expand Down Expand Up @@ -1476,6 +1474,11 @@ declare module "typescript" {
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterFromPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionFromLineAndCharacter(line: number, character: number): number;
getSyntacticDiagnostics(): Diagnostic[];
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
}
/**
* Represents an immutable snapshot of a script at a specified time.Once acquired, the
Expand Down
Loading

0 comments on commit 626277c

Please sign in to comment.