initial change

This commit is contained in:
Mohamed Hegazy
2014-07-18 16:55:11 -07:00
parent fce3bdd171
commit 253273820d
156 changed files with 50722 additions and 54 deletions

View File

@@ -0,0 +1,116 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
export class BraceMatcher {
// Given a script name and position in the script, return a pair of text range if the
// position corresponds to a "brace matchin" characters (e.g. "{" or "(", etc.)
// If the position is not on any range, return an empty set.
public static getMatchSpans(syntaxTree: TypeScript.SyntaxTree, position: number): TypeScript.TextSpan[] {
var result: TypeScript.TextSpan[] = [];
var currentToken = findToken(syntaxTree.sourceUnit(), position);
BraceMatcher.getMatchingCloseBrace(currentToken, position, result);
BraceMatcher.getMatchingOpenBrace(currentToken, position, result);
return result;
}
private static getMatchingCloseBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
if (start(currentToken) === position) {
var closingBraceKind = BraceMatcher.getMatchingCloseBraceTokenKind(currentToken);
if (closingBraceKind !== null) {
var parentElement = currentToken.parent
var currentPosition = fullStart(currentToken.parent);
for (var i = 0, n = childCount(parentElement); i < n; i++) {
var element = childAt(parentElement, i);
if (element !== null && fullWidth(element) > 0) {
if (element.kind() === closingBraceKind) {
var range1 = new TypeScript.TextSpan(position, width(currentToken));
var range2 = new TypeScript.TextSpan(currentPosition + leadingTriviaWidth(element), width(element));
result.push(range1, range2);
break;
}
currentPosition += fullWidth(element);
}
}
}
}
}
private static getMatchingOpenBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
// Check if the current token to the left is a close brace
if (currentToken.fullStart() === position) {
currentToken = previousToken(currentToken);
}
if (currentToken !== null && start(currentToken) === (position - 1)) {
var openBraceKind = BraceMatcher.getMatchingOpenBraceTokenKind(currentToken);
if (openBraceKind !== null) {
var parentElement = currentToken.parent;
var currentPosition = fullStart(currentToken.parent) + fullWidth(parentElement);
for (var i = childCount(parentElement) - 1 ; i >= 0; i--) {
var element = childAt(parentElement, i);
if (element !== null && fullWidth(element) > 0) {
if (element.kind() === openBraceKind) {
var range1 = new TypeScript.TextSpan(position - 1, width(currentToken));
var range2 = new TypeScript.TextSpan(currentPosition - lastToken(element).trailingTriviaWidth() - width(element), width(element));
result.push(range1, range2);
break;
}
currentPosition -= fullWidth(element);
}
}
}
}
}
private static getMatchingCloseBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
var element = positionedElement !== null && positionedElement;
switch (element.kind()) {
case TypeScript.SyntaxKind.OpenBraceToken:
return TypeScript.SyntaxKind.CloseBraceToken
case TypeScript.SyntaxKind.OpenParenToken:
return TypeScript.SyntaxKind.CloseParenToken;
case TypeScript.SyntaxKind.OpenBracketToken:
return TypeScript.SyntaxKind.CloseBracketToken;
case TypeScript.SyntaxKind.LessThanToken:
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.GreaterThanToken : null;
}
return null;
}
private static getMatchingOpenBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
var element = positionedElement !== null && positionedElement;
switch (element.kind()) {
case TypeScript.SyntaxKind.CloseBraceToken:
return TypeScript.SyntaxKind.OpenBraceToken
case TypeScript.SyntaxKind.CloseParenToken:
return TypeScript.SyntaxKind.OpenParenToken;
case TypeScript.SyntaxKind.CloseBracketToken:
return TypeScript.SyntaxKind.OpenBracketToken;
case TypeScript.SyntaxKind.GreaterThanToken:
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.LessThanToken : null;
}
return null;
}
}
}

1088
src/services/breakpoints.ts Normal file

File diff suppressed because it is too large Load Diff

194
src/services/classifier.ts Normal file
View File

@@ -0,0 +1,194 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
export enum EndOfLineState {
Start,
InMultiLineCommentTrivia,
InSingleQuoteStringLiteral,
InDoubleQuoteStringLiteral,
}
export enum TokenClass {
Punctuation,
Keyword,
Operator,
Comment,
Whitespace,
Identifier,
NumberLiteral,
StringLiteral,
RegExpLiteral,
}
var noRegexTable: boolean[] = [];
noRegexTable[TypeScript.SyntaxKind.IdentifierName] = true;
noRegexTable[TypeScript.SyntaxKind.StringLiteral] = true;
noRegexTable[TypeScript.SyntaxKind.NumericLiteral] = true;
noRegexTable[TypeScript.SyntaxKind.RegularExpressionLiteral] = true;
noRegexTable[TypeScript.SyntaxKind.ThisKeyword] = true;
noRegexTable[TypeScript.SyntaxKind.PlusPlusToken] = true;
noRegexTable[TypeScript.SyntaxKind.MinusMinusToken] = true;
noRegexTable[TypeScript.SyntaxKind.CloseParenToken] = true;
noRegexTable[TypeScript.SyntaxKind.CloseBracketToken] = true;
noRegexTable[TypeScript.SyntaxKind.CloseBraceToken] = true;
noRegexTable[TypeScript.SyntaxKind.TrueKeyword] = true;
noRegexTable[TypeScript.SyntaxKind.FalseKeyword] = true;
export class Classifier {
private scanner: TypeScript.Scanner.IScanner;
private lastDiagnosticKey: string = null;
private reportDiagnostic = (position: number, fullWidth: number, key: string, args: any[]) => {
this.lastDiagnosticKey = key;
};
constructor(public host: IClassifierHost) {
}
/// COLORIZATION
public getClassificationsForLine(text: string, lexState: EndOfLineState): ClassificationResult {
var offset = 0;
if (lexState !== EndOfLineState.Start) {
// If we're in a string literal, then prepend: "\
// (and a newline). That way when we lex we'll think we're still in a string literal.
//
// If we're in a multiline comment, then prepend: /*
// (and a newline). That way when we lex we'll think we're still in a multiline comment.
if (lexState === EndOfLineState.InDoubleQuoteStringLiteral) {
text = '"\\\n' + text;
}
else if (lexState === EndOfLineState.InSingleQuoteStringLiteral) {
text = "'\\\n" + text;
}
else if (lexState === EndOfLineState.InMultiLineCommentTrivia) {
text = "/*\n" + text;
}
offset = 3;
}
var result = new ClassificationResult();
var simpleText = TypeScript.SimpleText.fromString(text);
this.scanner = Scanner.createScanner(ts.ScriptTarget.ES5, simpleText, this.reportDiagnostic);
var lastTokenKind = TypeScript.SyntaxKind.None;
var token: ISyntaxToken = null;
do {
this.lastDiagnosticKey = null;
token = this.scanner.scan(!noRegexTable[lastTokenKind]);
lastTokenKind = token.kind();
this.processToken(text, simpleText, offset, token, result);
}
while (token.kind() !== SyntaxKind.EndOfFileToken);
this.lastDiagnosticKey = null;
return result;
}
private processToken(text: string, simpleText: ISimpleText, offset: number, token: TypeScript.ISyntaxToken, result: ClassificationResult): void {
this.processTriviaList(text, offset, token.leadingTrivia(simpleText), result);
this.addResult(text, offset, result, width(token), token.kind());
this.processTriviaList(text, offset, token.trailingTrivia(simpleText), result);
if (fullEnd(token) >= text.length) {
// We're at the end.
if (this.lastDiagnosticKey === TypeScript.DiagnosticCode.AsteriskSlash_expected) {
result.finalLexState = EndOfLineState.InMultiLineCommentTrivia;
return;
}
if (token.kind() === TypeScript.SyntaxKind.StringLiteral) {
var tokenText = token.text();
if (tokenText.length > 0 && tokenText.charCodeAt(tokenText.length - 1) === TypeScript.CharacterCodes.backslash) {
var quoteChar = tokenText.charCodeAt(0);
result.finalLexState = quoteChar === TypeScript.CharacterCodes.doubleQuote
? EndOfLineState.InDoubleQuoteStringLiteral
: EndOfLineState.InSingleQuoteStringLiteral;
return;
}
}
}
}
private processTriviaList(text: string, offset: number, triviaList: TypeScript.ISyntaxTriviaList, result: ClassificationResult): void {
for (var i = 0, n = triviaList.count(); i < n; i++) {
var trivia = triviaList.syntaxTriviaAt(i);
this.addResult(text, offset, result, trivia.fullWidth(), trivia.kind());
}
}
private addResult(text: string, offset: number, result: ClassificationResult, length: number, kind: TypeScript.SyntaxKind): void {
if (length > 0) {
// If this is the first classification we're adding to the list, then remove any
// offset we have if we were continuing a construct from the previous line.
if (result.entries.length === 0) {
length -= offset;
}
result.entries.push(new ClassificationInfo(length, this.classFromKind(kind)));
}
}
private classFromKind(kind: TypeScript.SyntaxKind) {
if (TypeScript.SyntaxFacts.isAnyKeyword(kind)) {
return TokenClass.Keyword;
}
else if (TypeScript.SyntaxFacts.isBinaryExpressionOperatorToken(kind) ||
TypeScript.SyntaxFacts.isPrefixUnaryExpressionOperatorToken(kind)) {
return TokenClass.Operator;
}
else if (TypeScript.SyntaxFacts.isAnyPunctuation(kind)) {
return TokenClass.Punctuation;
}
switch (kind) {
case TypeScript.SyntaxKind.WhitespaceTrivia:
return TokenClass.Whitespace;
case TypeScript.SyntaxKind.MultiLineCommentTrivia:
case TypeScript.SyntaxKind.SingleLineCommentTrivia:
return TokenClass.Comment;
case TypeScript.SyntaxKind.NumericLiteral:
return TokenClass.NumberLiteral;
case TypeScript.SyntaxKind.StringLiteral:
return TokenClass.StringLiteral;
case TypeScript.SyntaxKind.RegularExpressionLiteral:
return TokenClass.RegExpLiteral;
case TypeScript.SyntaxKind.IdentifierName:
default:
return TokenClass.Identifier;
}
}
}
export interface IClassifierHost extends TypeScript.ILogger {
}
export class ClassificationResult {
public finalLexState: EndOfLineState = EndOfLineState.Start;
public entries: ClassificationInfo[] = [];
constructor() {
}
}
export class ClassificationInfo {
constructor(public length: number, public classification: TokenClass) {
}
}
}

View File

@@ -0,0 +1,53 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export class Comment {
constructor(private _trivia: ISyntaxTrivia,
public endsLine: boolean,
public _start: number,
public _end: number) {
}
public start(): number {
return this._start;
}
public end(): number {
return this._end;
}
public fullText(): string {
return this._trivia.fullText();
}
public kind(): SyntaxKind {
return this._trivia.kind();
}
public structuralEquals(ast: Comment, includingPosition: boolean): boolean {
if (includingPosition) {
if (this.start() !== ast.start() || this.end() !== ast.end()) {
return false;
}
}
return this._trivia.fullText() === ast._trivia.fullText() &&
this.endsLine === ast.endsLine;
}
}
}

View File

@@ -0,0 +1,755 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.ASTHelpers {
//export function scriptIsElided(sourceUnit: SourceUnitSyntax): boolean {
// return isDTSFile(sourceUnit.syntaxTree.fileName()) || moduleMembersAreElided(sourceUnit.moduleElements);
//}
//export function moduleIsElided(declaration: ModuleDeclarationSyntax): boolean {
// return hasModifier(declaration.modifiers, PullElementFlags.Ambient) || moduleMembersAreElided(declaration.moduleElements);
//}
//function moduleMembersAreElided(members: IModuleElementSyntax[]): boolean {
// for (var i = 0, n = members.length; i < n; i++) {
// var member = members[i];
// // We should emit *this* module if it contains any non-interface types.
// // Caveat: if we have contain a module, then we should be emitted *if we want to
// // emit that inner module as well.
// if (member.kind() === SyntaxKind.ModuleDeclaration) {
// if (!moduleIsElided(<ModuleDeclarationSyntax>member)) {
// return false;
// }
// }
// else if (member.kind() !== SyntaxKind.InterfaceDeclaration) {
// return false;
// }
// }
// return true;
//}
//export function enumIsElided(declaration: EnumDeclarationSyntax): boolean {
// if (hasModifier(declaration.modifiers, PullElementFlags.Ambient)) {
// return true;
// }
// return false;
//}
export function isValidAstNode(ast: ISyntaxElement): boolean {
return ast && !isShared(ast) && start(ast) !== -1 && end(ast) !== -1;
}
export function isValidSpan(ast: ISpan): boolean {
if (!ast)
return false;
if (ast.start() === -1 || ast.end() === -1)
return false;
return true;
}
///
/// Return the ISyntaxElement containing "position"
///
export function getAstAtPosition(script: ISyntaxElement, pos: number, useTrailingTriviaAsLimChar: boolean = true, forceInclusive: boolean = false): ISyntaxElement {
var top: ISyntaxElement = null;
var pre = function (cur: ISyntaxElement, walker: IAstWalker) {
if (!isShared(cur) && isValidAstNode(cur)) {
var isInvalid1 = cur.kind() === SyntaxKind.ExpressionStatement && width(cur) === 0;
if (isInvalid1) {
walker.options.goChildren = false;
}
else {
// Add "cur" to the stack if it contains our position
// For "identifier" nodes, we need a special case: A position equal to "limChar" is
// valid, since the position corresponds to a caret position (in between characters)
// For example:
// bar
// 0123
// If "position === 3", the caret is at the "right" of the "r" character, which should be considered valid
var inclusive =
forceInclusive ||
cur.kind() === SyntaxKind.IdentifierName ||
cur.kind() === SyntaxKind.MemberAccessExpression ||
cur.kind() === SyntaxKind.QualifiedName ||
//cur.kind() === SyntaxKind.TypeRef ||
cur.kind() === SyntaxKind.VariableDeclaration ||
cur.kind() === SyntaxKind.VariableDeclarator ||
cur.kind() === SyntaxKind.InvocationExpression ||
pos === end(script) + lastToken(script).trailingTriviaWidth(); // Special "EOF" case
var minChar = start(cur);
var limChar = end(cur) + (useTrailingTriviaAsLimChar ? trailingTriviaWidth(cur) : 0) + (inclusive ? 1 : 0);
if (pos >= minChar && pos < limChar) {
// Ignore empty lists
if ((cur.kind() !== SyntaxKind.List && cur.kind() !== SyntaxKind.SeparatedList) || end(cur) > start(cur)) {
// TODO: Since ISyntaxElement is sometimes not correct wrt to position, only add "cur" if it's better
// than top of the stack.
if (top === null) {
top = cur;
}
else if (start(cur) >= start(top) &&
(end(cur) + (useTrailingTriviaAsLimChar ? trailingTriviaWidth(cur) : 0)) <= (end(top) + (useTrailingTriviaAsLimChar ? trailingTriviaWidth(top) : 0))) {
// this new node appears to be better than the one we're
// storing. Make this the new node.
// However, If the current top is a missing identifier, we
// don't want to replace it with another missing identifier.
// We want to return the first missing identifier found in a
// depth first walk of the tree.
if (width(top) !== 0 || width(cur) !== 0) {
top = cur;
}
}
}
}
// Don't go further down the tree if pos is outside of [minChar, limChar]
walker.options.goChildren = (minChar <= pos && pos <= limChar);
}
}
};
getAstWalkerFactory().walk(script, pre);
return top;
}
export function getExtendsHeritageClause(clauses: HeritageClauseSyntax[]): HeritageClauseSyntax {
return getHeritageClause(clauses, SyntaxKind.ExtendsHeritageClause);
}
export function getImplementsHeritageClause(clauses: HeritageClauseSyntax[]): HeritageClauseSyntax {
return getHeritageClause(clauses, SyntaxKind.ImplementsHeritageClause);
}
function getHeritageClause(clauses: HeritageClauseSyntax[], kind: SyntaxKind): HeritageClauseSyntax {
if (clauses) {
for (var i = 0, n = clauses.length; i < n; i++) {
var child = clauses[i];
if (child.typeNames.length > 0 && child.kind() === kind) {
return child;
}
}
}
return null;
}
export function isCallExpression(ast: ISyntaxElement): boolean {
return (ast && ast.kind() === SyntaxKind.InvocationExpression) ||
(ast && ast.kind() === SyntaxKind.ObjectCreationExpression);
}
export function isCallExpressionTarget(ast: ISyntaxElement): boolean {
return !!getCallExpressionTarget(ast);
}
export function getCallExpressionTarget(ast: ISyntaxElement): ISyntaxElement {
if (!ast) {
return null;
}
var current = ast;
while (current && current.parent) {
if (current.parent.kind() === SyntaxKind.MemberAccessExpression &&
(<MemberAccessExpressionSyntax>current.parent).name === current) {
current = current.parent;
continue;
}
break;
}
if (current && current.parent) {
if (current.parent.kind() === SyntaxKind.InvocationExpression || current.parent.kind() === SyntaxKind.ObjectCreationExpression) {
return current === (<InvocationExpressionSyntax>current.parent).expression ? current : null;
}
}
return null;
}
function isNameOfSomeDeclaration(ast: ISyntaxElement) {
if (ast === null || ast.parent === null) {
return false;
}
if (ast.kind() !== SyntaxKind.IdentifierName) {
return false;
}
switch (ast.parent.kind()) {
case SyntaxKind.ClassDeclaration:
return (<ClassDeclarationSyntax>ast.parent).identifier === ast;
case SyntaxKind.InterfaceDeclaration:
return (<InterfaceDeclarationSyntax>ast.parent).identifier === ast;
case SyntaxKind.EnumDeclaration:
return (<EnumDeclarationSyntax>ast.parent).identifier === ast;
case SyntaxKind.ModuleDeclaration:
return (<ModuleDeclarationSyntax>ast.parent).name === ast || (<ModuleDeclarationSyntax>ast.parent).stringLiteral === ast;
case SyntaxKind.VariableDeclarator:
return (<VariableDeclaratorSyntax>ast.parent).propertyName === ast;
case SyntaxKind.FunctionDeclaration:
return (<FunctionDeclarationSyntax>ast.parent).identifier === ast;
case SyntaxKind.MemberFunctionDeclaration:
return (<MemberFunctionDeclarationSyntax>ast.parent).propertyName === ast;
case SyntaxKind.Parameter:
return (<ParameterSyntax>ast.parent).identifier === ast;
case SyntaxKind.TypeParameter:
return (<TypeParameterSyntax>ast.parent).identifier === ast;
case SyntaxKind.SimplePropertyAssignment:
return (<SimplePropertyAssignmentSyntax>ast.parent).propertyName === ast;
case SyntaxKind.FunctionPropertyAssignment:
return (<FunctionPropertyAssignmentSyntax>ast.parent).propertyName === ast;
case SyntaxKind.EnumElement:
return (<EnumElementSyntax>ast.parent).propertyName === ast;
case SyntaxKind.ImportDeclaration:
return (<ImportDeclarationSyntax>ast.parent).identifier === ast;
case SyntaxKind.MethodSignature:
return (<MethodSignatureSyntax>ast.parent).propertyName === ast;
case SyntaxKind.PropertySignature:
return (<MethodSignatureSyntax>ast.parent).propertyName === ast;
}
return false;
}
export function isDeclarationASTOrDeclarationNameAST(ast: ISyntaxElement) {
return isNameOfSomeDeclaration(ast) || ASTHelpers.isDeclarationAST(ast);
}
export function getEnclosingParameterForInitializer(ast: ISyntaxElement): ParameterSyntax {
var current = ast;
while (current) {
switch (current.kind()) {
case SyntaxKind.EqualsValueClause:
if (current.parent && current.parent.kind() === SyntaxKind.Parameter) {
return <ParameterSyntax>current.parent;
}
break;
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
// exit early
return null;
}
current = current.parent;
}
return null;
}
export function getEnclosingMemberDeclaration(ast: ISyntaxElement): ISyntaxElement {
var current = ast;
while (current) {
switch (current.kind()) {
case SyntaxKind.MemberVariableDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return current;
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
// exit early
return null;
}
current = current.parent;
}
return null;
}
export function isNameOfFunction(ast: ISyntaxElement) {
return ast
&& ast.parent
&& ast.kind() === SyntaxKind.IdentifierName
&& ast.parent.kind() === SyntaxKind.FunctionDeclaration
&& (<FunctionDeclarationSyntax>ast.parent).identifier === ast;
}
export function isNameOfMemberFunction(ast: ISyntaxElement) {
return ast
&& ast.parent
&& ast.kind() === SyntaxKind.IdentifierName
&& ast.parent.kind() === SyntaxKind.MemberFunctionDeclaration
&& (<MemberFunctionDeclarationSyntax>ast.parent).propertyName === ast;
}
export function isNameOfMemberAccessExpression(ast: ISyntaxElement) {
if (ast &&
ast.parent &&
ast.parent.kind() === SyntaxKind.MemberAccessExpression &&
(<MemberAccessExpressionSyntax>ast.parent).name === ast) {
return true;
}
return false;
}
export function isRightSideOfQualifiedName(ast: ISyntaxElement) {
if (ast &&
ast.parent &&
ast.parent.kind() === SyntaxKind.QualifiedName &&
(<QualifiedNameSyntax>ast.parent).right === ast) {
return true;
}
return false;
}
export function parentIsModuleDeclaration(ast: ISyntaxElement) {
return ast.parent && ast.parent.kind() === SyntaxKind.ModuleDeclaration;
}
export function isDeclarationAST(ast: ISyntaxElement): boolean {
switch (ast.kind()) {
case SyntaxKind.VariableDeclarator:
return getVariableStatement(<VariableDeclaratorSyntax>ast) !== null;
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.Parameter:
case SyntaxKind.SimpleArrowFunctionExpression:
case SyntaxKind.ParenthesizedArrowFunctionExpression:
case SyntaxKind.IndexSignature:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ArrayType:
case SyntaxKind.ObjectType:
case SyntaxKind.TypeParameter:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MemberVariableDeclaration:
case SyntaxKind.IndexMemberDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumElement:
case SyntaxKind.SimplePropertyAssignment:
case SyntaxKind.FunctionPropertyAssignment:
case SyntaxKind.FunctionExpression:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.PropertySignature:
return true;
default:
return false;
}
}
export function preComments(element: ISyntaxElement, text: ISimpleText): Comment[]{
if (element) {
switch (element.kind()) {
case SyntaxKind.VariableStatement:
case SyntaxKind.ExpressionStatement:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.IfStatement:
case SyntaxKind.SimplePropertyAssignment:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.ReturnStatement:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.MemberVariableDeclaration:
case SyntaxKind.EnumElement:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.FunctionPropertyAssignment:
case SyntaxKind.Parameter:
return convertNodeLeadingComments(element, text);
}
}
return null;
}
export function postComments(element: ISyntaxElement, text: ISimpleText): Comment[] {
if (element) {
switch (element.kind()) {
case SyntaxKind.ExpressionStatement:
return convertNodeTrailingComments(element, text, /*allowWithNewLine:*/ true);
case SyntaxKind.VariableStatement:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.IfStatement:
case SyntaxKind.SimplePropertyAssignment:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.ReturnStatement:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.MemberVariableDeclaration:
case SyntaxKind.EnumElement:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.FunctionPropertyAssignment:
case SyntaxKind.Parameter:
return convertNodeTrailingComments(element, text);
}
}
return null;
}
function convertNodeTrailingComments(node: ISyntaxElement, text: ISimpleText, allowWithNewLine = false): Comment[]{
// Bail out quickly before doing any expensive math computation.
var _lastToken = lastToken(node);
if (_lastToken === null || !_lastToken.hasTrailingTrivia()) {
return null;
}
if (!allowWithNewLine && SyntaxUtilities.isLastTokenOnLine(_lastToken, text)) {
return null;
}
return convertComments(_lastToken.trailingTrivia(text), fullStart(node) + fullWidth(node) - _lastToken.trailingTriviaWidth(text));
}
function convertNodeLeadingComments(element: ISyntaxElement, text: ISimpleText): Comment[]{
if (element) {
return convertTokenLeadingComments(firstToken(element), text);
}
return null;
}
export function convertTokenLeadingComments(token: ISyntaxToken, text: ISimpleText): Comment[]{
if (token === null) {
return null;
}
return token.hasLeadingTrivia()
? convertComments(token.leadingTrivia(text), token.fullStart())
: null;
}
export function convertTokenTrailingComments(token: ISyntaxToken, text: ISimpleText): Comment[] {
if (token === null) {
return null;
}
return token.hasTrailingTrivia()
? convertComments(token.trailingTrivia(text), fullEnd(token) - token.trailingTriviaWidth(text))
: null;
}
function convertComments(triviaList: ISyntaxTriviaList, commentStartPosition: number): Comment[]{
var result: Comment[] = null;
for (var i = 0, n = triviaList.count(); i < n; i++) {
var trivia = triviaList.syntaxTriviaAt(i);
if (trivia.isComment()) {
var hasTrailingNewLine = ((i + 1) < n) && triviaList.syntaxTriviaAt(i + 1).isNewLine();
result = result || [];
result.push(convertComment(trivia, commentStartPosition, hasTrailingNewLine));
}
commentStartPosition += trivia.fullWidth();
}
return result;
}
function convertComment(trivia: ISyntaxTrivia, commentStartPosition: number, hasTrailingNewLine: boolean): Comment {
var comment = new Comment(trivia, hasTrailingNewLine, commentStartPosition, commentStartPosition + trivia.fullWidth());
return comment;
}
export function docComments(ast: ISyntaxElement, text: ISimpleText): Comment[] {
if (isDeclarationAST(ast)) {
var comments: Comment[] = null;
if (ast.kind() === SyntaxKind.VariableDeclarator) {
// Get the doc comments for a variable off of the variable statement. That's what
// they'll be attached to in the tree.
comments = TypeScript.ASTHelpers.preComments(getVariableStatement(<VariableDeclaratorSyntax>ast), text);
}
else if (ast.kind() === SyntaxKind.Parameter) {
// First check if the parameter was written like so:
// (
// /** blah */ a,
// /** blah */ b);
comments = TypeScript.ASTHelpers.preComments(ast, text);
if (!comments) {
// Now check if it was written like so:
// (/** blah */ a, /** blah */ b);
// In this case, the comment will belong to the preceding token.
var previousToken = findToken(syntaxTree(ast).sourceUnit(), firstToken(ast).fullStart() - 1);
if (previousToken && (previousToken.kind() === SyntaxKind.OpenParenToken || previousToken.kind() === SyntaxKind.CommaToken)) {
comments = convertTokenTrailingComments(previousToken, text);
}
}
}
else {
comments = TypeScript.ASTHelpers.preComments(ast, text);
}
if (comments && comments.length > 0) {
return comments.filter(c => isDocComment(c));
}
}
return sentinelEmptyArray;
}
export function isDocComment(comment: Comment) {
if (comment.kind() === SyntaxKind.MultiLineCommentTrivia) {
var fullText = comment.fullText();
return fullText.charAt(2) === "*" && fullText.charAt(3) !== "/";
}
return false;
}
export function getParameterList(ast: ISyntaxElement): ParameterListSyntax {
if (ast) {
switch (ast.kind()) {
case SyntaxKind.ConstructorDeclaration:
return getParameterList((<ConstructorDeclarationSyntax>ast).callSignature);
case SyntaxKind.FunctionDeclaration:
return getParameterList((<FunctionDeclarationSyntax>ast).callSignature);
case SyntaxKind.ParenthesizedArrowFunctionExpression:
return getParameterList((<ParenthesizedArrowFunctionExpressionSyntax>ast).callSignature);
case SyntaxKind.ConstructSignature:
return getParameterList((<ConstructSignatureSyntax>ast).callSignature);
case SyntaxKind.MemberFunctionDeclaration:
return getParameterList((<MemberFunctionDeclarationSyntax>ast).callSignature);
case SyntaxKind.FunctionPropertyAssignment:
return getParameterList((<FunctionPropertyAssignmentSyntax>ast).callSignature);
case SyntaxKind.FunctionExpression:
return getParameterList((<FunctionExpressionSyntax>ast).callSignature);
case SyntaxKind.MethodSignature:
return getParameterList((<MethodSignatureSyntax>ast).callSignature);
case SyntaxKind.ConstructorType:
return (<ConstructorTypeSyntax>ast).parameterList;
case SyntaxKind.FunctionType:
return (<FunctionTypeSyntax>ast).parameterList;
case SyntaxKind.CallSignature:
return (<CallSignatureSyntax>ast).parameterList;
case SyntaxKind.GetAccessor:
return getParameterList((<GetAccessorSyntax>ast).callSignature);
case SyntaxKind.SetAccessor:
return getParameterList((<SetAccessorSyntax>ast).callSignature);
}
}
return null;
}
export function getType(ast: ISyntaxElement): ITypeSyntax {
if (ast) {
switch (ast.kind()) {
case SyntaxKind.FunctionDeclaration:
return getType((<FunctionDeclarationSyntax>ast).callSignature);
case SyntaxKind.ParenthesizedArrowFunctionExpression:
return getType((<ParenthesizedArrowFunctionExpressionSyntax>ast).callSignature);
case SyntaxKind.ConstructSignature:
return getType((<ConstructSignatureSyntax>ast).callSignature);
case SyntaxKind.MemberFunctionDeclaration:
return getType((<MemberFunctionDeclarationSyntax>ast).callSignature);
case SyntaxKind.FunctionPropertyAssignment:
return getType((<FunctionPropertyAssignmentSyntax>ast).callSignature);
case SyntaxKind.FunctionExpression:
return getType((<FunctionExpressionSyntax>ast).callSignature);
case SyntaxKind.MethodSignature:
return getType((<MethodSignatureSyntax>ast).callSignature);
case SyntaxKind.CallSignature:
return getType((<CallSignatureSyntax>ast).typeAnnotation);
case SyntaxKind.IndexSignature:
return getType((<IndexSignatureSyntax>ast).typeAnnotation);
case SyntaxKind.PropertySignature:
return getType((<PropertySignatureSyntax>ast).typeAnnotation);
case SyntaxKind.GetAccessor:
return getType((<GetAccessorSyntax>ast).callSignature);
case SyntaxKind.Parameter:
return getType((<ParameterSyntax>ast).typeAnnotation);
case SyntaxKind.MemberVariableDeclaration:
return getType((<MemberVariableDeclarationSyntax>ast).variableDeclarator);
case SyntaxKind.VariableDeclarator:
return getType((<VariableDeclaratorSyntax>ast).typeAnnotation);
case SyntaxKind.CatchClause:
return getType((<CatchClauseSyntax>ast).typeAnnotation);
case SyntaxKind.ConstructorType:
return (<ConstructorTypeSyntax>ast).type;
case SyntaxKind.FunctionType:
return (<FunctionTypeSyntax>ast).type;
case SyntaxKind.TypeAnnotation:
return (<TypeAnnotationSyntax>ast).type;
}
}
return null;
}
function getVariableStatement(variableDeclarator: VariableDeclaratorSyntax): VariableStatementSyntax {
if (variableDeclarator && variableDeclarator.parent && variableDeclarator.parent.parent && variableDeclarator.parent.parent.parent &&
variableDeclarator.parent.kind() === SyntaxKind.SeparatedList &&
variableDeclarator.parent.parent.kind() === SyntaxKind.VariableDeclaration &&
variableDeclarator.parent.parent.parent.kind() === SyntaxKind.VariableStatement) {
return <VariableStatementSyntax>variableDeclarator.parent.parent.parent;
}
return null;
}
export function getVariableDeclaratorModifiers(variableDeclarator: VariableDeclaratorSyntax): ISyntaxToken[] {
var variableStatement = getVariableStatement(variableDeclarator);
return variableStatement ? variableStatement.modifiers : Syntax.emptyList<ISyntaxToken>();
}
export function isIntegerLiteralAST(expression: ISyntaxElement): boolean {
if (expression) {
switch (expression.kind()) {
case SyntaxKind.PlusExpression:
case SyntaxKind.NegateExpression:
// Note: if there is a + or - sign, we can only allow a normal integer following
// (and not a hex integer). i.e. -0xA is a legal expression, but it is not a
// *literal*.
expression = (<PrefixUnaryExpressionSyntax>expression).operand;
return expression.kind() === SyntaxKind.NumericLiteral && IntegerUtilities.isInteger((<ISyntaxToken>expression).text());
case SyntaxKind.NumericLiteral:
// If it doesn't have a + or -, then either an integer literal or a hex literal
// is acceptable.
var text = (<ISyntaxToken>expression).text();
return IntegerUtilities.isInteger(text) || IntegerUtilities.isHexInteger(text);
}
}
return false;
}
export function getEnclosingModuleDeclaration(ast: ISyntaxElement): ModuleDeclarationSyntax {
while (ast) {
if (ast.kind() === SyntaxKind.ModuleDeclaration) {
return <ModuleDeclarationSyntax>ast;
}
ast = ast.parent;
}
return null;
}
function isEntireNameOfModuleDeclaration(nameAST: ISyntaxElement) {
return parentIsModuleDeclaration(nameAST) && (<ModuleDeclarationSyntax>nameAST.parent).name === nameAST;
}
export function getModuleDeclarationFromNameAST(ast: ISyntaxElement): ModuleDeclarationSyntax {
if (ast) {
switch (ast.kind()) {
case SyntaxKind.StringLiteral:
if (parentIsModuleDeclaration(ast) && (<ModuleDeclarationSyntax>ast.parent).stringLiteral === ast) {
return <ModuleDeclarationSyntax>ast.parent;
}
return null;
case SyntaxKind.IdentifierName:
case SyntaxKind.QualifiedName:
if (isEntireNameOfModuleDeclaration(ast)) {
return <ModuleDeclarationSyntax>ast.parent;
}
break;
default:
return null;
}
// Only qualified names can be name of module declaration if they didnt satisfy above conditions
for (ast = ast.parent; ast && ast.kind() === SyntaxKind.QualifiedName; ast = ast.parent) {
if (isEntireNameOfModuleDeclaration(ast)) {
return <ModuleDeclarationSyntax>ast.parent;
}
}
}
return null;
}
export function isLastNameOfModule(ast: ModuleDeclarationSyntax, astName: ISyntaxElement): boolean {
if (ast) {
if (ast.stringLiteral) {
return astName === ast.stringLiteral;
}
else if (ast.name.kind() === SyntaxKind.QualifiedName) {
return astName === (<QualifiedNameSyntax>ast.name).right;
}
else {
return astName === ast.name;
}
}
return false;
}
export function getNameOfIdenfierOrQualifiedName(name: ISyntaxElement): string {
if (name.kind() === SyntaxKind.IdentifierName) {
return (<ISyntaxToken>name).text();
}
else {
Debug.assert(name.kind() == SyntaxKind.QualifiedName);
var dotExpr = <QualifiedNameSyntax>name;
return getNameOfIdenfierOrQualifiedName(dotExpr.left) + "." + getNameOfIdenfierOrQualifiedName(dotExpr.right);
}
}
export function getModuleNames(name: ISyntaxElement, result?: ISyntaxToken[]): ISyntaxToken[] {
result = result || [];
if (name.kind() === SyntaxKind.QualifiedName) {
getModuleNames((<QualifiedNameSyntax>name).left, result);
result.push((<QualifiedNameSyntax>name).right);
}
else {
result.push(<ISyntaxToken>name);
}
return result;
}
}

View File

@@ -0,0 +1,716 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
function walkListChildren(preAst: ISyntaxNodeOrToken[], walker: AstWalker): void {
for (var i = 0, n = preAst.length; i < n; i++) {
walker.walk(preAst[i]);
}
}
function walkThrowStatementChildren(preAst: ThrowStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkPrefixUnaryExpressionChildren(preAst: PrefixUnaryExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.operand);
}
function walkPostfixUnaryExpressionChildren(preAst: PostfixUnaryExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.operand);
}
function walkDeleteExpressionChildren(preAst: DeleteExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkTypeArgumentListChildren(preAst: TypeArgumentListSyntax, walker: AstWalker): void {
walker.walk(preAst.typeArguments);
}
function walkTypeOfExpressionChildren(preAst: TypeOfExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkVoidExpressionChildren(preAst: VoidExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkArgumentListChildren(preAst: ArgumentListSyntax, walker: AstWalker): void {
walker.walk(preAst.typeArgumentList);
walker.walk(preAst.arguments);
}
function walkArrayLiteralExpressionChildren(preAst: ArrayLiteralExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expressions);
}
function walkSimplePropertyAssignmentChildren(preAst: SimplePropertyAssignmentSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.expression);
}
function walkFunctionPropertyAssignmentChildren(preAst: FunctionPropertyAssignmentSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkGetAccessorChildren(preAst: GetAccessorSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkSeparatedListChildren(preAst: ISyntaxNodeOrToken[], walker: AstWalker): void {
for (var i = 0, n = preAst.length; i < n; i++) {
walker.walk(preAst[i]);
}
}
function walkSetAccessorChildren(preAst: SetAccessorSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkObjectLiteralExpressionChildren(preAst: ObjectLiteralExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyAssignments);
}
function walkCastExpressionChildren(preAst: CastExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.type);
walker.walk(preAst.expression);
}
function walkParenthesizedExpressionChildren(preAst: ParenthesizedExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkElementAccessExpressionChildren(preAst: ElementAccessExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.argumentExpression);
}
function walkMemberAccessExpressionChildren(preAst: MemberAccessExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.name);
}
function walkQualifiedNameChildren(preAst: QualifiedNameSyntax, walker: AstWalker): void {
walker.walk(preAst.left);
walker.walk(preAst.right);
}
function walkBinaryExpressionChildren(preAst: BinaryExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.left);
walker.walk(preAst.right);
}
function walkEqualsValueClauseChildren(preAst: EqualsValueClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.value);
}
function walkTypeParameterChildren(preAst: TypeParameterSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.constraint);
}
function walkTypeParameterListChildren(preAst: TypeParameterListSyntax, walker: AstWalker): void {
walker.walk(preAst.typeParameters);
}
function walkGenericTypeChildren(preAst: GenericTypeSyntax, walker: AstWalker): void {
walker.walk(preAst.name);
walker.walk(preAst.typeArgumentList);
}
function walkTypeAnnotationChildren(preAst: TypeAnnotationSyntax, walker: AstWalker): void {
walker.walk(preAst.type);
}
function walkTypeQueryChildren(preAst: TypeQuerySyntax, walker: AstWalker): void {
walker.walk(preAst.name);
}
function walkInvocationExpressionChildren(preAst: InvocationExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.argumentList);
}
function walkObjectCreationExpressionChildren(preAst: ObjectCreationExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.argumentList);
}
function walkTrinaryExpressionChildren(preAst: ConditionalExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.condition);
walker.walk(preAst.whenTrue);
walker.walk(preAst.whenFalse);
}
function walkFunctionExpressionChildren(preAst: FunctionExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkFunctionTypeChildren(preAst: FunctionTypeSyntax, walker: AstWalker): void {
walker.walk(preAst.typeParameterList);
walker.walk(preAst.parameterList);
walker.walk(preAst.type);
}
function walkParenthesizedArrowFunctionExpressionChildren(preAst: ParenthesizedArrowFunctionExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
walker.walk(preAst.expression);
}
function walkSimpleArrowFunctionExpressionChildren(preAst: SimpleArrowFunctionExpressionSyntax, walker: AstWalker): void {
walker.walk(preAst.parameter);
walker.walk(preAst.block);
walker.walk(preAst.expression);
}
function walkMemberFunctionDeclarationChildren(preAst: MemberFunctionDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkFuncDeclChildren(preAst: FunctionDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkIndexMemberDeclarationChildren(preAst: IndexMemberDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.indexSignature);
}
function walkIndexSignatureChildren(preAst: IndexSignatureSyntax, walker: AstWalker): void {
walker.walk(preAst.parameters);
walker.walk(preAst.typeAnnotation);
}
function walkCallSignatureChildren(preAst: CallSignatureSyntax, walker: AstWalker): void {
walker.walk(preAst.typeParameterList);
walker.walk(preAst.parameterList);
walker.walk(preAst.typeAnnotation);
}
function walkConstraintChildren(preAst: ConstraintSyntax, walker: AstWalker): void {
walker.walk(preAst.typeOrExpression);
}
function walkConstructorDeclarationChildren(preAst: ConstructorDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.callSignature);
walker.walk(preAst.block);
}
function walkConstructorTypeChildren(preAst: FunctionTypeSyntax, walker: AstWalker): void {
walker.walk(preAst.typeParameterList);
walker.walk(preAst.parameterList);
walker.walk(preAst.type);
}
function walkConstructSignatureChildren(preAst: ConstructSignatureSyntax, walker: AstWalker): void {
walker.walk(preAst.callSignature);
}
function walkParameterChildren(preAst: ParameterSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.typeAnnotation);
walker.walk(preAst.equalsValueClause);
}
function walkParameterListChildren(preAst: ParameterListSyntax, walker: AstWalker): void {
walker.walk(preAst.parameters);
}
function walkPropertySignatureChildren(preAst: PropertySignatureSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.typeAnnotation);
}
function walkVariableDeclaratorChildren(preAst: VariableDeclaratorSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.typeAnnotation);
walker.walk(preAst.equalsValueClause);
}
function walkMemberVariableDeclarationChildren(preAst: MemberVariableDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.variableDeclarator);
}
function walkMethodSignatureChildren(preAst: MethodSignatureSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.callSignature);
}
function walkReturnStatementChildren(preAst: ReturnStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkForStatementChildren(preAst: ForStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.variableDeclaration);
walker.walk(preAst.initializer);
walker.walk(preAst.condition);
walker.walk(preAst.incrementor);
walker.walk(preAst.statement);
}
function walkForInStatementChildren(preAst: ForInStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.variableDeclaration);
walker.walk(preAst.left);
walker.walk(preAst.expression);
walker.walk(preAst.statement);
}
function walkIfStatementChildren(preAst: IfStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.condition);
walker.walk(preAst.statement);
walker.walk(preAst.elseClause);
}
function walkElseClauseChildren(preAst: ElseClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.statement);
}
function walkWhileStatementChildren(preAst: WhileStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.condition);
walker.walk(preAst.statement);
}
function walkDoStatementChildren(preAst: DoStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.condition);
walker.walk(preAst.statement);
}
function walkBlockChildren(preAst: BlockSyntax, walker: AstWalker): void {
walker.walk(preAst.statements);
}
function walkVariableDeclarationChildren(preAst: VariableDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.variableDeclarators);
}
function walkCaseSwitchClauseChildren(preAst: CaseSwitchClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.statements);
}
function walkDefaultSwitchClauseChildren(preAst: DefaultSwitchClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.statements);
}
function walkSwitchStatementChildren(preAst: SwitchStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
walker.walk(preAst.switchClauses);
}
function walkTryStatementChildren(preAst: TryStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.block);
walker.walk(preAst.catchClause);
walker.walk(preAst.finallyClause);
}
function walkCatchClauseChildren(preAst: CatchClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.typeAnnotation);
walker.walk(preAst.block);
}
function walkExternalModuleReferenceChildren(preAst: ExternalModuleReferenceSyntax, walker: AstWalker): void {
walker.walk(preAst.stringLiteral);
}
function walkFinallyClauseChildren(preAst: FinallyClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.block);
}
function walkClassDeclChildren(preAst: ClassDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.typeParameterList);
walker.walk(preAst.heritageClauses);
walker.walk(preAst.classElements);
}
function walkScriptChildren(preAst: SourceUnitSyntax, walker: AstWalker): void {
walker.walk(preAst.moduleElements);
}
function walkHeritageClauseChildren(preAst: HeritageClauseSyntax, walker: AstWalker): void {
walker.walk(preAst.typeNames);
}
function walkInterfaceDeclerationChildren(preAst: InterfaceDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.typeParameterList);
walker.walk(preAst.heritageClauses);
walker.walk(preAst.body);
}
function walkObjectTypeChildren(preAst: ObjectTypeSyntax, walker: AstWalker): void {
walker.walk(preAst.typeMembers);
}
function walkArrayTypeChildren(preAst: ArrayTypeSyntax, walker: AstWalker): void {
walker.walk(preAst.type);
}
function walkModuleDeclarationChildren(preAst: ModuleDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.name);
walker.walk(preAst.stringLiteral);
walker.walk(preAst.moduleElements);
}
function walkModuleNameModuleReferenceChildren(preAst: ModuleNameModuleReferenceSyntax, walker: AstWalker): void {
walker.walk(preAst.moduleName);
}
function walkEnumDeclarationChildren(preAst: EnumDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.enumElements);
}
function walkEnumElementChildren(preAst: EnumElementSyntax, walker: AstWalker): void {
walker.walk(preAst.propertyName);
walker.walk(preAst.equalsValueClause);
}
function walkImportDeclarationChildren(preAst: ImportDeclarationSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.moduleReference);
}
function walkExportAssignmentChildren(preAst: ExportAssignmentSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
}
function walkWithStatementChildren(preAst: WithStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.condition);
walker.walk(preAst.statement);
}
function walkExpressionStatementChildren(preAst: ExpressionStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.expression);
}
function walkLabeledStatementChildren(preAst: LabeledStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.identifier);
walker.walk(preAst.statement);
}
function walkVariableStatementChildren(preAst: VariableStatementSyntax, walker: AstWalker): void {
walker.walk(preAst.variableDeclaration);
}
var childrenWalkers: IAstWalkChildren[] = new Array<IAstWalkChildren>(SyntaxKind.LastNode + 1);
// Tokens/trivia can't ever be walked into.
for (var i = SyntaxKind.FirstToken, n = SyntaxKind.LastToken; i <= n; i++) {
childrenWalkers[i] = null;
}
for (var i = SyntaxKind.FirstTrivia, n = SyntaxKind.LastTrivia; i <= n; i++) {
childrenWalkers[i] = null;
}
childrenWalkers[SyntaxKind.AddAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.AddExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.AndAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.AnyKeyword] = null;
childrenWalkers[SyntaxKind.ArgumentList] = walkArgumentListChildren;
childrenWalkers[SyntaxKind.ArrayLiteralExpression] = walkArrayLiteralExpressionChildren;
childrenWalkers[SyntaxKind.ArrayType] = walkArrayTypeChildren;
childrenWalkers[SyntaxKind.SimpleArrowFunctionExpression] = walkSimpleArrowFunctionExpressionChildren;
childrenWalkers[SyntaxKind.ParenthesizedArrowFunctionExpression] = walkParenthesizedArrowFunctionExpressionChildren;
childrenWalkers[SyntaxKind.AssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.BitwiseAndExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.BitwiseExclusiveOrExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.BitwiseNotExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.BitwiseOrExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.Block] = walkBlockChildren;
childrenWalkers[SyntaxKind.BooleanKeyword] = null;
childrenWalkers[SyntaxKind.BreakStatement] = null;
childrenWalkers[SyntaxKind.CallSignature] = walkCallSignatureChildren;
childrenWalkers[SyntaxKind.CaseSwitchClause] = walkCaseSwitchClauseChildren;
childrenWalkers[SyntaxKind.CastExpression] = walkCastExpressionChildren;
childrenWalkers[SyntaxKind.CatchClause] = walkCatchClauseChildren;
childrenWalkers[SyntaxKind.ClassDeclaration] = walkClassDeclChildren;
childrenWalkers[SyntaxKind.CommaExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.ConditionalExpression] = walkTrinaryExpressionChildren;
childrenWalkers[SyntaxKind.Constraint] = walkConstraintChildren;
childrenWalkers[SyntaxKind.ConstructorDeclaration] = walkConstructorDeclarationChildren;
childrenWalkers[SyntaxKind.ConstructSignature] = walkConstructSignatureChildren;
childrenWalkers[SyntaxKind.ContinueStatement] = null;
childrenWalkers[SyntaxKind.ConstructorType] = walkConstructorTypeChildren;
childrenWalkers[SyntaxKind.DebuggerStatement] = null;
childrenWalkers[SyntaxKind.DefaultSwitchClause] = walkDefaultSwitchClauseChildren;
childrenWalkers[SyntaxKind.DeleteExpression] = walkDeleteExpressionChildren;
childrenWalkers[SyntaxKind.DivideAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.DivideExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.DoStatement] = walkDoStatementChildren;
childrenWalkers[SyntaxKind.ElementAccessExpression] = walkElementAccessExpressionChildren;
childrenWalkers[SyntaxKind.ElseClause] = walkElseClauseChildren;
childrenWalkers[SyntaxKind.EmptyStatement] = null;
childrenWalkers[SyntaxKind.EnumDeclaration] = walkEnumDeclarationChildren;
childrenWalkers[SyntaxKind.EnumElement] = walkEnumElementChildren;
childrenWalkers[SyntaxKind.EqualsExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.EqualsValueClause] = walkEqualsValueClauseChildren;
childrenWalkers[SyntaxKind.EqualsWithTypeConversionExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.ExclusiveOrAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.ExportAssignment] = walkExportAssignmentChildren;
childrenWalkers[SyntaxKind.ExpressionStatement] = walkExpressionStatementChildren;
childrenWalkers[SyntaxKind.ExtendsHeritageClause] = walkHeritageClauseChildren;
childrenWalkers[SyntaxKind.ExternalModuleReference] = walkExternalModuleReferenceChildren;
childrenWalkers[SyntaxKind.FalseKeyword] = null;
childrenWalkers[SyntaxKind.FinallyClause] = walkFinallyClauseChildren;
childrenWalkers[SyntaxKind.ForInStatement] = walkForInStatementChildren;
childrenWalkers[SyntaxKind.ForStatement] = walkForStatementChildren;
childrenWalkers[SyntaxKind.FunctionDeclaration] = walkFuncDeclChildren;
childrenWalkers[SyntaxKind.FunctionExpression] = walkFunctionExpressionChildren;
childrenWalkers[SyntaxKind.FunctionPropertyAssignment] = walkFunctionPropertyAssignmentChildren;
childrenWalkers[SyntaxKind.FunctionType] = walkFunctionTypeChildren;
childrenWalkers[SyntaxKind.GenericType] = walkGenericTypeChildren;
childrenWalkers[SyntaxKind.GetAccessor] = walkGetAccessorChildren;
childrenWalkers[SyntaxKind.GreaterThanExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.GreaterThanOrEqualExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.IfStatement] = walkIfStatementChildren;
childrenWalkers[SyntaxKind.ImplementsHeritageClause] = walkHeritageClauseChildren;
childrenWalkers[SyntaxKind.ImportDeclaration] = walkImportDeclarationChildren;
childrenWalkers[SyntaxKind.IndexMemberDeclaration] = walkIndexMemberDeclarationChildren;
childrenWalkers[SyntaxKind.IndexSignature] = walkIndexSignatureChildren;
childrenWalkers[SyntaxKind.InExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.InstanceOfExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.InterfaceDeclaration] = walkInterfaceDeclerationChildren;
childrenWalkers[SyntaxKind.InvocationExpression] = walkInvocationExpressionChildren;
childrenWalkers[SyntaxKind.LabeledStatement] = walkLabeledStatementChildren;
childrenWalkers[SyntaxKind.LeftShiftAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.LeftShiftExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.LessThanExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.LessThanOrEqualExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.List] = walkListChildren;
childrenWalkers[SyntaxKind.LogicalAndExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.LogicalNotExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.LogicalOrExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.MemberAccessExpression] = walkMemberAccessExpressionChildren;
childrenWalkers[SyntaxKind.MemberFunctionDeclaration] = walkMemberFunctionDeclarationChildren;
childrenWalkers[SyntaxKind.MemberVariableDeclaration] = walkMemberVariableDeclarationChildren;
childrenWalkers[SyntaxKind.MethodSignature] = walkMethodSignatureChildren;
childrenWalkers[SyntaxKind.ModuleDeclaration] = walkModuleDeclarationChildren;
childrenWalkers[SyntaxKind.ModuleNameModuleReference] = walkModuleNameModuleReferenceChildren;
childrenWalkers[SyntaxKind.ModuloAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.ModuloExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.MultiplyAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.MultiplyExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.IdentifierName] = null;
childrenWalkers[SyntaxKind.NegateExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.None] = null;
childrenWalkers[SyntaxKind.NotEqualsExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.NotEqualsWithTypeConversionExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.NullKeyword] = null;
childrenWalkers[SyntaxKind.NumberKeyword] = null;
childrenWalkers[SyntaxKind.NumericLiteral] = null;
childrenWalkers[SyntaxKind.ObjectCreationExpression] = walkObjectCreationExpressionChildren;
childrenWalkers[SyntaxKind.ObjectLiteralExpression] = walkObjectLiteralExpressionChildren;
childrenWalkers[SyntaxKind.ObjectType] = walkObjectTypeChildren;
childrenWalkers[SyntaxKind.OmittedExpression] = null;
childrenWalkers[SyntaxKind.OrAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.Parameter] = walkParameterChildren;
childrenWalkers[SyntaxKind.ParameterList] = walkParameterListChildren;
childrenWalkers[SyntaxKind.ParenthesizedExpression] = walkParenthesizedExpressionChildren;
childrenWalkers[SyntaxKind.PlusExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.PostDecrementExpression] = walkPostfixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.PostIncrementExpression] = walkPostfixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.PreDecrementExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.PreIncrementExpression] = walkPrefixUnaryExpressionChildren;
childrenWalkers[SyntaxKind.PropertySignature] = walkPropertySignatureChildren;
childrenWalkers[SyntaxKind.QualifiedName] = walkQualifiedNameChildren;
childrenWalkers[SyntaxKind.RegularExpressionLiteral] = null;
childrenWalkers[SyntaxKind.ReturnStatement] = walkReturnStatementChildren;
childrenWalkers[SyntaxKind.SourceUnit] = walkScriptChildren;
childrenWalkers[SyntaxKind.SeparatedList] = walkSeparatedListChildren;
childrenWalkers[SyntaxKind.SetAccessor] = walkSetAccessorChildren;
childrenWalkers[SyntaxKind.SignedRightShiftAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.SignedRightShiftExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.SimplePropertyAssignment] = walkSimplePropertyAssignmentChildren;
childrenWalkers[SyntaxKind.StringLiteral] = null;
childrenWalkers[SyntaxKind.StringKeyword] = null;
childrenWalkers[SyntaxKind.SubtractAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.SubtractExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.SuperKeyword] = null;
childrenWalkers[SyntaxKind.SwitchStatement] = walkSwitchStatementChildren;
childrenWalkers[SyntaxKind.ThisKeyword] = null;
childrenWalkers[SyntaxKind.ThrowStatement] = walkThrowStatementChildren;
childrenWalkers[SyntaxKind.TriviaList] = null;
childrenWalkers[SyntaxKind.TrueKeyword] = null;
childrenWalkers[SyntaxKind.TryStatement] = walkTryStatementChildren;
childrenWalkers[SyntaxKind.TypeAnnotation] = walkTypeAnnotationChildren;
childrenWalkers[SyntaxKind.TypeArgumentList] = walkTypeArgumentListChildren;
childrenWalkers[SyntaxKind.TypeOfExpression] = walkTypeOfExpressionChildren;
childrenWalkers[SyntaxKind.TypeParameter] = walkTypeParameterChildren;
childrenWalkers[SyntaxKind.TypeParameterList] = walkTypeParameterListChildren;
childrenWalkers[SyntaxKind.TypeQuery] = walkTypeQueryChildren;
childrenWalkers[SyntaxKind.UnsignedRightShiftAssignmentExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.UnsignedRightShiftExpression] = walkBinaryExpressionChildren;
childrenWalkers[SyntaxKind.VariableDeclaration] = walkVariableDeclarationChildren;
childrenWalkers[SyntaxKind.VariableDeclarator] = walkVariableDeclaratorChildren;
childrenWalkers[SyntaxKind.VariableStatement] = walkVariableStatementChildren;
childrenWalkers[SyntaxKind.VoidExpression] = walkVoidExpressionChildren;
childrenWalkers[SyntaxKind.VoidKeyword] = null;
childrenWalkers[SyntaxKind.WhileStatement] = walkWhileStatementChildren;
childrenWalkers[SyntaxKind.WithStatement] = walkWithStatementChildren;
// Verify the code is up to date with the enum
for (var e in SyntaxKind) {
if (SyntaxKind.hasOwnProperty(e) && StringUtilities.isString(SyntaxKind[e])) {
TypeScript.Debug.assert(childrenWalkers[e] !== undefined, "Fix initWalkers: " + SyntaxKind[e]);
}
}
export class AstWalkOptions {
public goChildren = true;
public stopWalking = false;
}
interface IAstWalkChildren {
(preAst: ISyntaxElement, walker: AstWalker): void;
}
export interface IAstWalker {
options: AstWalkOptions;
state: any
}
interface AstWalker {
walk(ast: ISyntaxElement): void;
}
class SimplePreAstWalker implements AstWalker {
public options: AstWalkOptions = new AstWalkOptions();
constructor(
private pre: (ast: ISyntaxElement, state: any) => void,
public state: any) {
}
public walk(ast: ISyntaxElement): void {
if (!ast) {
return;
}
this.pre(ast, this.state);
var walker = childrenWalkers[ast.kind()];
if (walker) {
walker(ast, this);
}
}
}
class SimplePrePostAstWalker implements AstWalker {
public options: AstWalkOptions = new AstWalkOptions();
constructor(
private pre: (ast: ISyntaxElement, state: any) => void,
private post: (ast: ISyntaxElement, state: any) => void,
public state: any) {
}
public walk(ast: ISyntaxElement): void {
if (!ast) {
return;
}
this.pre(ast, this.state);
var walker = childrenWalkers[ast.kind()];
if (walker) {
walker(ast, this);
}
this.post(ast, this.state);
}
}
class NormalAstWalker implements AstWalker {
public options: AstWalkOptions = new AstWalkOptions();
constructor(
private pre: (ast: ISyntaxElement, walker: IAstWalker) => void,
private post: (ast: ISyntaxElement, walker: IAstWalker) => void,
public state: any) {
}
public walk(ast: ISyntaxElement): void {
if (!ast) {
return;
}
// If we're stopping, then bail out immediately.
if (this.options.stopWalking) {
return;
}
this.pre(ast, this);
// If we were asked to stop, then stop.
if (this.options.stopWalking) {
return;
}
if (this.options.goChildren) {
// Call the "walkChildren" function corresponding to "nodeType".
var walker = childrenWalkers[ast.kind()];
if (walker) {
walker(ast, this);
}
}
else {
// no go only applies to children of node issuing it
this.options.goChildren = true;
}
if (this.post) {
this.post(ast, this);
}
}
}
export class AstWalkerFactory {
public walk(ast: ISyntaxElement, pre: (ast: ISyntaxElement, walker: IAstWalker) => void, post?: (ast: ISyntaxElement, walker: IAstWalker) => void, state?: any): void {
new NormalAstWalker(pre, post, state).walk(ast);
}
public simpleWalk(ast: ISyntaxElement, pre: (ast: ISyntaxElement, state: any) => void, post?: (ast: ISyntaxElement, state: any) => void, state?: any): void {
if (post) {
new SimplePrePostAstWalker(pre, post, state).walk(ast);
}
else {
new SimplePreAstWalker(pre, state).walk(ast);
}
}
}
var globalAstWalkerFactory = new AstWalkerFactory();
export function getAstWalkerFactory(): AstWalkerFactory {
return globalAstWalkerFactory;
}
}

View File

@@ -0,0 +1,96 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
module TypeScript {
class Base64Format {
static encodedValues = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
static encode(inValue: number) {
if (inValue < 64) {
return Base64Format.encodedValues.charAt(inValue);
}
throw TypeError(inValue + ": not a 64 based value");
}
static decodeChar(inChar: string) {
if (inChar.length === 1) {
return Base64Format.encodedValues.indexOf(inChar);
}
else {
throw TypeError('"' + inChar + '" must have length 1');
}
}
}
export class Base64VLQFormat {
static encode(inValue: number) {
// Add a new least significant bit that has the sign of the value.
// if negative number the least significant bit that gets added to the number has value 1
// else least significant bit value that gets added is 0
// eg. -1 changes to binary : 01 [1] => 3
// +1 changes to binary : 01 [0] => 2
if (inValue < 0) {
inValue = ((-inValue) << 1) + 1;
}
else {
inValue = inValue << 1;
}
// Encode 5 bits at a time starting from least significant bits
var encodedStr = "";
do {
var currentDigit = inValue & 31; // 11111
inValue = inValue >> 5;
if (inValue > 0) {
// There are still more digits to decode, set the msb (6th bit)
currentDigit = currentDigit | 32;
}
encodedStr = encodedStr + Base64Format.encode(currentDigit);
} while (inValue > 0);
return encodedStr;
}
static decode(inString: string) {
var result = 0;
var negative = false;
var shift = 0;
for (var i = 0; i < inString.length; i++) {
var byte = Base64Format.decodeChar(inString[i]);
if (i === 0) {
// Sign bit appears in the LSBit of the first value
if ((byte & 1) === 1) {
negative = true;
}
result = (byte >> 1) & 15; // 1111x
}
else {
result = result | ((byte & 31) << shift); // 11111
}
shift += (i === 0) ? 4 : 5;
if ((byte & 32) === 32) {
// Continue
}
else {
return { value: negative ? -(result) : result, rest: inString.substr(i + 1) };
}
}
throw new Error(getDiagnosticMessage(DiagnosticCode.Base64_value_0_finished_with_a_continuation_bit, [inString]));
}
}
}

View File

@@ -0,0 +1,131 @@
///<reference path='..\core\integerUtilities.ts' />
module TypeScript {
export class BloomFilter {
private bitArray: boolean[];
private hashFunctionCount: number;
public static falsePositiveProbability: number = 0.0001;
/*
* From the bloom filter calculator here: http://hur.st/bloomfilter?n=4&p=1.0E-20
*
* 1) n = Number of items in the filter
*
* 2) p = Probability of false positives, (a double between 0 and 1).
*
* 3) m = Number of bits in the filter
*
* 4) k = Number of hash functions
*
* m = ceil((n * log(p)) / log(1.0 / (pow(2.0, log(2.0)))))
*
* k = round(log(2.0) * m / n)
*
*/
constructor(expectedCount: number) {
var m: number = Math.max(1, BloomFilter.computeM(expectedCount));
var k: number = Math.max(1, BloomFilter.computeK(expectedCount));;
// We must have size in even bytes, so that when we deserialize from bytes we get a bit array with the same count.
// The count is used by the hash functions.
var sizeInEvenBytes = (m + 7) & ~7;
this.bitArray = [];
for (var i = 0, len = sizeInEvenBytes; i < len; i++) {
this.bitArray[i] = false;
}
this.hashFunctionCount = k;
}
// m = ceil((n * log(p)) / log(1.0 / (pow(2.0, log(2.0)))))
static computeM(expectedCount: number): number {
var p: number = BloomFilter.falsePositiveProbability;
var n: number = expectedCount;
var numerator = n * Math.log(p);
var denominator = Math.log(1.0 / Math.pow(2.0, Math.log(2.0)));
return Math.ceil(numerator / denominator);
}
// k = round(log(2.0) * m / n)
static computeK(expectedCount: number): number {
var n: number = expectedCount;
var m: number = BloomFilter.computeM(expectedCount);
var temp = Math.log(2.0) * m / n;
return Math.round(temp);
}
/** Modification of the murmurhash2 algorithm. Code is simpler because it operates over
* strings instead of byte arrays. Because each string character is two bytes, it is known
* that the input will be an even number of bytes (though not necessarily a multiple of 4).
*
* This is needed over the normal 'string.GetHashCode()' because we need to be able to generate
* 'k' different well distributed hashes for any given string s. Also, we want to be able to
* generate these hashes without allocating any memory. My ideal solution would be to use an
* MD5 hash. However, there appears to be no way to do MD5 in .Net where you can:
*
* a) feed it individual values instead of a byte[]
*
* b) have the hash computed into a byte[] you provide instead of a newly allocated one
*
* Generating 'k' pieces of garbage on each insert and lookup seems very wasteful. So,
* instead, we use murmur hash since it provides well distributed values, allows for a
* seed, and allocates no memory.
*
* Murmur hash is public domain. Actual code is included below as reference.
*/
private computeHash(key: string, seed: number): number {
return Hash.computeMurmur2StringHashCode(key, seed);
}
public addKeys(keys: IIndexable<any>) {
for (var name in keys) {
if (keys[name]) {
this.add(name);
}
}
}
public add(value: string) {
for (var i = 0; i < this.hashFunctionCount; i++) {
var hash = this.computeHash(value, i);
hash = hash % this.bitArray.length;
this.bitArray[Math.abs(hash)] = true;
}
}
public probablyContains(value: string): boolean {
for (var i = 0; i < this.hashFunctionCount; i++) {
var hash = this.computeHash(value, i);
hash = hash % this.bitArray.length;
if (!this.bitArray[Math.abs(hash)]) {
return false;
}
}
return true;
}
public isEquivalent(filter: BloomFilter): boolean {
return BloomFilter.isEquivalent(this.bitArray, filter.bitArray)
&& this.hashFunctionCount === filter.hashFunctionCount;
}
static isEquivalent(array1: boolean[], array2: boolean[]): boolean {
if (array1.length !== array2.length) {
return false;
}
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,47 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export interface ILogger {
information(): boolean;
debug(): boolean;
warning(): boolean;
error(): boolean;
fatal(): boolean;
log(s: string): void;
}
export class NullLogger implements ILogger {
public information(): boolean { return false; }
public debug(): boolean { return false; }
public warning(): boolean { return false; }
public error(): boolean { return false; }
public fatal(): boolean { return false; }
public log(s: string): void {
}
}
export function timeFunction(logger: ILogger, funcDescription: string, func: () => any): any {
var start = (new Date()).getTime();
var result = func();
var end = (new Date()).getTime();
if (logger.information()) {
logger.log(funcDescription + " completed in " + (end - start) + " msec");
}
return result;
}
}

View File

@@ -0,0 +1,151 @@
///<reference path='references.ts' />
module TypeScript {
export interface IncrementalParse {
(oldSyntaxTree: SyntaxTree, textChangeRange: TextChangeRange, newText: ISimpleText): SyntaxTree
}
export class Document {
private _bloomFilter: BloomFilter = null;
// By default, our Document class doesn't support incremental update of its contents.
// However, we enable other layers (like teh services layer) to inject the capability
// into us by setting this function.
public static incrementalParse: IncrementalParse = null;
constructor(private compilationSettings: ts.CompilerOptions,
public filename: string,
public referencedFiles: string[],
private _scriptSnapshot: IScriptSnapshot,
public byteOrderMark: ByteOrderMark,
public version: number,
public isOpen: boolean,
private _syntaxTree: SyntaxTree,
private _soruceFile: ts.SourceFile) {
}
public isDeclareFile(): boolean {
return isDTSFile(this.filename);
}
public sourceUnit(): SourceUnitSyntax {
// If we don't have a script, create one from our parse tree.
return this.syntaxTree().sourceUnit();
}
public diagnostics(): Diagnostic[] {
return this.syntaxTree().diagnostics();
}
public lineMap(): LineMap {
return this.syntaxTree().lineMap();
}
public syntaxTree(): SyntaxTree {
if (!this._syntaxTree) {
var start = new Date().getTime();
this._syntaxTree = Parser.parse(
this.filename, SimpleText.fromScriptSnapshot(this._scriptSnapshot), this.compilationSettings.target, this.isDeclareFile());
var time = new Date().getTime() - start;
//TypeScript.syntaxTreeParseTime += time;
}
return this._syntaxTree;
}
public sourceFile(): ts.SourceFile {
if (!this._soruceFile) {
var start = new Date().getTime();
this._soruceFile = ts.createSourceFile(this.filename, this._scriptSnapshot.getText(0, this._scriptSnapshot.getLength()), this.compilationSettings.target);
var time = new Date().getTime() - start;
//TypeScript.astParseTime += time;
}
return this._soruceFile;
}
public bloomFilter(): BloomFilter {
if (!this._bloomFilter) {
var identifiers = createIntrinsicsObject<boolean>();
var pre = function (cur: TypeScript.ISyntaxElement) {
if (ASTHelpers.isValidAstNode(cur)) {
if (cur.kind() === SyntaxKind.IdentifierName) {
var nodeText = tokenValueText((<TypeScript.ISyntaxToken>cur));
identifiers[nodeText] = true;
}
}
};
TypeScript.getAstWalkerFactory().simpleWalk(this.sourceUnit(), pre, null, identifiers);
var identifierCount = 0;
for (var name in identifiers) {
if (identifiers[name]) {
identifierCount++;
}
}
this._bloomFilter = new BloomFilter(identifierCount);
this._bloomFilter.addKeys(identifiers);
}
return this._bloomFilter;
}
// Returns true if this file should get emitted into its own unique output file.
// Otherwise, it should be written into a single output file along with the rest of hte
// documents in the compilation.
public emitToOwnOutputFile(): boolean {
// If we haven't specified an output file in our settings, then we're definitely
// emitting to our own file. Also, if we're an external module, then we're
// definitely emitting to our own file.
return !this.compilationSettings.out || this.syntaxTree().isExternalModule();
}
public update(scriptSnapshot: IScriptSnapshot, version: number, isOpen: boolean, textChangeRange: TextChangeRange): Document {
// See if we are currently holding onto a syntax tree. We may not be because we're
// either a closed file, or we've just been lazy and haven't had to create the syntax
// tree yet. Access the field instead of the method so we don't accidently realize
// the old syntax tree.
var oldSyntaxTree = this._syntaxTree;
if (textChangeRange !== null && Debug.shouldAssert(AssertionLevel.Normal)) {
var oldText = this._scriptSnapshot;
var newText = scriptSnapshot;
TypeScript.Debug.assert((oldText.getLength() - textChangeRange.span().length() + textChangeRange.newLength()) === newText.getLength());
if (Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
var oldTextPrefix = oldText.getText(0, textChangeRange.span().start());
var newTextPrefix = newText.getText(0, textChangeRange.span().start());
TypeScript.Debug.assert(oldTextPrefix === newTextPrefix);
var oldTextSuffix = oldText.getText(textChangeRange.span().end(), oldText.getLength());
var newTextSuffix = newText.getText(textChangeRange.newSpan().end(), newText.getLength());
TypeScript.Debug.assert(oldTextSuffix === newTextSuffix);
}
}
var text = SimpleText.fromScriptSnapshot(scriptSnapshot);
// If we don't have a text change, or we don't have an old syntax tree, then do a full
// parse. Otherwise, do an incremental parse.
var newSyntaxTree = textChangeRange === null || oldSyntaxTree === null || Document.incrementalParse === null
? TypeScript.Parser.parse(this.filename, text, this.compilationSettings.target, TypeScript.isDTSFile(this.filename))
: Document.incrementalParse(oldSyntaxTree, textChangeRange, text);
return new Document(this.compilationSettings, this.filename, this.referencedFiles, scriptSnapshot, this.byteOrderMark, version, isOpen, newSyntaxTree, /*soruceFile*/ null);
}
public static create(compilationSettings: ts.CompilerOptions, fileName: string, scriptSnapshot: IScriptSnapshot, byteOrderMark: ByteOrderMark, version: number, isOpen: boolean, referencedFiles: string[]): Document {
return new Document(compilationSettings, fileName, referencedFiles, scriptSnapshot, byteOrderMark, version, isOpen, /*syntaxTree:*/ null, /*soruceFile*/ null);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,6 @@
declare class Enumerator {
public atEnd(): boolean;
public moveNext(): boolean;
public item(): any;
constructor (o: any);
}

View File

View File

@@ -0,0 +1,187 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
var proto = "__proto__"
class BlockIntrinsics<T> {
public prototype: T = undefined;
public toString: T = undefined;
public toLocaleString: T = undefined;
public valueOf: T = undefined;
public hasOwnProperty: T = undefined;
public propertyIsEnumerable: T = undefined;
public isPrototypeOf: T = undefined;
[s: string]: T;
constructor() {
// initialize the 'constructor' field
this["constructor"] = undefined;
// First we set it to null, because that's the only way to erase the value in node. Then we set it to undefined in case we are not in node, since
// in StringHashTable below, we check for undefined explicitly.
this[proto] = null;
this[proto] = undefined;
}
}
export function createIntrinsicsObject<T>(): IIndexable<T> {
return new BlockIntrinsics<T>();
}
export interface IHashTable<T> {
getAllKeys(): string[];
add(key: string, data: T): boolean;
addOrUpdate(key: string, data: T): boolean;
map(fn: (k: string, value: T, context: any) => void , context: any): void;
every(fn: (k: string, value: T, context: any) => void , context: any): boolean;
some(fn: (k: string, value: T, context: any) => void , context: any): boolean;
count(): number;
lookup(key: string): T;
}
export class StringHashTable<T> implements IHashTable<T> {
private itemCount = 0;
private table: IIndexable<T> = createIntrinsicsObject<T>();
public getAllKeys(): string[] {
var result: string[] = [];
for (var k in this.table) {
if (this.table[k] !== undefined) {
result.push(k);
}
}
return result;
}
public add(key: string, data: T): boolean {
if (this.table[key] !== undefined) {
return false;
}
this.table[key] = data;
this.itemCount++;
return true;
}
public addOrUpdate(key: string, data: T): boolean {
if (this.table[key] !== undefined) {
this.table[key] = data;
return false;
}
this.table[key] = data;
this.itemCount++;
return true;
}
public map(fn: (k: string, value: T, context: any) => void , context: any) {
for (var k in this.table) {
var data = this.table[k];
if (data !== undefined) {
fn(k, this.table[k], context);
}
}
}
public every(fn: (k: string, value: T, context: any) => void , context: any) {
for (var k in this.table) {
var data = this.table[k];
if (data !== undefined) {
if (!fn(k, this.table[k], context)) {
return false;
}
}
}
return true;
}
public some(fn: (k: string, value: T, context: any) => void , context: any) {
for (var k in this.table) {
var data = this.table[k];
if (data !== undefined) {
if (fn(k, this.table[k], context)) {
return true;
}
}
}
return false;
}
public count(): number {
return this.itemCount;
}
public lookup(key: string) : T {
var data = this.table[key];
return data === undefined ? null : data;
}
public remove(key: string): void {
if (this.table[key] !== undefined) {
this.table[key] = undefined;
this.itemCount--;
}
}
}
export class IdentiferNameHashTable<T> extends StringHashTable<T> {
public getAllKeys(): string[]{
var result: string[] = [];
super.map((k, v, c) => {
if (v !== undefined) {
result.push(k.substring(1));
}
}, null);
return result;
}
public add(key: string, data: T): boolean {
return super.add("#" + key, data);
}
public addOrUpdate(key: string, data: T): boolean {
return super.addOrUpdate("#" + key, data);
}
public map(fn: (k: string, value: T, context: any) => void , context: any) {
return super.map((k, v, c) => fn(k.substring(1), v, c), context);
}
public every(fn: (k: string, value: T, context: any) => void , context: any) {
return super.every((k, v, c) => fn(k.substring(1), v, c), context);
}
public some(fn: (k: string, value: any, context: any) => void , context: any) {
return super.some((k, v, c) => fn(k.substring(1), v, c), context);
}
public lookup(key: string): T {
return super.lookup("#" + key);
}
}
}

View File

@@ -0,0 +1,11 @@
module TypeScript {
export class IdentifierWalker extends SyntaxWalker {
constructor(public list: IIndexable<boolean>) {
super();
}
public visitToken(token: ISyntaxToken): void {
this.list[token.text()] = true;
}
}
}

View File

@@ -0,0 +1,64 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='enumerator.ts' />
///<reference path='process.ts' />
///<reference path='core\references.ts' />
module TypeScript {
export interface IFindFileResult {
fileInformation: FileInformation;
path: string;
}
export module IOUtils {
// Creates the directory including its parent if not already present
function createDirectoryStructure(ioHost: IEnvironment, dirName: string) {
if (ioHost.directoryExists(dirName)) {
return;
}
var parentDirectory = ioHost.directoryName(dirName);
if (parentDirectory != "") {
createDirectoryStructure(ioHost, parentDirectory);
}
ioHost.createDirectory(dirName);
}
// Creates a file including its directory structure if not already present
export function writeFileAndFolderStructure(ioHost: IEnvironment, fileName: string, contents: string, writeByteOrderMark: boolean): void {
var start = new Date().getTime();
var path = ioHost.absolutePath(fileName);
TypeScript.ioHostResolvePathTime += new Date().getTime() - start;
var start = new Date().getTime();
var dirName = ioHost.directoryName(path);
TypeScript.ioHostDirectoryNameTime += new Date().getTime() - start;
var start = new Date().getTime();
createDirectoryStructure(ioHost, dirName);
TypeScript.ioHostCreateDirectoryStructureTime += new Date().getTime() - start;
var start = new Date().getTime();
ioHost.writeFile(path, contents, writeByteOrderMark);
TypeScript.ioHostWriteFileTime += new Date().getTime() - start;
}
export function combine(prefix: string, suffix: string): string {
return prefix + "/" + suffix;
}
}
}

View File

@@ -0,0 +1,266 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path="references.ts" />
module TypeScript {
export interface IOptions {
name?: string;
flag?: boolean;
short?: string;
usage?: {
locCode: string; // DiagnosticCode
args: string[]
};
set?: (s: string) => void;
type?: string; // DiagnosticCode
experimental?: boolean;
}
export class OptionsParser {
private DEFAULT_SHORT_FLAG = "-";
private DEFAULT_LONG_FLAG = "--";
private printedVersion: boolean = false;
// Find the option record for the given string. Returns null if not found.
private findOption(arg: string) {
var upperCaseArg = arg && arg.toUpperCase();
for (var i = 0; i < this.options.length; i++) {
var current = this.options[i];
if (upperCaseArg === (current.short && current.short.toUpperCase()) ||
upperCaseArg === (current.name && current.name.toUpperCase())) {
return current;
}
}
return null;
}
public unnamed: string[] = [];
public options: IOptions[] = [];
constructor(public host: IEnvironment, public version: string) {
}
public printUsage() {
this.printVersion();
var optionsWord = getLocalizedText(DiagnosticCode.options, null);
var fileWord = getLocalizedText(DiagnosticCode.file1, null);
var tscSyntax = "tsc [" + optionsWord + "] [" + fileWord + " ..]";
var syntaxHelp = getLocalizedText(DiagnosticCode.Syntax_0, [tscSyntax]);
this.host.standardOut.WriteLine(syntaxHelp);
this.host.standardOut.WriteLine("");
this.host.standardOut.WriteLine(getLocalizedText(DiagnosticCode.Examples, null) + " tsc hello.ts");
this.host.standardOut.WriteLine(" tsc --out foo.js foo.ts");
this.host.standardOut.WriteLine(" tsc @args.txt");
this.host.standardOut.WriteLine("");
this.host.standardOut.WriteLine(getLocalizedText(DiagnosticCode.Options, null));
var output: string[][] = [];
var maxLength = 0;
var i = 0;
this.options = this.options.sort(function (a, b) {
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
if (aName > bName) {
return 1;
} else if (aName < bName) {
return -1;
} else {
return 0;
}
});
// Build up output array
for (i = 0; i < this.options.length; i++) {
var option = this.options[i];
if (option.experimental) {
continue;
}
if (!option.usage) {
break;
}
var usageString = " ";
var type = option.type ? (" " + TypeScript.getLocalizedText(option.type, null)) : "";
if (option.short) {
usageString += this.DEFAULT_SHORT_FLAG + option.short + type + ", ";
}
usageString += this.DEFAULT_LONG_FLAG + option.name + type;
output.push([usageString, TypeScript.getLocalizedText(option.usage.locCode, option.usage.args)]);
if (usageString.length > maxLength) {
maxLength = usageString.length;
}
}
var fileDescription = getLocalizedText(DiagnosticCode.Insert_command_line_options_and_files_from_a_file, null);
output.push([" @<" + fileWord + ">", fileDescription]);
// Print padded output
for (i = 0; i < output.length; i++) {
this.host.standardOut.WriteLine(output[i][0] + (new Array(maxLength - output[i][0].length + 3)).join(" ") + output[i][1]);
}
}
public printVersion() {
if (!this.printedVersion) {
this.host.standardOut.WriteLine(getLocalizedText(DiagnosticCode.Version_0, [this.version]));
this.printedVersion = true;
}
}
public option(name: string, config: IOptions, short?: string) {
if (!config) {
config = <any>short;
short = null;
}
config.name = name;
config.short = short;
config.flag = false;
this.options.push(config);
}
public flag(name: string, config: IOptions, short?: string) {
if (!config) {
config = <any>short;
short = null;
}
config.name = name;
config.short = short;
config.flag = true
this.options.push(config);
}
// Parse an arguments string
public parseString(argString: string) {
var position = 0;
var tokens = argString.match(/\s+|"|[^\s"]+/g);
function peek() {
return tokens[position];
}
function consume() {
return tokens[position++];
}
function consumeQuotedString() {
var value = '';
consume(); // skip opening quote.
var token = peek();
while (token && token !== '"') {
consume();
value += token;
token = peek();
}
consume(); // skip ending quote;
return value;
}
var args: string[] = [];
var currentArg = '';
while (position < tokens.length) {
var token = peek();
if (token === '"') {
currentArg += consumeQuotedString();
} else if (token.match(/\s/)) {
if (currentArg.length > 0) {
args.push(currentArg);
currentArg = '';
}
consume();
} else {
consume();
currentArg += token;
}
}
if (currentArg.length > 0) {
args.push(currentArg);
}
this.parse(args);
}
// Parse arguments as they come from the platform: split into arguments.
public parse(args: string[]) {
var position = 0;
function consume() {
return args[position++];
}
while (position < args.length) {
var current = consume();
var match = current.match(/^(--?|@)(.*)/);
var value: any = null;
if (match) {
if (match[1] === '@') {
this.parseString(this.host.readFile(match[2], null).contents);
} else {
var arg = match[2];
var option = this.findOption(arg);
if (option === null) {
this.host.standardOut.WriteLine(getDiagnosticMessage(DiagnosticCode.Unknown_compiler_option_0, [arg]));
this.host.standardOut.WriteLine(getLocalizedText(DiagnosticCode.Use_the_0_flag_to_see_options, ["--help"]));
} else {
if (!option.flag) {
value = consume();
if (value === undefined) {
// No value provided
this.host.standardOut.WriteLine(getDiagnosticMessage(DiagnosticCode.Option_0_specified_without_1, [arg, getLocalizedText(option.type, null)]));
this.host.standardOut.WriteLine(getLocalizedText(DiagnosticCode.Use_the_0_flag_to_see_options, ["--help"]));
continue;
}
}
option.set(value);
}
}
} else {
this.unnamed.push(current);
}
}
}
}
}

View File

@@ -0,0 +1,193 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export function stripStartAndEndQuotes(str: string) {
var firstCharCode = str && str.charCodeAt(0);
if (str && str.length >= 2 && firstCharCode === str.charCodeAt(str.length - 1) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) {
return str.substring(1, str.length - 1);
}
return str;
}
export function isSingleQuoted(str: string) {
return str && str.length >= 2 && str.charCodeAt(0) === str.charCodeAt(str.length - 1) && str.charCodeAt(0) === CharacterCodes.singleQuote;
}
export function isDoubleQuoted(str: string) {
return str && str.length >= 2 && str.charCodeAt(0) === str.charCodeAt(str.length - 1) && str.charCodeAt(0) === CharacterCodes.doubleQuote;
}
export function isQuoted(str: string) {
return isDoubleQuoted(str) || isSingleQuoted(str);
}
export function quoteStr(str: string) {
return "\"" + str + "\"";
}
var switchToForwardSlashesRegEx = /\\/g;
export function switchToForwardSlashes(path: string) {
return path.replace(switchToForwardSlashesRegEx, "/");
}
export function trimModName(modName: string) {
// in case's it's a declare file...
if (modName.length > 5 && modName.substring(modName.length - 5, modName.length) === ".d.ts") {
return modName.substring(0, modName.length - 5);
}
if (modName.length > 3 && modName.substring(modName.length - 3, modName.length) === ".ts") {
return modName.substring(0, modName.length - 3);
}
// in case's it's a .js file
if (modName.length > 3 && modName.substring(modName.length - 3, modName.length) === ".js") {
return modName.substring(0, modName.length - 3);
}
return modName;
}
export function getDeclareFilePath(fname: string) {
return isTSFile(fname) ? changePathToDTS(fname) : changePathToDTS(fname);
}
function isFileOfExtension(fname: string, ext: string) {
var invariantFname = fname.toLocaleUpperCase();
var invariantExt = ext.toLocaleUpperCase();
var extLength = invariantExt.length;
return invariantFname.length > extLength && invariantFname.substring(invariantFname.length - extLength, invariantFname.length) === invariantExt;
}
export function isTSFile(fname: string) {
return isFileOfExtension(fname, ".ts");
}
export function isDTSFile(fname: string) {
return isFileOfExtension(fname, ".d.ts");
}
export function getPrettyName(modPath: string, quote=true, treatAsFileName=false): any {
var modName = treatAsFileName ? switchToForwardSlashes(modPath) : trimModName(stripStartAndEndQuotes(modPath));
var components = this.getPathComponents(modName);
return components.length ? (quote ? quoteStr(components[components.length - 1]) : components[components.length - 1]) : modPath;
}
export function getPathComponents(path: string) {
return path.split("/");
}
export function getRelativePathToFixedPath(fixedModFilePath: string, absoluteModPath: string, isAbsoultePathURL = true) {
absoluteModPath = switchToForwardSlashes(absoluteModPath);
var modComponents = this.getPathComponents(absoluteModPath);
var fixedModComponents = this.getPathComponents(fixedModFilePath);
// Find the component that differs
var joinStartIndex = 0;
for (; joinStartIndex < modComponents.length && joinStartIndex < fixedModComponents.length ; joinStartIndex++) {
if (fixedModComponents[joinStartIndex] !== modComponents[joinStartIndex]) {
break;
}
}
// Get the relative path
if (joinStartIndex !== 0) {
var relativePath = "";
var relativePathComponents = modComponents.slice(joinStartIndex, modComponents.length);
for (; joinStartIndex < fixedModComponents.length; joinStartIndex++) {
if (fixedModComponents[joinStartIndex] !== "") {
relativePath = relativePath + "../";
}
}
return relativePath + relativePathComponents.join("/");
}
if (isAbsoultePathURL && absoluteModPath.indexOf("://") === -1) {
absoluteModPath = "file:///" + absoluteModPath;
}
return absoluteModPath;
}
export function changePathToDTS(modPath: string) {
return trimModName(stripStartAndEndQuotes(modPath)) + ".d.ts";
}
export function isRelative(path: string) {
return path.length > 0 && path.charAt(0) === ".";
}
export function isRooted(path: string) {
return path.length > 0 && (path.charAt(0) === "\\" || path.charAt(0) === "/" || (path.indexOf(":\\") !== -1) || (path.indexOf(":/") !== -1));
}
export function getRootFilePath(outFname: string) {
if (outFname === "") {
return outFname;
}
else {
var isPath = outFname.indexOf("/") !== -1;
return isPath ? filePath(outFname) : "";
}
}
export function filePathComponents(fullPath: string) {
fullPath = switchToForwardSlashes(fullPath);
var components = getPathComponents(fullPath);
return components.slice(0, components.length - 1);
}
export function filePath(fullPath: string) {
var path = filePathComponents(fullPath);
return path.join("/") + "/";
}
export function convertToDirectoryPath(dirPath: string) {
if (dirPath && dirPath.charAt(dirPath.length - 1) !== "/") {
dirPath += "/";
}
return dirPath;
}
var normalizePathRegEx = /^\\\\[^\\]/;
export function normalizePath(path: string): string {
// If it's a UNC style path (i.e. \\server\share), convert to a URI style (i.e. file://server/share)
if (normalizePathRegEx.test(path)) {
path = "file:" + path;
}
var parts = this.getPathComponents(switchToForwardSlashes(path));
var normalizedParts: string[] = [];
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part === ".") {
continue;
}
if (normalizedParts.length > 0 && ArrayUtilities.last(normalizedParts) !== ".." && part === "..") {
normalizedParts.pop();
continue;
}
normalizedParts.push(part);
}
return normalizedParts.join("/");
}
}

View File

@@ -0,0 +1,196 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
///
/// Preprocessing
///
export interface IPreProcessedFileInfo {
referencedFiles: IFileReference[];
importedFiles: IFileReference[];
diagnostics: Diagnostic[];
isLibFile: boolean;
}
interface ITripleSlashDirectiveProperties {
noDefaultLib: boolean;
diagnostics: Diagnostic[];
referencedFiles: IFileReference[];
}
function isNoDefaultLibMatch(comment: string): RegExpExecArray {
var isNoDefaultLibRegex = /^(\/\/\/\s*<reference\s+no-default-lib=)('|")(.+?)\2\s*\/>/gim;
return isNoDefaultLibRegex.exec(comment);
}
export var tripleSlashReferenceRegExp = /^(\/\/\/\s*<reference\s+path=)('|")(.+?)\2\s*(static=('|")(.+?)\2\s*)*\/>/;
function getFileReferenceFromReferencePath(fileName: string, text: ISimpleText, position: number, comment: string, diagnostics: Diagnostic[]): IFileReference {
// First, just see if they've written: /// <reference\s+
// If so, then we'll consider this a reference directive and we'll report errors if it's
// malformed. Otherwise, we'll completely ignore this.
var lineMap = text.lineMap();
var simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim;
if (simpleReferenceRegEx.exec(comment)) {
var isNoDefaultLib = isNoDefaultLibMatch(comment);
if (!isNoDefaultLib) {
var fullReferenceRegEx = tripleSlashReferenceRegExp;
var fullReference = fullReferenceRegEx.exec(comment);
if (!fullReference) {
// It matched the start of a reference directive, but wasn't well formed. Report
// an appropriate error to the user.
diagnostics.push(new Diagnostic(fileName, lineMap, position, comment.length, DiagnosticCode.Invalid_reference_directive_syntax));
}
else {
var path: string = normalizePath(fullReference[3]);
var adjustedPath = normalizePath(path);
var isResident = fullReference.length >= 7 && fullReference[6] === "true";
return {
line: 0,
character: 0,
position: 0,
length: 0,
path: switchToForwardSlashes(adjustedPath),
isResident: isResident
};
}
}
}
return null;
}
var reportDiagnostic = () => { };
function processImports(text: ISimpleText, scanner: Scanner.IScanner, token: ISyntaxToken, importedFiles: IFileReference[]): void {
var lineChar = { line: -1, character: -1 };
var lineMap = text.lineMap();
var start = new Date().getTime();
// Look for:
// import foo = module("foo")
while (token.kind() !== SyntaxKind.EndOfFileToken) {
if (token.kind() === SyntaxKind.ImportKeyword) {
var importToken = token;
token = scanner.scan(/*allowRegularExpression:*/ false);
if (SyntaxFacts.isIdentifierNameOrAnyKeyword(token)) {
token = scanner.scan(/*allowRegularExpression:*/ false);
if (token.kind() === SyntaxKind.EqualsToken) {
token = scanner.scan(/*allowRegularExpression:*/ false);
if (token.kind() === SyntaxKind.ModuleKeyword || token.kind() === SyntaxKind.RequireKeyword) {
token = scanner.scan(/*allowRegularExpression:*/ false);
if (token.kind() === SyntaxKind.OpenParenToken) {
token = scanner.scan(/*allowRegularExpression:*/ false);
lineMap.fillLineAndCharacterFromPosition(TypeScript.start(importToken, text), lineChar);
if (token.kind() === SyntaxKind.StringLiteral) {
var ref = {
line: lineChar.line,
character: lineChar.character,
position: TypeScript.start(token, text),
length: width(token),
path: stripStartAndEndQuotes(switchToForwardSlashes(token.text())),
isResident: false
};
importedFiles.push(ref);
}
}
}
}
}
}
token = scanner.scan(/*allowRegularExpression:*/ false);
}
var totalTime = new Date().getTime() - start;
TypeScript.fileResolutionScanImportsTime += totalTime;
}
function processTripleSlashDirectives(fileName: string, text: ISimpleText, firstToken: ISyntaxToken): ITripleSlashDirectiveProperties {
var leadingTrivia = firstToken.leadingTrivia(text);
var position = 0;
var lineChar = { line: -1, character: -1 };
var noDefaultLib = false;
var diagnostics: Diagnostic[] = [];
var referencedFiles: IFileReference[] = [];
var lineMap = text.lineMap()
for (var i = 0, n = leadingTrivia.count(); i < n; i++) {
var trivia = leadingTrivia.syntaxTriviaAt(i);
if (trivia.kind() === SyntaxKind.SingleLineCommentTrivia) {
var triviaText = trivia.fullText();
var referencedCode = getFileReferenceFromReferencePath(fileName, text, position, triviaText, diagnostics);
if (referencedCode) {
lineMap.fillLineAndCharacterFromPosition(position, lineChar);
referencedCode.position = position;
referencedCode.length = trivia.fullWidth();
referencedCode.line = lineChar.line;
referencedCode.character = lineChar.character;
referencedFiles.push(referencedCode);
}
// is it a lib file?
var isNoDefaultLib = isNoDefaultLibMatch(triviaText);
if (isNoDefaultLib) {
noDefaultLib = isNoDefaultLib[3] === "true";
}
}
position += trivia.fullWidth();
}
return { noDefaultLib: noDefaultLib, diagnostics: diagnostics, referencedFiles: referencedFiles };
}
export function preProcessFile(fileName: string, sourceText: IScriptSnapshot, readImportFiles = true): IPreProcessedFileInfo {
var text = SimpleText.fromScriptSnapshot(sourceText);
var scanner = Scanner.createScanner(ts.ScriptTarget.ES5, text, reportDiagnostic);
var firstToken = scanner.scan(/*allowRegularExpression:*/ false);
// only search out dynamic mods
// if you find a dynamic mod, ignore every other mod inside, until you balance rcurlies
// var position
var importedFiles: IFileReference[] = [];
if (readImportFiles) {
processImports(text, scanner, firstToken, importedFiles);
}
var properties = processTripleSlashDirectives(fileName, text, firstToken);
return { referencedFiles: properties.referencedFiles, importedFiles: importedFiles, isLibFile: properties.noDefaultLib, diagnostics: properties.diagnostics };
}
export function getReferencedFiles(fileName: string, sourceText: IScriptSnapshot): IFileReference[] {
return preProcessFile(fileName, sourceText, false).referencedFiles;
}
} // Tools

View File

@@ -0,0 +1,17 @@
//declare module process {
// export var argv: string[];
// export var platform: string;
// export function on(event: string, handler: (arg: any) => void ): void;
// export module stdout {
// export function write(str: string): any;
// export function on(event: string, action: () => void ): void;
// }
// export module stderr {
// export function write(str: string): any;
// export function on(event: string, action: () => void): void;
// }
// export module mainModule {
// export var filename: string;
// }
// export function exit(exitCode?: number): any;
//}

View File

@@ -0,0 +1,28 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
// Note: This is being using by the host (VS) and is marshaled back and forth. When changing this make sure the changes
// are reflected in the managed side as well.
export interface IFileReference extends ILineAndCharacter {
path: string;
isResident: boolean;
position: number;
length: number;
}
}

View File

@@ -0,0 +1,255 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export interface IResolvedFile {
path: string;
referencedFiles: string[];
importedFiles: string[];
}
export interface IReferenceResolverHost {
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot;
resolveRelativePath(path: string, directory: string): string;
fileExists(path: string): boolean;
directoryExists(path: string): boolean;
getParentDirectory(path: string): string;
}
export class ReferenceResolutionResult {
resolvedFiles: IResolvedFile[] = [];
diagnostics: TypeScript.Diagnostic[] = [];
seenNoDefaultLibTag: boolean = false;
}
class ReferenceLocation {
constructor(public filePath: string, public lineMap: LineMap, public position: number, public length: number, public isImported: boolean) {
}
}
export class ReferenceResolver {
private inputFileNames: string[];
private host: IReferenceResolverHost;
private visited: IIndexable<string>;
constructor(inputFileNames: string[], host: IReferenceResolverHost, private useCaseSensitiveFileResolution: boolean) {
this.inputFileNames = inputFileNames;
this.host = host;
this.visited = {};
}
public static resolve(inputFileNames: string[], host: IReferenceResolverHost, useCaseSensitiveFileResolution: boolean): ReferenceResolutionResult {
var resolver = new ReferenceResolver(inputFileNames, host, useCaseSensitiveFileResolution);
return resolver.resolveInputFiles();
}
public resolveInputFiles(): ReferenceResolutionResult {
var result = new ReferenceResolutionResult();
if (!this.inputFileNames || this.inputFileNames.length <= 0) {
// Nothing to do.
return result;
}
// Loop over the files and extract references
var referenceLocation = new ReferenceLocation(null, null, 0, 0, false);
this.inputFileNames.forEach(fileName =>
this.resolveIncludedFile(fileName, referenceLocation, result));
return result;
}
private resolveIncludedFile(path: string, referenceLocation: ReferenceLocation, resolutionResult: ReferenceResolutionResult): string {
var normalizedPath = this.getNormalizedFilePath(path, referenceLocation.filePath);
if (this.isSameFile(normalizedPath, referenceLocation.filePath)) {
// Cannot reference self
if (!referenceLocation.isImported) {
resolutionResult.diagnostics.push(
new TypeScript.Diagnostic(referenceLocation.filePath, referenceLocation.lineMap,
referenceLocation.position, referenceLocation.length, DiagnosticCode.A_file_cannot_have_a_reference_to_itself, null));
}
return normalizedPath;
}
if (!isTSFile(normalizedPath) && !isDTSFile(normalizedPath)) {
var dtsFile = normalizedPath + ".d.ts";
var tsFile = normalizedPath + ".ts";
if (this.host.fileExists(tsFile)) {
normalizedPath = tsFile;
}
else {
normalizedPath = dtsFile;
}
}
if (!this.host.fileExists(normalizedPath)) {
if (!referenceLocation.isImported) {
resolutionResult.diagnostics.push(
new TypeScript.Diagnostic(referenceLocation.filePath, referenceLocation.lineMap,
referenceLocation.position, referenceLocation.length, DiagnosticCode.Cannot_resolve_referenced_file_0, [path]));
}
return normalizedPath;
}
// Preprocess the file and resolve its imports/references
return this.resolveFile(normalizedPath, resolutionResult);
}
private resolveImportedFile(path: string, referenceLocation: ReferenceLocation, resolutionResult: ReferenceResolutionResult): string {
var isRelativePath = TypeScript.isRelative(path);
var isRootedPath = isRelativePath ? false : isRooted(path);
if (isRelativePath || isRootedPath) {
// Handle as a normal include file
return this.resolveIncludedFile(path, referenceLocation, resolutionResult);
}
else {
// Search for the file
var parentDirectory = this.host.getParentDirectory(referenceLocation.filePath);
var searchFilePath: string = null;
var dtsFileName = path + ".d.ts";
var tsFilePath = path + ".ts";
var start = new Date().getTime();
// SPEC: Nov 18
// An external import declaration that specifies a relative external module name (section 11.2.1) resolves the name
// relative to the directory of the containing source file.
// If a source file with the resulting path and file extension '.ts' exists, that file is added as a dependency.
// Otherwise, if a source file with the resulting path and file extension '.d.ts' exists, that file is added as a dependency.
do {
// Search for ".ts" file first
currentFilePath = this.host.resolveRelativePath(tsFilePath, parentDirectory);
if (this.host.fileExists(currentFilePath)) {
// Found the file
searchFilePath = currentFilePath;
break;
}
// Search for ".d.ts" file
var currentFilePath = this.host.resolveRelativePath(dtsFileName, parentDirectory);
if (this.host.fileExists(currentFilePath)) {
// Found the file
searchFilePath = currentFilePath;
break;
}
parentDirectory = this.host.getParentDirectory(parentDirectory);
}
while (parentDirectory);
TypeScript.fileResolutionImportFileSearchTime += new Date().getTime() - start;
if (!searchFilePath) {
// Cannot find file import, do not reprot an error, the typeChecker will report it later on
return path;
}
// Preprocess the file and resolve its imports/references
return this.resolveFile(searchFilePath, resolutionResult);
}
}
private resolveFile(normalizedPath: string, resolutionResult: ReferenceResolutionResult): string {
// If we have processed this file before, skip it
var visitedPath = this.isVisited(normalizedPath);
if (!visitedPath) {
// Record that we have seen it
this.recordVisitedFile(normalizedPath);
// Preprocess the file
var start = new Date().getTime();
var scriptSnapshot = this.host.getScriptSnapshot(normalizedPath);
var totalTime = new Date().getTime() - start;
TypeScript.fileResolutionIOTime += totalTime;
var lineMap = LineMap1.fromScriptSnapshot(scriptSnapshot);
var preprocessedFileInformation = TypeScript.preProcessFile(normalizedPath, scriptSnapshot);
resolutionResult.diagnostics.push.apply(resolutionResult.diagnostics, preprocessedFileInformation.diagnostics);
// If this file has a "no-default-lib = 'true'" tag
if (preprocessedFileInformation.isLibFile) {
resolutionResult.seenNoDefaultLibTag = true;
}
// Resolve explicit references
var normalizedReferencePaths: string[] = [];
preprocessedFileInformation.referencedFiles.forEach(fileReference => {
var currentReferenceLocation = new ReferenceLocation(normalizedPath, lineMap, fileReference.position, fileReference.length, /* isImported */ false);
var normalizedReferencePath = this.resolveIncludedFile(fileReference.path, currentReferenceLocation, resolutionResult);
normalizedReferencePaths.push(normalizedReferencePath);
});
// Resolve imports
var normalizedImportPaths: string[] = [];
for (var i = 0; i < preprocessedFileInformation.importedFiles.length; i++) {
var fileImport = preprocessedFileInformation.importedFiles[i];
var currentReferenceLocation = new ReferenceLocation(normalizedPath, lineMap, fileImport.position, fileImport.length, /* isImported */ true);
var normalizedImportPath = this.resolveImportedFile(fileImport.path, currentReferenceLocation, resolutionResult);
normalizedImportPaths.push(normalizedImportPath);
}
// Add the file to the result list
resolutionResult.resolvedFiles.push({
path: normalizedPath,
referencedFiles: normalizedReferencePaths,
importedFiles: normalizedImportPaths
});
}
else {
normalizedPath = visitedPath;
}
return normalizedPath;
}
private getNormalizedFilePath(path: string, parentFilePath: string): string {
var parentFileDirectory = parentFilePath ? this.host.getParentDirectory(parentFilePath) : "";
var normalizedPath = this.host.resolveRelativePath(path, parentFileDirectory);
return normalizedPath;
}
private getUniqueFileId(filePath: string): string {
return this.useCaseSensitiveFileResolution ? filePath : filePath.toLocaleUpperCase();
}
private recordVisitedFile(filePath: string): void {
this.visited[this.getUniqueFileId(filePath)] = filePath;
}
private isVisited(filePath: string): string {
return this.visited[this.getUniqueFileId(filePath)];
}
private isSameFile(filePath1: string, filePath2: string): boolean {
if (!filePath1 || !filePath2) {
return false;
}
if (this.useCaseSensitiveFileResolution) {
return filePath1 === filePath2;
}
else {
return filePath1.toLocaleUpperCase() === filePath2.toLocaleUpperCase();
}
}
}
}

View File

@@ -0,0 +1,37 @@
/////<reference path='resources\references.ts' />
/////<reference path='core\references.ts' />
/////<reference path='text\references.ts' />
/////<reference path='syntax\references.ts' />
/////<reference path='diagnostics.ts' />
/////<reference path='document.ts' />
/////<reference path='flags.ts' />
/////<reference path='hashTable.ts' />
/////<reference path='ast.ts' />
/////<reference path='astHelpers.ts' />
/////<reference path='astWalker.ts' />
/////<reference path='base64.ts' />
/////<reference path='sourceMapping.ts' />
/////<reference path='emitter.ts' />
/////<reference path='types.ts' />
/////<reference path='pathUtils.ts' />
/////<reference path='referenceResolution.ts' />
/////<reference path='precompile.ts' />
/////<reference path='referenceResolver.ts' />
/////<reference path='declarationEmitter.ts' />
/////<reference path='bloomFilter.ts' />
/////<reference path='identifierWalker.ts' />
/////<reference path='settings.ts' />
/////<reference path='typecheck\pullFlags.ts' />
/////<reference path='typecheck\pullDecls.ts' />
/////<reference path='typecheck\pullSymbols.ts' />
/////<reference path='typecheck\pullTypeEnclosingTypeWalker.ts' />
/////<reference path='typecheck\pullTypeResolutionContext.ts' />
/////<reference path='typecheck\pullTypeResolution.ts' />
/////<reference path='typecheck\pullSemanticInfo.ts' />
/////<reference path='typecheck\pullDeclCollection.ts' />
/////<reference path='typecheck\pullSymbolBinder.ts' />
/////<reference path='typecheck\pullHelpers.ts' />
/////<reference path='typecheck\pullInstantiationHelpers.ts' />
/////<reference path='typecheck\pullTypeInstantiation.ts' />
/////<reference path='typescript.ts' />

View File

@@ -0,0 +1,15 @@
///<reference path='references.ts' />
module TypeScript {
export function settingsChangeAffectsSyntax(before: ts.CompilerOptions, after: ts.CompilerOptions): boolean {
// If the automatic semicolon insertion option has changed, then we have to dump all
// syntax trees in order to reparse them with the new option.
//
// If the language version changed, then that affects what types of things we parse. So
// we have to dump all syntax trees.
//
// If propagateEnumConstants changes, then that affects the constant value data we've
// stored in the ISyntaxElement.
return before.module !== after.module || before.target !== after.target;
}
}

View File

@@ -0,0 +1,270 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export class SourceMapPosition {
public sourceLine: number;
public sourceColumn: number;
public emittedLine: number;
public emittedColumn: number;
}
export class SourceMapping {
public start = new SourceMapPosition();
public end = new SourceMapPosition();
public nameIndex: number = -1;
public childMappings: SourceMapping[] = [];
}
export class SourceMapEntry {
constructor(
public emittedFile: string,
public emittedLine: number,
public emittedColumn: number,
public sourceFile: string,
public sourceLine: number,
public sourceColumn: number,
public sourceName: string) {
Debug.assert(isFinite(emittedLine));
Debug.assert(isFinite(emittedColumn));
Debug.assert(isFinite(sourceColumn));
Debug.assert(isFinite(sourceLine));
}
}
export class SourceMapper {
static MapFileExtension = ".map";
private jsFileName: string;
private sourceMapPath: string;
private sourceMapDirectory: string;
private sourceRoot: string;
public names: string[] = [];
private mappingLevel: ISpan[] = [];
// Below two arrays represent the information about sourceFile at that index.
private tsFilePaths: string[] = [];
private allSourceMappings: SourceMapping[][] = [];
public currentMappings: SourceMapping[][];
public currentNameIndex: number[];
private sourceMapEntries: SourceMapEntry[] = [];
constructor(private jsFile: TextWriter,
private sourceMapOut: TextWriter,
document: Document,
jsFilePath: string,
emitOptions: EmitOptions,
resolvePath: (path: string) => string) {
this.setSourceMapOptions(document, jsFilePath, emitOptions, resolvePath);
this.setNewSourceFile(document, emitOptions);
}
public getOutputFile(): OutputFile {
var result = this.sourceMapOut.getOutputFile();
result.sourceMapEntries = this.sourceMapEntries;
return result;
}
public increaseMappingLevel(ast: ISpan) {
this.mappingLevel.push(ast);
}
public decreaseMappingLevel(ast: any) {
Debug.assert(this.mappingLevel.length > 0, "Mapping level should never be less than 0. This suggests a missing start call.");
var expectedAst = this.mappingLevel.pop();
if (ast !== expectedAst) {
var expectedAstInfo: any = (<any>expectedAst).kind ? SyntaxKind[(<any>expectedAst).kind] : [expectedAst.start(), expectedAst.end()];
var astInfo: any = (<any>ast).kind ? SyntaxKind[(<any>ast).kind] : [ast.start(), ast.end()]
Debug.fail(
"Provided ast is not the expected ISyntaxElement, Expected: " + expectedAstInfo + " Given: " + astInfo)
}
}
public setNewSourceFile(document: Document, emitOptions: EmitOptions) {
// Set new mappings
var sourceMappings: SourceMapping[] = [];
this.allSourceMappings.push(sourceMappings);
this.currentMappings = [sourceMappings];
this.currentNameIndex = [];
// Set new source file path
this.setNewSourceFilePath(document, emitOptions);
}
private setSourceMapOptions(document: Document, jsFilePath: string, emitOptions: EmitOptions, resolvePath: (path: string) => string) {
// Decode mapRoot and sourceRoot
// Js File Name = pretty name of js file
var prettyJsFileName = TypeScript.getPrettyName(jsFilePath, false, true);
var prettyMapFileName = prettyJsFileName + SourceMapper.MapFileExtension;
this.jsFileName = prettyJsFileName;
// Figure out sourceMapPath and sourceMapDirectory
if (emitOptions.sourceMapRootDirectory()) {
// Get the sourceMap Directory
this.sourceMapDirectory = emitOptions.sourceMapRootDirectory();
if (document.emitToOwnOutputFile()) {
// For modules or multiple emit files the mapRoot will have directory structure like the sources
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
this.sourceMapDirectory = this.sourceMapDirectory + switchToForwardSlashes(getRootFilePath((document.fileName)).replace(emitOptions.commonDirectoryPath(), ""));
}
if (isRelative(this.sourceMapDirectory)) {
// The relative paths are relative to the common directory
this.sourceMapDirectory = emitOptions.commonDirectoryPath() + this.sourceMapDirectory;
this.sourceMapDirectory = convertToDirectoryPath(switchToForwardSlashes(resolvePath(this.sourceMapDirectory)));
this.sourceMapPath = getRelativePathToFixedPath(getRootFilePath(jsFilePath), this.sourceMapDirectory + prettyMapFileName);
}
else {
this.sourceMapPath = this.sourceMapDirectory + prettyMapFileName;
}
}
else {
this.sourceMapPath = prettyMapFileName;
this.sourceMapDirectory = getRootFilePath(jsFilePath);
}
this.sourceRoot = emitOptions.sourceRootDirectory();
}
private setNewSourceFilePath(document: Document, emitOptions: EmitOptions) {
var tsFilePath = switchToForwardSlashes(document.fileName);
if (emitOptions.sourceRootDirectory()) {
// Use the relative path corresponding to the common directory path
tsFilePath = getRelativePathToFixedPath(emitOptions.commonDirectoryPath(), tsFilePath);
}
else {
// Source locations relative to map file location
tsFilePath = getRelativePathToFixedPath(this.sourceMapDirectory, tsFilePath);
}
this.tsFilePaths.push(tsFilePath);
}
// Generate source mapping.
// Creating files can cause exceptions, they will be caught higher up in TypeScriptCompiler.emit
public emitSourceMapping(): void {
Debug.assert(
this.mappingLevel.length === 0,
"Mapping level is not 0. This suggest a missing end call. Value: " +
this.mappingLevel.map(item => ['Node of type', SyntaxKind[(<any>item).kind], 'at', item.start(), 'to', item.end()].join(' ')).join(', '));
// Output map file name into the js file
this.jsFile.WriteLine("//# sourceMappingURL=" + this.sourceMapPath);
// Now output map file
var mappingsString = "";
var prevEmittedColumn = 0;
var prevEmittedLine = 0;
var prevSourceColumn = 0;
var prevSourceLine = 0;
var prevSourceIndex = 0;
var prevNameIndex = 0;
var emitComma = false;
var recordedPosition: SourceMapPosition = null;
for (var sourceIndex = 0; sourceIndex < this.tsFilePaths.length; sourceIndex++) {
var recordSourceMapping = (mappedPosition: SourceMapPosition, nameIndex: number) => {
if (recordedPosition !== null &&
recordedPosition.emittedColumn === mappedPosition.emittedColumn &&
recordedPosition.emittedLine === mappedPosition.emittedLine) {
// This position is already recorded
return;
}
// Record this position
if (prevEmittedLine !== mappedPosition.emittedLine) {
while (prevEmittedLine < mappedPosition.emittedLine) {
prevEmittedColumn = 0;
mappingsString = mappingsString + ";";
prevEmittedLine++;
}
emitComma = false;
}
else if (emitComma) {
mappingsString = mappingsString + ",";
}
this.sourceMapEntries.push(new SourceMapEntry(
this.jsFileName,
mappedPosition.emittedLine + 1,
mappedPosition.emittedColumn + 1,
this.tsFilePaths[sourceIndex],
mappedPosition.sourceLine,
mappedPosition.sourceColumn + 1,
nameIndex >= 0 ? this.names[nameIndex] : undefined));
// 1. Relative Column
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.emittedColumn - prevEmittedColumn);
prevEmittedColumn = mappedPosition.emittedColumn;
// 2. Relative sourceIndex
mappingsString = mappingsString + Base64VLQFormat.encode(sourceIndex - prevSourceIndex);
prevSourceIndex = sourceIndex;
// 3. Relative sourceLine 0 based
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.sourceLine - 1 - prevSourceLine);
prevSourceLine = mappedPosition.sourceLine - 1;
// 4. Relative sourceColumn 0 based
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.sourceColumn - prevSourceColumn);
prevSourceColumn = mappedPosition.sourceColumn;
// 5. Relative namePosition 0 based
if (nameIndex >= 0) {
mappingsString = mappingsString + Base64VLQFormat.encode(nameIndex - prevNameIndex);
prevNameIndex = nameIndex;
}
emitComma = true;
recordedPosition = mappedPosition;
};
// Record starting spans
var recordSourceMappingSiblings = (sourceMappings: SourceMapping[]) => {
for (var i = 0; i < sourceMappings.length; i++) {
var sourceMapping = sourceMappings[i];
recordSourceMapping(sourceMapping.start, sourceMapping.nameIndex);
recordSourceMappingSiblings(sourceMapping.childMappings);
recordSourceMapping(sourceMapping.end, sourceMapping.nameIndex);
}
};
recordSourceMappingSiblings(this.allSourceMappings[sourceIndex]);
}
// Write the actual map file
this.sourceMapOut.Write(JSON.stringify({
version: 3,
file: this.jsFileName,
sourceRoot: this.sourceRoot,
sources: this.tsFilePaths,
names: this.names,
mappings: mappingsString
}));
// Closing files could result in exceptions, report them if they occur
this.sourceMapOut.Close();
}
}
}

View File

@@ -0,0 +1,736 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='typescript.ts'/>
///<reference path='io.ts'/>
///<reference path='optionsParser.ts'/>
module TypeScript {
class SourceFile {
constructor(public scriptSnapshot: IScriptSnapshot, public byteOrderMark: ByteOrderMark) {
}
}
class DiagnosticsLogger implements ILogger {
constructor(public ioHost: IEnvironment) {
}
public information(): boolean { return false; }
public debug(): boolean { return false; }
public warning(): boolean { return false; }
public error(): boolean { return false; }
public fatal(): boolean { return false; }
public log(s: string): void {
this.ioHost.standardOut.WriteLine(s);
}
}
export class BatchCompiler implements IReferenceResolverHost {
public compilerVersion = "1.0.1.0";
private inputFiles: string[] = [];
private compilationSettings: ImmutableCompilationSettings;
private resolvedFiles: IResolvedFile[] = [];
private fileNameToSourceFile = new StringHashTable<SourceFile>();
private hasErrors: boolean = false;
private logger: ILogger = null;
constructor(private ioHost: IEnvironment) {
}
// Begin batch compilation
public batchCompile() {
// Parse command line options
if (this.parseOptions()) {
var start = new Date().getTime();
if (this.compilationSettings.gatherDiagnostics()) {
this.logger = new DiagnosticsLogger(this.ioHost);
} else {
this.logger = new NullLogger();
}
if (this.compilationSettings.watch()) {
// Watch will cause the program to stick around as long as the files exist
this.watchFiles();
return;
}
// Resolve the compilation environemnt
this.resolve();
this.compile();
if (this.compilationSettings.gatherDiagnostics()) {
this.logger.log("");
this.logger.log("File resolution time: " + TypeScript.fileResolutionTime);
this.logger.log(" file read: " + TypeScript.fileResolutionIOTime);
this.logger.log(" scan imports: " + TypeScript.fileResolutionScanImportsTime);
this.logger.log(" import search: " + TypeScript.fileResolutionImportFileSearchTime);
this.logger.log(" get lib.d.ts: " + TypeScript.fileResolutionGetDefaultLibraryTime);
this.logger.log("SyntaxTree parse time: " + TypeScript.syntaxTreeParseTime);
this.logger.log("Syntax Diagnostics time: " + TypeScript.syntaxDiagnosticsTime);
this.logger.log("Create declarations time: " + TypeScript.createDeclarationsTime);
this.logger.log("");
this.logger.log("Type check time: " + TypeScript.typeCheckTime);
this.logger.log("");
this.logger.log("Emit time: " + TypeScript.emitTime);
this.logger.log("Declaration emit time: " + TypeScript.declarationEmitTime);
this.logger.log("Total number of symbols created: " + TypeScript.pullSymbolID);
this.logger.log("Specialized types created: " + TypeScript.nSpecializationsCreated);
this.logger.log("Specialized signatures created: " + TypeScript.nSpecializedSignaturesCreated);
this.logger.log(" IsExternallyVisibleTime: " + TypeScript.declarationEmitIsExternallyVisibleTime);
this.logger.log(" TypeSignatureTime: " + TypeScript.declarationEmitTypeSignatureTime);
this.logger.log(" GetBoundDeclTypeTime: " + TypeScript.declarationEmitGetBoundDeclTypeTime);
this.logger.log(" IsOverloadedCallSignatureTime: " + TypeScript.declarationEmitIsOverloadedCallSignatureTime);
this.logger.log(" FunctionDeclarationGetSymbolTime: " + TypeScript.declarationEmitFunctionDeclarationGetSymbolTime);
this.logger.log(" GetBaseTypeTime: " + TypeScript.declarationEmitGetBaseTypeTime);
this.logger.log(" GetAccessorFunctionTime: " + TypeScript.declarationEmitGetAccessorFunctionTime);
this.logger.log(" GetTypeParameterSymbolTime: " + TypeScript.declarationEmitGetTypeParameterSymbolTime);
this.logger.log(" GetImportDeclarationSymbolTime: " + TypeScript.declarationEmitGetImportDeclarationSymbolTime);
this.logger.log("Emit write file time: " + TypeScript.emitWriteFileTime);
this.logger.log("Compiler resolve path time: " + TypeScript.compilerResolvePathTime);
this.logger.log("Compiler directory name time: " + TypeScript.compilerDirectoryNameTime);
this.logger.log("Compiler directory exists time: " + TypeScript.compilerDirectoryExistsTime);
this.logger.log("Compiler file exists time: " + TypeScript.compilerFileExistsTime);
this.logger.log("IO host resolve path time: " + TypeScript.ioHostResolvePathTime);
this.logger.log("IO host directory name time: " + TypeScript.ioHostDirectoryNameTime);
this.logger.log("IO host create directory structure time: " + TypeScript.ioHostCreateDirectoryStructureTime);
this.logger.log("IO host write file time: " + TypeScript.ioHostWriteFileTime);
this.logger.log("Node make directory time: " + TypeScript.nodeMakeDirectoryTime);
this.logger.log("Node writeFileSync time: " + TypeScript.nodeWriteFileSyncTime);
this.logger.log("Node createBuffer time: " + TypeScript.nodeCreateBufferTime);
this.logger.log("Total time: " + (new Date().getTime() - start));
}
}
// Exit with the appropriate error code
this.ioHost.quit(this.hasErrors ? 1 : 0);
}
private resolve() {
// Resolve file dependencies, if requested
var includeDefaultLibrary = !this.compilationSettings.noLib();
var resolvedFiles: IResolvedFile[] = [];
var start = new Date().getTime();
if (!this.compilationSettings.noResolve()) {
// Resolve references
var resolutionResults = ReferenceResolver.resolve(this.inputFiles, this, this.compilationSettings.useCaseSensitiveFileResolution());
resolvedFiles = resolutionResults.resolvedFiles;
// Only include the library if useDefaultLib is set to true and did not see any 'no-default-lib' comments
includeDefaultLibrary = !this.compilationSettings.noLib() && !resolutionResults.seenNoDefaultLibTag;
// Populate any diagnostic messages generated during resolution
resolutionResults.diagnostics.forEach(d => this.addDiagnostic(d));
}
else {
for (var i = 0, n = this.inputFiles.length; i < n; i++) {
var inputFile = this.inputFiles[i];
var referencedFiles: string[] = [];
var importedFiles: string[] = [];
// If declaration files are going to be emitted, preprocess the file contents and add in referenced files as well
if (this.compilationSettings.generateDeclarationFiles()) {
var references = getReferencedFiles(inputFile, this.getScriptSnapshot(inputFile));
for (var j = 0; j < references.length; j++) {
referencedFiles.push(references[j].path);
}
inputFile = this.resolvePath(inputFile);
}
resolvedFiles.push({
path: inputFile,
referencedFiles: referencedFiles,
importedFiles: importedFiles
});
}
}
var defaultLibStart = new Date().getTime();
if (includeDefaultLibrary) {
var libraryResolvedFile: IResolvedFile = {
path: this.getDefaultLibraryFilePath(),
referencedFiles: [],
importedFiles: []
};
// Prepend the library to the resolved list
resolvedFiles = [libraryResolvedFile].concat(resolvedFiles);
}
TypeScript.fileResolutionGetDefaultLibraryTime += new Date().getTime() - defaultLibStart;
this.resolvedFiles = resolvedFiles;
TypeScript.fileResolutionTime = new Date().getTime() - start;
}
// Returns true if compilation failed from some reason.
private compile(): void {
var compiler = new TypeScriptCompiler(this.logger, this.compilationSettings);
this.resolvedFiles.forEach(resolvedFile => {
var sourceFile = this.getSourceFile(resolvedFile.path);
compiler.addFile(resolvedFile.path, sourceFile.scriptSnapshot, sourceFile.byteOrderMark, /*version:*/ 0, /*isOpen:*/ false, resolvedFile.referencedFiles);
});
for (var it = compiler.compile((path: string) => this.resolvePath(path)); it.moveNext();) {
var result = it.current();
result.diagnostics.forEach(d => this.addDiagnostic(d));
if (!this.tryWriteOutputFiles(result.outputFiles)) {
return;
}
}
}
// Parse command line options
private parseOptions() {
var opts = new OptionsParser(this.ioHost, this.compilerVersion);
var mutableSettings = new CompilationSettings();
opts.option('out', {
usage: {
locCode: DiagnosticCode.Concatenate_and_emit_output_to_single_file,
args: null
},
type: DiagnosticCode.file2,
set: (str) => {
mutableSettings.outFileOption = str;
}
});
opts.option('outDir', {
usage: {
locCode: DiagnosticCode.Redirect_output_structure_to_the_directory,
args: null
},
type: DiagnosticCode.DIRECTORY,
set: (str) => {
mutableSettings.outDirOption = str;
}
});
opts.flag('sourcemap', {
usage: {
locCode: DiagnosticCode.Generates_corresponding_0_file,
args: ['.map']
},
set: () => {
mutableSettings.mapSourceFiles = true;
}
});
opts.option('mapRoot', {
usage: {
locCode: DiagnosticCode.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
args: null
},
type: DiagnosticCode.LOCATION,
set: (str) => {
mutableSettings.mapRoot = str;
}
});
opts.option('sourceRoot', {
usage: {
locCode: DiagnosticCode.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
args: null
},
type: DiagnosticCode.LOCATION,
set: (str) => {
mutableSettings.sourceRoot = str;
}
});
opts.flag('declaration', {
usage: {
locCode: DiagnosticCode.Generates_corresponding_0_file,
args: ['.d.ts']
},
set: () => {
mutableSettings.generateDeclarationFiles = true;
}
}, 'd');
if (this.ioHost.watchFile) {
opts.flag('watch', {
usage: {
locCode: DiagnosticCode.Watch_input_files,
args: null
},
set: () => {
mutableSettings.watch = true;
}
}, 'w');
}
opts.flag('propagateEnumConstants', {
experimental: true,
set: () => { mutableSettings.propagateEnumConstants = true; }
});
opts.flag('removeComments', {
usage: {
locCode: DiagnosticCode.Do_not_emit_comments_to_output,
args: null
},
set: () => {
mutableSettings.removeComments = true;
}
});
opts.flag('noResolve', {
experimental: true,
usage: {
locCode: DiagnosticCode.Skip_resolution_and_preprocessing,
args: null
},
set: () => {
mutableSettings.noResolve = true;
}
});
opts.flag('noLib', {
experimental: true,
set: () => {
mutableSettings.noLib = true;
}
});
opts.flag('diagnostics', {
experimental: true,
set: () => {
mutableSettings.gatherDiagnostics = true;
}
});
opts.option('target', {
usage: {
locCode: DiagnosticCode.Specify_ECMAScript_target_version_0_default_or_1,
args: ['ES3', 'ES5']
},
type: DiagnosticCode.VERSION,
set: (type) => {
type = type.toLowerCase();
if (type === 'es3') {
mutableSettings.codeGenTarget = LanguageVersion.EcmaScript3;
}
else if (type === 'es5') {
mutableSettings.codeGenTarget = LanguageVersion.EcmaScript5;
}
else {
this.addDiagnostic(
new Diagnostic(null, null, 0, 0, DiagnosticCode.Argument_for_0_option_must_be_1_or_2, ["target", "ES3", "ES5"]));
}
}
}, 't');
opts.option('module', {
usage: {
locCode: DiagnosticCode.Specify_module_code_generation_0_or_1,
args: ['commonjs', 'amd']
},
type: DiagnosticCode.KIND,
set: (type) => {
type = type.toLowerCase();
if (type === 'commonjs') {
mutableSettings.moduleGenTarget = ModuleGenTarget.Synchronous;
}
else if (type === 'amd') {
mutableSettings.moduleGenTarget = ModuleGenTarget.Asynchronous;
}
else {
this.addDiagnostic(
new Diagnostic(null, null, 0, 0, DiagnosticCode.Argument_for_0_option_must_be_1_or_2, ["module", "commonjs", "amd"]));
}
}
}, 'm');
var needsHelp = false;
opts.flag('help', {
usage: {
locCode: DiagnosticCode.Print_this_message,
args: null
},
set: () => {
needsHelp = true;
}
}, 'h');
opts.flag('useCaseSensitiveFileResolution', {
experimental: true,
set: () => {
mutableSettings.useCaseSensitiveFileResolution = true;
}
});
var shouldPrintVersionOnly = false;
opts.flag('version', {
usage: {
locCode: DiagnosticCode.Print_the_compiler_s_version_0,
args: [this.compilerVersion]
},
set: () => {
shouldPrintVersionOnly = true;
}
}, 'v');
var locale: string = null;
opts.option('locale', {
experimental: true,
usage: {
locCode: DiagnosticCode.Specify_locale_for_errors_and_messages_For_example_0_or_1,
args: ['en', 'ja-jp']
},
type: DiagnosticCode.STRING,
set: (value) => {
locale = value;
}
});
opts.flag('noImplicitAny', {
usage: {
locCode: DiagnosticCode.Warn_on_expressions_and_declarations_with_an_implied_any_type,
args: null
},
set: () => {
mutableSettings.noImplicitAny = true;
}
});
if (Environment.supportsCodePage()) {
opts.option('codepage', {
usage: {
locCode: DiagnosticCode.Specify_the_codepage_to_use_when_opening_source_files,
args: null
},
type: DiagnosticCode.NUMBER,
set: (arg) => {
mutableSettings.codepage = parseInt(arg, 10);
}
});
}
opts.parse(this.ioHost.arguments);
this.compilationSettings = ImmutableCompilationSettings.fromCompilationSettings(mutableSettings);
if (locale) {
if (!this.setLocale(locale)) {
return false;
}
}
this.inputFiles.push.apply(this.inputFiles, opts.unnamed);
if (shouldPrintVersionOnly) {
opts.printVersion();
return false;
}
// If no source files provided to compiler - print usage information
else if (this.inputFiles.length === 0 || needsHelp) {
opts.printUsage();
return false;
}
return !this.hasErrors;
}
private setLocale(locale: string): boolean {
var matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
if (!matchResult) {
this.addDiagnostic(new Diagnostic(null, null, 0, 0, DiagnosticCode.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, ['en', 'ja-jp']));
return false;
}
var language = matchResult[1];
var territory = matchResult[3];
// First try the entire locale, then fall back to just language if that's all we have.
if (!this.setLanguageAndTerritory(language, territory) &&
!this.setLanguageAndTerritory(language, null)) {
this.addDiagnostic(new Diagnostic(null, null, 0, 0, DiagnosticCode.Unsupported_locale_0, [locale]));
return false;
}
return true;
}
private setLanguageAndTerritory(language: string, territory: string): boolean {
var compilerFilePath = this.ioHost.executingFilePath();
var containingDirectoryPath = this.ioHost.directoryName(compilerFilePath);
var filePath = IOUtils.combine(containingDirectoryPath, language);
if (territory) {
filePath = filePath + "-" + territory;
}
filePath = this.resolvePath(IOUtils.combine(filePath, "diagnosticMessages.generated.json"));
if (!this.fileExists(filePath)) {
return false;
}
var fileContents = this.ioHost.readFile(filePath, this.compilationSettings.codepage());
TypeScript.LocalizedDiagnosticMessages = JSON.parse(fileContents.contents);
return true;
}
// Handle -watch switch
private watchFiles() {
if (!this.ioHost.watchFile) {
this.addDiagnostic(
new Diagnostic(null, null, 0, 0, DiagnosticCode.Current_host_does_not_support_0_option, ['-w[atch]']));
return;
}
var lastResolvedFileSet: string[] = []
var watchers: { [x: string]: IFileWatcher; } = {};
var firstTime = true;
var addWatcher = (fileName: string) => {
if (!watchers[fileName]) {
var watcher = this.ioHost.watchFile(fileName, onWatchedFileChange);
watchers[fileName] = watcher;
}
};
var removeWatcher = (fileName: string) => {
if (watchers[fileName]) {
watchers[fileName].close();
delete watchers[fileName];
}
};
var onWatchedFileChange = () => {
// Clean errors for previous compilation
this.hasErrors = false;
// Clear out any source file data we've cached.
this.fileNameToSourceFile = new StringHashTable<SourceFile>();
// Resolve file dependencies, if requested
this.resolve();
// Check if any new files were added to the environment as a result of the file change
var oldFiles = lastResolvedFileSet;
var newFiles = this.resolvedFiles.map(resolvedFile => resolvedFile.path).sort();
var i = 0, j = 0;
while (i < oldFiles.length && j < newFiles.length) {
var compareResult = oldFiles[i].localeCompare(newFiles[j]);
if (compareResult === 0) {
// No change here
i++;
j++;
}
else if (compareResult < 0) {
// Entry in old list does not exist in the new one, it was removed
removeWatcher(oldFiles[i]);
i++;
}
else {
// Entry in new list does exist in the new one, it was added
addWatcher(newFiles[j]);
j++;
}
}
// All remaining unmatched items in the old list have been removed
for (var k = i; k < oldFiles.length; k++) {
removeWatcher(oldFiles[k]);
}
// All remaing unmatched items in the new list have been added
for (k = j; k < newFiles.length; k++) {
addWatcher(newFiles[k]);
}
// Update the state
lastResolvedFileSet = newFiles;
// Print header
if (!firstTime) {
var fileNames = "";
for (var k = 0; k < lastResolvedFileSet.length; k++) {
fileNames += Environment.newLine + " " + lastResolvedFileSet[k];
}
this.ioHost.standardError.WriteLine(getLocalizedText(DiagnosticCode.NL_Recompiling_0, [fileNames]));
}
else {
firstTime = false;
}
// Trigger a new compilation
this.compile();
};
// Switch to using stdout for all error messages
this.ioHost.standardOut = this.ioHost.standardOut;
onWatchedFileChange();
}
private getSourceFile(fileName: string): SourceFile {
var sourceFile: SourceFile = this.fileNameToSourceFile.lookup(fileName);
if (!sourceFile) {
// Attempt to read the file
var fileInformation: FileInformation;
try {
fileInformation = this.ioHost.readFile(fileName, this.compilationSettings.codepage());
}
catch (e) {
this.addDiagnostic(new Diagnostic(null, null, 0, 0, DiagnosticCode.Cannot_read_file_0_1, [fileName, e.message]));
fileInformation = new FileInformation("", ByteOrderMark.None);
}
var snapshot = ScriptSnapshot.fromString(fileInformation.contents);
var sourceFile = new SourceFile(snapshot, fileInformation.byteOrderMark);
this.fileNameToSourceFile.add(fileName, sourceFile);
}
return sourceFile;
}
private getDefaultLibraryFilePath(): string {
var compilerFilePath = this.ioHost.executingFilePath();
var containingDirectoryPath = this.ioHost.directoryName(compilerFilePath);
var libraryFilePath = this.resolvePath(IOUtils.combine(containingDirectoryPath, "lib.d.ts"));
return libraryFilePath;
}
/// IReferenceResolverHost methods
getScriptSnapshot(fileName: string): IScriptSnapshot {
return this.getSourceFile(fileName).scriptSnapshot;
}
resolveRelativePath(path: string, directory: string): string {
var unQuotedPath = stripStartAndEndQuotes(path);
var normalizedPath: string;
if (isRooted(unQuotedPath) || !directory) {
normalizedPath = unQuotedPath;
} else {
normalizedPath = IOUtils.combine(directory, unQuotedPath);
}
// get the absolute path
normalizedPath = this.resolvePath(normalizedPath);
// Switch to forward slashes
normalizedPath = switchToForwardSlashes(normalizedPath);
return normalizedPath;
}
private fileExistsCache = createIntrinsicsObject<boolean>();
fileExists(path: string): boolean {
var exists = this.fileExistsCache[path];
if (exists === undefined) {
var start = new Date().getTime();
exists = this.ioHost.fileExists(path);
this.fileExistsCache[path] = exists;
TypeScript.compilerFileExistsTime += new Date().getTime() - start;
}
return exists;
}
getParentDirectory(path: string): string {
var start = new Date().getTime();
var result = this.ioHost.directoryName(path);
TypeScript.compilerDirectoryNameTime += new Date().getTime() - start;
return result;
}
private addDiagnostic(diagnostic: Diagnostic): void {
var diagnosticInfo = diagnostic.info();
if (diagnosticInfo.category === DiagnosticCategory.Error) {
this.hasErrors = true;
}
this.ioHost.standardError.Write(TypeScriptCompiler.getFullDiagnosticText(diagnostic, path => this.resolvePath(path)));
}
private tryWriteOutputFiles(outputFiles: OutputFile[]): boolean {
for (var i = 0, n = outputFiles.length; i < n; i++) {
var outputFile = outputFiles[i];
try {
this.writeFile(outputFile.name, outputFile.text, outputFile.writeByteOrderMark);
}
catch (e) {
this.addDiagnostic(
new Diagnostic(outputFile.name, null, 0, 0, DiagnosticCode.Emit_Error_0, [e.message]));
return false;
}
}
return true;
}
writeFile(fileName: string, contents: string, writeByteOrderMark: boolean): void {
var start = new Date().getTime();
IOUtils.writeFileAndFolderStructure(this.ioHost, fileName, contents, writeByteOrderMark);
TypeScript.emitWriteFileTime += new Date().getTime() - start;
}
directoryExists(path: string): boolean {
var start = new Date().getTime();
var result = this.ioHost.directoryExists(path);
TypeScript.compilerDirectoryExistsTime += new Date().getTime() - start;
return result;
}
// For performance reasons we cache the results of resolvePath. This avoids costly lookup
// on the disk once we've already resolved a path once.
private resolvePathCache = createIntrinsicsObject<string>();
resolvePath(path: string): string {
var cachedValue = this.resolvePathCache[path];
if (!cachedValue) {
var start = new Date().getTime();
cachedValue = this.ioHost.absolutePath(path);
this.resolvePathCache[path] = cachedValue;
TypeScript.compilerResolvePathTime += new Date().getTime() - start;
}
return cachedValue;
}
}
// Start the batch compilation using the current hosts IO
var batch = new TypeScript.BatchCompiler(Environment);
batch.batchCompile();
}

View File

@@ -0,0 +1,102 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export class MemberName {
public prefix: string = "";
public suffix: string = "";
public isString() { return false; }
public isArray() { return false; }
public isMarker() { return !this.isString() && !this.isArray(); }
public toString(): string {
return MemberName.memberNameToString(this);
}
static memberNameToString(memberName: MemberName, markerInfo?: number[], markerBaseLength: number = 0): string {
var result = memberName.prefix;
if (memberName.isString()) {
result += (<MemberNameString>memberName).text;
}
else if (memberName.isArray()) {
var ar = <MemberNameArray>memberName;
for (var index = 0; index < ar.entries.length; index++) {
if (ar.entries[index].isMarker()) {
if (markerInfo) {
markerInfo.push(markerBaseLength + result.length);
}
continue;
}
result += MemberName.memberNameToString(ar.entries[index], markerInfo, markerBaseLength + result.length);
result += ar.delim;
}
}
result += memberName.suffix;
return result;
}
static create(text: string): MemberName;
static create(entry: MemberName, prefix: string, suffix: string): MemberName;
static create(arg1: any, arg2?: any, arg3?: any): MemberName {
if (typeof arg1 === "string") {
return new MemberNameString(arg1);
}
else {
var result = new MemberNameArray();
if (arg2)
result.prefix = arg2;
if (arg3)
result.suffix = arg3;
result.entries.push(arg1);
return result;
}
}
}
export class MemberNameString extends MemberName {
constructor(public text: string) {
super();
}
public isString() { return true; }
}
export class MemberNameArray extends MemberName {
public delim: string = "";
public entries: MemberName[] = [];
public isArray() { return true; }
public add(entry: MemberName) {
this.entries.push(entry);
}
public addAll(entries: MemberName[]) {
for (var i = 0 ; i < entries.length; i++) {
this.entries.push(entries[i]);
}
}
constructor() {
super();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,31 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='typescript.ts' />
module Tools {
export interface IWalkContext {
goChildren: boolean;
goNextSibling: boolean;
// visit siblings in reverse execution order
reverseSiblings: boolean;
}
export class BaseWalkContext implements IWalkContext {
public goChildren = true;
public goNextSibling = true;
public reverseSiblings = false;
}
}

View File

@@ -0,0 +1,472 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
// Information about a specific host file.
class HostFileInformation {
private _sourceText: TypeScript.IScriptSnapshot;
constructor(
public fileName: string,
private host: ILanguageServiceHost,
public version: number,
public isOpen: boolean,
public byteOrderMark: TypeScript.ByteOrderMark) {
this._sourceText = null;
}
public getScriptSnapshot(): TypeScript.IScriptSnapshot {
if (this._sourceText === null) {
this._sourceText = this.host.getScriptSnapshot(this.fileName);
}
return this._sourceText;
}
}
// Cache host information about scripts. Should be refreshed
// at each language service public entry point, since we don't know when
// set of scripts handled by the host changes.
class HostCache {
private _fileNameToEntry: TypeScript.StringHashTable<HostFileInformation>;
private _compilationSettings: TypeScript.ImmutableCompilationSettings;
constructor(host: ILanguageServiceHost) {
// script id => script index
this._fileNameToEntry = new TypeScript.StringHashTable<HostFileInformation>();
var fileNames = host.getScriptFileNames();
for (var i = 0, n = fileNames.length; i < n; i++) {
var fileName = fileNames[i];
this._fileNameToEntry.add(TypeScript.switchToForwardSlashes(fileName), new HostFileInformation(
fileName, host, host.getScriptVersion(fileName), host.getScriptIsOpen(fileName), host.getScriptByteOrderMark(fileName)));
}
var settings = host.getCompilationSettings();
if (!settings) {
// Set "ES5" target by default for language service
settings = new TypeScript.CompilationSettings();
settings.codeGenTarget = TypeScript.LanguageVersion.EcmaScript5;
}
this._compilationSettings = TypeScript.ImmutableCompilationSettings.fromCompilationSettings(settings);
}
public compilationSettings() {
return this._compilationSettings;
}
public contains(fileName: string): boolean {
return this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName)) !== null;
}
public getHostFileName(fileName: string) {
var hostCacheEntry = this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName));
if (hostCacheEntry) {
return hostCacheEntry.fileName;
}
return fileName;
}
public getFileNames(): string[]{
return this._fileNameToEntry.getAllKeys();
}
public getVersion(fileName: string): number {
return this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName)).version;
}
public isOpen(fileName: string): boolean {
return this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName)).isOpen;
}
public getByteOrderMark(fileName: string): TypeScript.ByteOrderMark {
return this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName)).byteOrderMark;
}
public getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot {
return this._fileNameToEntry.lookup(TypeScript.switchToForwardSlashes(fileName)).getScriptSnapshot();
}
public getScriptTextChangeRangeSinceVersion(fileName: string, lastKnownVersion: number): TypeScript.TextChangeRange {
var currentVersion = this.getVersion(fileName);
if (lastKnownVersion === currentVersion) {
return TypeScript.TextChangeRange.unchanged; // "No changes"
}
var scriptSnapshot = this.getScriptSnapshot(fileName);
return scriptSnapshot.getTextChangeRangeSinceVersion(lastKnownVersion);
}
}
export class SyntaxTreeCache {
private _hostCache: HostCache;
// For our syntactic only features, we also keep a cache of the syntax tree for the
// currently edited file.
private _currentFileName: string = "";
private _currentFileVersion: number = -1;
private _currentFileSyntaxTree: TypeScript.SyntaxTree = null;
private _currentFileScriptSnapshot: TypeScript.IScriptSnapshot = null;
constructor(private _host: ILanguageServiceHost) {
this._hostCache = new HostCache(_host);
}
public getCurrentFileSyntaxTree(fileName: string): TypeScript.SyntaxTree {
this._hostCache = new HostCache(this._host);
var version = this._hostCache.getVersion(fileName);
var syntaxTree: TypeScript.SyntaxTree = null;
if (this._currentFileSyntaxTree === null || this._currentFileName !== fileName) {
var scriptSnapshot = this._hostCache.getScriptSnapshot(fileName);
syntaxTree = this.createSyntaxTree(fileName, scriptSnapshot);
}
else if (this._currentFileVersion !== version) {
var scriptSnapshot = this._hostCache.getScriptSnapshot(fileName);
syntaxTree = this.updateSyntaxTree(fileName, scriptSnapshot, this._currentFileSyntaxTree, this._currentFileVersion);
}
if (syntaxTree !== null) {
// All done, ensure state is up to date
this._currentFileScriptSnapshot = scriptSnapshot;
this._currentFileVersion = version;
this._currentFileName = fileName;
this._currentFileSyntaxTree = syntaxTree;
}
return this._currentFileSyntaxTree;
}
private createSyntaxTree(fileName: string, scriptSnapshot: TypeScript.IScriptSnapshot): TypeScript.SyntaxTree {
var text = TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot);
// For the purposes of features that use this syntax tree, we can just use the default
// compilation settings. The features only use the syntax (and not the diagnostics),
// and the syntax isn't affected by the compilation settings.
var syntaxTree = TypeScript.Parser.parse(fileName, text,
TypeScript.ImmutableCompilationSettings.defaultSettings().codeGenTarget(), TypeScript.isDTSFile(fileName));
return syntaxTree;
}
private updateSyntaxTree(fileName: string, scriptSnapshot: TypeScript.IScriptSnapshot, previousSyntaxTree: TypeScript.SyntaxTree, previousFileVersion: number): TypeScript.SyntaxTree {
var editRange = this._hostCache.getScriptTextChangeRangeSinceVersion(fileName, previousFileVersion);
// Debug.assert(newLength >= 0);
// The host considers the entire buffer changed. So parse a completely new tree.
if (editRange === null) {
return this.createSyntaxTree(fileName, scriptSnapshot);
}
var nextSyntaxTree = IncrementalParser.parse(
previousSyntaxTree, editRange, SimpleText.fromScriptSnapshot(scriptSnapshot));
this.ensureInvariants(fileName, editRange, nextSyntaxTree, this._currentFileScriptSnapshot, scriptSnapshot);
return nextSyntaxTree;
}
private ensureInvariants(fileName: string, editRange: TypeScript.TextChangeRange, incrementalTree: TypeScript.SyntaxTree, oldScriptSnapshot: TypeScript.IScriptSnapshot, newScriptSnapshot: TypeScript.IScriptSnapshot) {
// First, verify that the edit range and the script snapshots make sense.
// If this fires, then the edit range is completely bogus. Somehow the lengths of the
// old snapshot, the change range and the new snapshot aren't in sync. This is very
// bad.
var expectedNewLength = oldScriptSnapshot.getLength() - editRange.span().length() + editRange.newLength();
var actualNewLength = newScriptSnapshot.getLength();
function provideMoreDebugInfo() {
var debugInformation = ["expected length:", expectedNewLength, "and actual length:", actualNewLength, "are not equal\r\n"];
var oldSpan = editRange.span();
function prettyPrintString(s: string): string {
return '"' + s.replace(/\r/g, '\\r').replace(/\n/g, '\\n') + '"';
}
debugInformation.push('Edit range (old text) (start: ' + oldSpan.start() + ', end: ' + oldSpan.end() + ') \r\n');
debugInformation.push('Old text edit range contents: ' + prettyPrintString(oldScriptSnapshot.getText(oldSpan.start(), oldSpan.end())));
var newSpan = editRange.newSpan();
debugInformation.push('Edit range (new text) (start: ' + newSpan.start() + ', end: ' + newSpan.end() + ') \r\n');
debugInformation.push('New text edit range contents: ' + prettyPrintString(newScriptSnapshot.getText(newSpan.start(), newSpan.end())));
return debugInformation.join(' ');
}
Debug.assert(
expectedNewLength === actualNewLength,
"Expected length is different from actual!",
provideMoreDebugInfo);
if (Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
// If this fires, the text change range is bogus. It says the change starts at point
// 'X', but we can see a text difference *before* that point.
var oldPrefixText = oldScriptSnapshot.getText(0, editRange.span().start());
var newPrefixText = newScriptSnapshot.getText(0, editRange.span().start());
Debug.assert(oldPrefixText === newPrefixText, 'Expected equal prefix texts!');
// If this fires, the text change range is bogus. It says the change goes only up to
// point 'X', but we can see a text difference *after* that point.
var oldSuffixText = oldScriptSnapshot.getText(editRange.span().end(), oldScriptSnapshot.getLength());
var newSuffixText = newScriptSnapshot.getText(editRange.newSpan().end(), newScriptSnapshot.getLength());
Debug.assert(oldSuffixText === newSuffixText, 'Expected equal suffix texts!');
// Ok, text change range and script snapshots look ok. Let's verify that our
// incremental parsing worked properly.
//var normalTree = this.createSyntaxTree(fileName, newScriptSnapshot);
//Debug.assert(normalTree.structuralEquals(incrementalTree), 'Expected equal incremental and normal trees');
// Ok, the trees looked good. So at least our incremental parser agrees with the
// normal parser. Now, verify that the incremental tree matches the contents of the
// script snapshot.
var incrementalTreeText = fullText(incrementalTree.sourceUnit());
var actualSnapshotText = newScriptSnapshot.getText(0, newScriptSnapshot.getLength());
Debug.assert(incrementalTreeText === actualSnapshotText, 'Expected full texts to be equal');
}
}
}
export class LanguageServiceCompiler {
private logger: TypeScript.ILogger;
// The underlying typescript compiler we defer most operations to.
private compiler: TypeScript.TypeScriptCompiler = null;
// A cache of all the information about the files on the host side.
private hostCache: HostCache = null;
constructor(private host: ILanguageServiceHost, private documentRegistry: IDocumentRegistry, private cancellationToken: CancellationToken) {
this.logger = this.host;
}
private synchronizeHostData(): void {
TypeScript.timeFunction(this.logger, "synchronizeHostData()", () => {
this.synchronizeHostDataWorker();
});
}
private synchronizeHostDataWorker(): void {
// Reset the cache at start of every refresh
this.hostCache = new HostCache(this.host);
var compilationSettings = this.hostCache.compilationSettings();
// If we don't have a compiler, then create a new one.
if (this.compiler === null) {
this.compiler = new TypeScript.TypeScriptCompiler(this.logger, compilationSettings);
}
var oldSettings = this.compiler.compilationSettings();
var changesInCompilationSettingsAffectSyntax =
oldSettings && compilationSettings && !compareDataObjects(oldSettings, compilationSettings) && settingsChangeAffectsSyntax(oldSettings, compilationSettings);
// let the compiler know about the current compilation settings.
this.compiler.setCompilationSettings(compilationSettings);
// Now, remove any files from the compiler that are no longer in the host.
var compilerFileNames = this.compiler.fileNames();
for (var i = 0, n = compilerFileNames.length; i < n; i++) {
this.cancellationToken.throwIfCancellationRequested();
var fileName = compilerFileNames[i];
if (!this.hostCache.contains(fileName) || changesInCompilationSettingsAffectSyntax) {
this.compiler.removeFile(fileName);
this.documentRegistry.releaseDocument(fileName, oldSettings);
}
}
// Now, for every file the host knows about, either add the file (if the compiler
// doesn't know about it.). Or notify the compiler about any changes (if it does
// know about it.)
var hostFileNames = this.hostCache.getFileNames();
for (var i = 0, n = hostFileNames.length; i < n; i++) {
var fileName = hostFileNames[i];
var version = this.hostCache.getVersion(fileName);
var isOpen = this.hostCache.isOpen(fileName);
var scriptSnapshot = this.hostCache.getScriptSnapshot(fileName);
var document: Document = this.compiler.getDocument(fileName)
if (document) {
//
// If the document is the same, assume no update
//
if (document.version === version && document.isOpen === isOpen) {
continue;
}
// Only perform incremental parsing on open files that are being edited. If a file was
// open, but is now closed, we want to reparse entirely so we don't have any tokens that
// are holding onto expensive script snapshot instances on the host. Similarly, if a
// file was closed, then we always want to reparse. This is so our tree doesn't keep
// the old buffer alive that represented the file on disk (as the host has moved to a
// new text buffer).
var textChangeRange: TextChangeRange = null;
if (document.isOpen && isOpen) {
textChangeRange = this.hostCache.getScriptTextChangeRangeSinceVersion(fileName, document.version);
}
document = this.documentRegistry.updateDocument(document, fileName, compilationSettings, scriptSnapshot, version, isOpen, textChangeRange);
}
else {
document = this.documentRegistry.acquireDocument(fileName, compilationSettings, scriptSnapshot, this.hostCache.getByteOrderMark(fileName), version, isOpen, []);
}
this.compiler.addOrUpdateFile(document);
}
}
// Methods that defer to the host cache to get the result.
public getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot {
this.synchronizeHostData();
return this.hostCache.getScriptSnapshot(fileName);
}
// Methods that does not require updating the host cache information
public getCachedHostFileName(fileName: string) {
if (!this.hostCache) {
this.synchronizeHostData();
}
return this.hostCache.getHostFileName(fileName);
}
public getCachedTopLevelDeclaration(fileName: string) {
if (!this.hostCache) {
this.synchronizeHostData();
}
return this.compiler.topLevelDeclaration(fileName);
}
// Methods that defer to the compiler to get the result.
public compilationSettings(): TypeScript.ImmutableCompilationSettings {
this.synchronizeHostData();
return this.compiler.compilationSettings();
}
public fileNames(): string[] {
this.synchronizeHostData();
return this.compiler.fileNames();
}
public cleanupSemanticCache(): void {
this.compiler.cleanupSemanticCache();
}
public getDocument(fileName: string): TypeScript.Document {
this.synchronizeHostData();
return this.compiler.getDocument(fileName);
}
public getSemanticInfoChain(): SemanticInfoChain {
this.synchronizeHostData();
return this.compiler.getSemanticInfoChain();
}
public getSyntacticDiagnostics(fileName: string): TypeScript.Diagnostic[] {
this.synchronizeHostData();
return this.compiler.getSyntacticDiagnostics(fileName);
}
public getSemanticDiagnostics(fileName: string): TypeScript.Diagnostic[] {
this.synchronizeHostData();
return this.compiler.getSemanticDiagnostics(fileName);
}
public getCompilerOptionsDiagnostics(resolvePath: (path: string) => string): TypeScript.Diagnostic[] {
this.synchronizeHostData();
return this.compiler.getCompilerOptionsDiagnostics(resolvePath);
}
public getSymbolInformationFromAST(ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetSymbolInformationFromAST(ast, document);
}
public getCallInformationFromAST(ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetCallInformationFromAST(ast, document);
}
public getVisibleMemberSymbolsFromAST(ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetVisibleMemberSymbolsFromAST(ast, document);
}
public getVisibleDeclsFromAST(ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetVisibleDeclsFromAST(ast, document);
}
public getContextualMembersFromAST(ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetContextualMembersFromAST(ast, document);
}
public pullGetDeclInformation(decl: TypeScript.PullDecl, ast: TypeScript.ISyntaxElement, document: TypeScript.Document) {
this.synchronizeHostData();
return this.compiler.pullGetDeclInformation(decl, ast, document);
}
public topLevelDeclaration(fileName: string) {
this.synchronizeHostData();
return this.compiler.topLevelDeclaration(fileName);
}
public getDeclForAST(ast: TypeScript.ISyntaxElement): TypeScript.PullDecl {
this.synchronizeHostData();
return this.compiler.getDeclForAST(ast);
}
public emit(fileName: string, resolvePath: (path: string) => string): TypeScript.EmitOutput {
this.synchronizeHostData();
return this.compiler.emit(fileName, resolvePath);
}
public emitDeclarations(fileName: string, resolvePath: (path: string) => string): TypeScript.EmitOutput {
this.synchronizeHostData();
return this.compiler.emitDeclarations(fileName, resolvePath);
}
public canEmitDeclarations(fileName: string) {
this.synchronizeHostData();
return this.compiler.canEmitDeclarations(fileName);
}
public dispose(): void {
if (this.compiler) {
var fileNames = this.compiler.fileNames();
for (var i = 0; i < fileNames.length; ++i) {
this.documentRegistry.releaseDocument(fileNames[i], this.compiler.compilationSettings());
}
}
}
}
}

View File

@@ -0,0 +1,193 @@
// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='references.ts' />
module TypeScript.Services {
export class CompletionHelpers {
private static getSpan(ast: ISyntaxElement): TextSpan {
return new TextSpan(start(ast), width(ast));
}
private static symbolDeclarationIntersectsPosition(symbol: PullSymbol, fileName: string, position: number) {
var decl = symbol.getDeclarations()[0];
if (decl.fileName() === fileName && this.getSpan(decl.ast()).intersectsWithPosition(position)) {
// This is the symbol declaration from the given position in the file
return true;
}
return false;
}
public static filterContextualMembersList(contextualMemberSymbols: TypeScript.PullSymbol[], existingMembers: TypeScript.PullVisibleSymbolsInfo, fileName: string, position: number): TypeScript.PullSymbol[] {
if (!existingMembers || !existingMembers.symbols || existingMembers.symbols.length === 0) {
return contextualMemberSymbols;
}
var existingMemberSymbols = existingMembers.symbols;
var existingMemberNames = TypeScript.createIntrinsicsObject<boolean>();
for (var i = 0, n = existingMemberSymbols.length; i < n; i++) {
if (this.symbolDeclarationIntersectsPosition(existingMemberSymbols[i], fileName, position)) {
// If this is the current item we are editing right now, do not filter it out
continue;
}
existingMemberNames[TypeScript.stripStartAndEndQuotes(existingMemberSymbols[i].getDisplayName())] = true;
}
var filteredMembers: TypeScript.PullSymbol[] = [];
for (var j = 0, m = contextualMemberSymbols.length; j < m; j++) {
var contextualMemberSymbol = contextualMemberSymbols[j];
if (!existingMemberNames[TypeScript.stripStartAndEndQuotes(contextualMemberSymbol.getDisplayName())]) {
if (this.symbolDeclarationIntersectsPosition(contextualMemberSymbol, fileName, position)) {
// If this contextual member symbol was created as part of editing the current position, do not use it
continue;
}
filteredMembers.push(contextualMemberSymbol);
}
}
return filteredMembers;
}
public static isCompletionListBlocker(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
// We shouldn't be getting a possition that is outside the file because
// isEntirelyInsideComment can't handle when the position is out of bounds,
// callers should be fixed, however we should be resiliant to bad inputs
// so we return true (this position is a blocker for getting completions)
if (position < 0 || position > fullWidth(sourceUnit)) {
return true;
}
// This method uses Fidelity completely. Some information can be reached using the AST, but not everything.
return TypeScript.Syntax.isEntirelyInsideComment(sourceUnit, position) ||
TypeScript.Syntax.isEntirelyInStringOrRegularExpressionLiteral(sourceUnit, position) ||
CompletionHelpers.isIdentifierDefinitionLocation(sourceUnit, position) ||
CompletionHelpers.isRightOfIllegalDot(sourceUnit, position);
}
public static getContainingObjectLiteralApplicableForCompletion(sourceUnit: TypeScript.SourceUnitSyntax, position: number): TypeScript.ISyntaxElement {
// The locations in an object literal expression that are applicable for completion are property name definition locations.
var previousToken = CompletionHelpers.getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
if (previousToken) {
var parent = previousToken.parent;
switch (previousToken.kind()) {
case TypeScript.SyntaxKind.OpenBraceToken: // var x = { |
case TypeScript.SyntaxKind.CommaToken: // var x = { a: 0, |
if (parent && parent.kind() === TypeScript.SyntaxKind.SeparatedList) {
parent = parent.parent;
}
if (parent && parent.kind() === TypeScript.SyntaxKind.ObjectLiteralExpression) {
return parent;
}
break;
}
}
return null;
}
public static isIdentifierDefinitionLocation(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
var positionedToken = CompletionHelpers.getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
if (positionedToken) {
var containingNodeKind = Syntax.containingNode(positionedToken) && Syntax.containingNode(positionedToken).kind();
switch (positionedToken.kind()) {
case TypeScript.SyntaxKind.CommaToken:
return containingNodeKind === TypeScript.SyntaxKind.ParameterList ||
containingNodeKind === TypeScript.SyntaxKind.VariableDeclaration ||
containingNodeKind === TypeScript.SyntaxKind.EnumDeclaration; // enum { foo, |
case TypeScript.SyntaxKind.OpenParenToken:
return containingNodeKind === TypeScript.SyntaxKind.ParameterList ||
containingNodeKind === TypeScript.SyntaxKind.CatchClause;
case TypeScript.SyntaxKind.OpenBraceToken:
return containingNodeKind === TypeScript.SyntaxKind.EnumDeclaration; // enum { |
case TypeScript.SyntaxKind.PublicKeyword:
case TypeScript.SyntaxKind.PrivateKeyword:
case TypeScript.SyntaxKind.StaticKeyword:
case TypeScript.SyntaxKind.DotDotDotToken:
return containingNodeKind === TypeScript.SyntaxKind.Parameter;
case TypeScript.SyntaxKind.ClassKeyword:
case TypeScript.SyntaxKind.ModuleKeyword:
case TypeScript.SyntaxKind.EnumKeyword:
case TypeScript.SyntaxKind.InterfaceKeyword:
case TypeScript.SyntaxKind.FunctionKeyword:
case TypeScript.SyntaxKind.VarKeyword:
case TypeScript.SyntaxKind.GetKeyword:
case TypeScript.SyntaxKind.SetKeyword:
return true;
}
// Previous token may have been a keyword that was converted to an identifier.
switch (positionedToken.text()) {
case "class":
case "interface":
case "enum":
case "module":
return true;
}
}
return false;
}
public static getNonIdentifierCompleteTokenOnLeft(sourceUnit: TypeScript.SourceUnitSyntax, position: number): TypeScript.ISyntaxToken {
var positionedToken = Syntax.findCompleteTokenOnLeft(sourceUnit, position, /*includeSkippedTokens*/true);
if (positionedToken && position === end(positionedToken) && positionedToken.kind() == TypeScript.SyntaxKind.EndOfFileToken) {
// EndOfFile token is not intresting, get the one before it
positionedToken = previousToken(positionedToken, /*includeSkippedTokens*/true);
}
if (positionedToken && position === end(positionedToken) && positionedToken.kind() === TypeScript.SyntaxKind.IdentifierName) {
// The caret is at the end of an identifier, the decession to provide completion depends on the previous token
positionedToken = previousToken(positionedToken, /*includeSkippedTokens*/true);
}
return positionedToken;
}
public static isRightOfIllegalDot(sourceUnit: TypeScript.SourceUnitSyntax, position: number): boolean {
var positionedToken = CompletionHelpers.getNonIdentifierCompleteTokenOnLeft(sourceUnit, position);
if (positionedToken) {
switch (positionedToken.kind()) {
case TypeScript.SyntaxKind.DotToken:
var leftOfDotPositionedToken = previousToken(positionedToken, /*includeSkippedTokens*/true);
return leftOfDotPositionedToken && leftOfDotPositionedToken.kind() === TypeScript.SyntaxKind.NumericLiteral;
case TypeScript.SyntaxKind.NumericLiteral:
var text = positionedToken.text();
return text.charAt(text.length - 1) === ".";
}
}
return false;
}
public static getValidCompletionEntryDisplayName(displayName: string): string {
if (displayName && displayName.length > 0) {
var firstChar = displayName.charCodeAt(0);
if (firstChar === TypeScript.CharacterCodes.singleQuote || firstChar === TypeScript.CharacterCodes.doubleQuote) {
// If the user entered name for the symbol was quoted, removing the quotes is not enough, as the name could be an
// invalid identifer name. We need to check if whatever was inside the quotes is actually a valid identifier name.
displayName = TypeScript.stripStartAndEndQuotes(displayName);
}
if (TypeScript.Scanner.isValidIdentifier(TypeScript.SimpleText.fromString(displayName), TypeScript.LanguageVersion.EcmaScript5)) {
return displayName;
}
}
return null;
}
}
}

View File

@@ -0,0 +1,68 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
export interface CachedCompletionEntryDetails extends CompletionEntryDetails{
isResolved(): boolean;
}
export class ResolvedCompletionEntry implements CachedCompletionEntryDetails {
constructor(public name: string,
public kind: string,
public kindModifiers: string,
public type: string,
public fullSymbolName: string,
public docComment: string) {
}
public isResolved(): boolean {
return true;
}
}
export class DeclReferenceCompletionEntry implements CachedCompletionEntryDetails {
public type: string = null;
public fullSymbolName: string = null;
public docComment: string = null;
private hasBeenResolved = false;
constructor(public name: string,
public kind: string,
public kindModifiers: string,
public decl: TypeScript.PullDecl) {
}
public isResolved(): boolean {
return this.hasBeenResolved;
}
public resolve(type: string, fullSymbolName: string, docComments: string) {
this.type = type;
this.fullSymbolName = fullSymbolName;
this.docComment = docComments;
this.hasBeenResolved = true;
}
}
export class CompletionSession {
constructor(public fileName: string,
public position: number,
public entries: TypeScript.IdentiferNameHashTable<CachedCompletionEntryDetails>) {
}
}
}

View File

@@ -0,0 +1,205 @@
///<reference path='references.ts' />
module TypeScript {
export class ArrayUtilities {
public static sequenceEquals<T>(array1: T[], array2: T[], equals: (v1: T, v2: T) => boolean) {
if (array1 === array2) {
return true;
}
if (array1 === null || array2 === null) {
return false;
}
if (array1.length !== array2.length) {
return false;
}
for (var i = 0, n = array1.length; i < n; i++) {
if (!equals(array1[i], array2[i])) {
return false;
}
}
return true;
}
public static contains<T>(array: T[], value: T): boolean {
for (var i = 0; i < array.length; i++) {
if (array[i] === value) {
return true;
}
}
return false;
}
// Gets unique element array
public static distinct<T>(array: T[], equalsFn?: (a: T, b: T) => boolean): T[] {
var result: T[] = [];
// TODO: use map when available
for (var i = 0, n = array.length; i < n; i++) {
var current = array[i];
for (var j = 0; j < result.length; j++) {
if (equalsFn(result[j], current)) {
break;
}
}
if (j === result.length) {
result.push(current);
}
}
return result;
}
public static last<T>(array: T[]): T {
if (array.length === 0) {
throw Errors.argumentOutOfRange('array');
}
return array[array.length - 1];
}
public static lastOrDefault<T>(array: T[], predicate: (v: T, index: number) => boolean): T {
for (var i = array.length - 1; i >= 0; i--) {
var v = array[i];
if (predicate(v, i)) {
return v;
}
}
return null;
}
public static firstOrDefault<T>(array: T[], func: (v: T, index: number) => boolean): T {
for (var i = 0, n = array.length; i < n; i++) {
var value = array[i];
if (func(value, i)) {
return value;
}
}
return null;
}
public static first<T>(array: T[], func?: (v: T, index: number) => boolean): T {
for (var i = 0, n = array.length; i < n; i++) {
var value = array[i];
if (!func || func(value, i)) {
return value;
}
}
throw Errors.invalidOperation();
}
public static sum<T>(array: T[], func: (v: T) => number): number {
var result = 0;
for (var i = 0, n = array.length; i < n; i++) {
result += func(array[i]);
}
return result;
}
public static select<T,S>(values: T[], func: (v: T) => S): S[] {
var result: S[] = new Array<S>(values.length);
for (var i = 0; i < values.length; i++) {
result[i] = func(values[i]);
}
return result;
}
public static where<T>(values: T[], func: (v: T) => boolean): T[] {
var result = new Array<T>();
for (var i = 0; i < values.length; i++) {
if (func(values[i])) {
result.push(values[i]);
}
}
return result;
}
public static any<T>(array: T[], func: (v: T) => boolean): boolean {
for (var i = 0, n = array.length; i < n; i++) {
if (func(array[i])) {
return true;
}
}
return false;
}
public static all<T>(array: T[], func: (v: T) => boolean): boolean {
for (var i = 0, n = array.length; i < n; i++) {
if (!func(array[i])) {
return false;
}
}
return true;
}
public static binarySearch(array: number[], value: number): number {
var low = 0;
var high = array.length - 1;
while (low <= high) {
var middle = low + ((high - low) >> 1);
var midValue = array[middle];
if (midValue === value) {
return middle;
}
else if (midValue > value) {
high = middle - 1;
}
else {
low = middle + 1;
}
}
return ~low;
}
public static createArray<T>(length: number, defaultValue: any): T[] {
var result = new Array<T>(length);
for (var i = 0; i < length; i++) {
result[i] = defaultValue;
}
return result;
}
public static grow<T>(array: T[], length: number, defaultValue: T): void {
var count = length - array.length;
for (var i = 0; i < count; i++) {
array.push(defaultValue);
}
}
public static copy<T>(sourceArray: T[], sourceIndex: number, destinationArray: T[], destinationIndex: number, length: number): void {
for (var i = 0; i < length; i++) {
destinationArray[destinationIndex + i] = sourceArray[sourceIndex + i];
}
}
public static indexOf<T>(array: T[], predicate: (v: T) => boolean): number {
for (var i = 0, n = array.length; i < n; i++) {
if (predicate(array[i])) {
return i;
}
}
return -1;
}
}
}

View File

@@ -0,0 +1,82 @@
///<reference path='references.ts' />
module TypeScript {
export interface IBitMatrix {
// Returns true if the bit at the specified indices is set. False otherwise.
valueAt(x: number, y: number): boolean;
// Sets the value at this specified indices.
setValueAt(x: number, y: number, value: boolean): void;
// Releases the bit matrix, allowing its resources to be used by another matrix.
// This instance cannot be used after it is released.
release(): void;
}
export module BitMatrix {
var pool: BitMatrixImpl[] = [];
class BitMatrixImpl implements IBitMatrix {
public isReleased = false;
private vectors: IBitVector[] = [];
constructor(public allowUndefinedValues: boolean) {
}
public valueAt(x: number, y: number): boolean {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
var vector = this.vectors[x];
if (!vector) {
return this.allowUndefinedValues ? undefined : false;
}
return vector.valueAt(y);
}
public setValueAt(x: number, y: number, value: boolean): void {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
var vector = this.vectors[x];
if (!vector) {
if (value === undefined) {
// If they're storing an undefined value, and we don't even have a vector,
// then we can short circuit early here.
return;
}
vector = BitVector.getBitVector(this.allowUndefinedValues);
this.vectors[x] = vector;
}
vector.setValueAt(y, value);
}
public release() {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
this.isReleased = true;
// Release all the vectors back.
for (var name in this.vectors) {
if (this.vectors.hasOwnProperty(name)) {
var vector = this.vectors[name];
vector.release();
}
}
this.vectors.length = 0;
pool.push(this);
}
}
export function getBitMatrix(allowUndefinedValues: boolean): IBitMatrix {
if (pool.length === 0) {
return new BitMatrixImpl(allowUndefinedValues);
}
var matrix = pool.pop();
matrix.isReleased = false;
matrix.allowUndefinedValues = allowUndefinedValues;
return matrix;
}
}
}

View File

@@ -0,0 +1,210 @@
///<reference path='references.ts'/>
module TypeScript {
export interface IBitVector {
// Returns the value at the specified index. If this is a bi-state vector, then the result
// will only be 'true' or 'false'. If this is a tri-state vector, then the result can be
// 'true', 'false', or 'undefined'.
valueAt(index: number): boolean;
// Sets the value at this specified bit. For a bi-state vector the value must be 'true' or
// 'false'. For a tri-state vector, it can be 'true', 'false', or 'undefined'.
setValueAt(index: number, value: boolean): void;
// Releases the bit vector, allowing its resources to be used by another BitVector.
// This instance cannot be used after it is released.
release(): void;
}
export module BitVector {
var pool: BitVectorImpl[] = [];
enum Constants {
// We only use up to 30 bits in a number. That way the encoded value can always fit
// within an int so that the underlying engine doesn't use a 64bit float here.
MaxBitsPerEncodedNumber = 30,
BitsPerEncodedBiStateValue = 1,
// For a tri state vector we need 2 bits per encoded value. 00 for 'undefined',
// '01' for 'false' and '10' for true.
BitsPerEncodedTriStateValue = 2,
BiStateEncodedTrue = 1, // 1
BiStateClearBitsMask = 1, // 1
TriStateEncodedFalse = 1, // 01
TriStateEncodedTrue = 2, // 10
TriStateClearBitsMask = 3, // 11
}
class BitVectorImpl implements IBitVector {
public isReleased = false;
private bits: number[] = [];
constructor(public allowUndefinedValues: boolean) {
}
private computeTriStateArrayIndex(index: number): number {
// The number of values that can be encoded in a single number.
var encodedValuesPerNumber = Constants.MaxBitsPerEncodedNumber / Constants.BitsPerEncodedTriStateValue;
return (index / encodedValuesPerNumber) >>> 0;
}
private computeBiStateArrayIndex(index: number): number {
// The number of values that can be encoded in a single number.
var encodedValuesPerNumber = Constants.MaxBitsPerEncodedNumber / Constants.BitsPerEncodedBiStateValue;
return (index / encodedValuesPerNumber) >>> 0;
}
private computeTriStateEncodedValueIndex(index: number): number {
// The number of values that can be encoded in a single number.
var encodedValuesPerNumber = Constants.MaxBitsPerEncodedNumber / Constants.BitsPerEncodedTriStateValue;
return (index % encodedValuesPerNumber) * Constants.BitsPerEncodedTriStateValue;
}
private computeBiStateEncodedValueIndex(index: number): number {
// The number of values that can be encoded in a single number.
var encodedValuesPerNumber = Constants.MaxBitsPerEncodedNumber / Constants.BitsPerEncodedBiStateValue;
return (index % encodedValuesPerNumber) * Constants.BitsPerEncodedBiStateValue;
}
public valueAt(index: number): boolean {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
if (this.allowUndefinedValues) {
// tri-state bit vector. 2 bits per value.
var arrayIndex = this.computeTriStateArrayIndex(index);
var encoded = this.bits[arrayIndex];
if (encoded === undefined) {
// We don't even have an encoded value at this array position. That's
// equivalent to 'undefined' for a tri-state vector.
return undefined;
}
var bitIndex = this.computeTriStateEncodedValueIndex(index);
if (encoded & (Constants.TriStateEncodedTrue << bitIndex)) {
return true;
}
else if (encoded & (Constants.TriStateEncodedFalse << bitIndex)) {
return false;
}
else {
return undefined;
}
}
else {
// Normal bitvector. One bit per value stored.
var arrayIndex = this.computeBiStateArrayIndex(index);
var encoded = this.bits[arrayIndex];
if (encoded === undefined) {
// We don't even have an encoded value at this array position. That's
// equivalent to 'false' for a bi-state vector.
return false;
}
// If we don't support undefined values, then we use one bit per value. Just
// index to that bit and see if it's set or not.
var bitIndex = this.computeBiStateEncodedValueIndex(index);
if (encoded & (Constants.BiStateEncodedTrue << bitIndex)) {
return true;
}
else {
return false;
}
}
}
public setValueAt(index: number, value: boolean): void {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
if (this.allowUndefinedValues) {
Debug.assert(value === true || value === false || value === undefined, "value must only be true, false or undefined.");
var arrayIndex = this.computeTriStateArrayIndex(index);
var encoded = this.bits[arrayIndex];
if (encoded === undefined) {
if (value === undefined) {
// They're trying to set a bit to undefined that we don't even have an entry
// for. We can bail out quickly here.
return;
}
encoded = 0;
}
// First, we clear out any bits set at the appropriate index.
var bitIndex = this.computeTriStateEncodedValueIndex(index);
// Create a mask similar to: 11111111100111111
// i.e. all 1's except for 2 zeroes in the appropriate place.
var clearMask = ~(Constants.TriStateClearBitsMask << bitIndex)
encoded = encoded & clearMask;
if (value === true) {
encoded = encoded | (Constants.TriStateEncodedTrue << bitIndex);
}
else if (value === false) {
encoded = encoded | (Constants.TriStateEncodedFalse << bitIndex);
}
// else {
// They're setting the value to 'undefined'. We already cleared the value
// so there's nothing we need to do here.
// }
this.bits[arrayIndex] = encoded;
}
else {
Debug.assert(value === true || value === false, "value must only be true or false.");
var arrayIndex = this.computeBiStateArrayIndex(index);
var encoded = this.bits[arrayIndex];
if (encoded === undefined) {
if (value === false) {
// They're trying to set a bit to false that we don't even have an entry
// for. We can bail out quickly here.
return;
}
encoded = 0;
}
var bitIndex = this.computeBiStateEncodedValueIndex(index);
// First, clear out the bit at this location.
encoded = encoded & ~(Constants.BiStateClearBitsMask << bitIndex);
if (value) {
encoded = encoded | (Constants.BiStateEncodedTrue << bitIndex);
}
// else {
// They're setting the value to 'false'. We already cleared the value
// so there's nothing we need to do here.
// }
this.bits[arrayIndex] = encoded;
}
}
public release() {
Debug.assert(!this.isReleased, "Should not use a released bitvector");
this.isReleased = true;
this.bits.length = 0;
pool.push(this);
}
}
export function getBitVector(allowUndefinedValues: boolean): IBitVector {
if (pool.length === 0) {
return new BitVectorImpl(allowUndefinedValues);
}
var vector = pool.pop();
vector.isReleased = false;
vector.allowUndefinedValues = allowUndefinedValues;
return vector;
}
}
}

View File

@@ -0,0 +1,3 @@
interface ICancellationToken {
isCancellationRequested(): boolean;
}

View File

@@ -0,0 +1,7 @@
///<reference path='ICancellationToken.ts' />
interface ICancellationTokenSource {
token(): ICancellationToken;
cancel(): void;
}

View File

@@ -0,0 +1,9 @@
///<reference path='references.ts' />
module TypeScript {
export enum Constants {
// 2^30-1
Max31BitInteger = 1073741823,
Min31BitInteger = -1073741824,
}
}

View File

@@ -0,0 +1,32 @@
///<reference path='references.ts' />
module TypeScript {
export enum AssertionLevel {
None = 0,
Normal = 1,
Aggressive = 2,
VeryAggressive = 3,
}
export class Debug {
private static currentAssertionLevel = AssertionLevel.None;
public static shouldAssert(level: AssertionLevel): boolean {
return this.currentAssertionLevel >= level;
}
public static assert(expression: any, message: string = "", verboseDebugInfo: () => string = null): void {
if (!expression) {
var verboseDebugString = "";
if (verboseDebugInfo) {
verboseDebugString = "\r\nVerbose Debug Information:" + verboseDebugInfo();
}
throw new Error("Debug Failure. False expression: " + message + verboseDebugString);
}
}
public static fail(message?: string): void {
Debug.assert(false, message);
}
}
}

View File

@@ -0,0 +1,10 @@
///<reference path='references.ts' />
module TypeScript {
export enum DiagnosticCategory {
Warning,
Error,
Message,
NoPrefix,
}
}

View File

@@ -0,0 +1,200 @@
///<reference path='references.ts' />
module TypeScript {
export var LocalizedDiagnosticMessages: IIndexable<any> = null;
export class Location {
private _fileName: string;
private _lineMap: LineMap;
private _start: number;
private _length: number;
constructor(fileName: string, lineMap: LineMap, start: number, length: number) {
this._fileName = fileName;
this._lineMap = lineMap;
this._start = start;
this._length = length;
}
public fileName(): string {
return this._fileName;
}
public lineMap(): LineMap {
return this._lineMap;
}
public line(): number {
return this._lineMap ? this._lineMap.getLineNumberFromPosition(this.start()) : 0;
}
public character(): number {
return this._lineMap ? this._lineMap.getLineAndCharacterFromPosition(this.start()).character() : 0;
}
public start(): number {
return this._start;
}
public length(): number {
return this._length;
}
public static equals(location1: Location, location2: Location): boolean {
return location1._fileName === location2._fileName &&
location1._start === location2._start &&
location1._length === location2._length;
}
}
export class Diagnostic extends Location {
private _diagnosticKey: string;
private _arguments: any[];
private _additionalLocations: Location[];
constructor(fileName: string, lineMap: LineMap, start: number, length: number, diagnosticKey: string, _arguments: any[]= null, additionalLocations: Location[] = null) {
super(fileName, lineMap, start, length);
this._diagnosticKey = diagnosticKey;
this._arguments = (_arguments && _arguments.length > 0) ? _arguments : null;
this._additionalLocations = (additionalLocations && additionalLocations.length > 0) ? additionalLocations : null;
}
public toJSON(key: any): any {
var result: any = {};
result.start = this.start();
result.length = this.length();
result.diagnosticCode = this._diagnosticKey;
var _arguments: any[] = (<any>this).arguments();
if (_arguments && _arguments.length > 0) {
result.arguments = _arguments;
}
return result;
}
public diagnosticKey(): string {
return this._diagnosticKey;
}
public arguments(): any[] {
return this._arguments;
}
/**
* Get the text of the message in the given language.
*/
public text(): string {
return TypeScript.getLocalizedText(this._diagnosticKey, this._arguments);
}
/**
* Get the text of the message including the error code in the given language.
*/
public message(): string {
return TypeScript.getDiagnosticMessage(this._diagnosticKey, this._arguments);
}
/**
* If a derived class has additional information about other referenced symbols, it can
* expose the locations of those symbols in a general way, so they can be reported along
* with the error.
*/
public additionalLocations(): Location[] {
return this._additionalLocations || [];
}
public static equals(diagnostic1: Diagnostic, diagnostic2: Diagnostic): boolean {
return Location.equals(diagnostic1, diagnostic2) &&
diagnostic1._diagnosticKey === diagnostic2._diagnosticKey &&
ArrayUtilities.sequenceEquals(diagnostic1._arguments, diagnostic2._arguments, (v1, v2) => v1 === v2);
}
public info(): DiagnosticInfo {
return getDiagnosticInfoFromKey(this.diagnosticKey());
}
}
export function newLine(): string {
// TODO: We need to expose an extensibility point on our hosts to have them tell us what
// they want the newline string to be. That way we can get the correct result regardless
// of which host we use
return Environment ? Environment.newLine : "\r\n";
}
function getLargestIndex(diagnostic: string): number {
var largest = -1;
var regex = /\{(\d+)\}/g;
var match: RegExpExecArray;
while (match = regex.exec(diagnostic)) {
var val = parseInt(match[1]);
if (!isNaN(val) && val > largest) {
largest = val;
}
}
return largest;
}
function getDiagnosticInfoFromKey(diagnosticKey: string): DiagnosticInfo {
var result: DiagnosticInfo = diagnosticInformationMap[diagnosticKey];
Debug.assert(result);
return result;
}
export function getLocalizedText(diagnosticKey: string, args: any[]): string {
if (LocalizedDiagnosticMessages) {
//Debug.assert(LocalizedDiagnosticMessages.hasOwnProperty(diagnosticKey));
}
var diagnosticMessageText: string = LocalizedDiagnosticMessages ? LocalizedDiagnosticMessages[diagnosticKey] : diagnosticKey;
Debug.assert(diagnosticMessageText !== undefined && diagnosticMessageText !== null);
var actualCount = args ? args.length : 0;
// We have a string like "foo_0_bar_1". We want to find the largest integer there.
// (i.e.'1'). We then need one more arg than that to be correct.
var expectedCount = 1 + getLargestIndex(diagnosticKey);
if (expectedCount !== actualCount) {
throw new Error(getLocalizedText(DiagnosticCode.Expected_0_arguments_to_message_got_1_instead, [expectedCount, actualCount]));
}
// This should also be the same number of arguments as the message text
var valueCount = 1 + getLargestIndex(diagnosticMessageText);
if (valueCount !== expectedCount) {
throw new Error(getLocalizedText(DiagnosticCode.Expected_the_message_0_to_have_1_arguments_but_it_had_2, [diagnosticMessageText, expectedCount, valueCount]));
}
diagnosticMessageText = diagnosticMessageText.replace(/{(\d+)}/g, function (match, num?) {
return typeof args[num] !== 'undefined'
? args[num]
: match;
});
diagnosticMessageText = diagnosticMessageText.replace(/{(NL)}/g, function (match) {
return TypeScript.newLine();
});
return diagnosticMessageText;
}
export function getDiagnosticMessage(diagnosticKey: string, args: any[]): string {
var diagnostic = getDiagnosticInfoFromKey(diagnosticKey);
var diagnosticMessageText = getLocalizedText(diagnosticKey, args);
var message: string;
if (diagnostic.category === DiagnosticCategory.Error) {
message = getLocalizedText(DiagnosticCode.error_TS_0_1, [diagnostic.code, diagnosticMessageText]);
}
else if (diagnostic.category === DiagnosticCategory.Warning) {
message = getLocalizedText(DiagnosticCode.warning_TS_0_1, [diagnostic.code, diagnosticMessageText]);
}
else {
message = diagnosticMessageText;
}
return message;
}
}

View File

@@ -0,0 +1,9 @@
///<reference path='references.ts' />
module TypeScript {
export interface DiagnosticInfo {
category: DiagnosticCategory;
message: string;
code: number;
}
}

View File

@@ -0,0 +1,505 @@
///<reference path='references.ts' />
///<reference path='..\compiler\enumerator.ts' />
///<reference path='..\compiler\process.ts' />
declare var Buffer: {
new (str: string, encoding?: string): any;
}
module TypeScript {
export var nodeMakeDirectoryTime = 0;
export var nodeCreateBufferTime = 0;
export var nodeWriteFileSyncTime = 0;
export enum ByteOrderMark {
None = 0,
Utf8 = 1,
Utf16BigEndian = 2,
Utf16LittleEndian = 3,
}
export class FileInformation {
constructor(public contents: string, public byteOrderMark: ByteOrderMark) {
}
}
export interface IFileWatcher {
close(): void;
}
export interface IEnvironment {
supportsCodePage(): boolean;
readFile(path: string, codepage: number): FileInformation;
writeFile(path: string, contents: string, writeByteOrderMark: boolean): void;
deleteFile(path: string): void;
fileExists(path: string): boolean;
directoryExists(path: string): boolean;
directoryName(path: string): string;
createDirectory(path: string): void;
absolutePath(path: string): string;
listFiles(path: string, re?: RegExp, options?: { recursive?: boolean; }): string[];
arguments: string[];
standardOut: ITextWriter;
standardError: ITextWriter;
executingFilePath(): string;
currentDirectory(): string;
newLine: string;
watchFile(fileName: string, callback: (x: string) => void): IFileWatcher;
quit(exitCode?: number): void;
}
function throwIOError(message: string, error: Error) {
var errorMessage = message;
if (error && error.message) {
errorMessage += (" " + error.message);
}
throw new Error(errorMessage);
}
export var Environment: IEnvironment = (function () {
// Create an IO object for use inside WindowsScriptHost hosts
// Depends on WSCript and FileSystemObject
function getWindowsScriptHostEnvironment(): IEnvironment {
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");
} catch (e) {
return null;
}
var streamObjectPool: any[] = [];
function getStreamObject(): any {
if (streamObjectPool.length > 0) {
return streamObjectPool.pop();
}
else {
return new ActiveXObject("ADODB.Stream");
}
}
function releaseStreamObject(obj: any) {
streamObjectPool.push(obj);
}
var args: string[] = [];
for (var i = 0; i < WScript.Arguments.length; i++) {
args[i] = WScript.Arguments.Item(i);
}
return {
// On windows, the newline sequence is always "\r\n";
newLine: "\r\n",
currentDirectory: () => (<any>WScript).CreateObject("WScript.Shell").CurrentDirectory,
supportsCodePage: () => (<any>WScript).ReadFile,
absolutePath: path => fso.GetAbsolutePathName(path),
readFile: function (path, codepage) {
try {
// If a codepage is requested, defer to our host to do the reading. If it
// fails, fall back to our normal BOM/utf8 logic.
if (codepage !== null && this.supportsCodePage()) {
try {
var contents = (<any>WScript).ReadFile(path, codepage);
return new FileInformation(contents, ByteOrderMark.None);
}
catch (e) {
// We couldn't read it with that code page. Fall back to the normal
// BOM/utf8 logic below.
}
}
// Initially just read the first two bytes of the file to see if there's a bom.
var streamObj = getStreamObject();
streamObj.Open();
streamObj.Type = 2; // Text data
// Start reading individual chars without any interpretation. That way we can check for a bom.
streamObj.Charset = 'x-ansi';
streamObj.LoadFromFile(path);
var bomChar = streamObj.ReadText(2); // Read the BOM char
// Position has to be at 0 before changing the encoding
streamObj.Position = 0;
var byteOrderMark = ByteOrderMark.None;
if (bomChar.charCodeAt(0) === 0xFE && bomChar.charCodeAt(1) === 0xFF) {
streamObj.Charset = 'unicode';
byteOrderMark = ByteOrderMark.Utf16BigEndian;
}
else if (bomChar.charCodeAt(0) === 0xFF && bomChar.charCodeAt(1) === 0xFE) {
streamObj.Charset = 'unicode';
byteOrderMark = ByteOrderMark.Utf16LittleEndian;
}
else if (bomChar.charCodeAt(0) === 0xEF && bomChar.charCodeAt(1) === 0xBB) {
streamObj.Charset = 'utf-8';
byteOrderMark = ByteOrderMark.Utf8;
}
else {
// Always read a file as utf8 if it has no bom.
streamObj.Charset = 'utf-8';
}
// Read the whole file
var contents = streamObj.ReadText(-1 /* read from the current position to EOS */);
streamObj.Close();
releaseStreamObject(streamObj);
return new FileInformation(contents, byteOrderMark);
}
catch (err) {
// -2147024809 is the javascript value for 0x80070057 which is the HRESULT for
// "the parameter is incorrect".
var message: string;
if (err.number === -2147024809) {
message = TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Unsupported_file_encoding, null);
}
else {
message = TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Cannot_read_file_0_1, [path, err.message]);
}
throw new Error(message);
}
},
writeFile: (path, contents, writeByteOrderMark) => {
// First, convert the text contents passed in to binary in UTF8 format.
var textStream = getStreamObject();
textStream.Charset = 'utf-8';
textStream.Open();
textStream.WriteText(contents, 0 /*do not add newline*/);
// If they don't want the BOM, then skip it (it will be added automatically
// when we write the utf8 bytes out above).
if (!writeByteOrderMark) {
textStream.Position = 3;
}
else {
textStream.Position = 0;
}
// Now, write all those bytes out to a file.
var fileStream = getStreamObject();
fileStream.Type = 1; //binary data.
fileStream.Open();
textStream.CopyTo(fileStream);
// Flush and save the file.
fileStream.Flush();
fileStream.SaveToFile(path, 2 /*overwrite*/);
fileStream.Close();
textStream.Flush();
textStream.Close();
},
fileExists: path => fso.FileExists(path),
deleteFile: path => {
if (fso.FileExists(path)) {
fso.DeleteFile(path, true); // true: delete read-only files
}
},
directoryExists: path => <boolean>fso.FolderExists(path),
directoryName: path => fso.GetParentFolderName(path),
createDirectory: function (path) {
try {
if (!this.directoryExists(path)) {
fso.CreateFolder(path);
}
} catch (e) {
throwIOError(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Could_not_create_directory_0, [path]), e);
}
},
listFiles: (path, spec?, options?) => {
options = options || <{ recursive?: boolean; }>{};
function filesInFolder(folder: any, root: string): string[] {
var paths: string[] = [];
var fc: Enumerator;
if (options.recursive) {
fc = new Enumerator(folder.subfolders);
for (; !fc.atEnd(); fc.moveNext()) {
paths = paths.concat(filesInFolder(fc.item(), root + "\\" + fc.item().Name));
}
}
fc = new Enumerator(folder.files);
for (; !fc.atEnd(); fc.moveNext()) {
if (!spec || fc.item().Name.match(spec)) {
paths.push(root + "\\" + fc.item().Name);
}
}
return paths;
}
var folder: any = fso.GetFolder(path);
var paths: string[] = [];
return filesInFolder(folder, path);
},
arguments: <string[]>args,
standardOut: WScript.StdOut,
standardError: WScript.StdErr,
executingFilePath: () => WScript.ScriptFullName,
quit: (exitCode = 0) => {
try {
WScript.Quit(exitCode);
} catch (e) {
}
},
watchFile: null,
};
};
function getNodeEnvironment(): IEnvironment {
var _fs = require('fs');
var _path = require('path');
var _module = require('module');
var _os = require('os');
return {
// On node pick up the newline character from the OS
newLine: _os.EOL,
currentDirectory: () => (<any>process).cwd(),
supportsCodePage: () => false,
absolutePath: path => _path.resolve(path),
readFile: (file, codepage) => {
if (codepage !== null) {
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.codepage_option_not_supported_on_current_platform, null));
}
var buffer = _fs.readFileSync(file);
switch (buffer[0]) {
case 0xFE:
if (buffer[1] === 0xFF) {
// utf16-be. Reading the buffer as big endian is not supported, so convert it to
// Little Endian first
var i = 0;
while ((i + 1) < buffer.length) {
var temp = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = temp;
i += 2;
}
return new FileInformation(buffer.toString("ucs2", 2), ByteOrderMark.Utf16BigEndian);
}
break;
case 0xFF:
if (buffer[1] === 0xFE) {
// utf16-le
return new FileInformation(buffer.toString("ucs2", 2), ByteOrderMark.Utf16LittleEndian);
}
break;
case 0xEF:
if (buffer[1] === 0xBB) {
// utf-8
return new FileInformation(buffer.toString("utf8", 3), ByteOrderMark.Utf8);
}
}
// Default behaviour
return new FileInformation(buffer.toString("utf8", 0), ByteOrderMark.None);
},
writeFile: (path, contents, writeByteOrderMark) => {
function mkdirRecursiveSync(path: string) {
var stats = _fs.statSync(path);
if (stats.isFile()) {
throw "\"" + path + "\" exists but isn't a directory.";
}
else if (stats.isDirectory()) {
return;
}
else {
mkdirRecursiveSync(_path.dirname(path));
_fs.mkdirSync(path, 509 /*775 in octal*/);
}
}
var start = new Date().getTime();
mkdirRecursiveSync(_path.dirname(path));
TypeScript.nodeMakeDirectoryTime += new Date().getTime() - start;
if (writeByteOrderMark) {
contents = '\uFEFF' + contents;
}
var start = new Date().getTime();
var chunkLength = 4 * 1024;
var fileDescriptor = _fs.openSync(path, "w");
try {
for (var index = 0; index < contents.length; index += chunkLength) {
var bufferStart = new Date().getTime();
var buffer = new Buffer(contents.substr(index, chunkLength), "utf8");
TypeScript.nodeCreateBufferTime += new Date().getTime() - bufferStart;
_fs.writeSync(fileDescriptor, buffer, 0, buffer.length, null);
}
}
finally {
_fs.closeSync(fileDescriptor);
}
TypeScript.nodeWriteFileSyncTime += new Date().getTime() - start;
},
fileExists: path => _fs.existsSync(path),
deleteFile: path => {
try {
_fs.unlinkSync(path);
} catch (e) {
}
},
directoryExists: path =>
_fs.existsSync(path) && _fs.statSync(path).isDirectory(),
directoryName: path => {
var dirPath = _path.dirname(path);
// Node will just continue to repeat the root path, rather than return null
if (dirPath === path) {
dirPath = null;
}
return dirPath;
},
createDirectory: function (path) {
try {
if (!this.directoryExists(path)) {
_fs.mkdirSync(path);
}
} catch (e) {
throwIOError(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Could_not_create_directory_0, [path]), e);
}
},
listFiles: (path, spec?, options?) => {
options = options || <{ recursive?: boolean; }>{};
function filesInFolder(folder: string): string[] {
var paths: string[] = [];
var files = _fs.readdirSync(folder);
for (var i = 0; i < files.length; i++) {
var pathToFile = _path.join(folder, files[i]);
var stat = _fs.statSync(pathToFile);
if (options.recursive && stat.isDirectory()) {
paths = paths.concat(filesInFolder(pathToFile));
}
else if (stat.isFile() && (!spec || files[i].match(spec))) {
paths.push(pathToFile);
}
}
return paths;
}
return filesInFolder(path);
},
arguments: process.argv.slice(2),
standardOut: {
Write: str => process.stdout.write(str),
WriteLine: str => process.stdout.write(str + '\n'),
Close() { }
},
standardError: {
Write: str => process.stderr.write(str),
WriteLine: str => process.stderr.write(str + '\n'),
Close() { }
},
executingFilePath: () => process.mainModule.filename,
quit: code => {
var stderrFlushed = process.stderr.write('');
var stdoutFlushed = process.stdout.write('');
process.stderr.on('drain', function () {
stderrFlushed = true;
if (stdoutFlushed) {
process.exit(code);
}
});
process.stdout.on('drain', function () {
stdoutFlushed = true;
if (stderrFlushed) {
process.exit(code);
}
});
setTimeout(function () {
process.exit(code);
}, 5);
},
watchFile: (fileName, callback) => {
var firstRun = true;
var processingChange = false;
var fileChanged: any = function (curr: any, prev: any) {
if (!firstRun) {
if (curr.mtime < prev.mtime) {
return;
}
_fs.unwatchFile(fileName, fileChanged);
if (!processingChange) {
processingChange = true;
callback(fileName);
setTimeout(function () { processingChange = false; }, 100);
}
}
firstRun = false;
_fs.watchFile(fileName, { persistent: true, interval: 500 }, fileChanged);
};
fileChanged();
return {
fileName: fileName,
close: function () {
_fs.unwatchFile(fileName, fileChanged);
}
};
},
};
};
if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") {
return getWindowsScriptHostEnvironment();
}
else if (typeof module !== 'undefined' && module.exports) {
return getNodeEnvironment();
}
else {
return null; // Unsupported host
}
})();
}

View File

@@ -0,0 +1,29 @@
///<reference path='references.ts' />
module TypeScript {
export class Errors {
public static argument(argument: string, message?: string): Error {
return new Error("Invalid argument: " + argument + ". " + message);
}
public static argumentOutOfRange(argument: string): Error {
return new Error("Argument out of range: " + argument);
}
public static argumentNull(argument: string): Error {
return new Error("Argument null: " + argument);
}
public static abstract(): Error {
return new Error("Operation not implemented properly by subclass.");
}
public static notYetImplemented(): Error {
return new Error("Not yet implemented.");
}
public static invalidOperation(message?: string): Error {
return new Error("Invalid operation: " + message);
}
}
}

112
src/services/core/hash.ts Normal file
View File

@@ -0,0 +1,112 @@
///<reference path='references.ts' />
module TypeScript {
export class Hash {
// This table uses FNV1a as a string hash
private static FNV_BASE = 2166136261;
private static FNV_PRIME = 16777619;
private static computeFnv1aCharArrayHashCode(text: number[], start: number, len: number): number {
var hashCode = Hash.FNV_BASE;
var end = start + len;
for (var i = start; i < end; i++) {
hashCode = IntegerUtilities.integerMultiplyLow32Bits(hashCode ^ text[i], Hash.FNV_PRIME);
}
return hashCode;
}
public static computeSimple31BitCharArrayHashCode(key: number[], start: number, len: number): number {
// Start with an int.
var hash = 0;
for (var i = 0; i < len; i++) {
var ch = key[start + i];
// Left shift keeps things as a 32bit int. And we're only doing two adds. Chakra and
// V8 recognize this as not needing to go past the 53 bits needed for the float
// mantissa. Or'ing with 0 keeps this 32 bits.
hash = ((((hash << 5) - hash) | 0) + ch) | 0;
}
// Ensure we fit in 31 bits. That way if/when this gets stored, it won't require any heap
// allocation.
return hash & 0x7FFFFFFF;
}
public static computeSimple31BitStringHashCode(key: string): number {
// Start with an int.
var hash = 0;
var start = 0;
var len = key.length;
for (var i = 0; i < len; i++) {
var ch = key.charCodeAt(start + i);
// Left shift keeps things as a 32bit int. And we're only doing two adds. Chakra and
// V8 recognize this as not needing to go past the 53 bits needed for the float
// mantissa. Or'ing with 0 keeps this 32 bits.
hash = ((((hash << 5) - hash) | 0) + ch) | 0;
}
// Ensure we fit in 31 bits. That way if/when this gets stored, it won't require any heap
// allocation.
return hash & 0x7FFFFFFF;
}
public static computeMurmur2StringHashCode(key: string, seed: number): number {
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
var m: number = 0x5bd1e995;
var r: number = 24;
// Initialize the hash to a 'random' value
var numberOfCharsLeft = key.length;
var h = Math.abs(seed ^ numberOfCharsLeft);
// Mix 4 bytes at a time into the hash. NOTE: 4 bytes is two chars, so we iterate
// through the string two chars at a time.
var index = 0;
while (numberOfCharsLeft >= 2) {
var c1 = key.charCodeAt(index);
var c2 = key.charCodeAt(index + 1);
var k = Math.abs(c1 | (c2 << 16));
k = IntegerUtilities.integerMultiplyLow32Bits(k, m);
k ^= k >> r;
k = IntegerUtilities.integerMultiplyLow32Bits(k, m);
h = IntegerUtilities.integerMultiplyLow32Bits(h, m);
h ^= k;
index += 2;
numberOfCharsLeft -= 2;
}
// Handle the last char (or 2 bytes) if they exist. This happens if the original string had
// odd length.
if (numberOfCharsLeft === 1) {
h ^= key.charCodeAt(index);
h = IntegerUtilities.integerMultiplyLow32Bits(h, m);
}
// Do a few final mixes of the hash to ensure the last few bytes are well-incorporated.
h ^= h >> 13;
h = IntegerUtilities.integerMultiplyLow32Bits(h, m);
h ^= h >> 15;
return h;
}
public static combine(value: number, currentHash: number): number {
// Ensure we stay within 31 bits.
return (((currentHash << 5) + currentHash) + value) & 0x7FFFFFFF;
}
}
}

View File

@@ -0,0 +1,7 @@
///<reference path='references.ts'/>
module TypeScript {
export interface IIndexable<T> {
[s: string]: T;
}
}

View File

@@ -0,0 +1,28 @@
///<reference path='references.ts' />
module TypeScript {
export module IntegerUtilities {
export function integerDivide(numerator: number, denominator: number): number {
return (numerator / denominator) >> 0;
}
export function integerMultiplyLow32Bits(n1: number, n2: number): number {
var n1Low16 = n1 & 0x0000ffff;
var n1High16 = n1 >>> 16;
var n2Low16 = n2 & 0x0000ffff;
var n2High16 = n2 >>> 16;
var resultLow32 = (((n1 & 0xffff0000) * n2) >>> 0) + (((n1 & 0x0000ffff) * n2) >>> 0) >>> 0;
return resultLow32;
}
export function isInteger(text: string): boolean {
return /^[0-9]+$/.test(text);
}
export function isHexInteger(text: string): boolean {
return /^0(x|X)[0-9a-fA-F]+$/.test(text);
}
}
}

View File

@@ -0,0 +1,8 @@
/// <reference path='references.ts' />
module TypeScript {
export interface Iterator<T> {
moveNext(): boolean;
current(): T;
}
}

View File

@@ -0,0 +1,8 @@
///<reference path='references.ts' />
module TypeScript {
export interface ILineAndCharacter {
line: number;
character: number;
}
}

View File

@@ -0,0 +1,82 @@
///<reference path='references.ts' />
module TypeScript {
export class LineMap {
public static empty = new LineMap(() => [0], 0);
private _lineStarts: number[] = null;
constructor(private _computeLineStarts: () => number[], private length: number) {
}
public toJSON(key: any) {
return { lineStarts: this.lineStarts(), length: this.length };
}
public equals(other: LineMap): boolean {
return this.length === other.length &&
ArrayUtilities.sequenceEquals(this.lineStarts(), other.lineStarts(), (v1, v2) => v1 === v2);
}
public lineStarts(): number[] {
if (this._lineStarts === null) {
this._lineStarts = this._computeLineStarts();
}
return this._lineStarts;
}
public lineCount(): number {
return this.lineStarts().length;
}
public getPosition(line: number, character: number): number {
return this.lineStarts()[line] + character;
}
public getLineNumberFromPosition(position: number): number {
if (position < 0 || position > this.length) {
throw Errors.argumentOutOfRange("position");
}
if (position === this.length) {
// this can happen when the user tried to get the line of items
// that are at the absolute end of this text (i.e. the EndOfLine
// token, or missing tokens that are at the end of the text).
// In this case, we want the last line in the text.
return this.lineCount() - 1;
}
// Binary search to find the right line
var lineNumber = ArrayUtilities.binarySearch(this.lineStarts(), position);
if (lineNumber < 0) {
lineNumber = (~lineNumber) - 1;
}
return lineNumber;
}
public getLineStartPosition(lineNumber: number): number {
return this.lineStarts()[lineNumber];
}
public fillLineAndCharacterFromPosition(position: number, lineAndCharacter: ILineAndCharacter): void {
if (position < 0 || position > this.length) {
throw Errors.argumentOutOfRange("position");
}
var lineNumber = this.getLineNumberFromPosition(position);
lineAndCharacter.line = lineNumber;
lineAndCharacter.character = position - this.lineStarts()[lineNumber];
}
public getLineAndCharacterFromPosition(position: number): LineAndCharacter {
if (position < 0 || position > this.length) {
throw Errors.argumentOutOfRange("position");
}
var lineNumber = this.getLineNumberFromPosition(position);
return new LineAndCharacter(lineNumber, position - this.lineStarts()[lineNumber]);
}
}
}

View File

@@ -0,0 +1,35 @@
///<reference path='references.ts' />
module TypeScript {
export class LineAndCharacter {
private _line: number = 0;
private _character: number = 0;
/**
* Initializes a new instance of a LinePosition with the given line and character. ArgumentOutOfRangeException if "line" or "character" is less than zero.
* @param line The line of the line position. The first line in a file is defined as line 0 (zero based line numbering).
* @param character The character position in the line.
*/
constructor(line: number, character: number) {
if (line < 0) {
throw Errors.argumentOutOfRange("line");
}
if (character < 0) {
throw Errors.argumentOutOfRange("character");
}
this._line = line;
this._character = character;
}
public line(): number {
return this._line;
}
public character(): number {
return this._character;
}
}
}

View File

@@ -0,0 +1,13 @@
///<reference path='references.ts' />
module TypeScript {
export class MathPrototype {
public static max(a: number, b: number): number {
return a >= b ? a : b;
}
public static min(a: number, b: number): number {
return a <= b ? a : b;
}
}
}

View File

@@ -0,0 +1,24 @@
///<reference path='require.ts' />
///<reference path='..\resources\references.ts' />
///<reference path='arrayUtilities.ts' />
///<reference path='bitVector.ts' />
///<reference path='bitMatrix.ts' />
///<reference path='constants.ts' />
///<reference path='debug.ts' />
///<reference path='diagnosticCategory.ts' />
///<reference path='diagnosticCore.ts' />
///<reference path='diagnosticInfo.ts' />
///<reference path='errors.ts' />
///<reference path='hash.ts' />
///<reference path='environment.ts' />
///<reference path='indexable.ts' />
///<reference path='integerUtilities.ts' />
///<reference path='iterator.ts' />
///<reference path='lineAndCharacter.ts' />
///<reference path='lineMap.ts' />
///<reference path='linePosition.ts' />
// ///<reference path='stringTable.ts' />
///<reference path='stringUtilities.ts' />
///<reference path='timer.ts' />

View File

@@ -0,0 +1,5 @@
///<reference path='references.ts' />
// Forward declarations of the variables we use from 'node'.
declare var require: any;
declare var module: any;

View File

@@ -0,0 +1,159 @@
///<reference path='references.ts' />
module TypeScript.Collections {
export var DefaultStringTableCapacity = 256;
class StringTableEntry {
constructor(public Text: string,
public HashCode: number,
public Next: StringTableEntry) {
}
}
// A table of interned strings. Faster and better than an arbitrary hashtable for the needs of the
// scanner. Specifically, the scanner operates over a sliding window of characters, with a start
// and end pointer for the current lexeme. The scanner then wants to get the *interned* string
// represented by that subsection.
//
// Importantly, if the string is already interned, then it wants ask "is the string represented by
// this section of a char array contained within the table" in a non-allocating fashion. i.e. if
// you have "[' ', 'p', 'u', 'b', 'l', 'i', 'c', ' ']" and you ask to get the string represented by
// range [1, 7), then this table will return "public" without any allocations if that value was
// already in the table.
//
// Of course, if the value is not in the table then there will be an initial cost to allocate the
// string and the bucket for the table. However, that is only incurred the first time each unique
// string is added.
export class StringTable {
// TODO: uncomment this once typecheck bug is fixed.
private entries: StringTableEntry[];
private count: number = 0;
constructor(capacity: number) {
var size = Hash.getPrime(capacity);
this.entries = ArrayUtilities.createArray<StringTableEntry>(size, null);
}
public addCharArray(key: number[], start: number, len: number): string {
// Compute the hash for this key. Also ensure that it fits within 31 bits (so that it
// stays a non-heap integer, and so we can index into the array safely).
var hashCode = Hash.computeSimple31BitCharArrayHashCode(key, start, len) & 0x7FFFFFFF;
// Debug.assert(hashCode > 0);
// First see if we already have the string represented by "key[start, start + len)" already
// present in this table. If we do, just return that string. Do this without any
// allocations
var entry = this.findCharArrayEntry(key, start, len, hashCode);
if (entry !== null) {
return entry.Text;
}
// We don't have an entry for that string in our table. Convert that
var slice: number[] = key.slice(start, start + len);
return this.addEntry(StringUtilities.fromCharCodeArray(slice), hashCode);
}
private findCharArrayEntry(key: number[], start: number, len: number, hashCode: number) {
for (var e = this.entries[hashCode % this.entries.length]; e !== null; e = e.Next) {
if (e.HashCode === hashCode && StringTable.textCharArrayEquals(e.Text, key, start, len)) {
return e;
}
}
return null;
}
private addEntry(text: string, hashCode: number): string {
var index = hashCode % this.entries.length;
var e = new StringTableEntry(text, hashCode, this.entries[index]);
this.entries[index] = e;
// We grow when our load factor equals 1. I tried different load factors (like .75 and
// .5), however they seemed to have no effect on running time. With a load factor of 1
// we seem to get about 80% slot fill rate with an average of around 1.25 table entries
// per slot.
if (this.count === this.entries.length) {
this.grow();
}
this.count++;
return e.Text;
}
//private dumpStats() {
// var standardOut = Environment.standardOut;
// standardOut.WriteLine("----------------------")
// standardOut.WriteLine("String table stats");
// standardOut.WriteLine("Count : " + this.count);
// standardOut.WriteLine("Entries Length : " + this.entries.length);
// var longestSlot = 0;
// var occupiedSlots = 0;
// for (var i = 0; i < this.entries.length; i++) {
// if (this.entries[i] !== null) {
// occupiedSlots++;
// var current = this.entries[i];
// var slotCount = 0;
// while (current !== null) {
// slotCount++;
// current = current.Next;
// }
// longestSlot = MathPrototype.max(longestSlot, slotCount);
// }
// }
// standardOut.WriteLine("Occupied slots : " + occupiedSlots);
// standardOut.WriteLine("Longest slot : " + longestSlot);
// standardOut.WriteLine("Avg Length/Slot : " + (this.count / occupiedSlots));
// standardOut.WriteLine("----------------------");
//}
private grow(): void {
// this.dumpStats();
var newSize = Hash.expandPrime(this.entries.length);
var oldEntries = this.entries;
var newEntries: StringTableEntry[] = ArrayUtilities.createArray<StringTableEntry>(newSize, null);
this.entries = newEntries;
for (var i = 0; i < oldEntries.length; i++) {
var e = oldEntries[i];
while (e !== null) {
var newIndex = e.HashCode % newSize;
var tmp = e.Next;
e.Next = newEntries[newIndex];
newEntries[newIndex] = e;
e = tmp;
}
}
// this.dumpStats();
}
private static textCharArrayEquals(text: string, array: number[], start: number, length: number): boolean {
if (text.length !== length) {
return false;
}
var s = start;
for (var i = 0; i < length; i++) {
if (text.charCodeAt(i) !== array[s]) {
return false;
}
s++;
}
return true;
}
}
export var DefaultStringTable = new StringTable(DefaultStringTableCapacity);
}

View File

@@ -0,0 +1,21 @@
///<reference path='references.ts' />
module TypeScript {
export class StringUtilities {
public static isString(value: any): boolean {
return Object.prototype.toString.apply(value, []) === '[object String]';
}
public static endsWith(string: string, value: string): boolean {
return string.substring(string.length - value.length, string.length) === value;
}
public static startsWith(string: string, value: string): boolean {
return string.substr(0, value.length) === value;
}
public static repeat(value: string, count: number) {
return Array(count + 1).join(value);
}
}
}

View File

@@ -0,0 +1,52 @@
///<reference path='references.ts' />
var global: any = <any>Function("return this").call(null);
module TypeScript {
module Clock {
export var now: () => number;
export var resolution: number;
declare module WScript {
export function InitializeProjection(): void;
}
declare module TestUtilities {
export function QueryPerformanceCounter(): number;
export function QueryPerformanceFrequency(): number;
}
if (typeof WScript !== "undefined" && typeof global['WScript'].InitializeProjection !== "undefined") {
// Running in JSHost.
global['WScript'].InitializeProjection();
now = function () {
return TestUtilities.QueryPerformanceCounter();
};
resolution = TestUtilities.QueryPerformanceFrequency();
}
else {
now = function () {
return Date.now();
};
resolution = 1000;
}
}
export class Timer {
public startTime: number;
public time = 0;
public start() {
this.time = 0;
this.startTime = Clock.now();
}
public end() {
// Set time to MS.
this.time = (Clock.now() - this.startTime);
}
}
}

View File

@@ -0,0 +1,74 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='classifier.ts' />
///<reference path='languageService.ts' />
///<reference path='formatting\formatting.ts' />
// Access to "Debug" object
var debugObjectHost = (<any>this);
module TypeScript.Services {
export interface ICoreServicesHost {
logger: TypeScript.ILogger;
}
export class CoreServices {
constructor (public host: ICoreServicesHost) {
}
public getPreProcessedFileInfo(fileName: string, sourceText: TypeScript.IScriptSnapshot): TypeScript.IPreProcessedFileInfo {
return TypeScript.preProcessFile(fileName, sourceText);
}
public getDefaultCompilationSettings(): ts.CompilerOptions {
// Set "ES5" target by default for language service
return {
target: ts.ScriptTarget.ES5
}
}
public dumpMemory(): string {
if (!debugObjectHost || !debugObjectHost.Debug || !debugObjectHost.Debug.dumpHeap) {
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.This_version_of_the_Javascript_runtime_does_not_support_the_0_function, ['Debug.dumpHeap()']));
}
var objects = debugObjectHost.Debug.dumpHeap(2);
var totalSize = 0;
for (var i = 0; i < objects.length; i++) {
totalSize += objects[i].size;
}
return "There are " + objects.length + " object(s) accessible from 'global', for a total of " + totalSize + " byte(s).";
}
public getMemoryInfo(): any[] {
if (!debugObjectHost || !debugObjectHost.Debug || !debugObjectHost.Debug.getMemoryInfo) {
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.This_version_of_the_Javascript_runtime_does_not_support_the_0_function, ['Debug.getMemoryInfo()']));
}
return debugObjectHost.Debug.getMemoryInfo();
}
public collectGarbage(): void {
if (!debugObjectHost || !debugObjectHost.CollectGarbage) {
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.This_version_of_the_Javascript_runtime_does_not_support_the_0_function, ['collectGarbage()']));
}
debugObjectHost.CollectGarbage();
}
}
}

View File

@@ -0,0 +1,7 @@
///<reference path='references.ts' />
module TypeScript.Services {
export interface ILanguageServicesDiagnostics {
log(content: string): void;
}
}

View File

@@ -0,0 +1,363 @@
///<reference path='..\compiler\io.ts'/>
///<reference path='..\compiler\typescript.ts'/>
///<reference path='..\..\samples\node\node.d.ts'/>
module DiagnosticsParser {
class FourSlashGenerator {
private fsFiles: { [s: string]: IFourSlashFile; };
constructor(private diagnosticsFile: string) {
this.fsFiles = {};
}
public generate(args: string[]): void {
if (IO.fileExists(this.diagnosticsFile)) {
this.parseDiagnostics(args);
if (args !== undefined) {
for (var i = 0; i < args.length; i++) {
for (var fsFile in this.fsFiles) {
var strippedScriptId = fsFile.replace(/.+\\/, '');
if (strippedScriptId === args[i]) {
this.writeFile(fsFile);
}
}
}
} else {
for (var fsFile in this.fsFiles) {
this.writeFile(fsFile);
}
}
}
}
private parseDiagnostics(args: string[]) {
var file: string = IO.readFile(this.diagnosticsFile);
var lines: string[] = file.split('\r\n');
var updateMode: boolean;
var scriptId: string;
var editBlock: string;
var editRange: TypeScript.ScriptEditRange;
var startPosition: number;
var caretPosition: number;
var collapsingBlock: string;
var openEditTag = /^.*<Edit>/;
var closeEditTag = /<Edit\/>.*/;
// Loops through the lines of the diagnostics file
for (var i = 0; i < lines.length; i++) {
// Indicates the user opening a file
if (lines[i].match(/\/\/=New=\\\\/)) {
updateMode = false;
// Indicates the user making updates to an opened file
} else if (lines[i].match(/\/\/=Update=\\\\/)) {
updateMode = true;
// Record the filePath of the opened/modified file
} else if (lines[i].match(/^scriptId: /)) {
var newScriptId = lines[i].replace(/^scriptId: /, '');
if (scriptId !== undefined && newScriptId !== scriptId && collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
startPosition = undefined;
caretPosition = undefined;
collapsingBlock = undefined
}
scriptId = newScriptId;
// Records the editRange (minChar, limChar, deltaOfCaret)
} else if (lines[i].match(/^editRange\(/)) {
//Capture numbers in editRange(minChar=###, limChar=###, delta=###)
var match = /editRange\(minChar=([0-9]+), limChar=([0-9]+), delta=(-?[0-9]+)\)/.exec(lines[i]);
if (match !== null) {
editRange = new TypeScript.ScriptEditRange(parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10));
}
// Indicates the beginning of added text and records
} else if (lines[i].match(openEditTag)) {
var editLines: string[] = [];
lines[i] = lines[i].replace(openEditTag, '');
while (lines[i].match(closeEditTag) === null) {
editLines.push(lines[i]);
i++;
}
editLines.push(lines[i].replace(closeEditTag, ''));
editBlock = editLines.join('\r\n');
// Indicates the end of the edit block, and then adds the text to the relevant file
} else if (lines[i].match(/\\\\=====\/\//) && editRange !== null) {
if (updateMode) {
var range: number = editRange.limChar - editRange.minChar;
var deleting: boolean = (range !== 0);
if (deleting) {
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
caretPosition = editRange.minChar + (editBlock !== undefined ? editBlock.length : 0);
this.fsFiles[scriptId].deleteAt(editRange.limChar, caretPosition, range, editBlock);
startPosition = undefined;
caretPosition = undefined;
collapsingBlock = undefined;
} else {
if (editRange.minChar === caretPosition) {
collapsingBlock += editBlock;
} else {
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
startPosition = editRange.minChar;
collapsingBlock = editBlock;
}
caretPosition = editRange.minChar + editRange.delta;
}
} else {
this.fsFiles[scriptId] = new FourSlashFile(scriptId, editBlock);
}
editBlock = undefined;
}
}
// Add any text that hasn't been stored to its relevant file
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
}
private writeFile(scriptId: string): void {
if (this.fsFiles[scriptId] !== undefined) {
var fsFileName = this.fsFiles[scriptId].getFsFileName();
IO.writeFile(fsFileName, this.fsFiles[scriptId].getFile());
}
}
}
interface IFourSlashFile {
getFile(): string;
getFsFileName(): string;
getOriginalScriptId(): string;
updateInternalFile(minChar: number, limChar: number, content: string): void;
insertAt(position: number, caretPosition: number, content: string): void;
deleteAt(position: number, caretPosition: number, amount: number, content?: string): void;
addInternalState(caretPosition: number): void;
}
interface IFourSlashNode {
getNode(): string;
}
class FourSlashFile implements IFourSlashFile {
private static header = "/// <reference path=\"fourslash.ts\" />" + '\r\n' + '\r\n';
private fsScriptId: string;
private commands: IFourSlashNode[];
private internalState: string;
constructor(private originalScriptId: string, private startingContent: string) {
this.fsScriptId = 'tests\\cases\\fourslash\\' + originalScriptId.replace(/.+\\(.+).ts/, '$1_generated.ts');
this.commands = [];
this.internalState = startingContent;
}
public getFile(): string {
var file: string;
//Split on \r\n and add ////
var rnlines = this.startingContent.split('\r\n');
rnlines.forEach((line, index, array) => { array[index] = '////' + line });
var rnfile = rnlines.join('\r\n');
//Repeat for \n
var nlines = rnfile.split(/([^\r]\n|\r[^\n])/);
nlines.forEach((line, index, array) => { if (line.indexOf('////') !== 0) { array[index] = '////' + line } });
var nfile = nlines.join('\n');
file = FourSlashFile.header + nfile + '\r\n' + '\r\n';
this.commands.forEach((command, index, array) => { file += command.getNode() });
return file;
}
private getWithWhitespace(text: string) {
return text.replace(/ /g, '\u00B7').replace(/\r/g, '\u00B6').replace(/\n/g, '\u2193\n').replace(/\t/g, '\u2192\ ');
}
public getFsFileName(): string {
return this.fsScriptId;
}
public getOriginalScriptId(): string {
return this.originalScriptId;
}
public updateInternalFile(minChar: number, limChar: number, content: string): void {
this.internalState = this.internalState.substring(0, minChar) + content + this.internalState.substr(limChar);
}
public insertAt(position: number, caretPosition: number, content: string): void {
var posOffset = this.getInsertOffset(position);
var insertNode = new FourSlashInsert(position - posOffset, content.replace(/(\r\n|\r|\n)/g, '\n'));
this.commands.push(insertNode);
this.updateInternalFile(position, position, content);
this.addInternalState(caretPosition);
}
public deleteAt(position: number, caretPosition: number, amount: number, content?: string): void {
var posOffset = this.getInsertOffset(position);
var amountOffset = this.getDeleteOffset(position, amount);
var deleteNode = new FourSlashDelete(position - posOffset, amount - amountOffset, (content !== undefined ? content.replace(/(\r\n|\r|\n)/g, '\n') : undefined));
this.commands.push(deleteNode);
this.updateInternalFile(position - amount, position, content);
this.addInternalState(caretPosition);
}
public addInternalState(caretPosition: number): void {
var caretedState = this.internalState.substr(0, caretPosition) + '|' + this.internalState.substr(caretPosition);
var internalState = new FourSlashStateComment(caretedState);
this.commands.push(internalState);
}
private getInsertOffset(position: number) {
var offset: number = 0;
var match = this.internalState.substring(0, position).match(/\r\n/g);
if (match !== null) {
offset = match.length;
}
offset = (match !== null ? match.length : 0);
return offset;
}
private getDeleteOffset(position: number, amount: number) {
var offset: number = 0;
var match = this.internalState.substring(position - amount, position).match(/\r\n/g);
if (match !== null) {
offset = match.length;
}
return offset;
}
}
class FourSlashInsert implements IFourSlashNode {
constructor(private position: number, private content: string) {
this.content = this.content.replace(/([\/\\"'])/g, '\\$1');
this.content = this.content.replace(/\r\n/g, '\\r\\n');
this.content = this.content.replace(/(\r|\n)/g, '\\n');
}
public getNode(): string {
var insertNode: string = "goTo.position(" + this.position.toString() + ");" + "\n" +
"edit.insert(\'" + this.content + "\');" + "\n";
return insertNode;
}
}
class FourSlashDelete implements IFourSlashNode {
constructor(private position: number, private deletionAmount: number, private content: string) {
if (this.content !== undefined) {
this.content = this.content.replace(/([\/\\"'])/g, '\\$1');
this.content = this.content.replace(/\r\n/g, '\\r\\n');
this.content = this.content.replace(/(\r|\n)/g, '\\n');
}
}
public getNode(): string {
var deleteNode: string = "goTo.position(" + this.position.toString() + ");" + "\n" +
"edit.backspace(" + this.deletionAmount + ");" + "\n";
if (this.content !== undefined && this.content !== "") {
deleteNode += "edit.insert(\'" + this.content + "\');" + "\n";
}
return deleteNode;
}
}
class FourSlashStateComment implements IFourSlashNode {
constructor(private state: string) { }
public getNode(): string {
var lines = this.state.replace(/\r\n|\r|\n/g, '\r\n').split('\n');
lines.forEach((line, index, array) => { array[index] = '//-' + line; });
var stateNode = lines.join('\n');
return stateNode + '\n' + '\n';
}
}
class DiagnosticsLocator {
private diagnosticsFile: string = 'diagnostics.txt';
public find(): string {
return this.findDiagnosticsInFolder("C:/Users");
}
private findDiagnosticsInFolder(path: string): string {
if (IO.directoryExists(path)) {
var diagnosticsPath = undefined;
var dir: string[] = IO.dir(path, undefined, { recursive: true });
for (var i = 0; i < dir.length; i++) {
if (dir[i].indexOf(this.diagnosticsFile) !== -1) {
diagnosticsPath = dir[i];
}
}
return diagnosticsPath;
}
}
}
var diagnosticsFile: string = undefined;
var diagnosticArgs: string[] = undefined
if (process.argv.length > 2) {
if (IO.fileExists(process.argv[2])) {
diagnosticsFile = process.argv[2];
}
//Grab remaining arguments
diagnosticArgs = process.argv.slice(3, process.argv.length - 1);
}
if (diagnosticsFile !== undefined) {
new FourSlashGenerator(diagnosticsFile).generate(diagnosticArgs);
}
}

6
src/services/document.ts Normal file
View File

@@ -0,0 +1,6 @@
///<reference path="references.ts" />
module TypeScript.Services {
// Inject support for incremental parsing to the core compiler Document class.
Document.incrementalParse = IncrementalParser.parse;
}

354
src/services/es5compat.ts Normal file
View File

@@ -0,0 +1,354 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*----------------- ThirdPartyNotices -------------------------------------------------------
This file is based on or incorporates material from the projects listed below
(collectively "Third Party Code"). Microsoft is not the original author of the
Third Party Code. The original copyright notice and the license, under which
Microsoft received such Third Party Code, are set forth below. Such license and
notices are provided for informational purposes only. Microsoft licenses the Third
Party Code to you under the terms of the Apache 2.0 License.
--
Array filter Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
Array forEach Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
Array indexOf Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
Array map Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
Array Reduce Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce
Array some Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some
String Trim Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
Date now Compatibility Method,
Available at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/now
Copyright (c) 2007 - 2012 Mozilla Developer Network and individual contributors
Licensed by Microsoft under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR
CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and
limitations under the License.
--
Original License provided for Informational Purposes Only
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------- End of ThirdPartyNotices --------------------------------------------------- */
// Compatibility with non ES5 compliant engines
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
// Compatibility with non ES5 compliant engines
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement: any, fromIndex?: any) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len: any = t.length >>> 0;
if (len === 0) {
return -1;
}
var n: any = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
}
else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || <any>-1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k: any = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun: any, thisp?: any)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res: any[] = [];
for (var i = 0; i < len; i++)
{
if (<any>i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.com/#x15.4.4.19
if (!Array.prototype.map) {
Array.prototype.map = function(callback: any, thisArg?: any) {
var T: any = undefined, A: any, k: any;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (thisArg) {
T = thisArg;
}
// 6. Let A be a new array created as if by the expression new Array(len) where Array is
// the standard built-in constructor with that name and len is the value of len.
A = new Array(len);
// 7. Let k be 0
k = 0;
// 8. Repeat, while k < len
while(k < len) {
var kValue: any, mappedValue: any;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[ k ];
// ii. Let mappedValue be the result of calling the Call internal method of callback
// with T as the this value and argument list containing kValue, k, and O.
mappedValue = callback.call(T, kValue, k, O);
// iii. Call the DefineOwnProperty internal method of A with arguments
// Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
// and false.
// In browsers that support Object.defineProperty, use the following:
// Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
// For best browser support, use the following:
A[ k ] = mappedValue;
}
// d. Increase k by 1.
k++;
}
// 9. return A
return A;
};
}
if (!Array.prototype.reduce) {
Array.prototype.reduce = function reduce(accumulator: any){
if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
var i = 0, l = this.length >> 0, curr: any;
if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
throw new TypeError("First argument is not callable");
if(arguments.length < 2) {
if (l === 0) throw new TypeError("Array length is 0 and no second argument");
curr = this[0];
i = 1; // start accumulating at the second element
}
else
curr = arguments[1];
while (i < l) {
if(<any>i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
++i;
}
return curr;
};
}
// Compatibility with non ES5 compliant engines
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.com/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback: any, thisArg?: any) {
var T: any, k: any;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0; // Hack to convert O.length to a UInt32
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if ({ }.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (thisArg) {
T = thisArg;
}
else {
T = undefined; // added to stop definite assignment error
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue: any;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as the this value and
// argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
// Compatibility with non ES5 compliant engines
if (!Date.now) {
Date.now = function() {
return (new Date()).getTime();
};
}
// Compatibility with non ES5 compliant engines
// Production steps of ECMA-262, Edition 5.1, 15.4.4.17
if (!Array.prototype.some)
{
Array.prototype.some = function(fun: any /*, thisp */)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
var idx = i.toString(); // REVIEW: this line is not from the Mozilla page, necessary to avoid our compile time checks against non-string/any types in an in expression
if (idx in t && fun.call(thisp, t[i], i, t))
return true;
}
return false;
};
}

View File

@@ -0,0 +1,152 @@
// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='references.ts' />
module TypeScript.Services {
export class FindReferenceHelpers {
public static compareSymbolsForLexicalIdentity(firstSymbol: TypeScript.PullSymbol, secondSymbol: TypeScript.PullSymbol): boolean {
// Unwrap modules so that we're always referring to the variable.
if (!firstSymbol.isAlias() && firstSymbol.isContainer()) {
var containerForFirstSymbol = (<TypeScript.PullContainerSymbol>firstSymbol);
if (containerForFirstSymbol.getInstanceSymbol()) {
firstSymbol = containerForFirstSymbol.getInstanceSymbol();
}
}
if (!secondSymbol.isAlias() && secondSymbol.isContainer()) {
var containerForSecondSymbol = (<TypeScript.PullContainerSymbol>secondSymbol);
if (containerForSecondSymbol.getInstanceSymbol()) {
secondSymbol = containerForSecondSymbol.getInstanceSymbol();
}
}
if (firstSymbol.kind === secondSymbol.kind) {
if (firstSymbol === secondSymbol) {
return true;
}
// If we have two variables and they have the same name and the same parent, then
// they are the same symbol.
if (firstSymbol.kind === TypeScript.PullElementKind.Variable &&
firstSymbol.name === secondSymbol.name &&
firstSymbol.getDeclarations() && firstSymbol.getDeclarations().length >= 1 &&
secondSymbol.getDeclarations() && secondSymbol.getDeclarations().length >= 1) {
var firstSymbolDecl = firstSymbol.getDeclarations()[0];
var secondSymbolDecl = secondSymbol.getDeclarations()[0];
return firstSymbolDecl.getParentDecl() === secondSymbolDecl.getParentDecl();
}
// If we have two properties that belong to an object literal, then we need ot see
// if they came from teh same object literal ast.
if (firstSymbol.kind === TypeScript.PullElementKind.Property &&
firstSymbol.name === secondSymbol.name &&
firstSymbol.getDeclarations() && firstSymbol.getDeclarations().length >= 1 &&
secondSymbol.getDeclarations() && secondSymbol.getDeclarations().length >= 1) {
var firstSymbolDecl = firstSymbol.getDeclarations()[0];
var secondSymbolDecl = secondSymbol.getDeclarations()[0];
var firstParentDecl = firstSymbolDecl.getParentDecl();
var secondParentDecl = secondSymbolDecl.getParentDecl()
if (firstParentDecl.kind === TypeScript.PullElementKind.ObjectLiteral &&
secondParentDecl.kind === TypeScript.PullElementKind.ObjectLiteral) {
return firstParentDecl.ast() === secondParentDecl.ast();
}
}
// check if we are dealing with the implementation of interface method or a method override
if (firstSymbol.name === secondSymbol.name) {
// at this point firstSymbol.kind === secondSymbol.kind so we can pick any of those
switch (firstSymbol.kind) {
case PullElementKind.Property:
case PullElementKind.Method:
case PullElementKind.GetAccessor:
case PullElementKind.SetAccessor:
// these kinds can only be defined in types
var t1 = <PullTypeSymbol>firstSymbol.getContainer();
var t2 = <PullTypeSymbol>secondSymbol.getContainer();
t1._resolveDeclaredSymbol();
t2._resolveDeclaredSymbol();
return t1.hasBase(t2) || t2.hasBase(t1);
break;
}
}
return false;
}
else {
switch (firstSymbol.kind) {
case TypeScript.PullElementKind.Class: {
return this.checkSymbolsForDeclarationEquality(firstSymbol, secondSymbol);
}
case TypeScript.PullElementKind.Property: {
if (firstSymbol.isAccessor()) {
var getterSymbol = (<TypeScript.PullAccessorSymbol>firstSymbol).getGetter();
var setterSymbol = (<TypeScript.PullAccessorSymbol>firstSymbol).getSetter();
if (getterSymbol && getterSymbol === secondSymbol) {
return true;
}
if (setterSymbol && setterSymbol === secondSymbol) {
return true;
}
}
return false;
}
case TypeScript.PullElementKind.Function: {
if (secondSymbol.isAccessor()) {
var getterSymbol = (<TypeScript.PullAccessorSymbol>secondSymbol).getGetter();
var setterSymbol = (<TypeScript.PullAccessorSymbol>secondSymbol).getSetter();
if (getterSymbol && getterSymbol === firstSymbol) {
return true;
}
if (setterSymbol && setterSymbol === firstSymbol) {
return true;
}
}
return false;
}
case TypeScript.PullElementKind.ConstructorMethod: {
return this.checkSymbolsForDeclarationEquality(firstSymbol, secondSymbol);
}
}
}
return firstSymbol === secondSymbol;
}
private static checkSymbolsForDeclarationEquality(firstSymbol: TypeScript.PullSymbol, secondSymbol: TypeScript.PullSymbol): boolean {
var firstSymbolDeclarations: TypeScript.PullDecl[] = firstSymbol.getDeclarations();
var secondSymbolDeclarations: TypeScript.PullDecl[] = secondSymbol.getDeclarations();
for (var i = 0, iLen = firstSymbolDeclarations.length; i < iLen; i++) {
for (var j = 0, jLen = secondSymbolDeclarations.length; j < jLen; j++) {
if (this.declarationsAreSameOrParents(firstSymbolDeclarations[i], secondSymbolDeclarations[j])) {
return true;
}
}
}
return false;
}
private static declarationsAreSameOrParents(firstDecl: TypeScript.PullDecl, secondDecl: TypeScript.PullDecl): boolean {
var firstParent: TypeScript.PullDecl = firstDecl.getParentDecl();
var secondParent: TypeScript.PullDecl = secondDecl.getParentDecl();
if (firstDecl === secondDecl ||
firstDecl === secondParent ||
firstParent === secondDecl ||
firstParent === secondParent) {
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,320 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class Formatter extends MultipleTokenIndenter {
private previousTokenSpan: TokenSpan = null;
private previousTokenParent: IndentationNodeContext = null;
// TODO: implement it with skipped tokens in Fidelity
private scriptHasErrors: boolean = false;
private rulesProvider: RulesProvider;
private formattingRequestKind: FormattingRequestKind;
private formattingContext: FormattingContext;
constructor(textSpan: TextSpan,
sourceUnit: SourceUnitSyntax,
indentFirstToken: boolean,
options: FormattingOptions,
snapshot: ITextSnapshot,
rulesProvider: RulesProvider,
formattingRequestKind: FormattingRequestKind) {
super(textSpan, sourceUnit, snapshot, indentFirstToken, options);
this.previousTokenParent = this.parent().clone(this.indentationNodeContextPool());
this.rulesProvider = rulesProvider;
this.formattingRequestKind = formattingRequestKind;
this.formattingContext = new FormattingContext(this.snapshot(), this.formattingRequestKind);
}
public static getEdits(textSpan: TextSpan,
sourceUnit: SourceUnitSyntax,
options: FormattingOptions,
indentFirstToken: boolean,
snapshot: ITextSnapshot,
rulesProvider: RulesProvider,
formattingRequestKind: FormattingRequestKind): TextEditInfo[] {
var walker = new Formatter(textSpan, sourceUnit, indentFirstToken, options, snapshot, rulesProvider, formattingRequestKind);
visitNodeOrToken(walker, sourceUnit);
return walker.edits();
}
public visitTokenInSpan(token: ISyntaxToken): void {
if (token.fullWidth() !== 0) {
var tokenSpan = new TextSpan(this.position() + token.leadingTriviaWidth(), width(token));
if (this.textSpan().containsTextSpan(tokenSpan)) {
this.processToken(token);
}
}
// Call the base class to process the token and indent it if needed
super.visitTokenInSpan(token);
}
private processToken(token: ISyntaxToken): void {
var position = this.position();
// Extract any leading comments
if (token.leadingTriviaWidth() !== 0) {
this.processTrivia(token.leadingTrivia(), position);
position += token.leadingTriviaWidth();
}
// Push the token
var currentTokenSpan = new TokenSpan(token.kind(), position, width(token));
if (!this.parent().hasSkippedOrMissingTokenChild()) {
if (this.previousTokenSpan) {
// Note that formatPair calls TrimWhitespaceInLineRange in between the 2 tokens
this.formatPair(this.previousTokenSpan, this.previousTokenParent, currentTokenSpan, this.parent());
}
else {
// We still want to trim whitespace even if it is the first trivia of the first token. Trim from the beginning of the span to the trivia
this.trimWhitespaceInLineRange(this.getLineNumber(this.textSpan()), this.getLineNumber(currentTokenSpan));
}
}
this.previousTokenSpan = currentTokenSpan;
if (this.previousTokenParent) {
// Make sure to clear the previous parent before assigning a new value to it
this.indentationNodeContextPool().releaseNode(this.previousTokenParent, /* recursive */true);
}
this.previousTokenParent = this.parent().clone(this.indentationNodeContextPool());
position += width(token);
// Extract any trailing comments
if (token.trailingTriviaWidth() !== 0) {
this.processTrivia(token.trailingTrivia(), position);
}
}
private processTrivia(triviaList: ISyntaxTriviaList, fullStart: number) {
var position = fullStart;
for (var i = 0, n = triviaList.count(); i < n ; i++) {
var trivia = triviaList.syntaxTriviaAt(i);
// For a comment, format it like it is a token. For skipped text, eat it up as a token, but skip the formatting
if (trivia.isComment() || trivia.isSkippedToken()) {
var currentTokenSpan = new TokenSpan(trivia.kind(), position, trivia.fullWidth());
if (this.textSpan().containsTextSpan(currentTokenSpan)) {
if (trivia.isComment() && this.previousTokenSpan) {
// Note that formatPair calls TrimWhitespaceInLineRange in between the 2 tokens
this.formatPair(this.previousTokenSpan, this.previousTokenParent, currentTokenSpan, this.parent());
}
else {
// We still want to trim whitespace even if it is the first trivia of the first token. Trim from the beginning of the span to the trivia
var startLine = this.getLineNumber(this.previousTokenSpan || this.textSpan());
this.trimWhitespaceInLineRange(startLine, this.getLineNumber(currentTokenSpan));
}
this.previousTokenSpan = currentTokenSpan;
if (this.previousTokenParent) {
// Make sure to clear the previous parent before assigning a new value to it
this.indentationNodeContextPool().releaseNode(this.previousTokenParent, /* recursive */true);
}
this.previousTokenParent = this.parent().clone(this.indentationNodeContextPool());
}
}
position += trivia.fullWidth();
}
}
private findCommonParents(parent1: IndentationNodeContext, parent2: IndentationNodeContext): IndentationNodeContext {
// TODO: disable debug assert message
var shallowParent: IndentationNodeContext;
var shallowParentDepth: number;
var deepParent: IndentationNodeContext;
var deepParentDepth: number;
if (parent1.depth() < parent2.depth()) {
shallowParent = parent1;
shallowParentDepth = parent1.depth();
deepParent = parent2;
deepParentDepth = parent2.depth();
}
else {
shallowParent = parent2;
shallowParentDepth = parent2.depth();
deepParent = parent1;
deepParentDepth = parent1.depth();
}
Debug.assert(shallowParentDepth >= 0, "Expected shallowParentDepth >= 0");
Debug.assert(deepParentDepth >= 0, "Expected deepParentDepth >= 0");
Debug.assert(deepParentDepth >= shallowParentDepth, "Expected deepParentDepth >= shallowParentDepth");
while (deepParentDepth > shallowParentDepth) {
deepParent = <IndentationNodeContext>deepParent.parent();
deepParentDepth--;
}
Debug.assert(deepParentDepth === shallowParentDepth, "Expected deepParentDepth === shallowParentDepth");
while (deepParent.node() && shallowParent.node()) {
if (deepParent.node() === shallowParent.node()) {
return deepParent;
}
deepParent = <IndentationNodeContext>deepParent.parent();
shallowParent = <IndentationNodeContext>shallowParent.parent();
}
// The root should be the first element in the parent chain, we can not be here unless something wrong
// happened along the way
throw Errors.invalidOperation();
}
private formatPair(t1: TokenSpan, t1Parent: IndentationNodeContext, t2: TokenSpan, t2Parent: IndentationNodeContext): void {
var token1Line = this.getLineNumber(t1);
var token2Line = this.getLineNumber(t2);
// Find common parent
var commonParent= this.findCommonParents(t1Parent, t2Parent);
// Update the context
this.formattingContext.updateContext(t1, t1Parent, t2, t2Parent, commonParent);
// Find rules matching the current context
var rule = this.rulesProvider.getRulesMap().GetRule(this.formattingContext);
if (rule != null) {
// Record edits from the rule
this.RecordRuleEdits(rule, t1, t2);
// Handle the case where the next line is moved to be the end of this line.
// In this case we don't indent the next line in the next pass.
if ((rule.Operation.Action == RuleAction.Space || rule.Operation.Action == RuleAction.Delete) &&
token1Line != token2Line) {
this.forceSkipIndentingNextToken(t2.start());
}
// Handle the case where token2 is moved to the new line.
// In this case we indent token2 in the next pass but we set
// sameLineIndent flag to notify the indenter that the indentation is within the line.
if (rule.Operation.Action == RuleAction.NewLine && token1Line == token2Line) {
this.forceIndentNextToken(t2.start());
}
}
// We need to trim trailing whitespace between the tokens if they were on different lines, and no rule was applied to put them on the same line
if (token1Line != token2Line && (!rule || (rule.Operation.Action != RuleAction.Delete && rule.Flag != RuleFlags.CanDeleteNewLines))) {
this.trimWhitespaceInLineRange(token1Line, token2Line, t1);
}
}
private getLineNumber(span: TextSpan): number {
return this.snapshot().getLineNumberFromPosition(span.start());
}
private trimWhitespaceInLineRange(startLine: number, endLine: number, token?: TokenSpan): void {
for (var lineNumber = startLine; lineNumber < endLine; ++lineNumber) {
var line = this.snapshot().getLineFromLineNumber(lineNumber);
this.trimWhitespace(line, token);
}
}
private trimWhitespace(line: ITextSnapshotLine, token?: TokenSpan): void {
// Don't remove the trailing spaces inside comments (this includes line comments and block comments)
if (token && (token.kind == SyntaxKind.MultiLineCommentTrivia || token.kind == SyntaxKind.SingleLineCommentTrivia) && token.start() <= line.endPosition() && token.end() >= line.endPosition())
return;
var text = line.getText();
var index = 0;
for (index = text.length - 1; index >= 0; --index) {
if (!CharacterInfo.isWhitespace(text.charCodeAt(index))) {
break;
}
}
++index;
if (index < text.length) {
this.recordEdit(line.startPosition() + index, line.length() - index, "");
}
}
private RecordRuleEdits(rule: Rule, t1: TokenSpan, t2: TokenSpan): void {
if (rule.Operation.Action == RuleAction.Ignore) {
return;
}
var betweenSpan: TextSpan;
switch (rule.Operation.Action) {
case RuleAction.Delete:
{
betweenSpan = new TextSpan(t1.end(), t2.start() - t1.end());
if (betweenSpan.length() > 0) {
this.recordEdit(betweenSpan.start(), betweenSpan.length(), "");
return;
}
}
break;
case RuleAction.NewLine:
{
if (!(rule.Flag == RuleFlags.CanDeleteNewLines || this.getLineNumber(t1) == this.getLineNumber(t2))) {
return;
}
betweenSpan = new TextSpan(t1.end(), t2.start() - t1.end());
var doEdit = false;
var betweenText = this.snapshot().getText(betweenSpan);
var lineFeedLoc = betweenText.indexOf(this.options.newLineCharacter);
if (lineFeedLoc < 0) {
// no linefeeds, do the edit
doEdit = true;
}
else {
// We only require one line feed. If there is another one, do the edit
lineFeedLoc = betweenText.indexOf(this.options.newLineCharacter, lineFeedLoc + 1);
if (lineFeedLoc >= 0) {
doEdit = true;
}
}
if (doEdit) {
this.recordEdit(betweenSpan.start(), betweenSpan.length(), this.options.newLineCharacter);
return;
}
}
break;
case RuleAction.Space:
{
if (!(rule.Flag == RuleFlags.CanDeleteNewLines || this.getLineNumber(t1) == this.getLineNumber(t2))) {
return;
}
betweenSpan = new TextSpan(t1.end(), t2.start() - t1.end());
if (betweenSpan.length() > 1 || this.snapshot().getText(betweenSpan) != " ") {
this.recordEdit(betweenSpan.start(), betweenSpan.length(), " ");
return;
}
}
break;
}
}
}
}

View File

@@ -0,0 +1,40 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='..\typescriptServices.ts' />
///<reference path='textSnapshot.ts' />
///<reference path='textSnapshotLine.ts' />
///<reference path='snapshotPoint.ts' />
///<reference path='formattingContext.ts' />
///<reference path='formattingManager.ts' />
///<reference path='formattingRequestKind.ts' />
///<reference path='rule.ts' />
///<reference path='ruleAction.ts' />
///<reference path='ruleDescriptor.ts' />
///<reference path='ruleFlag.ts' />
///<reference path='ruleOperation.ts' />
///<reference path='ruleOperationContext.ts' />
///<reference path='rules.ts' />
///<reference path='rulesMap.ts' />
///<reference path='rulesProvider.ts' />
///<reference path='textEditInfo.ts' />
///<reference path='tokenRange.ts' />
///<reference path='tokenSpan.ts' />
///<reference path='indentationNodeContext.ts' />
///<reference path='indentationNodeContextPool.ts' />
///<reference path='indentationTrackingWalker.ts' />
///<reference path='multipleTokenIndenter.ts' />
///<reference path='singleTokenIndenter.ts' />
///<reference path='formatter.ts' />

View File

@@ -0,0 +1,116 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// <reference path="formatting.ts"/>
module TypeScript.Services.Formatting {
export class FormattingContext {
public currentTokenSpan: TokenSpan = null;
public nextTokenSpan: TokenSpan = null;
public contextNode: IndentationNodeContext = null;
public currentTokenParent: IndentationNodeContext = null;
public nextTokenParent: IndentationNodeContext = null;
private contextNodeAllOnSameLine: boolean = null;
private nextNodeAllOnSameLine: boolean = null;
private tokensAreOnSameLine: boolean = null;
private contextNodeBlockIsOnOneLine: boolean = null;
private nextNodeBlockIsOnOneLine: boolean = null;
constructor(private snapshot: ITextSnapshot, public formattingRequestKind: FormattingRequestKind) {
Debug.assert(this.snapshot != null, "snapshot is null");
}
public updateContext(currentTokenSpan: TokenSpan, currentTokenParent: IndentationNodeContext, nextTokenSpan: TokenSpan, nextTokenParent: IndentationNodeContext, commonParent: IndentationNodeContext) {
Debug.assert(currentTokenSpan != null, "currentTokenSpan is null");
Debug.assert(currentTokenParent != null, "currentTokenParent is null");
Debug.assert(nextTokenSpan != null, "nextTokenSpan is null");
Debug.assert(nextTokenParent != null, "nextTokenParent is null");
Debug.assert(commonParent != null, "commonParent is null");
this.currentTokenSpan = currentTokenSpan;
this.currentTokenParent = currentTokenParent;
this.nextTokenSpan = nextTokenSpan;
this.nextTokenParent = nextTokenParent;
this.contextNode = commonParent;
this.contextNodeAllOnSameLine = null;
this.nextNodeAllOnSameLine = null;
this.tokensAreOnSameLine = null;
this.contextNodeBlockIsOnOneLine = null;
this.nextNodeBlockIsOnOneLine = null;
}
public ContextNodeAllOnSameLine(): boolean {
if (this.contextNodeAllOnSameLine === null) {
this.contextNodeAllOnSameLine = this.NodeIsOnOneLine(this.contextNode);
}
return this.contextNodeAllOnSameLine;
}
public NextNodeAllOnSameLine(): boolean {
if (this.nextNodeAllOnSameLine === null) {
this.nextNodeAllOnSameLine = this.NodeIsOnOneLine(this.nextTokenParent);
}
return this.nextNodeAllOnSameLine;
}
public TokensAreOnSameLine(): boolean {
if (this.tokensAreOnSameLine === null) {
var startLine = this.snapshot.getLineNumberFromPosition(this.currentTokenSpan.start());
var endLine = this.snapshot.getLineNumberFromPosition(this.nextTokenSpan.start());
this.tokensAreOnSameLine = (startLine == endLine);
}
return this.tokensAreOnSameLine;
}
public ContextNodeBlockIsOnOneLine() {
if (this.contextNodeBlockIsOnOneLine === null) {
this.contextNodeBlockIsOnOneLine = this.BlockIsOnOneLine(this.contextNode);
}
return this.contextNodeBlockIsOnOneLine;
}
public NextNodeBlockIsOnOneLine() {
if (this.nextNodeBlockIsOnOneLine === null) {
this.nextNodeBlockIsOnOneLine = this.BlockIsOnOneLine(this.nextTokenParent);
}
return this.nextNodeBlockIsOnOneLine;
}
public NodeIsOnOneLine(node: IndentationNodeContext): boolean {
var startLine = this.snapshot.getLineNumberFromPosition(node.start());
var endLine = this.snapshot.getLineNumberFromPosition(node.end());
return startLine == endLine;
}
// Now we know we have a block (or a fake block represented by some other kind of node with an open and close brace as children).
// IMPORTANT!!! This relies on the invariant that IsBlockContext must return true ONLY for nodes with open and close braces as immediate children
public BlockIsOnOneLine(node: IndentationNodeContext): boolean {
var block = <BlockSyntax>node.node();
// Now check if they are on the same line
return this.snapshot.getLineNumberFromPosition(end(block.openBraceToken)) ===
this.snapshot.getLineNumberFromPosition(start(block.closeBraceToken));
}
}
}

View File

@@ -0,0 +1,125 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// <reference path="formatting.ts"/>
module TypeScript.Services.Formatting {
export class FormattingManager {
private options: FormattingOptions;
constructor(private syntaxTree: SyntaxTree, private snapshot: ITextSnapshot, private rulesProvider: RulesProvider, editorOptions: TypeScript.Services.EditorOptions) {
//
// TODO: convert to use FormattingOptions instead of EditorOptions
this.options = new FormattingOptions(!editorOptions.ConvertTabsToSpaces, editorOptions.TabSize, editorOptions.IndentSize, editorOptions.NewLineCharacter)
}
public formatSelection(minChar: number, limChar: number): TypeScript.Services.TextEdit[] {
var span = TextSpan.fromBounds(minChar, limChar);
return this.formatSpan(span, FormattingRequestKind.FormatSelection);
}
public formatDocument(minChar: number, limChar: number): TypeScript.Services.TextEdit[] {
var span = TextSpan.fromBounds(minChar, limChar);
return this.formatSpan(span, FormattingRequestKind.FormatDocument);
}
public formatOnPaste(minChar: number, limChar: number): TypeScript.Services.TextEdit[] {
var span = TextSpan.fromBounds(minChar, limChar);
return this.formatSpan(span, FormattingRequestKind.FormatOnPaste);
}
public formatOnSemicolon(caretPosition: number): TypeScript.Services.TextEdit[] {
var sourceUnit = this.syntaxTree.sourceUnit();
var semicolonPositionedToken = findToken(sourceUnit, caretPosition - 1);
if (semicolonPositionedToken.kind() === SyntaxKind.SemicolonToken) {
// Find the outer most parent that this semicolon terminates
var current: ISyntaxElement = semicolonPositionedToken;
while (current.parent !== null &&
end(current.parent) === end(semicolonPositionedToken) &&
current.parent.kind() !== SyntaxKind.List) {
current = current.parent;
}
// Compute the span
var span = new TextSpan(fullStart(current), fullWidth(current));
// Format the span
return this.formatSpan(span, FormattingRequestKind.FormatOnSemicolon);
}
return [];
}
public formatOnClosingCurlyBrace(caretPosition: number): TypeScript.Services.TextEdit[] {
var sourceUnit = this.syntaxTree.sourceUnit();
var closeBracePositionedToken = findToken(sourceUnit, caretPosition - 1);
if (closeBracePositionedToken.kind() === SyntaxKind.CloseBraceToken) {
// Find the outer most parent that this closing brace terminates
var current: ISyntaxElement = closeBracePositionedToken;
while (current.parent !== null &&
end(current.parent) === end(closeBracePositionedToken) &&
current.parent.kind() !== SyntaxKind.List) {
current = current.parent;
}
// Compute the span
var span = new TextSpan(fullStart(current), fullWidth(current));
// Format the span
return this.formatSpan(span, FormattingRequestKind.FormatOnClosingCurlyBrace);
}
return [];
}
public formatOnEnter(caretPosition: number): TypeScript.Services.TextEdit[] {
var lineNumber = this.snapshot.getLineNumberFromPosition(caretPosition);
if (lineNumber > 0) {
// Format both lines
var prevLine = this.snapshot.getLineFromLineNumber(lineNumber - 1);
var currentLine = this.snapshot.getLineFromLineNumber(lineNumber);
var span = TextSpan.fromBounds(prevLine.startPosition(), currentLine.endPosition());
// Format the span
return this.formatSpan(span, FormattingRequestKind.FormatOnEnter);
}
return [];
}
private formatSpan(span: TextSpan, formattingRequestKind: FormattingRequestKind): TypeScript.Services.TextEdit[] {
// Always format from the beginning of the line
var startLine = this.snapshot.getLineFromPosition(span.start());
span = TextSpan.fromBounds(startLine.startPosition(), span.end());
var result: TypeScript.Services.TextEdit[] = [];
var formattingEdits = Formatter.getEdits(span, this.syntaxTree.sourceUnit(), this.options, true, this.snapshot, this.rulesProvider, formattingRequestKind);
//
// TODO: Change the ILanguageService interface to return TextEditInfo (with start, and length) instead of TextEdit (with minChar and limChar)
formattingEdits.forEach((item) => {
var edit = new TypeScript.Services.TextEdit(item.position, item.position + item.length, item.replaceWith);
result.push(edit);
});
return result;
}
}
}

View File

@@ -0,0 +1,27 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// <reference path="formatting.ts"/>
module TypeScript.Services.Formatting {
export enum FormattingRequestKind {
FormatDocument,
FormatSelection,
FormatOnEnter,
FormatOnSemicolon,
FormatOnClosingCurlyBrace,
FormatOnPaste
}
}

View File

@@ -0,0 +1,103 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class IndentationNodeContext {
private _node: ISyntaxNode;
private _parent: IndentationNodeContext;
private _fullStart: number;
private _indentationAmount: number;
private _childIndentationAmountDelta: number;
private _depth: number;
private _hasSkippedOrMissingTokenChild: boolean;
constructor(parent: IndentationNodeContext, node: ISyntaxNode, fullStart: number, indentationAmount: number, childIndentationAmountDelta: number) {
this.update(parent, node, fullStart, indentationAmount, childIndentationAmountDelta);
}
public parent(): IndentationNodeContext {
return this._parent;
}
public node(): ISyntaxNode {
return this._node;
}
public fullStart(): number {
return this._fullStart;
}
public fullWidth(): number {
return fullWidth(this._node);
}
public start(): number {
return this._fullStart + leadingTriviaWidth(this._node);
}
public end(): number {
return this._fullStart + leadingTriviaWidth(this._node) + width(this._node);
}
public indentationAmount(): number {
return this._indentationAmount;
}
public childIndentationAmountDelta(): number {
return this._childIndentationAmountDelta;
}
public depth(): number {
return this._depth;
}
public kind(): SyntaxKind {
return this._node.kind();
}
public hasSkippedOrMissingTokenChild(): boolean {
if (this._hasSkippedOrMissingTokenChild === null) {
this._hasSkippedOrMissingTokenChild = Syntax.nodeHasSkippedOrMissingTokens(this._node);
}
return this._hasSkippedOrMissingTokenChild;
}
public clone(pool: IndentationNodeContextPool): IndentationNodeContext {
var parent: IndentationNodeContext = null;
if (this._parent) {
parent = this._parent.clone(pool);
}
return pool.getNode(parent, this._node, this._fullStart, this._indentationAmount, this._childIndentationAmountDelta);
}
public update(parent: IndentationNodeContext, node: ISyntaxNode, fullStart: number, indentationAmount: number, childIndentationAmountDelta: number) {
this._parent = parent;
this._node = node;
this._fullStart = fullStart;
this._indentationAmount = indentationAmount;
this._childIndentationAmountDelta = childIndentationAmountDelta;
this._hasSkippedOrMissingTokenChild = null;
if (parent) {
this._depth = parent.depth() + 1;
}
else {
this._depth = 0;
}
}
}
}

View File

@@ -0,0 +1,43 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class IndentationNodeContextPool {
private nodes: IndentationNodeContext[] = [];
public getNode(parent: IndentationNodeContext, node: ISyntaxNode, fullStart: number, indentationLevel: number, childIndentationLevelDelta: number): IndentationNodeContext {
if (this.nodes.length > 0) {
var cachedNode = this.nodes.pop();
cachedNode.update(parent, node, fullStart, indentationLevel, childIndentationLevelDelta);
return cachedNode;
}
return new IndentationNodeContext(parent, node, fullStart, indentationLevel, childIndentationLevelDelta);
}
public releaseNode(node: IndentationNodeContext, recursive: boolean = false): void {
this.nodes.push(node);
if (recursive) {
var parent = node.parent();
if (parent) {
this.releaseNode(parent, recursive);
}
}
}
}
}

View File

@@ -0,0 +1,349 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class IndentationTrackingWalker extends SyntaxWalker {
private _position: number = 0;
private _parent: IndentationNodeContext = null;
private _textSpan: TextSpan;
private _snapshot: ITextSnapshot;
private _lastTriviaWasNewLine: boolean;
private _indentationNodeContextPool: IndentationNodeContextPool;
private _text: ISimpleText;
constructor(textSpan: TextSpan, sourceUnit: SourceUnitSyntax, snapshot: ITextSnapshot, indentFirstToken: boolean, public options: FormattingOptions) {
super();
// Create a pool object to manage context nodes while walking the tree
this._indentationNodeContextPool = new IndentationNodeContextPool();
this._textSpan = textSpan;
this._text = sourceUnit.syntaxTree.text;
this._snapshot = snapshot;
this._parent = this._indentationNodeContextPool.getNode(null, sourceUnit, 0, 0, 0);
// Is the first token in the span at the start of a new line.
this._lastTriviaWasNewLine = indentFirstToken;
}
public position(): number {
return this._position;
}
public parent(): IndentationNodeContext {
return this._parent;
}
public textSpan(): TextSpan {
return this._textSpan;
}
public snapshot(): ITextSnapshot {
return this._snapshot;
}
public indentationNodeContextPool(): IndentationNodeContextPool {
return this._indentationNodeContextPool;
}
public forceIndentNextToken(tokenStart: number): void {
this._lastTriviaWasNewLine = true;
this.forceRecomputeIndentationOfParent(tokenStart, true);
}
public forceSkipIndentingNextToken(tokenStart: number): void {
this._lastTriviaWasNewLine = false;
this.forceRecomputeIndentationOfParent(tokenStart, false);
}
public indentToken(token: ISyntaxToken, indentationAmount: number, commentIndentationAmount: number): void {
throw Errors.abstract();
}
public visitTokenInSpan(token: ISyntaxToken): void {
if (this._lastTriviaWasNewLine) {
// Compute the indentation level at the current token
var indentationAmount = this.getTokenIndentationAmount(token);
var commentIndentationAmount = this.getCommentIndentationAmount(token);
// Process the token
this.indentToken(token, indentationAmount, commentIndentationAmount);
}
}
public visitToken(token: ISyntaxToken): void {
var tokenSpan = new TextSpan(this._position, token.fullWidth());
if (tokenSpan.intersectsWithTextSpan(this._textSpan)) {
this.visitTokenInSpan(token);
// Only track new lines on tokens within the range. Make sure to check that the last trivia is a newline, and not just one of the trivia
var trivia = token.trailingTrivia();
this._lastTriviaWasNewLine = trivia.hasNewLine() && trivia.syntaxTriviaAt(trivia.count() - 1).kind() == SyntaxKind.NewLineTrivia;
}
// Update the position
this._position += token.fullWidth();
}
public visitNode(node: ISyntaxNode): void {
var nodeSpan = new TextSpan(this._position, fullWidth(node));
if (nodeSpan.intersectsWithTextSpan(this._textSpan)) {
// Update indentation level
var indentation = this.getNodeIndentation(node);
// Update the parent
var currentParent = this._parent;
this._parent = this._indentationNodeContextPool.getNode(currentParent, node, this._position, indentation.indentationAmount, indentation.indentationAmountDelta);
// Visit node
visitNodeOrToken(this, node);
// Reset state
this._indentationNodeContextPool.releaseNode(this._parent);
this._parent = currentParent;
}
else {
// We're skipping the node, so update our position accordingly.
this._position += fullWidth(node);
}
}
private getTokenIndentationAmount(token: ISyntaxToken): number {
// If this is the first token of a node, it should follow the node indentation and not the child indentation;
// (e.g.class in a class declaration or module in module declariotion).
// Open and close braces should follow the indentation of thier parent as well(e.g.
// class {
// }
// Also in a do-while statement, the while should be indented like the parent.
if (firstToken(this._parent.node()) === token ||
token.kind() === SyntaxKind.OpenBraceToken || token.kind() === SyntaxKind.CloseBraceToken ||
token.kind() === SyntaxKind.OpenBracketToken || token.kind() === SyntaxKind.CloseBracketToken ||
(token.kind() === SyntaxKind.WhileKeyword && this._parent.node().kind() == SyntaxKind.DoStatement)) {
return this._parent.indentationAmount();
}
return (this._parent.indentationAmount() + this._parent.childIndentationAmountDelta());
}
private getCommentIndentationAmount(token: ISyntaxToken): number {
// If this is token terminating an indentation scope, leading comments should be indented to follow the children
// indentation level and not the node
if (token.kind() === SyntaxKind.CloseBraceToken || token.kind() === SyntaxKind.CloseBracketToken) {
return (this._parent.indentationAmount() + this._parent.childIndentationAmountDelta());
}
return this._parent.indentationAmount();
}
private getNodeIndentation(node: ISyntaxNode, newLineInsertedByFormatting?: boolean): { indentationAmount: number; indentationAmountDelta: number; } {
var parent = this._parent;
// We need to get the parent's indentation, which could be one of 2 things. If first token of the parent is in the span, use the parent's computed indentation.
// If the parent was outside the span, use the actual indentation of the parent.
var parentIndentationAmount: number;
if (this._textSpan.containsPosition(parent.start())) {
parentIndentationAmount = parent.indentationAmount();
}
else {
if (parent.kind() === SyntaxKind.Block && !this.shouldIndentBlockInParent(this._parent.parent())) {
// Blocks preserve the indentation of their containing node (unless they're a
// standalone block in a list). i.e. if you have:
//
// function foo(
// a: number) {
//
// Then we expect the indentation of the block to be tied to the function, not to
// the line that the block is defined on. If we were to do the latter, then the
// indentation would be here:
//
// function foo(
// a: number) {
// |
//
// Instead of:
//
// function foo(
// a: number) {
// |
parent = this._parent.parent();
}
var line = this._snapshot.getLineFromPosition(parent.start()).getText();
var firstNonWhiteSpacePosition = Indentation.firstNonWhitespacePosition(line);
parentIndentationAmount = Indentation.columnForPositionInString(line, firstNonWhiteSpacePosition, this.options);
}
var parentIndentationAmountDelta = parent.childIndentationAmountDelta();
// The indentation level of the node
var indentationAmount: number;
// The delta it adds to its children.
var indentationAmountDelta: number;
var parentNode = parent.node();
switch (node.kind()) {
default:
// General case
// This node should follow the child indentation set by its parent
// This node does not introduce any new indentation scope, indent any decendants of this node (tokens or child nodes)
// using the same indentation level
indentationAmount = (parentIndentationAmount + parentIndentationAmountDelta);
indentationAmountDelta = 0;
break;
// Statements introducing {}
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ObjectType:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.SwitchStatement:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.IndexMemberDeclaration:
case SyntaxKind.CatchClause:
// Statements introducing []
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ArrayType:
case SyntaxKind.ElementAccessExpression:
case SyntaxKind.IndexSignature:
// Other statements
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.CaseSwitchClause:
case SyntaxKind.DefaultSwitchClause:
case SyntaxKind.ReturnStatement:
case SyntaxKind.ThrowStatement:
case SyntaxKind.SimpleArrowFunctionExpression:
case SyntaxKind.ParenthesizedArrowFunctionExpression:
case SyntaxKind.VariableDeclaration:
case SyntaxKind.ExportAssignment:
// Expressions which have argument lists or parameter lists
case SyntaxKind.InvocationExpression:
case SyntaxKind.ObjectCreationExpression:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
// These nodes should follow the child indentation set by its parent;
// they introduce a new indenation scope; children should be indented at one level deeper
indentationAmount = (parentIndentationAmount + parentIndentationAmountDelta);
indentationAmountDelta = this.options.indentSpaces;
break;
case SyntaxKind.IfStatement:
if (parent.kind() === SyntaxKind.ElseClause &&
!SyntaxUtilities.isLastTokenOnLine((<ElseClauseSyntax>parentNode).elseKeyword, this._text)) {
// This is an else if statement with the if on the same line as the else, do not indent the if statmement.
// Note: Children indentation has already been set by the parent if statement, so no need to increment
indentationAmount = parentIndentationAmount;
}
else {
// Otherwise introduce a new indenation scope; children should be indented at one level deeper
indentationAmount = (parentIndentationAmount + parentIndentationAmountDelta);
}
indentationAmountDelta = this.options.indentSpaces;
break;
case SyntaxKind.ElseClause:
// Else should always follow its parent if statement indentation.
// Note: Children indentation has already been set by the parent if statement, so no need to increment
indentationAmount = parentIndentationAmount;
indentationAmountDelta = this.options.indentSpaces;
break;
case SyntaxKind.Block:
// Check if the block is a member in a list of statements (if the parent is a source unit, module, or block, or switch clause)
if (this.shouldIndentBlockInParent(parent)) {
indentationAmount = parentIndentationAmount + parentIndentationAmountDelta;
}
else {
indentationAmount = parentIndentationAmount;
}
indentationAmountDelta = this.options.indentSpaces;
break;
}
// If the parent happens to start on the same line as this node, then override the current node indenation with that
// of the parent. This avoid having to add an extra level of indentation for the children. e.g.:
// return {
// a:1
// };
// instead of:
// return {
// a:1
// };
// We also need to pass the delta (if it is nonzero) to the children, so that subsequent lines get indented. Essentially, if any node starting on the given line
// has a nonzero delta , the resulting delta should be inherited from this node. This is to indent cases like the following:
// return a
// || b;
// Lastly, it is possible the node indentation needs to be recomputed because the formatter inserted a newline before its first token.
// If this is the case, we know the node no longer starts on the same line as its parent (or at least we shouldn't treat it as such).
if (parentNode) {
if (!newLineInsertedByFormatting /*This could be false or undefined here*/) {
var parentStartLine = this._snapshot.getLineNumberFromPosition(parent.start());
var currentNodeStartLine = this._snapshot.getLineNumberFromPosition(this._position + leadingTriviaWidth(node));
if (parentStartLine === currentNodeStartLine || newLineInsertedByFormatting === false /*meaning a new line was removed and we are force recomputing*/) {
indentationAmount = parentIndentationAmount;
indentationAmountDelta = Math.min(this.options.indentSpaces, parentIndentationAmountDelta + indentationAmountDelta);
}
}
}
return {
indentationAmount: indentationAmount,
indentationAmountDelta: indentationAmountDelta
};
}
private shouldIndentBlockInParent(parent: IndentationNodeContext): boolean {
switch (parent.kind()) {
case SyntaxKind.SourceUnit:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.Block:
case SyntaxKind.CaseSwitchClause:
case SyntaxKind.DefaultSwitchClause:
return true;
default:
return false;
}
}
private forceRecomputeIndentationOfParent(tokenStart: number, newLineAdded: boolean /*as opposed to removed*/): void {
var parent = this._parent;
if (parent.fullStart() === tokenStart) {
// Temporarily pop the parent before recomputing
this._parent = parent.parent();
var indentation = this.getNodeIndentation(parent.node(), /* newLineInsertedByFormatting */ newLineAdded);
parent.update(parent.parent(), parent.node(), parent.fullStart(), indentation.indentationAmount, indentation.indentationAmountDelta);
this._parent = parent;
}
}
}
}

View File

@@ -0,0 +1,206 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class MultipleTokenIndenter extends IndentationTrackingWalker {
private _edits: TextEditInfo[] = [];
constructor(textSpan: TextSpan, sourceUnit: SourceUnitSyntax, snapshot: ITextSnapshot, indentFirstToken: boolean, options: FormattingOptions) {
super(textSpan, sourceUnit, snapshot, indentFirstToken, options);
}
public indentToken(token: ISyntaxToken, indentationAmount: number, commentIndentationAmount: number): void {
// Ignore generated tokens
if (token.fullWidth() === 0) {
return;
}
// If we have any skipped tokens as children, do not process this node for indentation or formatting
if (this.parent().hasSkippedOrMissingTokenChild()) {
return;
}
// Be strict, and only consider nodes that fall inside the span. This avoids indenting a multiline string
// on enter at the end of, as the whole token was not included in the span
var tokenSpan = new TextSpan(this.position() + token.leadingTriviaWidth(), width(token));
if (!this.textSpan().containsTextSpan(tokenSpan)) {
return;
}
// Compute an indentation string for this token
var indentationString = Indentation.indentationString(indentationAmount, this.options);
var commentIndentationString = Indentation.indentationString(commentIndentationAmount, this.options);
// Record any needed indentation edits
this.recordIndentationEditsForToken(token, indentationString, commentIndentationString);
}
public edits(): TextEditInfo[]{
return this._edits;
}
public recordEdit(position: number, length: number, replaceWith: string): void {
this._edits.push(new TextEditInfo(position, length, replaceWith));
}
private recordIndentationEditsForToken(token: ISyntaxToken, indentationString: string, commentIndentationString: string) {
var position = this.position();
var indentNextTokenOrTrivia = true;
var leadingWhiteSpace = ""; // We need to track the whitespace before a multiline comment
// Process any leading trivia if any
var triviaList = token.leadingTrivia();
if (triviaList) {
for (var i = 0, length = triviaList.count(); i < length; i++, position += trivia.fullWidth()) {
var trivia = triviaList.syntaxTriviaAt(i);
// Skip this trivia if it is not in the span
if (!this.textSpan().containsTextSpan(new TextSpan(position, trivia.fullWidth()))) {
continue;
}
switch (trivia.kind()) {
case SyntaxKind.MultiLineCommentTrivia:
// We will only indent the first line of the multiline comment if we were planning to indent the next trivia. However,
// subsequent lines will always be indented
this.recordIndentationEditsForMultiLineComment(trivia, position, commentIndentationString, leadingWhiteSpace, !indentNextTokenOrTrivia /* already indented first line */);
indentNextTokenOrTrivia = false;
leadingWhiteSpace = "";
break;
case SyntaxKind.SingleLineCommentTrivia:
case SyntaxKind.SkippedTokenTrivia:
if (indentNextTokenOrTrivia) {
this.recordIndentationEditsForSingleLineOrSkippedText(trivia, position, commentIndentationString);
indentNextTokenOrTrivia = false;
}
break;
case SyntaxKind.WhitespaceTrivia:
// If the next trivia is a comment, use the comment indentation level instead of the regular indentation level
// If the next trivia is a newline, this whole line is just whitespace, so don't do anything (trimming will take care of it)
var nextTrivia = length > i + 1 && triviaList.syntaxTriviaAt(i + 1);
var whiteSpaceIndentationString = nextTrivia && nextTrivia.isComment() ? commentIndentationString : indentationString;
if (indentNextTokenOrTrivia) {
if (!(nextTrivia && nextTrivia.isNewLine())) {
this.recordIndentationEditsForWhitespace(trivia, position, whiteSpaceIndentationString);
}
indentNextTokenOrTrivia = false;
}
leadingWhiteSpace += trivia.fullText();
break;
case SyntaxKind.NewLineTrivia:
// We hit a newline processing the trivia. We need to add the indentation to the
// next line as well. Note: don't bother indenting the newline itself. This will
// just insert ugly whitespace that most users probably will not want.
indentNextTokenOrTrivia = true;
leadingWhiteSpace = "";
break;
default:
throw Errors.invalidOperation();
}
}
}
if (token.kind() !== SyntaxKind.EndOfFileToken && indentNextTokenOrTrivia) {
// If the last trivia item was a new line, or no trivia items were encounterd record the
// indentation edit at the token position
if (indentationString.length > 0) {
this.recordEdit(position, 0, indentationString);
}
}
}
private recordIndentationEditsForSingleLineOrSkippedText(trivia: ISyntaxTrivia, fullStart: number, indentationString: string): void {
// Record the edit
if (indentationString.length > 0) {
this.recordEdit(fullStart, 0, indentationString);
}
}
private recordIndentationEditsForWhitespace(trivia: ISyntaxTrivia, fullStart: number, indentationString: string): void {
var text = trivia.fullText();
// Check if the current indentation matches the desired indentation or not
if (indentationString === text) {
return;
}
// Record the edit
this.recordEdit(fullStart, text.length, indentationString);
}
private recordIndentationEditsForMultiLineComment(trivia: ISyntaxTrivia, fullStart: number, indentationString: string, leadingWhiteSpace: string, firstLineAlreadyIndented: boolean): void {
// If the multiline comment spans multiple lines, we need to add the right indent amount to
// each successive line segment as well.
var position = fullStart;
var segments = Syntax.splitMultiLineCommentTriviaIntoMultipleLines(trivia);
if (segments.length <= 1) {
if (!firstLineAlreadyIndented) {
// Process the one-line multiline comment just like a single line comment
this.recordIndentationEditsForSingleLineOrSkippedText(trivia, fullStart, indentationString);
}
return;
}
// Find number of columns in first segment
var whiteSpaceColumnsInFirstSegment = Indentation.columnForPositionInString(leadingWhiteSpace, leadingWhiteSpace.length, this.options);
var indentationColumns = Indentation.columnForPositionInString(indentationString, indentationString.length, this.options);
var startIndex = 0;
if (firstLineAlreadyIndented) {
startIndex = 1;
position += segments[0].length;
}
for (var i = startIndex; i < segments.length; i++) {
var segment = segments[i];
this.recordIndentationEditsForSegment(segment, position, indentationColumns, whiteSpaceColumnsInFirstSegment);
position += segment.length;
}
}
private recordIndentationEditsForSegment(segment: string, fullStart: number, indentationColumns: number, whiteSpaceColumnsInFirstSegment: number): void {
// Indent subsequent lines using a column delta of the actual indentation relative to the first line
var firstNonWhitespacePosition = Indentation.firstNonWhitespacePosition(segment);
var leadingWhiteSpaceColumns = Indentation.columnForPositionInString(segment, firstNonWhitespacePosition, this.options);
var deltaFromFirstSegment = leadingWhiteSpaceColumns - whiteSpaceColumnsInFirstSegment;
var finalColumns = indentationColumns + deltaFromFirstSegment;
if (finalColumns < 0) {
finalColumns = 0;
}
var indentationString = Indentation.indentationString(finalColumns, this.options);
if (firstNonWhitespacePosition < segment.length &&
CharacterInfo.isLineTerminator(segment.charCodeAt(firstNonWhitespacePosition))) {
// If this segment was just a newline, then don't bother indenting it. That will just
// leave the user with an ugly indent in their output that they probably do not want.
return;
}
if (indentationString === segment.substring(0, firstNonWhitespacePosition)) {
return;
}
// Record the edit
this.recordEdit(fullStart, firstNonWhitespacePosition, indentationString);
}
}
}

View File

@@ -0,0 +1,32 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class Rule {
constructor(
public Descriptor: RuleDescriptor,
public Operation: RuleOperation,
public Flag: RuleFlags = RuleFlags.None) {
}
public toString() {
return "[desc=" + this.Descriptor + "," +
"operation=" + this.Operation + "," +
"flag=" + this.Flag + "]";
}
}
}

View File

@@ -0,0 +1,25 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export enum RuleAction {
Ignore,
Space,
NewLine,
Delete
}
}

View File

@@ -0,0 +1,46 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class RuleDescriptor {
constructor(public LeftTokenRange: Shared.TokenRange, public RightTokenRange: Shared.TokenRange) {
}
public toString(): string {
return "[leftRange=" + this.LeftTokenRange + "," +
"rightRange=" + this.RightTokenRange + "]";
}
static create1(left: SyntaxKind, right: SyntaxKind): RuleDescriptor {
return RuleDescriptor.create4(Shared.TokenRange.FromToken(left), Shared.TokenRange.FromToken(right))
}
static create2(left: Shared.TokenRange, right: SyntaxKind): RuleDescriptor {
return RuleDescriptor.create4(left, Shared.TokenRange.FromToken(right));
}
static create3(left: SyntaxKind, right: Shared.TokenRange): RuleDescriptor
//: this(TokenRange.FromToken(left), right)
{
return RuleDescriptor.create4(Shared.TokenRange.FromToken(left), right);
}
static create4(left: Shared.TokenRange, right: Shared.TokenRange): RuleDescriptor {
return new RuleDescriptor(left, right);
}
}
}

View File

@@ -0,0 +1,23 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export enum RuleFlags {
None,
CanDeleteNewLines
}
}

View File

@@ -0,0 +1,44 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class RuleOperation {
public Context: RuleOperationContext;
public Action: RuleAction;
constructor() {
this.Context = null;
this.Action = null;
}
public toString(): string {
return "[context=" + this.Context + "," +
"action=" + this.Action + "]";
}
static create1(action: RuleAction) {
return RuleOperation.create2(RuleOperationContext.Any, action)
}
static create2(context: RuleOperationContext, action: RuleAction) {
var result = new RuleOperation();
result.Context = context;
result.Action = action;
return result;
}
}
}

View File

@@ -0,0 +1,47 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class RuleOperationContext {
private customContextChecks: { (context: FormattingContext): boolean; }[];
constructor(...funcs: { (context: FormattingContext): boolean; }[]) {
this.customContextChecks = funcs;
}
static Any: RuleOperationContext = new RuleOperationContext();
public IsAny(): boolean {
return this == RuleOperationContext.Any;
}
public InContext(context: FormattingContext): boolean {
if (this.IsAny()) {
return true;
}
for (var i = 0, len = this.customContextChecks.length; i < len; i++) {
if (!this.customContextChecks[i](context)) {
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,678 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class Rules {
public getRuleName(rule: Rule) {
var o: IIndexable<any> = <any>this;
for (var name in o) {
if (o[name] === rule) {
return name;
}
}
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Unknown_rule, null));
}
[name: string]: any;
public IgnoreBeforeComment: Rule;
public IgnoreAfterLineComment: Rule;
// Space after keyword but not before ; or : or ?
public NoSpaceBeforeSemicolon: Rule;
public NoSpaceBeforeColon: Rule;
public NoSpaceBeforeQMark: Rule;
public SpaceAfterColon: Rule;
public SpaceAfterQMark: Rule;
public SpaceAfterSemicolon: Rule;
// Space/new line after }.
public SpaceAfterCloseBrace: Rule;
// Special case for (}, else) and (}, while) since else & while tokens are not part of the tree which makes SpaceAfterCloseBrace rule not applied
// Also should not apply to })
public SpaceBetweenCloseBraceAndElse: Rule;
public SpaceBetweenCloseBraceAndWhile: Rule;
public NoSpaceAfterCloseBrace: Rule;
// No space for indexer and dot
public NoSpaceBeforeDot: Rule;
public NoSpaceAfterDot: Rule;
public NoSpaceBeforeOpenBracket: Rule;
public NoSpaceAfterOpenBracket: Rule;
public NoSpaceBeforeCloseBracket: Rule;
public NoSpaceAfterCloseBracket: Rule;
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
public SpaceAfterOpenBrace: Rule;
public SpaceBeforeCloseBrace: Rule;
public NoSpaceBetweenEmptyBraceBrackets: Rule;
// Insert new line after { and before } in multi-line contexts.
public NewLineAfterOpenBraceInBlockContext: Rule;
// For functions and control block place } on a new line [multi-line rule]
public NewLineBeforeCloseBraceInBlockContext: Rule;
// Special handling of unary operators.
// Prefix operators generally shouldn't have a space between
// them and their target unary expression.
public NoSpaceAfterUnaryPrefixOperator: Rule;
public NoSpaceAfterUnaryPreincrementOperator: Rule;
public NoSpaceAfterUnaryPredecrementOperator: Rule;
public NoSpaceBeforeUnaryPostincrementOperator: Rule;
public NoSpaceBeforeUnaryPostdecrementOperator: Rule;
// More unary operator special-casing.
// DevDiv 181814: Be careful when removing leading whitespace
// around unary operators. Examples:
// 1 - -2 --X--> 1--2
// a + ++b --X--> a+++b
public SpaceAfterPostincrementWhenFollowedByAdd: Rule;
public SpaceAfterAddWhenFollowedByUnaryPlus: Rule;
public SpaceAfterAddWhenFollowedByPreincrement: Rule;
public SpaceAfterPostdecrementWhenFollowedBySubtract: Rule;
public SpaceAfterSubtractWhenFollowedByUnaryMinus: Rule;
public SpaceAfterSubtractWhenFollowedByPredecrement: Rule;
public NoSpaceBeforeComma: Rule;
public SpaceAfterCertainKeywords: Rule;
public NoSpaceBeforeOpenParenInFuncCall: Rule;
public SpaceAfterFunctionInFuncDecl: Rule;
public NoSpaceBeforeOpenParenInFuncDecl: Rule;
public SpaceAfterVoidOperator: Rule;
public NoSpaceBetweenReturnAndSemicolon: Rule;
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
// So, we have a rule to add a space for [),Any], [do,Any], [else,Any], and [case,Any]
public SpaceBetweenStatements: Rule;
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
public SpaceAfterTryFinally: Rule;
// For get/set members, we check for (identifier,identifier) since get/set don't have tokens and they are represented as just an identifier token.
// Though, we do extra check on the context to make sure we are dealing with get/set node. Example:
// get x() {}
// set x(val) {}
public SpaceAfterGetSetInMember: Rule;
// Special case for binary operators (that are keywords). For these we have to add a space and shouldn't follow any user options.
public SpaceBeforeBinaryKeywordOperator: Rule;
public SpaceAfterBinaryKeywordOperator: Rule;
// TypeScript-specific rules
// Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses
public NoSpaceAfterConstructor: Rule;
// Use of module as a function call. e.g.: import m2 = module("m2");
public NoSpaceAfterModuleImport: Rule;
// Add a space around certain TypeScript keywords
public SpaceAfterCertainTypeScriptKeywords: Rule;
public SpaceBeforeCertainTypeScriptKeywords: Rule;
// Treat string literals in module names as identifiers, and add a space between the literal and the opening Brace braces, e.g.: module "m2" {
public SpaceAfterModuleName: Rule;
// Lambda expressions
public SpaceAfterArrow: Rule;
// Optional parameters and var args
public NoSpaceAfterEllipsis: Rule;
public NoSpaceAfterOptionalParameters: Rule;
// generics
public NoSpaceBeforeOpenAngularBracket: Rule;
public NoSpaceBetweenCloseParenAndAngularBracket: Rule;
public NoSpaceAfterOpenAngularBracket: Rule;
public NoSpaceBeforeCloseAngularBracket: Rule;
public NoSpaceAfterCloseAngularBracket: Rule;
// Remove spaces in empty interface literals. e.g.: x: {}
public NoSpaceBetweenEmptyInterfaceBraceBrackets: Rule;
// These rules are higher in priority than user-configurable rules.
public HighPriorityCommonRules: Rule[];
// These rules are lower in priority than user-configurable rules.
public LowPriorityCommonRules: Rule[];
///
/// Rules controlled by user options
///
// Insert space after comma delimiter
public SpaceAfterComma: Rule;
public NoSpaceAfterComma: Rule;
// Insert space before and after binary operators
public SpaceBeforeBinaryOperator: Rule;
public SpaceAfterBinaryOperator: Rule;
public NoSpaceBeforeBinaryOperator: Rule;
public NoSpaceAfterBinaryOperator: Rule;
// Insert space after keywords in control flow statements
public SpaceAfterKeywordInControl: Rule;
public NoSpaceAfterKeywordInControl: Rule;
// Open Brace braces after function
//TypeScript: Function can have return types, which can be made of tons of different token kinds
public FunctionOpenBraceLeftTokenRange: Shared.TokenRange;
public SpaceBeforeOpenBraceInFunction: Rule;
public NewLineBeforeOpenBraceInFunction: Rule;
// Open Brace braces after TypeScript module/class/interface
public TypeScriptOpenBraceLeftTokenRange: Shared.TokenRange;
public SpaceBeforeOpenBraceInTypeScriptDeclWithBlock: Rule;
public NewLineBeforeOpenBraceInTypeScriptDeclWithBlock: Rule;
// Open Brace braces after control block
public ControlOpenBraceLeftTokenRange: Shared.TokenRange;
public SpaceBeforeOpenBraceInControl: Rule;
public NewLineBeforeOpenBraceInControl: Rule;
// Insert space after semicolon in for statement
public SpaceAfterSemicolonInFor: Rule;
public NoSpaceAfterSemicolonInFor: Rule;
// Insert space after opening and before closing nonempty parenthesis
public SpaceAfterOpenParen: Rule;
public SpaceBeforeCloseParen: Rule;
public NoSpaceBetweenParens: Rule;
public NoSpaceAfterOpenParen: Rule;
public NoSpaceBeforeCloseParen: Rule;
// Insert space after function keyword for anonymous functions
public SpaceAfterAnonymousFunctionKeyword: Rule;
public NoSpaceAfterAnonymousFunctionKeyword: Rule;
constructor() {
///
/// Common Rules
///
// Leave comments alone
this.IgnoreBeforeComment = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.Comments), RuleOperation.create1(RuleAction.Ignore));
this.IgnoreAfterLineComment = new Rule(RuleDescriptor.create3(SyntaxKind.SingleLineCommentTrivia, Shared.TokenRange.Any), RuleOperation.create1(RuleAction.Ignore));
// Space after keyword but not before ; or : or ?
this.NoSpaceBeforeSemicolon = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.SemicolonToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeColon = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.ColonToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
this.NoSpaceBeforeQMark = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.QuestionToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
this.SpaceAfterColon = new Rule(RuleDescriptor.create3(SyntaxKind.ColonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Space));
this.SpaceAfterQMark = new Rule(RuleDescriptor.create3(SyntaxKind.QuestionToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Space));
this.SpaceAfterSemicolon = new Rule(RuleDescriptor.create3(SyntaxKind.SemicolonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
// Space after }.
this.SpaceAfterCloseBrace = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsAfterCodeBlockContext), RuleAction.Space));
// Special case for (}, else) and (}, while) since else & while tokens are not part of the tree which makes SpaceAfterCloseBrace rule not applied
this.SpaceBetweenCloseBraceAndElse = new Rule(RuleDescriptor.create1(SyntaxKind.CloseBraceToken, SyntaxKind.ElseKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.SpaceBetweenCloseBraceAndWhile = new Rule(RuleDescriptor.create1(SyntaxKind.CloseBraceToken, SyntaxKind.WhileKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceAfterCloseBrace = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBraceToken, Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken, SyntaxKind.CommaToken, SyntaxKind.SemicolonToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// No space for indexer and dot
this.NoSpaceBeforeDot = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.DotToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterDot = new Rule(RuleDescriptor.create3(SyntaxKind.DotToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeOpenBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterOpenBracket = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeCloseBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Place a space before open brace in a function declaration
this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments;
this.SpaceBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
// Place a space before open brace in a TypeScript declaration that has braces as children (class, module, enum, etc)
this.TypeScriptOpenBraceLeftTokenRange = Shared.TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.MultiLineCommentTrivia]);
this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock = new Rule(RuleDescriptor.create2(this.TypeScriptOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsTypeScriptDeclWithBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
// Place a space before open brace in a control flow construct
this.ControlOpenBraceLeftTokenRange = Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.MultiLineCommentTrivia, SyntaxKind.DoKeyword, SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword, SyntaxKind.ElseKeyword]);
this.SpaceBeforeOpenBraceInControl = new Rule(RuleDescriptor.create2(this.ControlOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines);
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space));
this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space));
this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete));
// Insert new line after { and before } in multi-line contexts.
this.NewLineAfterOpenBraceInBlockContext = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsMultilineBlockContext), RuleAction.NewLine));
// For functions and control block place } on a new line [multi-line rule]
this.NewLineBeforeCloseBraceInBlockContext = new Rule(RuleDescriptor.create2(Shared.TokenRange.AnyIncludingMultilineComments, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsMultilineBlockContext), RuleAction.NewLine));
// Special handling of unary operators.
// Prefix operators generally shouldn't have a space between
// them and their target unary expression.
this.NoSpaceAfterUnaryPrefixOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.UnaryPrefixOperators, Shared.TokenRange.UnaryPrefixExpressions), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
this.NoSpaceAfterUnaryPreincrementOperator = new Rule(RuleDescriptor.create3(SyntaxKind.PlusPlusToken, Shared.TokenRange.UnaryPreincrementExpressions), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterUnaryPredecrementOperator = new Rule(RuleDescriptor.create3(SyntaxKind.MinusMinusToken, Shared.TokenRange.UnaryPredecrementExpressions), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeUnaryPostincrementOperator = new Rule(RuleDescriptor.create2(Shared.TokenRange.UnaryPostincrementExpressions, SyntaxKind.PlusPlusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeUnaryPostdecrementOperator = new Rule(RuleDescriptor.create2(Shared.TokenRange.UnaryPostdecrementExpressions, SyntaxKind.MinusMinusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// More unary operator special-casing.
// DevDiv 181814: Be careful when removing leading whitespace
// around unary operators. Examples:
// 1 - -2 --X--> 1--2
// a + ++b --X--> a+++b
this.SpaceAfterPostincrementWhenFollowedByAdd = new Rule(RuleDescriptor.create1(SyntaxKind.PlusPlusToken, SyntaxKind.PlusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterAddWhenFollowedByUnaryPlus = new Rule(RuleDescriptor.create1(SyntaxKind.PlusToken, SyntaxKind.PlusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterAddWhenFollowedByPreincrement = new Rule(RuleDescriptor.create1(SyntaxKind.PlusToken, SyntaxKind.PlusPlusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterPostdecrementWhenFollowedBySubtract = new Rule(RuleDescriptor.create1(SyntaxKind.MinusMinusToken, SyntaxKind.MinusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterSubtractWhenFollowedByUnaryMinus = new Rule(RuleDescriptor.create1(SyntaxKind.MinusToken, SyntaxKind.MinusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterSubtractWhenFollowedByPredecrement = new Rule(RuleDescriptor.create1(SyntaxKind.MinusToken, SyntaxKind.MinusMinusToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.NoSpaceBeforeComma = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CommaToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.SpaceAfterCertainKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.VarKeyword, SyntaxKind.ThrowKeyword, SyntaxKind.NewKeyword, SyntaxKind.DeleteKeyword, SyntaxKind.ReturnKeyword, SyntaxKind.TypeOfKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceBeforeOpenParenInFuncCall = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsFunctionCallOrNewContext), RuleAction.Delete));
this.SpaceAfterFunctionInFuncDecl = new Rule(RuleDescriptor.create3(SyntaxKind.FunctionKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space));
this.NoSpaceBeforeOpenParenInFuncDecl = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsFunctionDeclContext), RuleAction.Delete));
this.SpaceAfterVoidOperator = new Rule(RuleDescriptor.create3(SyntaxKind.VoidKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsVoidOpContext), RuleAction.Space));
this.NoSpaceBetweenReturnAndSemicolon = new Rule(RuleDescriptor.create1(SyntaxKind.ReturnKeyword, SyntaxKind.SemicolonToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
// So, we have a rule to add a space for [),Any], [do,Any], [else,Any], and [case,Any]
this.SpaceBetweenStatements = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotForContext), RuleAction.Space));
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
this.SpaceAfterTryFinally = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword]), SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
// get x() {}
// set x(val) {}
this.SpaceAfterGetSetInMember = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.GetKeyword, SyntaxKind.SetKeyword]), SyntaxKind.IdentifierName), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space));
// Special case for binary operators (that are keywords). For these we have to add a space and shouldn't follow any user options.
this.SpaceBeforeBinaryKeywordOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.BinaryKeywordOperators), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterBinaryKeywordOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.BinaryKeywordOperators, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
// TypeScript-specific higher priority rules
// Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses
this.NoSpaceAfterConstructor = new Rule(RuleDescriptor.create1(SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Use of module as a function call. e.g.: import m2 = module("m2");
this.NoSpaceAfterModuleImport = new Rule(RuleDescriptor.create2(Shared.TokenRange.FromTokens([SyntaxKind.ModuleKeyword, SyntaxKind.RequireKeyword]), SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Add a space around certain TypeScript keywords
this.SpaceAfterCertainTypeScriptKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.ClassKeyword, SyntaxKind.DeclareKeyword, SyntaxKind.EnumKeyword, SyntaxKind.ExportKeyword, SyntaxKind.ExtendsKeyword, SyntaxKind.GetKeyword, SyntaxKind.ImplementsKeyword, SyntaxKind.ImportKeyword, SyntaxKind.InterfaceKeyword, SyntaxKind.ModuleKeyword, SyntaxKind.PrivateKeyword, SyntaxKind.PublicKeyword, SyntaxKind.SetKeyword, SyntaxKind.StaticKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.SpaceBeforeCertainTypeScriptKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.ExtendsKeyword, SyntaxKind.ImplementsKeyword])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
// Treat string literals in module names as identifiers, and add a space between the literal and the opening Brace braces, e.g.: module "m2" {
this.SpaceAfterModuleName = new Rule(RuleDescriptor.create1(SyntaxKind.StringLiteral, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsModuleDeclContext), RuleAction.Space));
// Lambda expressions
this.SpaceAfterArrow = new Rule(RuleDescriptor.create3(SyntaxKind.EqualsGreaterThanToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
// Optional parameters and var args
this.NoSpaceAfterEllipsis = new Rule(RuleDescriptor.create1(SyntaxKind.DotDotDotToken, SyntaxKind.IdentifierName), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterOptionalParameters = new Rule(RuleDescriptor.create3(SyntaxKind.QuestionToken, Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.CommaToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBinaryOpContext), RuleAction.Delete));
// generics
this.NoSpaceBeforeOpenAngularBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.TypeNames, SyntaxKind.LessThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsTypeArgumentOrParameterContext), RuleAction.Delete));
this.NoSpaceBetweenCloseParenAndAngularBracket = new Rule(RuleDescriptor.create1(SyntaxKind.CloseParenToken, SyntaxKind.LessThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsTypeArgumentOrParameterContext), RuleAction.Delete));
this.NoSpaceAfterOpenAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.LessThanToken, Shared.TokenRange.TypeNames), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsTypeArgumentOrParameterContext), RuleAction.Delete));
this.NoSpaceBeforeCloseAngularBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.GreaterThanToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsTypeArgumentOrParameterContext), RuleAction.Delete));
this.NoSpaceAfterCloseAngularBracket = new Rule(RuleDescriptor.create3(SyntaxKind.GreaterThanToken, Shared.TokenRange.FromTokens([SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.GreaterThanToken, SyntaxKind.CommaToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsTypeArgumentOrParameterContext), RuleAction.Delete));
// Remove spaces in empty interface literals. e.g.: x: {}
this.NoSpaceBetweenEmptyInterfaceBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsObjectTypeContext), RuleAction.Delete));
// These rules are higher in priority than user-configurable rules.
this.HighPriorityCommonRules =
[
this.IgnoreBeforeComment, this.IgnoreAfterLineComment,
this.NoSpaceBeforeColon, this.SpaceAfterColon, this.NoSpaceBeforeQMark, this.SpaceAfterQMark,
this.NoSpaceBeforeDot, this.NoSpaceAfterDot,
this.NoSpaceAfterUnaryPrefixOperator,
this.NoSpaceAfterUnaryPreincrementOperator, this.NoSpaceAfterUnaryPredecrementOperator,
this.NoSpaceBeforeUnaryPostincrementOperator, this.NoSpaceBeforeUnaryPostdecrementOperator,
this.SpaceAfterPostincrementWhenFollowedByAdd,
this.SpaceAfterAddWhenFollowedByUnaryPlus, this.SpaceAfterAddWhenFollowedByPreincrement,
this.SpaceAfterPostdecrementWhenFollowedBySubtract,
this.SpaceAfterSubtractWhenFollowedByUnaryMinus, this.SpaceAfterSubtractWhenFollowedByPredecrement,
this.NoSpaceAfterCloseBrace,
this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext,
this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets,
this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember,
this.NoSpaceBetweenReturnAndSemicolon,
this.SpaceAfterCertainKeywords,
this.NoSpaceBeforeOpenParenInFuncCall,
this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator,
this.SpaceAfterVoidOperator,
// TypeScript-specific rules
this.NoSpaceAfterConstructor, this.NoSpaceAfterModuleImport,
this.SpaceAfterCertainTypeScriptKeywords, this.SpaceBeforeCertainTypeScriptKeywords,
this.SpaceAfterModuleName,
this.SpaceAfterArrow,
this.NoSpaceAfterEllipsis,
this.NoSpaceAfterOptionalParameters,
this.NoSpaceBetweenEmptyInterfaceBraceBrackets,
this.NoSpaceBeforeOpenAngularBracket,
this.NoSpaceBetweenCloseParenAndAngularBracket,
this.NoSpaceAfterOpenAngularBracket,
this.NoSpaceBeforeCloseAngularBracket,
this.NoSpaceAfterCloseAngularBracket
];
// These rules are lower in priority than user-configurable rules.
this.LowPriorityCommonRules =
[
this.NoSpaceBeforeSemicolon,
this.SpaceBeforeOpenBraceInControl, this.SpaceBeforeOpenBraceInFunction, this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock,
this.NoSpaceBeforeComma,
this.NoSpaceBeforeOpenBracket, this.NoSpaceAfterOpenBracket,
this.NoSpaceBeforeCloseBracket, this.NoSpaceAfterCloseBracket,
this.SpaceAfterSemicolon,
this.NoSpaceBeforeOpenParenInFuncDecl,
this.SpaceBetweenStatements, this.SpaceAfterTryFinally
];
///
/// Rules controlled by user options
///
// Insert space after comma delimiter
this.SpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceAfterComma = new Rule(RuleDescriptor.create3(SyntaxKind.CommaToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Insert space before and after binary operators
this.SpaceBeforeBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.BinaryOperators), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.SpaceAfterBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.BinaryOperators, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Space));
this.NoSpaceBeforeBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.BinaryOperators), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Delete));
this.NoSpaceAfterBinaryOperator = new Rule(RuleDescriptor.create4(Shared.TokenRange.BinaryOperators, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsBinaryOpContext), RuleAction.Delete));
// Insert space after keywords in control flow statements
this.SpaceAfterKeywordInControl = new Rule(RuleDescriptor.create2(Shared.TokenRange.Keywords, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext), RuleAction.Space));
this.NoSpaceAfterKeywordInControl = new Rule(RuleDescriptor.create2(Shared.TokenRange.Keywords, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext), RuleAction.Delete));
// Open Brace braces after function
//TypeScript: Function can have return types, which can be made of tons of different token kinds
this.NewLineBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeMultilineBlockContext), RuleAction.NewLine), RuleFlags.CanDeleteNewLines);
// Open Brace braces after TypeScript module/class/interface
this.NewLineBeforeOpenBraceInTypeScriptDeclWithBlock = new Rule(RuleDescriptor.create2(this.TypeScriptOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsTypeScriptDeclWithBlockContext, Rules.IsBeforeMultilineBlockContext), RuleAction.NewLine), RuleFlags.CanDeleteNewLines);
// Open Brace braces after control block
this.NewLineBeforeOpenBraceInControl = new Rule(RuleDescriptor.create2(this.ControlOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext, Rules.IsBeforeMultilineBlockContext), RuleAction.NewLine), RuleFlags.CanDeleteNewLines);
// Insert space after semicolon in for statement
this.SpaceAfterSemicolonInFor = new Rule(RuleDescriptor.create3(SyntaxKind.SemicolonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsForContext), RuleAction.Space));
this.NoSpaceAfterSemicolonInFor = new Rule(RuleDescriptor.create3(SyntaxKind.SemicolonToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsForContext), RuleAction.Delete));
// Insert space after opening and before closing nonempty parenthesis
this.SpaceAfterOpenParen = new Rule(RuleDescriptor.create3(SyntaxKind.OpenParenToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.SpaceBeforeCloseParen = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
this.NoSpaceBetweenParens = new Rule(RuleDescriptor.create1(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceAfterOpenParen = new Rule(RuleDescriptor.create3(SyntaxKind.OpenParenToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
this.NoSpaceBeforeCloseParen = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
// Insert space after function keyword for anonymous functions
this.SpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space));
this.NoSpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Delete));
}
///
/// Contexts
///
static IsForContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.ForStatement;
}
static IsNotForContext(context: FormattingContext): boolean {
return !Rules.IsForContext(context);
}
static IsBinaryOpContext(context: FormattingContext): boolean {
switch (context.contextNode.kind()) {
// binary expressions
case SyntaxKind.AssignmentExpression:
case SyntaxKind.AddAssignmentExpression:
case SyntaxKind.SubtractAssignmentExpression:
case SyntaxKind.MultiplyAssignmentExpression:
case SyntaxKind.DivideAssignmentExpression:
case SyntaxKind.ModuloAssignmentExpression:
case SyntaxKind.AndAssignmentExpression:
case SyntaxKind.ExclusiveOrAssignmentExpression:
case SyntaxKind.OrAssignmentExpression:
case SyntaxKind.LeftShiftAssignmentExpression:
case SyntaxKind.SignedRightShiftAssignmentExpression:
case SyntaxKind.UnsignedRightShiftAssignmentExpression:
case SyntaxKind.ConditionalExpression:
case SyntaxKind.LogicalOrExpression:
case SyntaxKind.LogicalAndExpression:
case SyntaxKind.BitwiseOrExpression:
case SyntaxKind.BitwiseExclusiveOrExpression:
case SyntaxKind.BitwiseAndExpression:
case SyntaxKind.EqualsWithTypeConversionExpression:
case SyntaxKind.NotEqualsWithTypeConversionExpression:
case SyntaxKind.EqualsExpression:
case SyntaxKind.NotEqualsExpression:
case SyntaxKind.LessThanExpression:
case SyntaxKind.GreaterThanExpression:
case SyntaxKind.LessThanOrEqualExpression:
case SyntaxKind.GreaterThanOrEqualExpression:
case SyntaxKind.InstanceOfExpression:
case SyntaxKind.InExpression:
case SyntaxKind.LeftShiftExpression:
case SyntaxKind.SignedRightShiftExpression:
case SyntaxKind.UnsignedRightShiftExpression:
case SyntaxKind.MultiplyExpression:
case SyntaxKind.DivideExpression:
case SyntaxKind.ModuloExpression:
case SyntaxKind.AddExpression:
case SyntaxKind.SubtractExpression:
return true;
// equal in import a = module('a');
case SyntaxKind.ImportDeclaration:
// equal in var a = 0;
case SyntaxKind.VariableDeclarator:
case SyntaxKind.EqualsValueClause:
return context.currentTokenSpan.kind === SyntaxKind.EqualsToken || context.nextTokenSpan.kind === SyntaxKind.EqualsToken;
// "in" keyword in for (var x in []) { }
case SyntaxKind.ForInStatement:
return context.currentTokenSpan.kind === SyntaxKind.InKeyword || context.nextTokenSpan.kind === SyntaxKind.InKeyword;
}
return false;
}
static IsNotBinaryOpContext(context: FormattingContext): boolean {
return !Rules.IsBinaryOpContext(context);
}
static IsSameLineTokenOrBeforeMultilineBlockContext(context: FormattingContext): boolean {
//// This check is mainly used inside SpaceBeforeOpenBraceInControl and SpaceBeforeOpenBraceInFunction.
////
//// Ex:
//// if (1) { ....
//// * ) and { are on the same line so apply the rule. Here we don't care whether it's same or multi block context
////
//// Ex:
//// if (1)
//// { ... }
//// * ) and { are on differnet lines. We only need to format if the block is multiline context. So in this case we don't format.
////
//// Ex:
//// if (1)
//// { ...
//// }
//// * ) and { are on differnet lines. We only need to format if the block is multiline context. So in this case we format.
return context.TokensAreOnSameLine() || Rules.IsBeforeMultilineBlockContext(context);
}
// This check is done before an open brace in a control construct, a function, or a typescript block declaration
static IsBeforeMultilineBlockContext(context: FormattingContext): boolean {
return Rules.IsBeforeBlockContext(context) && !(context.NextNodeAllOnSameLine() || context.NextNodeBlockIsOnOneLine());
}
static IsMultilineBlockContext(context: FormattingContext): boolean {
return Rules.IsBlockContext(context) && !(context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine());
}
static IsSingleLineBlockContext(context: FormattingContext): boolean {
return Rules.IsBlockContext(context) && (context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine());
}
static IsBlockContext(context: FormattingContext): boolean {
return Rules.NodeIsBlockContext(context.contextNode);
}
static IsBeforeBlockContext(context: FormattingContext): boolean {
return Rules.NodeIsBlockContext(context.nextTokenParent);
}
// IMPORTANT!!! This method must return true ONLY for nodes with open and close braces as immediate children
static NodeIsBlockContext(node: IndentationNodeContext): boolean {
if (Rules.NodeIsTypeScriptDeclWithBlockContext(node)) {
// This means we are in a context that looks like a block to the user, but in the grammar is actually not a node (it's a class, module, enum, object type literal, etc).
return true;
}
switch (node.kind()) {
case SyntaxKind.Block:
case SyntaxKind.SwitchStatement:
case SyntaxKind.ObjectLiteralExpression:
return true;
}
return false;
}
static IsFunctionDeclContext(context: FormattingContext): boolean {
switch (context.contextNode.kind()) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.MemberFunctionDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.SimpleArrowFunctionExpression:
case SyntaxKind.ParenthesizedArrowFunctionExpression:
case SyntaxKind.InterfaceDeclaration: // This one is not truly a function, but for formatting purposes, it acts just like one
return true;
}
return false;
}
static IsTypeScriptDeclWithBlockContext(context: FormattingContext): boolean {
return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode);
}
static NodeIsTypeScriptDeclWithBlockContext(node: IndentationNodeContext): boolean {
switch (node.kind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.ObjectType:
case SyntaxKind.ModuleDeclaration:
return true;
}
return false;
}
static IsAfterCodeBlockContext(context: FormattingContext): boolean {
switch (context.currentTokenParent.kind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.Block:
case SyntaxKind.SwitchStatement:
return true;
}
return false;
}
static IsControlDeclContext(context: FormattingContext): boolean {
switch (context.contextNode.kind()) {
case SyntaxKind.IfStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.TryStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.ElseClause:
case SyntaxKind.CatchClause:
case SyntaxKind.FinallyClause:
return true;
default:
return false;
}
}
static IsObjectContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.ObjectLiteralExpression;
}
static IsFunctionCallContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.InvocationExpression;
}
static IsNewContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.ObjectCreationExpression;
}
static IsFunctionCallOrNewContext(context: FormattingContext): boolean {
return Rules.IsFunctionCallContext(context) || Rules.IsNewContext(context);
}
static IsSameLineTokenContext(context: FormattingContext): boolean {
return context.TokensAreOnSameLine();
}
static IsNotFormatOnEnter(context: FormattingContext): boolean {
return context.formattingRequestKind != FormattingRequestKind.FormatOnEnter;
}
static IsModuleDeclContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.ModuleDeclaration;
}
static IsObjectTypeContext(context: FormattingContext): boolean {
return context.contextNode.kind() === SyntaxKind.ObjectType && context.contextNode.parent().kind() !== SyntaxKind.InterfaceDeclaration;
}
static IsTypeArgumentOrParameter(tokenKind: SyntaxKind, parentKind: SyntaxKind): boolean {
return ((tokenKind === SyntaxKind.LessThanToken || tokenKind === SyntaxKind.GreaterThanToken) &&
(parentKind === SyntaxKind.TypeParameterList || parentKind === SyntaxKind.TypeArgumentList));
}
static IsTypeArgumentOrParameterContext(context: FormattingContext): boolean {
return Rules.IsTypeArgumentOrParameter(context.currentTokenSpan.kind, context.currentTokenParent.kind()) ||
Rules.IsTypeArgumentOrParameter(context.nextTokenSpan.kind, context.nextTokenParent.kind());
}
static IsVoidOpContext(context: FormattingContext): boolean {
return context.currentTokenSpan.kind === SyntaxKind.VoidKeyword && context.currentTokenParent.kind() === SyntaxKind.VoidExpression;
}
}
}

View File

@@ -0,0 +1,189 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class RulesMap {
public map: RulesBucket[];
public mapRowLength: number;
constructor() {
this.map = [];
this.mapRowLength = 0;
}
static create(rules: Rule[]): RulesMap {
var result = new RulesMap();
result.Initialize(rules);
return result;
}
public Initialize(rules: Rule[]) {
this.mapRowLength = SyntaxKind.LastToken + 1;
this.map = <any> new Array(this.mapRowLength * this.mapRowLength);//new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
// This array is used only during construction of the rulesbucket in the map
var rulesBucketConstructionStateList: RulesBucketConstructionState[] = <any> new Array(this.map.length);//new Array<RulesBucketConstructionState>(this.map.length);
this.FillRules(rules, rulesBucketConstructionStateList);
return this.map;
}
public FillRules(rules: Rule[], rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
rules.forEach((rule) => {
this.FillRule(rule, rulesBucketConstructionStateList);
});
}
private GetRuleBucketIndex(row: number, column: number): number {
var rulesBucketIndex = (row * this.mapRowLength) + column;
//Debug.Assert(rulesBucketIndex < this.map.Length, "Trying to access an index outside the array.");
return rulesBucketIndex;
}
private FillRule(rule: Rule, rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
var specificRule = rule.Descriptor.LeftTokenRange != Shared.TokenRange.Any &&
rule.Descriptor.RightTokenRange != Shared.TokenRange.Any;
rule.Descriptor.LeftTokenRange.GetTokens().forEach((left) => {
rule.Descriptor.RightTokenRange.GetTokens().forEach((right) => {
var rulesBucketIndex = this.GetRuleBucketIndex(left, right);
var rulesBucket = this.map[rulesBucketIndex];
if (rulesBucket == undefined) {
rulesBucket = this.map[rulesBucketIndex] = new RulesBucket();
}
rulesBucket.AddRule(rule, specificRule, rulesBucketConstructionStateList, rulesBucketIndex);
})
})
}
public GetRule(context: FormattingContext): Rule {
var bucketIndex = this.GetRuleBucketIndex(context.currentTokenSpan.kind, context.nextTokenSpan.kind);
var bucket = this.map[bucketIndex];
if (bucket != null) {
for (var i = 0, len = bucket.Rules().length; i < len; i++) {
var rule = bucket.Rules()[i];
if (rule.Operation.Context.InContext(context))
return rule;
}
}
return null;
}
}
var MaskBitSize = 5;
var Mask = 0x1f;
export enum RulesPosition {
IgnoreRulesSpecific = 0,
IgnoreRulesAny = MaskBitSize * 1,
ContextRulesSpecific = MaskBitSize * 2,
ContextRulesAny = MaskBitSize * 3,
NoContextRulesSpecific = MaskBitSize * 4,
NoContextRulesAny = MaskBitSize * 5
}
export class RulesBucketConstructionState {
private rulesInsertionIndexBitmap: number;
constructor() {
//// The Rules list contains all the inserted rules into a rulebucket in the following order:
//// 1- Ignore rules with specific token combination
//// 2- Ignore rules with any token combination
//// 3- Context rules with specific token combination
//// 4- Context rules with any token combination
//// 5- Non-context rules with specific token combination
//// 6- Non-context rules with any token combination
////
//// The member rulesInsertionIndexBitmap is used to describe the number of rules
//// in each sub-bucket (above) hence can be used to know the index of where to insert
//// the next rule. It's a bitmap which contains 6 different sections each is given 5 bits.
////
//// Example:
//// In order to insert a rule to the end of sub-bucket (3), we get the index by adding
//// the values in the bitmap segments 3rd, 2nd, and 1st.
this.rulesInsertionIndexBitmap = 0;
}
public GetInsertionIndex(maskPosition: RulesPosition): number {
var index = 0;
var pos = 0;
var indexBitmap = this.rulesInsertionIndexBitmap;
while (pos <= maskPosition) {
index += (indexBitmap & Mask);
indexBitmap >>= MaskBitSize;
pos += MaskBitSize;
}
return index;
}
public IncreaseInsertionIndex(maskPosition: RulesPosition): void {
var value = (this.rulesInsertionIndexBitmap >> maskPosition) & Mask;
value++;
Debug.assert((value & Mask) == value, "Adding more rules into the sub-bucket than allowed. Maximum allowed is 32 rules.");
var temp = this.rulesInsertionIndexBitmap & ~(Mask << maskPosition);
temp |= value << maskPosition;
this.rulesInsertionIndexBitmap = temp;
}
}
export class RulesBucket {
private rules: Rule[];
constructor() {
this.rules = [];
}
public Rules(): Rule[] {
return this.rules;
}
public AddRule(rule: Rule, specificTokens: boolean, constructionState: RulesBucketConstructionState[], rulesBucketIndex: number): void {
var position: RulesPosition;
if (rule.Operation.Action == RuleAction.Ignore) {
position = specificTokens ?
RulesPosition.IgnoreRulesSpecific :
RulesPosition.IgnoreRulesAny;
}
else if (!rule.Operation.Context.IsAny()) {
position = specificTokens ?
RulesPosition.ContextRulesSpecific :
RulesPosition.ContextRulesAny;
}
else {
position = specificTokens ?
RulesPosition.NoContextRulesSpecific :
RulesPosition.NoContextRulesAny;
}
var state = constructionState[rulesBucketIndex];
if (state === undefined) {
state = constructionState[rulesBucketIndex] = new RulesBucketConstructionState();
}
var index = state.GetInsertionIndex(position);
this.rules.splice(index, 0, rule);
state.IncreaseInsertionIndex(position);
}
}
}

View File

@@ -0,0 +1,117 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// <reference path="formatting.ts"/>
module TypeScript.Services.Formatting {
export class RulesProvider {
private globalRules: Rules;
private options: TypeScript.Services.FormatCodeOptions;
private activeRules: Rule[];
private rulesMap: RulesMap;
constructor(private logger: TypeScript.ILogger) {
this.globalRules = new Rules();
}
public getRuleName(rule: Rule): string {
return this.globalRules.getRuleName(rule);
}
public getRuleByName(name: string): Rule {
return this.globalRules[name];
}
public getRulesMap() {
return this.rulesMap;
}
public ensureUpToDate(options: TypeScript.Services.FormatCodeOptions) {
if (this.options == null || !TypeScript.compareDataObjects(this.options, options)) {
var activeRules: Rule[] = TypeScript.timeFunction(this.logger, "RulesProvider: createActiveRules()", () => { return this.createActiveRules(options); });
var rulesMap: RulesMap = TypeScript.timeFunction(this.logger, "RulesProvider: RulesMap.create()", () => { return RulesMap.create(activeRules); });
this.activeRules = activeRules;
this.rulesMap = rulesMap;
this.options = TypeScript.Services.FormatCodeOptions.clone(options);
}
}
private createActiveRules(options: TypeScript.Services.FormatCodeOptions): Rule[] {
var rules = this.globalRules.HighPriorityCommonRules.slice(0);
if (options.InsertSpaceAfterCommaDelimiter) {
rules.push(this.globalRules.SpaceAfterComma);
}
else {
rules.push(this.globalRules.NoSpaceAfterComma);
}
if (options.InsertSpaceAfterFunctionKeywordForAnonymousFunctions) {
rules.push(this.globalRules.SpaceAfterAnonymousFunctionKeyword);
}
else {
rules.push(this.globalRules.NoSpaceAfterAnonymousFunctionKeyword);
}
if (options.InsertSpaceAfterKeywordsInControlFlowStatements) {
rules.push(this.globalRules.SpaceAfterKeywordInControl);
}
else {
rules.push(this.globalRules.NoSpaceAfterKeywordInControl);
}
if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis) {
rules.push(this.globalRules.SpaceAfterOpenParen);
rules.push(this.globalRules.SpaceBeforeCloseParen);
rules.push(this.globalRules.NoSpaceBetweenParens);
}
else {
rules.push(this.globalRules.NoSpaceAfterOpenParen);
rules.push(this.globalRules.NoSpaceBeforeCloseParen);
rules.push(this.globalRules.NoSpaceBetweenParens);
}
if (options.InsertSpaceAfterSemicolonInForStatements) {
rules.push(this.globalRules.SpaceAfterSemicolonInFor);
}
else {
rules.push(this.globalRules.NoSpaceAfterSemicolonInFor);
}
if (options.InsertSpaceBeforeAndAfterBinaryOperators) {
rules.push(this.globalRules.SpaceBeforeBinaryOperator);
rules.push(this.globalRules.SpaceAfterBinaryOperator);
}
else {
rules.push(this.globalRules.NoSpaceBeforeBinaryOperator);
rules.push(this.globalRules.NoSpaceAfterBinaryOperator);
}
if (options.PlaceOpenBraceOnNewLineForControlBlocks) {
rules.push(this.globalRules.NewLineBeforeOpenBraceInControl);
}
if (options.PlaceOpenBraceOnNewLineForFunctions) {
rules.push(this.globalRules.NewLineBeforeOpenBraceInFunction);
rules.push(this.globalRules.NewLineBeforeOpenBraceInTypeScriptDeclWithBlock);
}
rules = rules.concat(this.globalRules.LowPriorityCommonRules);
return rules;
}
}
}

View File

@@ -0,0 +1,46 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class SingleTokenIndenter extends IndentationTrackingWalker {
private indentationAmount: number = null;
private indentationPosition: number;
constructor(indentationPosition: number, sourceUnit: SourceUnitSyntax, snapshot: ITextSnapshot, indentFirstToken: boolean, options: FormattingOptions) {
super(new TextSpan(indentationPosition, 1), sourceUnit, snapshot, indentFirstToken, options);
this.indentationPosition = indentationPosition;
}
public static getIndentationAmount(position: number, sourceUnit: SourceUnitSyntax, snapshot: ITextSnapshot, options: FormattingOptions): number {
var walker = new SingleTokenIndenter(position, sourceUnit, snapshot, true, options);
visitNodeOrToken(walker, sourceUnit);
return walker.indentationAmount;
}
public indentToken(token: ISyntaxToken, indentationAmount: number, commentIndentationAmount: number): void {
// Compute an indentation string for this token
if (token.fullWidth() === 0 || (this.indentationPosition - this.position() < token.leadingTriviaWidth())) {
// The position is in the leading trivia, use comment indentation
this.indentationAmount = commentIndentationAmount;
}
else {
this.indentationAmount = indentationAmount;
}
}
}
}

View File

@@ -0,0 +1,30 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class SnapshotPoint {
constructor(public snapshot: ITextSnapshot, public position: number) {
}
public getContainingLine(): ITextSnapshotLine {
return this.snapshot.getLineFromPosition(this.position);
}
public add(offset: number): SnapshotPoint {
return new SnapshotPoint(this.snapshot, this.position + offset);
}
}
}

View File

@@ -0,0 +1,28 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class TextEditInfo {
constructor(public position: number, public length: number, public replaceWith: string) {
}
public toString() {
return "[ position: " + this.position + ", length: " + this.length + ", replaceWith: '" + this.replaceWith + "' ]";
}
}
}

View File

@@ -0,0 +1,84 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export interface ITextSnapshot {
getText(span: TextSpan): string;
getLineNumberFromPosition(position: number): number;
getLineFromPosition(position: number): ITextSnapshotLine;
getLineFromLineNumber(lineNumber: number): ITextSnapshotLine;
}
export class TextSnapshot implements ITextSnapshot {
private lines: TextSnapshotLine[];
constructor(private snapshot: ISimpleText) {
this.lines = [];
}
public getText(span: TextSpan): string {
return this.snapshot.substr(span.start(), span.length());
}
public getLineNumberFromPosition(position: number): number {
return this.snapshot.lineMap().getLineNumberFromPosition(position);
}
public getLineFromPosition(position: number): ITextSnapshotLine {
var lineNumber = this.getLineNumberFromPosition(position);
return this.getLineFromLineNumber(lineNumber);
}
public getLineFromLineNumber(lineNumber: number): ITextSnapshotLine {
var line = this.lines[lineNumber];
if (line === undefined) {
line = <TextSnapshotLine>this.getLineFromLineNumberWorker(lineNumber);
this.lines[lineNumber] = line;
}
return line;
}
private getLineFromLineNumberWorker(lineNumber: number): ITextSnapshotLine {
var lineMap = this.snapshot.lineMap().lineStarts();
var lineMapIndex = lineNumber; //Note: lineMap is 0-based
if (lineMapIndex < 0 || lineMapIndex >= lineMap.length)
throw new Error(TypeScript.getDiagnosticMessage(TypeScript.DiagnosticCode.Invalid_line_number_0, [lineMapIndex]));
var start = lineMap[lineMapIndex];
var end: number;
var endIncludingLineBreak: number;
var lineBreak = "";
if (lineMapIndex == lineMap.length) {
end = endIncludingLineBreak = this.snapshot.length();
}
else {
endIncludingLineBreak = (lineMapIndex >= lineMap.length - 1 ? this.snapshot.length() : lineMap[lineMapIndex + 1]);
for (var p = endIncludingLineBreak - 1; p >= start; p--) {
var c = this.snapshot.substr(p, 1);
//TODO: Other ones?
if (c != "\r" && c != "\n") {
break;
}
}
end = p + 1;
lineBreak = this.snapshot.substr(end, endIncludingLineBreak - end);
}
var result = new TextSnapshotLine(this, lineNumber, start, end, lineBreak);
return result;
}
}
}

View File

@@ -0,0 +1,80 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export interface ITextSnapshotLine {
snapshot(): ITextSnapshot;
start(): SnapshotPoint;
startPosition(): number;
end(): SnapshotPoint;
endPosition(): number;
endIncludingLineBreak(): SnapshotPoint;
endIncludingLineBreakPosition(): number;
length(): number;
lineNumber(): number;
getText(): string;
}
export class TextSnapshotLine implements ITextSnapshotLine {
constructor(private _snapshot: ITextSnapshot, private _lineNumber: number, private _start: number, private _end: number, private _lineBreak: string) {
}
public snapshot() {
return this._snapshot;
}
public start() {
return new SnapshotPoint(this._snapshot, this._start);
}
public startPosition() {
return this._start;
}
public end() {
return new SnapshotPoint(this._snapshot, this._end);
}
public endPosition() {
return this._end;
}
public endIncludingLineBreak() {
return new SnapshotPoint(this._snapshot, this._end + this._lineBreak.length);
}
public endIncludingLineBreakPosition() {
return this._end + this._lineBreak.length;
}
public length() {
return this._end - this._start;
}
public lineNumber() {
return this._lineNumber;
}
public getText(): string {
return this._snapshot.getText(TextSpan.fromBounds(this._start, this._end));
}
}
}

View File

@@ -0,0 +1,152 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export module Shared {
export interface ITokenAccess {
GetTokens(): SyntaxKind[];
Contains(token: SyntaxKind): boolean;
}
export class TokenRangeAccess implements ITokenAccess {
private tokens: SyntaxKind[];
constructor(from: SyntaxKind, to: SyntaxKind, except: SyntaxKind[]) {
this.tokens = [];
for (var token = from; token <= to; token++) {
if (except.indexOf(token) < 0) {
this.tokens.push(token);
}
}
}
public GetTokens(): SyntaxKind[] {
return this.tokens;
}
public Contains(token: SyntaxKind): boolean {
return this.tokens.indexOf(token) >= 0;
}
public toString(): string {
return "[tokenRangeStart=" + SyntaxKind[this.tokens[0]] + "," +
"tokenRangeEnd=" + SyntaxKind[this.tokens[this.tokens.length - 1]] + "]";
}
}
export class TokenValuesAccess implements ITokenAccess {
private tokens: SyntaxKind[];
constructor(tks: SyntaxKind[]) {
this.tokens = tks && tks.length ? tks : <SyntaxKind[]>[];
}
public GetTokens(): SyntaxKind[] {
return this.tokens;
}
public Contains(token: SyntaxKind): boolean {
return this.tokens.indexOf(token) >= 0;
}
}
export class TokenSingleValueAccess implements ITokenAccess {
constructor(public token: SyntaxKind) {
}
public GetTokens(): SyntaxKind[] {
return [this.token];
}
public Contains(tokenValue: SyntaxKind): boolean {
return tokenValue == this.token;
}
public toString(): string {
return "[singleTokenKind=" + SyntaxKind[this.token] + "]";
}
}
export class TokenAllAccess implements ITokenAccess {
public GetTokens(): SyntaxKind[] {
var result: SyntaxKind[] = [];
for (var token = SyntaxKind.FirstToken; token <= SyntaxKind.LastToken; token++) {
result.push(token);
}
return result;
}
public Contains(tokenValue: SyntaxKind): boolean {
return true;
}
public toString(): string {
return "[allTokens]";
}
}
export class TokenRange {
constructor(public tokenAccess: ITokenAccess) {
}
static FromToken(token: SyntaxKind): TokenRange {
return new TokenRange(new TokenSingleValueAccess(token));
}
static FromTokens(tokens: SyntaxKind[]): TokenRange {
return new TokenRange(new TokenValuesAccess(tokens));
}
static FromRange(f: SyntaxKind, to: SyntaxKind, except: SyntaxKind[] = []): TokenRange {
return new TokenRange(new TokenRangeAccess(f, to, except));
}
static AllTokens(): TokenRange {
return new TokenRange(new TokenAllAccess());
}
public GetTokens(): SyntaxKind[] {
return this.tokenAccess.GetTokens();
}
public Contains(token: SyntaxKind): boolean {
return this.tokenAccess.Contains(token);
}
public toString(): string {
return this.tokenAccess.toString();
}
static Any: TokenRange = TokenRange.AllTokens();
static AnyIncludingMultilineComments = TokenRange.FromTokens(TokenRange.Any.GetTokens().concat([SyntaxKind.MultiLineCommentTrivia]));
static Keywords = TokenRange.FromRange(SyntaxKind.FirstKeyword, SyntaxKind.LastKeyword);
static Operators = TokenRange.FromRange(SyntaxKind.SemicolonToken, SyntaxKind.SlashEqualsToken);
static BinaryOperators = TokenRange.FromRange(SyntaxKind.LessThanToken, SyntaxKind.SlashEqualsToken);
static BinaryKeywordOperators = TokenRange.FromTokens([SyntaxKind.InKeyword, SyntaxKind.InstanceOfKeyword]);
static ReservedKeywords = TokenRange.FromRange(SyntaxKind.FirstFutureReservedStrictKeyword, SyntaxKind.LastFutureReservedStrictKeyword);
static UnaryPrefixOperators = TokenRange.FromTokens([SyntaxKind.PlusPlusToken, SyntaxKind.MinusMinusToken, SyntaxKind.TildeToken, SyntaxKind.ExclamationToken]);
static UnaryPrefixExpressions = TokenRange.FromTokens([SyntaxKind.NumericLiteral, SyntaxKind.IdentifierName, SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.OpenBraceToken, SyntaxKind.ThisKeyword, SyntaxKind.NewKeyword]);
static UnaryPreincrementExpressions = TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.OpenParenToken, SyntaxKind.ThisKeyword, SyntaxKind.NewKeyword]);
static UnaryPostincrementExpressions = TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken, SyntaxKind.NewKeyword]);
static UnaryPredecrementExpressions = TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.OpenParenToken, SyntaxKind.ThisKeyword, SyntaxKind.NewKeyword]);
static UnaryPostdecrementExpressions = TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken, SyntaxKind.NewKeyword]);
static Comments = TokenRange.FromTokens([SyntaxKind.SingleLineCommentTrivia, SyntaxKind.MultiLineCommentTrivia]);
static TypeNames = TokenRange.FromTokens([SyntaxKind.IdentifierName, SyntaxKind.NumberKeyword, SyntaxKind.StringKeyword, SyntaxKind.BooleanKeyword, SyntaxKind.VoidKeyword, SyntaxKind.AnyKeyword]);
}
}
}

View File

@@ -0,0 +1,25 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='formatting.ts' />
module TypeScript.Services.Formatting {
export class TokenSpan extends TextSpan {
constructor(public kind: SyntaxKind, start: number, length: number) {
super(start, length);
}
}
}

View File

@@ -0,0 +1,352 @@
///<reference path='references.ts' />
module TypeScript.Services {
interface LexicalScope {
items: TypeScript.IIndexable<NavigateToItem>;
itemNames: string[];
childScopes: TypeScript.IIndexable<LexicalScope>;
childScopeNames: string[];
}
export class GetScriptLexicalStructureWalker extends TypeScript.SyntaxWalker {
private nameStack: string[] = [];
private kindStack: string[] = [];
private parentScopes: LexicalScope[] = [];
private currentScope: LexicalScope;
private createScope(): LexicalScope {
return {
items: TypeScript.createIntrinsicsObject<NavigateToItem>(),
childScopes: TypeScript.createIntrinsicsObject<LexicalScope>(),
childScopeNames: [],
itemNames: []
};
}
private pushNewContainerScope(containerName: string, kind: string): LexicalScope {
Debug.assert(containerName, "No scope name provided");
var key = kind + "+" + containerName;
this.nameStack.push(containerName);
this.kindStack.push(kind);
var parentScope = this.currentScope;
this.parentScopes.push(parentScope);
var scope = parentScope.childScopes[key];
if (!scope) {
scope = this.createScope()
parentScope.childScopes[key] = scope;
parentScope.childScopeNames.push(key);
}
this.currentScope = scope;
return parentScope;
}
private popScope() {
Debug.assert(this.parentScopes.length > 0, "No parent scopes to return to")
this.currentScope = this.parentScopes.pop();
this.kindStack.pop();
this.nameStack.pop();
}
constructor(private fileName: string) {
super();
this.currentScope = this.createScope();
}
private collectItems(items: NavigateToItem[], scope = this.currentScope) {
scope.itemNames.forEach(item => {
items.push(scope.items[item]);
});
scope.childScopeNames.forEach(childScope => {
this.collectItems(items, scope.childScopes[childScope]);
});
}
static getListsOfAllScriptLexicalStructure(items: NavigateToItem[], fileName: string, unit: TypeScript.SourceUnitSyntax) {
var visitor = new GetScriptLexicalStructureWalker(fileName);
visitNodeOrToken(visitor, unit);
visitor.collectItems(items);
}
private createItem(node: TypeScript.ISyntaxNode, modifiers: ISyntaxToken[], kind: string, name: string): void {
var key = kind + "+" + name;
if (this.currentScope.items[key] !== undefined) {
this.addAdditionalSpan(node, key);
return;
}
var item = new NavigateToItem();
item.name = name;
item.kind = kind;
item.matchKind = MatchKind.exact;
item.fileName = this.fileName;
item.kindModifiers = this.getKindModifiers(modifiers);
item.minChar = start(node);
item.limChar = end(node);
item.containerName = this.nameStack.join(".");
item.containerKind = this.kindStack.length === 0 ? "" : TypeScript.ArrayUtilities.last(this.kindStack);
this.currentScope.items[key] = item;
this.currentScope.itemNames.push(key);
}
private addAdditionalSpan(
node: TypeScript.ISyntaxNode,
key: string) {
var item = this.currentScope.items[key]
Debug.assert(item !== undefined);
var start = TypeScript.start(node);
var span = new SpanInfo(start, start + width(node));
if (item.additionalSpans) {
item.additionalSpans.push(span);
}
else {
item.additionalSpans = [span];
}
}
private getKindModifiers(modifiers: TypeScript.ISyntaxToken[]): string {
var result: string[] = [];
for (var i = 0, n = modifiers.length; i < n; i++) {
result.push(modifiers[i].text());
}
return result.length > 0 ? result.join(',') : ScriptElementKindModifier.none;
}
public visitModuleDeclaration(node: TypeScript.ModuleDeclarationSyntax): void {
var names = this.getModuleNames(node);
this.visitModuleDeclarationWorker(node, names, 0);
}
private visitModuleDeclarationWorker(node: TypeScript.ModuleDeclarationSyntax, names: string[], nameIndex: number): void {
if (nameIndex === names.length) {
// We're after all the module names, descend and process all children.
super.visitModuleDeclaration(node);
}
else {
var name = names[nameIndex];
var kind = ScriptElementKind.moduleElement;
this.createItem(node, node.modifiers, kind, name);
this.pushNewContainerScope(name, kind);
this.visitModuleDeclarationWorker(node, names, nameIndex + 1);
this.popScope();
}
}
private getModuleNames(node: TypeScript.ModuleDeclarationSyntax): string[] {
var result: string[] = [];
if (node.stringLiteral) {
result.push(node.stringLiteral.text());
}
else {
this.getModuleNamesHelper(node.name, result);
}
return result;
}
private getModuleNamesHelper(name: TypeScript.INameSyntax, result: string[]): void {
if (name.kind() === TypeScript.SyntaxKind.QualifiedName) {
var qualifiedName = <TypeScript.QualifiedNameSyntax>name;
this.getModuleNamesHelper(qualifiedName.left, result);
result.push(qualifiedName.right.text());
}
else {
result.push((<TypeScript.ISyntaxToken>name).text());
}
}
public visitClassDeclaration(node: TypeScript.ClassDeclarationSyntax): void {
var name = node.identifier.text();
var kind = ScriptElementKind.classElement;
this.createItem(node, node.modifiers, kind, name);
this.pushNewContainerScope(name, kind);
super.visitClassDeclaration(node);
this.popScope();
}
public visitInterfaceDeclaration(node: TypeScript.InterfaceDeclarationSyntax): void {
var name = node.identifier.text();
var kind = ScriptElementKind.interfaceElement;
this.createItem(node, node.modifiers, kind, name);
this.pushNewContainerScope(name, kind);
super.visitInterfaceDeclaration(node);
this.popScope();
}
public visitObjectType(node: TypeScript.ObjectTypeSyntax): void {
// Ignore an object type if we aren't inside an interface declaration. We don't want
// to add some random object type's members to the nav bar.
if (node.parent.kind() === SyntaxKind.InterfaceDeclaration) {
super.visitObjectType(node);
}
}
public visitEnumDeclaration(node: TypeScript.EnumDeclarationSyntax): void {
var name = node.identifier.text();
var kind = ScriptElementKind.enumElement;
this.createItem(node, node.modifiers, kind, name);
this.pushNewContainerScope(name, kind);
super.visitEnumDeclaration(node);
this.popScope();
}
public visitConstructorDeclaration(node: TypeScript.ConstructorDeclarationSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.constructorImplementationElement, "constructor");
// Search the parameter list of class properties
var parameters = node.callSignature.parameterList.parameters;
if (parameters) {
for (var i = 0, n = parameters.length; i < n; i++) {
var parameter = <ParameterSyntax>parameters[i];
Debug.assert(parameter.kind() === SyntaxKind.Parameter);
if (SyntaxUtilities.containsToken(parameter.modifiers, SyntaxKind.PublicKeyword) ||
SyntaxUtilities.containsToken(parameter.modifiers, SyntaxKind.PrivateKeyword)) {
this.createItem(node, parameter.modifiers, ScriptElementKind.memberVariableElement, parameter.identifier.text());
}
}
}
// No need to descend into a constructor;
}
public visitMemberFunctionDeclaration(node: TypeScript.MemberFunctionDeclarationSyntax): void {
this.createItem(node, node.modifiers, ScriptElementKind.memberFunctionElement, node.propertyName.text());
// No need to descend into a member function;
}
public visitGetAccessor(node: TypeScript.GetAccessorSyntax): void {
this.createItem(node, node.modifiers, ScriptElementKind.memberGetAccessorElement, node.propertyName.text());
// No need to descend into a member accessor;
}
public visitSetAccessor(node: TypeScript.SetAccessorSyntax): void {
this.createItem(node, node.modifiers, ScriptElementKind.memberSetAccessorElement, node.propertyName.text());
// No need to descend into a member accessor;
}
public visitVariableDeclarator(node: TypeScript.VariableDeclaratorSyntax): void {
var modifiers = node.parent.kind() === SyntaxKind.MemberVariableDeclaration
? (<MemberVariableDeclarationSyntax>node.parent).modifiers
: TypeScript.Syntax.emptyList<ISyntaxToken>();
var kind = node.parent.kind() === SyntaxKind.MemberVariableDeclaration
? ScriptElementKind.memberVariableElement
: ScriptElementKind.variableElement;
this.createItem(node, modifiers, kind, node.propertyName.text());
// No need to descend into a variable declarator;
}
public visitIndexSignature(node: TypeScript.IndexSignatureSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.indexSignatureElement, "[]");
// No need to descend into an index signature;
}
public visitEnumElement(node: TypeScript.EnumElementSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.memberVariableElement, node.propertyName.text());
// No need to descend into an enum element;
}
public visitCallSignature(node: TypeScript.CallSignatureSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.callSignatureElement, "()");
// No need to descend into a call signature;
}
public visitConstructSignature(node: TypeScript.ConstructSignatureSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.constructSignatureElement, "new()");
// No need to descend into a construct signature;
}
public visitMethodSignature(node: TypeScript.MethodSignatureSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.memberFunctionElement, node.propertyName.text());
// No need to descend into a method signature;
}
public visitPropertySignature(node: TypeScript.PropertySignatureSyntax): void {
this.createItem(node, TypeScript.Syntax.emptyList<ISyntaxToken>(), ScriptElementKind.memberVariableElement, node.propertyName.text());
// No need to descend into a property signature;
}
public visitFunctionDeclaration(node: TypeScript.FunctionDeclarationSyntax): void {
// in the case of:
// declare function
// the parser will synthesize an identifier.
// we shouldn't add an unnamed function declaration
if (width(node.identifier) > 0) {
this.createItem(node, node.modifiers, ScriptElementKind.functionElement, node.identifier.text());
}
// No need to descend into a function declaration;
}
// Common statement types. Don't even bother walking into them as we'll never find anything
// inside that we'd put in the navbar.
public visitBlock(node: TypeScript.BlockSyntax): void {
}
public visitIfStatement(node: TypeScript.IfStatementSyntax): void {
}
public visitExpressionStatement(node: TypeScript.ExpressionStatementSyntax): void {
}
public visitThrowStatement(node: TypeScript.ThrowStatementSyntax): void {
}
public visitReturnStatement(node: TypeScript.ReturnStatementSyntax): void {
}
public visitSwitchStatement(node: TypeScript.SwitchStatementSyntax): void {
}
public visitWithStatement(node: TypeScript.WithStatementSyntax): void {
}
public visitTryStatement(node: TypeScript.TryStatementSyntax): void {
}
public visitLabeledStatement(node: TypeScript.LabeledStatementSyntax): void {
}
}
}

155
src/services/indentation.ts Normal file
View File

@@ -0,0 +1,155 @@
///<reference path='references.ts' />
module TypeScript.Indentation {
export function columnForEndOfTokenAtPosition(syntaxTree: SyntaxTree, position: number, options: FormattingOptions): number {
var token = findToken(syntaxTree.sourceUnit(), position);
return columnForStartOfTokenAtPosition(syntaxTree, position, options) + width(token);
}
export function columnForStartOfTokenAtPosition(syntaxTree: SyntaxTree, position: number, options: FormattingOptions): number {
var token = findToken(syntaxTree.sourceUnit(), position);
// Walk backward from this token until we find the first token in the line. For each token
// we see (that is not the first tokem in line), push the entirety of the text into the text
// array. Then, for the first token, add its text (without its leading trivia) to the text
// array. i.e. if we have:
//
// var foo = a => bar();
//
// And we want the column for the start of 'bar', then we'll add the underlinded portions to
// the text array:
//
// var foo = a => bar();
// _
// __
// __
// ____
// ____
var firstTokenInLine = Syntax.firstTokenInLineContainingPosition(syntaxTree, token.fullStart());
var leadingTextInReverse: string[] = [];
var current = token;
while (current !== firstTokenInLine) {
current = previousToken(current);
if (current === firstTokenInLine) {
// We're at the first token in teh line.
// We don't want the leading trivia for this token. That will be taken care of in
// columnForFirstNonWhitespaceCharacterInLine. So just push the trailing trivia
// and then the token text.
leadingTextInReverse.push(current.trailingTrivia().fullText());
leadingTextInReverse.push(current.text());
}
else {
// We're at an intermediate token on the line. Just push all its text into the array.
leadingTextInReverse.push(current.fullText());
}
}
// Now, add all trivia to the start of the line on the first token in the list.
collectLeadingTriviaTextToStartOfLine(firstTokenInLine, leadingTextInReverse);
return columnForLeadingTextInReverse(leadingTextInReverse, options);
}
export function columnForStartOfFirstTokenInLineContainingPosition(syntaxTree: SyntaxTree, position: number, options: FormattingOptions): number {
// Walk backward through the tokens until we find the first one on the line.
var firstTokenInLine = Syntax.firstTokenInLineContainingPosition(syntaxTree, position);
var leadingTextInReverse: string[] = [];
// Now, add all trivia to the start of the line on the first token in the list.
collectLeadingTriviaTextToStartOfLine(firstTokenInLine, leadingTextInReverse);
return columnForLeadingTextInReverse(leadingTextInReverse, options);
}
// Collect all the trivia that precedes this token. Stopping when we hit a newline trivia
// or a multiline comment that spans multiple lines. This is meant to be called on the first
// token in a line.
function collectLeadingTriviaTextToStartOfLine(firstTokenInLine: ISyntaxToken,
leadingTextInReverse: string[]) {
var leadingTrivia = firstTokenInLine.leadingTrivia();
for (var i = leadingTrivia.count() - 1; i >= 0; i--) {
var trivia = leadingTrivia.syntaxTriviaAt(i);
if (trivia.kind() === SyntaxKind.NewLineTrivia) {
break;
}
if (trivia.kind() === SyntaxKind.MultiLineCommentTrivia) {
var lineSegments = Syntax.splitMultiLineCommentTriviaIntoMultipleLines(trivia);
leadingTextInReverse.push(ArrayUtilities.last(lineSegments));
if (lineSegments.length > 0) {
// This multiline comment actually spanned multiple lines. So we're done.
break;
}
// It was only on a single line, so keep on going.
}
leadingTextInReverse.push(trivia.fullText());
}
}
function columnForLeadingTextInReverse(leadingTextInReverse: string[],
options: FormattingOptions): number {
var column = 0;
// walk backwards. This means we're actually walking forward from column 0 to the start of
// the token.
for (var i = leadingTextInReverse.length - 1; i >= 0; i--) {
var text = leadingTextInReverse[i];
column = columnForPositionInStringWorker(text, text.length, column, options);
}
return column;
}
// Returns the column that this input string ends at (assuming it starts at column 0).
export function columnForPositionInString(input: string, position: number, options: FormattingOptions): number {
return columnForPositionInStringWorker(input, position, 0, options);
}
function columnForPositionInStringWorker(input: string, position: number, startColumn: number, options: FormattingOptions): number {
var column = startColumn;
var spacesPerTab = options.spacesPerTab;
for (var j = 0; j < position; j++) {
var ch = input.charCodeAt(j);
if (ch === CharacterCodes.tab) {
column += spacesPerTab - column % spacesPerTab;
}
else {
column++;
}
}
return column;
}
export function indentationString(column: number, options: FormattingOptions): string {
var numberOfTabs = 0;
var numberOfSpaces = Math.max(0, column);
if (options.useTabs) {
numberOfTabs = Math.floor(column / options.spacesPerTab);
numberOfSpaces -= numberOfTabs * options.spacesPerTab;
}
return StringUtilities.repeat('\t', numberOfTabs) +
StringUtilities.repeat(' ', numberOfSpaces);
}
export function firstNonWhitespacePosition(value: string): number {
for (var i = 0; i < value.length; i++) {
var ch = value.charCodeAt(i);
if (!CharacterInfo.isWhitespace(ch)) {
return i;
}
}
return value.length;
}
}

207
src/services/indenter.ts Normal file
View File

@@ -0,0 +1,207 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
export class Indenter {
public static getIndentation(node: TypeScript.SourceUnitSyntax, soruceText: TypeScript.IScriptSnapshot, position: number, editorOptions: TypeScript.Services.EditorOptions): number {
var indentation = 0;
var currentToken = TypeScript.findToken(node, position);
var currentNode: TypeScript.ISyntaxElement = currentToken;
if (currentToken.kind() === TypeScript.SyntaxKind.EndOfFileToken) {
// Ignore EOF tokens, pick the one before it
currentNode = previousToken(currentToken);
}
else if (Indenter.belongsToBracket(soruceText, currentToken, position)) {
// Let braces and brackets take the indentation of thier parents
currentNode = currentToken.parent;
}
if (currentNode === null) {
return indentation;
}
// Check if this is a valid node to provide indentation
if (currentNode.kind() === TypeScript.SyntaxKind.StringLiteral ||
currentNode.kind() === TypeScript.SyntaxKind.RegularExpressionLiteral) {
return indentation;
}
var currentElement = currentNode;
var parent = currentNode.parent;
while (parent !== null) {
// Skip nodes that start at the position, these will have the indentation level of thier parent
if (fullStart(parent) !== fullStart(currentNode)) {
if (Indenter.isInContainerNode(parent, currentElement)) {
indentation += editorOptions.IndentSize;
}
else {
var listIndentation = Indenter.getCustomListIndentation(parent, currentElement);
if (listIndentation !== -1) {
// Found a list node with special indentation, If the list items span multiple lines, we want
// to use the user-specified indentation; return.
return indentation + listIndentation;
}
}
}
currentNode = parent;
currentElement = parent;
parent = parent.parent;
}
return indentation;
}
private static belongsToBracket(sourceText: TypeScript.IScriptSnapshot, token: TypeScript.ISyntaxToken, position: number): boolean {
switch (token.kind()) {
case TypeScript.SyntaxKind.OpenBraceToken:
case TypeScript.SyntaxKind.CloseBraceToken:
case TypeScript.SyntaxKind.OpenParenToken:
case TypeScript.SyntaxKind.CloseParenToken:
case TypeScript.SyntaxKind.OpenBracketToken:
case TypeScript.SyntaxKind.CloseBracketToken:
// the current token is a bracket, check if the current position is separated from it by a new line
if (position < start(token)) {
var text = sourceText.getText(position, start(token));
for(var i = 0; i< text.length; i++){
if (TypeScript.CharacterInfo.isLineTerminator(text.charCodeAt(i))) {
return false;
}
}
}
return true;
}
return false;
}
private static isInContainerNode(parent: TypeScript.ISyntaxElement, element: TypeScript.ISyntaxElement): boolean {
switch (parent.kind()) {
case TypeScript.SyntaxKind.ClassDeclaration:
case TypeScript.SyntaxKind.ModuleDeclaration:
case TypeScript.SyntaxKind.EnumDeclaration:
case TypeScript.SyntaxKind.ImportDeclaration:
case TypeScript.SyntaxKind.Block:
case TypeScript.SyntaxKind.SwitchStatement:
case TypeScript.SyntaxKind.CaseSwitchClause:
case TypeScript.SyntaxKind.DefaultSwitchClause:
return true;
case TypeScript.SyntaxKind.ObjectType:
return true;
case TypeScript.SyntaxKind.InterfaceDeclaration:
return element.kind() !== TypeScript.SyntaxKind.ObjectType;
case TypeScript.SyntaxKind.FunctionDeclaration:
case TypeScript.SyntaxKind.MemberFunctionDeclaration:
case TypeScript.SyntaxKind.GetAccessor:
case TypeScript.SyntaxKind.SetAccessor:
case TypeScript.SyntaxKind.FunctionExpression:
case TypeScript.SyntaxKind.CatchClause:
case TypeScript.SyntaxKind.FinallyClause:
case TypeScript.SyntaxKind.FunctionDeclaration:
case TypeScript.SyntaxKind.ConstructorDeclaration:
case TypeScript.SyntaxKind.ForStatement:
case TypeScript.SyntaxKind.ForInStatement:
case TypeScript.SyntaxKind.WhileStatement:
case TypeScript.SyntaxKind.DoStatement:
case TypeScript.SyntaxKind.WithStatement:
case TypeScript.SyntaxKind.IfStatement:
case TypeScript.SyntaxKind.ElseClause:
// The block has already been conted before, ignore the container node
return element.kind() !== TypeScript.SyntaxKind.Block;
case TypeScript.SyntaxKind.TryStatement:
// If inside the try body, the block element will take care of the indentation
// If not, we do not want to indent, as the next token would probally be catch or finally
// and we want these on the same indentation level.
return false;
default:
return isNode(parent) && SyntaxUtilities.isStatement(parent);
}
}
private static getCustomListIndentation(list: TypeScript.ISyntaxElement, element: TypeScript.ISyntaxElement): number {
switch (list.kind()) {
case TypeScript.SyntaxKind.SeparatedList:
// If it is the first in the list, let it have its parents indentation; no custom indentation here.
for (var i = 0, n = childCount(list); i < n ; i++) {
var child = childAt(list, i);
if (child !== null && child === element)
return Indenter.getListItemIndentation(list, i - 1);
}
break;
case TypeScript.SyntaxKind.ArgumentList:
// The separated list has been handled in the previous case, this is just if we are after
// the last element of the list, we want to get the indentation of the last element of the list
var argumentList = <TypeScript.ArgumentListSyntax> list;
var _arguments = argumentList.arguments;
if (_arguments !== null && argumentList.closeParenToken === element) {
return Indenter.getListItemIndentation(_arguments, childCount(_arguments) - 1);
}
break;
case TypeScript.SyntaxKind.ParameterList:
// The separated list has been handled in the previous case, this is just if we are after
// the last element of the list, we want to get the indentation of the last element of the list
var parameterList = <TypeScript.ParameterListSyntax> list;
var parameters = parameterList.parameters;
if (parameters !== null && parameterList.closeParenToken === element) {
return Indenter.getListItemIndentation(parameters, childCount(parameters) - 1);
}
break;
case TypeScript.SyntaxKind.TypeArgumentList:
// The separated list has been handled in the previous case, this is just if we are after
// the last element of the list, we want to get the indentation of the last element of the list
var typeArgumentList = <TypeScript.TypeArgumentListSyntax> list;
var typeArguments = typeArgumentList.typeArguments;
if (typeArguments !== null && typeArgumentList.greaterThanToken === element) {
return Indenter.getListItemIndentation(typeArguments, childCount(typeArguments) - 1);
}
break;
case TypeScript.SyntaxKind.TypeParameterList:
// The separated list has been handled in the previous case, this is just if we are after
// the last element of the list, we want to get the indentation of the last element of the list
var typeParameterList = <TypeScript.TypeParameterListSyntax> list;
var typeParameters = typeParameterList.typeParameters;
if (typeParameters !== null && typeParameterList.greaterThanToken === element) {
return Indenter.getListItemIndentation(typeParameters, childCount(typeParameters) - 1);
}
break;
}
return -1;
}
private static getListItemIndentation(list: TypeScript.ISyntaxElement, elementIndex: number): number {
for (var i = elementIndex; i > 0 ; i--) {
var child = childAt(list, i);
var previousChild = childAt(list, i - 1);
if ((child !== null && firstToken(child).leadingTrivia().hasNewLine()) ||
(previousChild !== null && lastToken(previousChild).trailingTrivia().hasNewLine())) {
// TODO: get the trivia after new line
return leadingTriviaWidth(child);
}
}
return -1;
}
}
}

View File

@@ -0,0 +1,73 @@
// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='references.ts' />
module TypeScript.Services {
export class KeywordCompletions {
private static keywords = [
"break",
"case",
"catch",
"class",
"constructor",
"continue",
"debugger",
"declare",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"get",
"if",
"implements",
"import",
"in",
"instanceof",
"interface",
"module",
"new",
"null",
"private",
"public",
"require",
"return",
"set",
"static",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"while",
"with",
];
private static keywordCompletions: ResolvedCompletionEntry[] = null;
public static getKeywordCompltions(): ResolvedCompletionEntry[]{
if (KeywordCompletions.keywordCompletions === null) {
var completions: ResolvedCompletionEntry[] = [];
for (var i = 0, n = KeywordCompletions.keywords.length; i < n; i++) {
var keyword = KeywordCompletions.keywords[i];
var entry = new ResolvedCompletionEntry(/*name*/ keyword, ScriptElementKind.keyword, ScriptElementKindModifier.none, /*type*/null, /*fullName*/ keyword, /*docComment*/ null);
completions.push(entry);
}
KeywordCompletions.keywordCompletions = completions;
}
return KeywordCompletions.keywordCompletions;
}
}
}

View File

@@ -0,0 +1,345 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
///<reference path='diagnosticServices.ts' />
module TypeScript.Services {
//
// Public interface of the host of a language service instance.
//
export interface ILanguageServiceHost extends TypeScript.ILogger, TypeScript.IReferenceResolverHost {
getCompilationSettings(): ts.CompilerOptions;
getScriptFileNames(): string[];
getScriptVersion(fileName: string): number;
getScriptIsOpen(fileName: string): boolean;
getScriptByteOrderMark(fileName: string): TypeScript.ByteOrderMark;
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot;
getDiagnosticsObject(): TypeScript.Services.ILanguageServicesDiagnostics;
getLocalizedDiagnosticMessages(): any;
getCancellationToken(): ICancellationToken;
}
//
// Public services of a language service instance associated
// with a language service host instance
//
export interface ILanguageService {
// Note: refresh is a no-op now. It is only around for back compat purposes.
refresh(): void;
cleanupSemanticCache(): void;
getSyntacticDiagnostics(fileName: string): ts.Diagnostic[];
getSemanticDiagnostics(fileName: string): ts.Diagnostic[];
getCompilerOptionsDiagnostics(): ts.Diagnostic[];
getCompletionsAtPosition(fileName: string, position: number, isMemberCompletion: boolean): CompletionInfo;
getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails;
getTypeAtPosition(fileName: string, position: number): TypeInfo;
getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): SpanInfo;
getBreakpointStatementAtPosition(fileName: string, position: number): SpanInfo;
getSignatureAtPosition(fileName: string, position: number): SignatureInfo;
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getImplementorsAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[];
getScriptLexicalStructure(fileName: string): NavigateToItem[];
getOutliningRegions(fileName: string): TypeScript.TextSpan[];
getBraceMatchingAtPosition(fileName: string, position: number): TypeScript.TextSpan[];
getIndentationAtPosition(fileName: string, position: number, options: TypeScript.Services.EditorOptions): number;
getFormattingEditsForRange(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsForDocument(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsOnPaste(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextEdit[];
getEmitOutput(fileName: string): TypeScript.EmitOutput;
getSyntaxTree(fileName: string): TypeScript.SyntaxTree;
dispose(): void;
}
export function logInternalError(logger: TypeScript.ILogger, err: Error) {
logger.log("*INTERNAL ERROR* - Exception in typescript services: " + err.message);
}
export class ReferenceEntry {
public fileName: string = ""
public minChar: number = -1;
public limChar: number = -1;
public isWriteAccess: boolean = false;
constructor(fileName: string, minChar: number, limChar: number, isWriteAccess: boolean) {
this.fileName = fileName;
this.minChar = minChar;
this.limChar = limChar;
this.isWriteAccess = isWriteAccess;
}
}
export class NavigateToItem {
public name: string = "";
public kind: string = ""; // see ScriptElementKind
public kindModifiers: string = ""; // see ScriptElementKindModifier, comma separated
public matchKind: string = "";
public fileName: string = "";
public minChar: number = -1;
public limChar: number = -1;
public additionalSpans: SpanInfo[] = null;
public containerName: string = "";
public containerKind: string = ""; // see ScriptElementKind
}
export class TextEdit {
constructor(public minChar: number, public limChar: number, public text: string) {
}
static createInsert(pos: number, text: string): TextEdit {
return new TextEdit(pos, pos, text);
}
static createDelete(minChar: number, limChar: number): TextEdit {
return new TextEdit(minChar, limChar, "");
}
static createReplace(minChar: number, limChar: number, text: string): TextEdit {
return new TextEdit(minChar, limChar, text);
}
}
export class EditorOptions {
public IndentSize: number = 4;
public TabSize: number = 4;
public NewLineCharacter: string = "\r\n";
public ConvertTabsToSpaces: boolean = true;
public static clone(objectToClone: EditorOptions): EditorOptions {
var editorOptions = new EditorOptions();
editorOptions.IndentSize = objectToClone.IndentSize;
editorOptions.TabSize = objectToClone.TabSize;
editorOptions.NewLineCharacter = objectToClone.NewLineCharacter;
editorOptions.ConvertTabsToSpaces = objectToClone.ConvertTabsToSpaces;
return editorOptions;
}
}
export class FormatCodeOptions extends EditorOptions {
public InsertSpaceAfterCommaDelimiter: boolean = true;
public InsertSpaceAfterSemicolonInForStatements: boolean = true;
public InsertSpaceBeforeAndAfterBinaryOperators: boolean = true;
public InsertSpaceAfterKeywordsInControlFlowStatements: boolean = true;
public InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean = false;
public InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean = false;
public PlaceOpenBraceOnNewLineForFunctions: boolean = false;
public PlaceOpenBraceOnNewLineForControlBlocks: boolean = false;
public static clone(objectToClone: FormatCodeOptions ): FormatCodeOptions {
var formatCodeOptions = <FormatCodeOptions>EditorOptions.clone(objectToClone);
formatCodeOptions.InsertSpaceAfterCommaDelimiter = objectToClone.InsertSpaceAfterCommaDelimiter;
formatCodeOptions.InsertSpaceAfterSemicolonInForStatements = objectToClone.InsertSpaceAfterSemicolonInForStatements;
formatCodeOptions.InsertSpaceBeforeAndAfterBinaryOperators = objectToClone.InsertSpaceBeforeAndAfterBinaryOperators;
formatCodeOptions.InsertSpaceAfterKeywordsInControlFlowStatements = objectToClone.InsertSpaceAfterKeywordsInControlFlowStatements;
formatCodeOptions.InsertSpaceAfterFunctionKeywordForAnonymousFunctions = objectToClone.InsertSpaceAfterFunctionKeywordForAnonymousFunctions;
formatCodeOptions.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = objectToClone.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis;
formatCodeOptions.PlaceOpenBraceOnNewLineForFunctions = objectToClone.PlaceOpenBraceOnNewLineForFunctions;
formatCodeOptions.PlaceOpenBraceOnNewLineForControlBlocks = objectToClone.PlaceOpenBraceOnNewLineForControlBlocks;
return formatCodeOptions;
}
}
export class DefinitionInfo {
constructor(
public fileName: string,
public minChar: number,
public limChar: number,
public kind: string,
public name: string,
public containerKind: string,
public containerName: string) {
}
}
export class TypeInfo {
constructor(
public memberName: TypeScript.MemberName,
public docComment: string,
public fullSymbolName: string,
public kind: string,
public minChar: number,
public limChar: number) {
}
}
export class SpanInfo {
constructor(public minChar: number, public limChar: number, public text: string = null) {
}
}
export class SignatureInfo {
public actual: ActualSignatureInfo;
public formal: FormalSignatureItemInfo[] = []; // Formal signatures
public activeFormal: number; // Index of the "best match" formal signature
}
export class FormalSignatureItemInfo {
public signatureInfo: string;
public typeParameters: FormalTypeParameterInfo[] = [];
public parameters: FormalParameterInfo[] = []; // Array of parameters
public docComment: string; // Help for the signature
}
export class FormalTypeParameterInfo {
public name: string; // Type parameter name
public docComment: string; // Comments that contain help for the parameter
public minChar: number; // minChar for parameter info in the formal signature info string
public limChar: number; // lim char for parameter info in the formal signature info string
}
export class FormalParameterInfo {
public name: string; // Parameter name
public isVariable: boolean; // true if parameter is var args
public docComment: string; // Comments that contain help for the parameter
public minChar: number; // minChar for parameter info in the formal signature info string
public limChar: number; // lim char for parameter info in the formal signature info string
}
export class ActualSignatureInfo {
public parameterMinChar: number;
public parameterLimChar: number;
public currentParameterIsTypeParameter: boolean; // current parameter is a type argument or a normal argument
public currentParameter: number; // Index of active parameter in "parameters" or "typeParamters" array
}
export class CompletionInfo {
public maybeInaccurate = false;
public isMemberCompletion = false;
public entries: CompletionEntry[] = [];
}
export interface CompletionEntry {
name: string;
kind: string; // see ScriptElementKind
kindModifiers: string; // see ScriptElementKindModifier, comma separated
}
export interface CompletionEntryDetails {
name: string;
kind: string; // see ScriptElementKind
kindModifiers: string; // see ScriptElementKindModifier, comma separated
type: string;
fullSymbolName: string;
docComment: string;
}
export class ScriptElementKind {
static unknown = "";
// predefined type (void) or keyword (class)
static keyword = "keyword";
// top level script node
static scriptElement = "script";
// module foo {}
static moduleElement = "module";
// class X {}
static classElement = "class";
// interface Y {}
static interfaceElement = "interface";
// enum E
static enumElement = "enum";
// Inside module and script only
// var v = ..
static variableElement = "var";
// Inside function
static localVariableElement = "local var";
// Inside module and script only
// function f() { }
static functionElement = "function";
// Inside function
static localFunctionElement = "local function";
// class X { [public|private]* foo() {} }
static memberFunctionElement = "method";
// class X { [public|private]* [get|set] foo:number; }
static memberGetAccessorElement = "getter";
static memberSetAccessorElement = "setter";
// class X { [public|private]* foo:number; }
// interface Y { foo:number; }
static memberVariableElement = "property";
// class X { constructor() { } }
static constructorImplementationElement = "constructor";
// interface Y { ():number; }
static callSignatureElement = "call";
// interface Y { []:number; }
static indexSignatureElement = "index";
// interface Y { new():Y; }
static constructSignatureElement = "construct";
// function foo(*Y*: string)
static parameterElement = "parameter";
static typeParameterElement = "type parameter";
static primitiveType = "primitive type";
}
export class ScriptElementKindModifier {
static none = "";
static publicMemberModifier = "public";
static privateMemberModifier = "private";
static exportedModifier = "export";
static ambientModifier = "declare";
static staticModifier = "static";
}
export class MatchKind {
static none: string = null;
static exact = "exact";
static subString = "substring";
static prefix = "prefix";
}
export class DiagnosticCategory {
static none = "";
static error = "error";
static warning = "warning";
static message = "message";
}
}

View File

@@ -0,0 +1,342 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='typescriptServices.ts' />
///<reference path='diagnosticServices.ts' />
module TypeScript.Services {
//
// Public interface of the host of a language service instance.
//
export interface ILanguageServiceHost extends TypeScript.ILogger, TypeScript.IReferenceResolverHost {
getCompilationSettings(): TypeScript.CompilationSettings;
getScriptFileNames(): string[];
getScriptVersion(fileName: string): number;
getScriptIsOpen(fileName: string): boolean;
getScriptByteOrderMark(fileName: string): TypeScript.ByteOrderMark;
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot;
getDiagnosticsObject(): TypeScript.Services.ILanguageServicesDiagnostics;
getLocalizedDiagnosticMessages(): any;
}
//
// Public services of a language service instance associated
// with a language service host instance
//
export interface ILanguageService {
// Note: refresh is a no-op now. It is only around for back compat purposes.
refresh(): void;
cleanupSemanticCache(): void;
getSyntacticDiagnostics(fileName: string): TypeScript.Diagnostic[];
getSemanticDiagnostics(fileName: string): TypeScript.Diagnostic[];
getCompilerOptionsDiagnostics(): TypeScript.Diagnostic[];
getCompletionsAtPosition(fileName: string, position: number, isMemberCompletion: boolean): CompletionInfo;
getCompletionEntryDetails(fileName: string, position: number, entryName: string): CompletionEntryDetails;
getTypeAtPosition(fileName: string, position: number): TypeInfo;
getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): SpanInfo;
getBreakpointStatementAtPosition(fileName: string, position: number): SpanInfo;
getSignatureAtPosition(fileName: string, position: number): SignatureInfo;
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getImplementorsAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[];
getScriptLexicalStructure(fileName: string): NavigateToItem[];
getOutliningRegions(fileName: string): TypeScript.TextSpan[];
getBraceMatchingAtPosition(fileName: string, position: number): TypeScript.TextSpan[];
getIndentationAtPosition(fileName: string, position: number, options: TypeScript.Services.EditorOptions): number;
getFormattingEditsForRange(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsForDocument(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsOnPaste(fileName: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[];
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextEdit[];
getEmitOutput(fileName: string): TypeScript.EmitOutput;
getSyntaxTree(fileName: string): TypeScript.SyntaxTree;
}
export function logInternalError(logger: TypeScript.ILogger, err: Error) {
logger.log("*INTERNAL ERROR* - Exception in typescript services: " + err.message);
}
export class ReferenceEntry {
public fileName: string = ""
public minChar: number = -1;
public limChar: number = -1;
public isWriteAccess: boolean = false;
constructor(fileName: string, minChar: number, limChar: number, isWriteAccess: boolean) {
this.fileName = fileName;
this.minChar = minChar;
this.limChar = limChar;
this.isWriteAccess = isWriteAccess;
}
}
export class NavigateToItem {
public name: string = "";
public kind: string = ""; // see ScriptElementKind
public kindModifiers: string = ""; // see ScriptElementKindModifier, comma separated
public matchKind: string = "";
public fileName: string = "";
public minChar: number = -1;
public limChar: number = -1;
public additionalSpans: SpanInfo[] = null;
public containerName: string = "";
public containerKind: string = ""; // see ScriptElementKind
}
export class TextEdit {
constructor(public minChar: number, public limChar: number, public text: string) {
}
static createInsert(pos: number, text: string): TextEdit {
return new TextEdit(pos, pos, text);
}
static createDelete(minChar: number, limChar: number): TextEdit {
return new TextEdit(minChar, limChar, "");
}
static createReplace(minChar: number, limChar: number, text: string): TextEdit {
return new TextEdit(minChar, limChar, text);
}
}
export class EditorOptions {
public IndentSize: number = 4;
public TabSize: number = 4;
public NewLineCharacter: string = "\r\n";
public ConvertTabsToSpaces: boolean = true;
public static clone(objectToClone: EditorOptions): EditorOptions {
var editorOptions = new EditorOptions();
editorOptions.IndentSize = objectToClone.IndentSize;
editorOptions.TabSize = objectToClone.TabSize;
editorOptions.NewLineCharacter = objectToClone.NewLineCharacter;
editorOptions.ConvertTabsToSpaces = objectToClone.ConvertTabsToSpaces;
return editorOptions;
}
}
export class FormatCodeOptions extends EditorOptions {
public InsertSpaceAfterCommaDelimiter: boolean = true;
public InsertSpaceAfterSemicolonInForStatements: boolean = true;
public InsertSpaceBeforeAndAfterBinaryOperators: boolean = true;
public InsertSpaceAfterKeywordsInControlFlowStatements: boolean = true;
public InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean = false;
public InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean = false;
public PlaceOpenBraceOnNewLineForFunctions: boolean = false;
public PlaceOpenBraceOnNewLineForControlBlocks: boolean = false;
public static clone(objectToClone: FormatCodeOptions ): FormatCodeOptions {
var formatCodeOptions = <FormatCodeOptions>EditorOptions.clone(objectToClone);
formatCodeOptions.InsertSpaceAfterCommaDelimiter = objectToClone.InsertSpaceAfterCommaDelimiter;
formatCodeOptions.InsertSpaceAfterSemicolonInForStatements = objectToClone.InsertSpaceAfterSemicolonInForStatements;
formatCodeOptions.InsertSpaceBeforeAndAfterBinaryOperators = objectToClone.InsertSpaceBeforeAndAfterBinaryOperators;
formatCodeOptions.InsertSpaceAfterKeywordsInControlFlowStatements = objectToClone.InsertSpaceAfterKeywordsInControlFlowStatements;
formatCodeOptions.InsertSpaceAfterFunctionKeywordForAnonymousFunctions = objectToClone.InsertSpaceAfterFunctionKeywordForAnonymousFunctions;
formatCodeOptions.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis = objectToClone.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis;
formatCodeOptions.PlaceOpenBraceOnNewLineForFunctions = objectToClone.PlaceOpenBraceOnNewLineForFunctions;
formatCodeOptions.PlaceOpenBraceOnNewLineForControlBlocks = objectToClone.PlaceOpenBraceOnNewLineForControlBlocks;
return formatCodeOptions;
}
}
export class DefinitionInfo {
constructor(
public fileName: string,
public minChar: number,
public limChar: number,
public kind: string,
public name: string,
public containerKind: string,
public containerName: string) {
}
}
export class TypeInfo {
constructor(
public memberName: TypeScript.MemberName,
public docComment: string,
public fullSymbolName: string,
public kind: string,
public minChar: number,
public limChar: number) {
}
}
export class SpanInfo {
constructor(public minChar: number, public limChar: number, public text: string = null) {
}
}
export class SignatureInfo {
public actual: ActualSignatureInfo;
public formal: FormalSignatureItemInfo[] = []; // Formal signatures
public activeFormal: number; // Index of the "best match" formal signature
}
export class FormalSignatureItemInfo {
public signatureInfo: string;
public typeParameters: FormalTypeParameterInfo[] = [];
public parameters: FormalParameterInfo[] = []; // Array of parameters
public docComment: string; // Help for the signature
}
export class FormalTypeParameterInfo {
public name: string; // Type parameter name
public docComment: string; // Comments that contain help for the parameter
public minChar: number; // minChar for parameter info in the formal signature info string
public limChar: number; // lim char for parameter info in the formal signature info string
}
export class FormalParameterInfo {
public name: string; // Parameter name
public isVariable: boolean; // true if parameter is var args
public docComment: string; // Comments that contain help for the parameter
public minChar: number; // minChar for parameter info in the formal signature info string
public limChar: number; // lim char for parameter info in the formal signature info string
}
export class ActualSignatureInfo {
public parameterMinChar: number;
public parameterLimChar: number;
public currentParameterIsTypeParameter: boolean; // current parameter is a type argument or a normal argument
public currentParameter: number; // Index of active parameter in "parameters" or "typeParamters" array
}
export class CompletionInfo {
public maybeInaccurate = false;
public isMemberCompletion = false;
public entries: CompletionEntry[] = [];
}
export interface CompletionEntry {
name: string;
kind: string; // see ScriptElementKind
kindModifiers: string; // see ScriptElementKindModifier, comma separated
}
export interface CompletionEntryDetails {
name: string;
kind: string; // see ScriptElementKind
kindModifiers: string; // see ScriptElementKindModifier, comma separated
type: string;
fullSymbolName: string;
docComment: string;
}
export class ScriptElementKind {
static unknown = "";
// predefined type (void) or keyword (class)
static keyword = "keyword";
// top level script node
static scriptElement = "script";
// module foo {}
static moduleElement = "module";
// class X {}
static classElement = "class";
// interface Y {}
static interfaceElement = "interface";
// enum E
static enumElement = "enum";
// Inside module and script only
// var v = ..
static variableElement = "var";
// Inside function
static localVariableElement = "local var";
// Inside module and script only
// function f() { }
static functionElement = "function";
// Inside function
static localFunctionElement = "local function";
// class X { [public|private]* foo() {} }
static memberFunctionElement = "method";
// class X { [public|private]* [get|set] foo:number; }
static memberGetAccessorElement = "getter";
static memberSetAccessorElement = "setter";
// class X { [public|private]* foo:number; }
// interface Y { foo:number; }
static memberVariableElement = "property";
// class X { constructor() { } }
static constructorImplementationElement = "constructor";
// interface Y { ():number; }
static callSignatureElement = "call";
// interface Y { []:number; }
static indexSignatureElement = "index";
// interface Y { new():Y; }
static constructSignatureElement = "construct";
// function foo(*Y*: string)
static parameterElement = "parameter";
static typeParameterElement = "type parameter";
static primitiveType = "primitive type";
}
export class ScriptElementKindModifier {
static none = "";
static publicMemberModifier = "public";
static privateMemberModifier = "private";
static exportedModifier = "export";
static ambientModifier = "declare";
static staticModifier = "static";
}
export class MatchKind {
static none: string = null;
static exact = "exact";
static subString = "substring";
static prefix = "prefix";
}
export class DiagnosticCategory {
static none = "";
static error = "error";
static warning = "warning";
static message = "message";
}
}

View File

@@ -0,0 +1,109 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript.Services {
export class OutliningElementsCollector extends TypeScript.DepthLimitedWalker {
// The maximum depth for collecting spans; this will cause us to miss deeply nested function/modules spans,
// but will guarantee performance will not be closely tied to tree depth.
private static MaximumDepth: number = 10;
private inObjectLiteralExpression: boolean = false;
private elements: TypeScript.TextSpan[] = [];
constructor() {
super(OutliningElementsCollector.MaximumDepth);
}
public visitClassDeclaration(node: TypeScript.ClassDeclarationSyntax): void {
this.addOutlineRange(node, node.openBraceToken, node.closeBraceToken);
super.visitClassDeclaration(node);
}
public visitInterfaceDeclaration(node: TypeScript.InterfaceDeclarationSyntax): void {
this.addOutlineRange(node, node.body.openBraceToken, node.body.closeBraceToken);
super.visitInterfaceDeclaration(node);
}
public visitModuleDeclaration(node: TypeScript.ModuleDeclarationSyntax): void {
this.addOutlineRange(node, node.openBraceToken, node.closeBraceToken);
super.visitModuleDeclaration(node);
}
public visitEnumDeclaration(node: TypeScript.EnumDeclarationSyntax): void {
this.addOutlineRange(node, node.openBraceToken, node.closeBraceToken);
super.visitEnumDeclaration(node);
}
public visitFunctionDeclaration(node: TypeScript.FunctionDeclarationSyntax): void {
this.addOutlineRange(node, node.block, node.block);
super.visitFunctionDeclaration(node);
}
public visitFunctionExpression(node: TypeScript.FunctionExpressionSyntax): void {
this.addOutlineRange(node, node.block, node.block);
super.visitFunctionExpression(node);
}
public visitConstructorDeclaration(node: TypeScript.ConstructorDeclarationSyntax): void {
this.addOutlineRange(node, node.block, node.block);
super.visitConstructorDeclaration(node);
}
public visitMemberFunctionDeclaration(node: TypeScript.MemberFunctionDeclarationSyntax): void {
this.addOutlineRange(node, node.block, node.block);
super.visitMemberFunctionDeclaration(node);
}
public visitGetAccessor(node: TypeScript.GetAccessorSyntax): void {
if (!this.inObjectLiteralExpression) {
this.addOutlineRange(node, node.block, node.block);
}
super.visitGetAccessor(node);
}
public visitSetAccessor(node: TypeScript.SetAccessorSyntax): void {
if (!this.inObjectLiteralExpression) {
this.addOutlineRange(node, node.block, node.block);
}
super.visitSetAccessor(node);
}
public visitObjectLiteralExpression(node: TypeScript.ObjectLiteralExpressionSyntax): void {
var savedInObjectLiteralExpression = this.inObjectLiteralExpression;
this.inObjectLiteralExpression = true;
super.visitObjectLiteralExpression(node);
this.inObjectLiteralExpression = savedInObjectLiteralExpression;
}
private addOutlineRange(node: TypeScript.ISyntaxNode, startElement: TypeScript.ISyntaxNodeOrToken, endElement: TypeScript.ISyntaxNodeOrToken) {
if (startElement && endElement && !isShared(startElement) && !isShared(endElement)) {
// Compute the position
var start = TypeScript.start(startElement);
var end = TypeScript.end(endElement);
// Push the new range
this.elements.push(TypeScript.TextSpan.fromBounds(start, end));
}
}
public static collectElements(node: TypeScript.SourceUnitSyntax): TypeScript.TextSpan[] {
var collector = new OutliningElementsCollector();
visitNodeOrToken(collector, node);
return collector.elements;
}
}
}

View File

@@ -0,0 +1,733 @@
/// <reference path='typescriptServices.ts' />
/// <reference path="..\compiler\types.ts"/>
/// <reference path="..\compiler\core.ts"/>
/// <reference path="..\compiler\scanner.ts"/>
/// <reference path="..\compiler\parser.ts"/>
/// <reference path="..\compiler\checker.ts"/>
module TypeScript.Services {
// Information about a specific host file.
class HostFileInformation {
private _sourceText: TypeScript.IScriptSnapshot;
constructor(
public filename: string,
private host: ILanguageServiceHost,
public version: number,
public isOpen: boolean,
public byteOrderMark: TypeScript.ByteOrderMark) {
this._sourceText = null;
}
public getScriptSnapshot(): TypeScript.IScriptSnapshot {
if (this._sourceText === null) {
this._sourceText = this.host.getScriptSnapshot(this.filename);
}
return this._sourceText;
}
}
export function getDefaultCompilerOptions(): ts.CompilerOptions {
// Set "ES5" target by default for language service
return { target: ts.ScriptTarget.ES5 };
}
// Cache host information about scripts. Should be refreshed
// at each language service public entry point, since we don't know when
// set of scripts handled by the host changes.
class HostCache {
private _filenameToEntry: TypeScript.StringHashTable<HostFileInformation>;
private _compilationSettings: ts.CompilerOptions;
constructor(host: ILanguageServiceHost) {
// script id => script index
this._filenameToEntry = new TypeScript.StringHashTable<HostFileInformation>();
var filenames = host.getScriptFileNames();
for (var i = 0, n = filenames.length; i < n; i++) {
var filename = filenames[i];
this._filenameToEntry.add(TypeScript.switchToForwardSlashes(filename), new HostFileInformation(
filename, host, host.getScriptVersion(filename), host.getScriptIsOpen(filename), host.getScriptByteOrderMark(filename)));
}
this._compilationSettings = host.getCompilationSettings() || getDefaultCompilerOptions();
}
public compilationSettings() {
return this._compilationSettings;
}
public contains(filename: string): boolean {
return this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename)) !== null;
}
public getHostfilename(filename: string) {
var hostCacheEntry = this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename));
if (hostCacheEntry) {
return hostCacheEntry.filename;
}
return filename;
}
public getfilenames(): string[] {
return this._filenameToEntry.getAllKeys();
}
public getVersion(filename: string): number {
return this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename)).version;
}
public isOpen(filename: string): boolean {
return this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename)).isOpen;
}
public getByteOrderMark(filename: string): TypeScript.ByteOrderMark {
return this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename)).byteOrderMark;
}
public getScriptSnapshot(filename: string): TypeScript.IScriptSnapshot {
return this._filenameToEntry.lookup(TypeScript.switchToForwardSlashes(filename)).getScriptSnapshot();
}
public getScriptTextChangeRangeSinceVersion(filename: string, lastKnownVersion: number): TypeScript.TextChangeRange {
var currentVersion = this.getVersion(filename);
if (lastKnownVersion === currentVersion) {
return TypeScript.TextChangeRange.unchanged; // "No changes"
}
var scriptSnapshot = this.getScriptSnapshot(filename);
return scriptSnapshot.getTextChangeRangeSinceVersion(lastKnownVersion);
}
}
export class SyntaxTreeCache {
private _hostCache: HostCache;
// For our syntactic only features, we also keep a cache of the syntax tree for the
// currently edited file.
private _currentfilename: string = "";
private _currentFileVersion: number = -1;
private _currentFileSyntaxTree: TypeScript.SyntaxTree = null;
private _currentFileScriptSnapshot: TypeScript.IScriptSnapshot = null;
constructor(private _host: ILanguageServiceHost) {
this._hostCache = new HostCache(_host);
}
public getCurrentFileSyntaxTree(filename: string): TypeScript.SyntaxTree {
this._hostCache = new HostCache(this._host);
var version = this._hostCache.getVersion(filename);
var syntaxTree: TypeScript.SyntaxTree = null;
if (this._currentFileSyntaxTree === null || this._currentfilename !== filename) {
var scriptSnapshot = this._hostCache.getScriptSnapshot(filename);
syntaxTree = this.createSyntaxTree(filename, scriptSnapshot);
}
else if (this._currentFileVersion !== version) {
var scriptSnapshot = this._hostCache.getScriptSnapshot(filename);
syntaxTree = this.updateSyntaxTree(filename, scriptSnapshot, this._currentFileSyntaxTree, this._currentFileVersion);
}
if (syntaxTree !== null) {
// All done, ensure state is up to date
this._currentFileScriptSnapshot = scriptSnapshot;
this._currentFileVersion = version;
this._currentfilename = filename;
this._currentFileSyntaxTree = syntaxTree;
}
return this._currentFileSyntaxTree;
}
public getCurrentScriptSnapshot(filename: string): IScriptSnapshot {
// update _currentFileScriptSnapshot as a part of 'getCurrentFileSyntaxTree' call
this.getCurrentFileSyntaxTree(filename);
return this._currentFileScriptSnapshot;
}
private createSyntaxTree(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot): TypeScript.SyntaxTree {
var text = TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot);
// For the purposes of features that use this syntax tree, we can just use the default
// compilation settings. The features only use the syntax (and not the diagnostics),
// and the syntax isn't affected by the compilation settings.
var syntaxTree = TypeScript.Parser.parse(filename, text, getDefaultCompilerOptions().target, TypeScript.isDTSFile(filename));
return syntaxTree;
}
private updateSyntaxTree(filename: string, scriptSnapshot: TypeScript.IScriptSnapshot, previousSyntaxTree: TypeScript.SyntaxTree, previousFileVersion: number): TypeScript.SyntaxTree {
var editRange = this._hostCache.getScriptTextChangeRangeSinceVersion(filename, previousFileVersion);
// Debug.assert(newLength >= 0);
// The host considers the entire buffer changed. So parse a completely new tree.
if (editRange === null) {
return this.createSyntaxTree(filename, scriptSnapshot);
}
var nextSyntaxTree = IncrementalParser.parse(
previousSyntaxTree, editRange, SimpleText.fromScriptSnapshot(scriptSnapshot));
this.ensureInvariants(filename, editRange, nextSyntaxTree, this._currentFileScriptSnapshot, scriptSnapshot);
return nextSyntaxTree;
}
private ensureInvariants(filename: string, editRange: TypeScript.TextChangeRange, incrementalTree: TypeScript.SyntaxTree, oldScriptSnapshot: TypeScript.IScriptSnapshot, newScriptSnapshot: TypeScript.IScriptSnapshot) {
// First, verify that the edit range and the script snapshots make sense.
// If this fires, then the edit range is completely bogus. Somehow the lengths of the
// old snapshot, the change range and the new snapshot aren't in sync. This is very
// bad.
var expectedNewLength = oldScriptSnapshot.getLength() - editRange.span().length() + editRange.newLength();
var actualNewLength = newScriptSnapshot.getLength();
function provideMoreDebugInfo() {
var debugInformation = ["expected length:", expectedNewLength, "and actual length:", actualNewLength, "are not equal\r\n"];
var oldSpan = editRange.span();
function prettyPrintString(s: string): string {
return '"' + s.replace(/\r/g, '\\r').replace(/\n/g, '\\n') + '"';
}
debugInformation.push('Edit range (old text) (start: ' + oldSpan.start() + ', end: ' + oldSpan.end() + ') \r\n');
debugInformation.push('Old text edit range contents: ' + prettyPrintString(oldScriptSnapshot.getText(oldSpan.start(), oldSpan.end())));
var newSpan = editRange.newSpan();
debugInformation.push('Edit range (new text) (start: ' + newSpan.start() + ', end: ' + newSpan.end() + ') \r\n');
debugInformation.push('New text edit range contents: ' + prettyPrintString(newScriptSnapshot.getText(newSpan.start(), newSpan.end())));
return debugInformation.join(' ');
}
Debug.assert(
expectedNewLength === actualNewLength,
"Expected length is different from actual!",
provideMoreDebugInfo);
if (Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
// If this fires, the text change range is bogus. It says the change starts at point
// 'X', but we can see a text difference *before* that point.
var oldPrefixText = oldScriptSnapshot.getText(0, editRange.span().start());
var newPrefixText = newScriptSnapshot.getText(0, editRange.span().start());
Debug.assert(oldPrefixText === newPrefixText, 'Expected equal prefix texts!');
// If this fires, the text change range is bogus. It says the change goes only up to
// point 'X', but we can see a text difference *after* that point.
var oldSuffixText = oldScriptSnapshot.getText(editRange.span().end(), oldScriptSnapshot.getLength());
var newSuffixText = newScriptSnapshot.getText(editRange.newSpan().end(), newScriptSnapshot.getLength());
Debug.assert(oldSuffixText === newSuffixText, 'Expected equal suffix texts!');
// Ok, text change range and script snapshots look ok. Let's verify that our
// incremental parsing worked properly.
//var normalTree = this.createSyntaxTree(filename, newScriptSnapshot);
//Debug.assert(normalTree.structuralEquals(incrementalTree), 'Expected equal incremental and normal trees');
// Ok, the trees looked good. So at least our incremental parser agrees with the
// normal parser. Now, verify that the incremental tree matches the contents of the
// script snapshot.
var incrementalTreeText = fullText(incrementalTree.sourceUnit());
var actualSnapshotText = newScriptSnapshot.getText(0, newScriptSnapshot.getLength());
Debug.assert(incrementalTreeText === actualSnapshotText, 'Expected full texts to be equal');
}
}
}
class FormattingOptions {
constructor(public useTabs: boolean,
public spacesPerTab: number,
public indentSpaces: number,
public newLineCharacter: string) {
}
public static defaultOptions = new FormattingOptions(/*useTabs:*/ false, /*spacesPerTab:*/ 4, /*indentSpaces:*/ 4, /*newLineCharacter*/ "\r\n");
}
class DocumentRegistryEntry {
public refCount: number = 0;
public owners: string[] = [];
constructor(public document: Document) {
}
}
export interface IDocumentRegistry {
acquireDocument(
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
byteOrderMark: ByteOrderMark,
version: number,
isOpen: boolean,
referencedFiles: string[]): TypeScript.Document;
updateDocument(
document: Document,
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
version: number,
isOpen: boolean,
textChangeRange: TextChangeRange
): TypeScript.Document;
releaseDocument(filename: string, compilationSettings: ts.CompilerOptions): void
}
export class NonCachingDocumentRegistry implements IDocumentRegistry {
public static Instance: IDocumentRegistry = new NonCachingDocumentRegistry();
public acquireDocument(
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
byteOrderMark: ByteOrderMark,
version: number,
isOpen: boolean,
referencedFiles: string[]= []): TypeScript.Document {
return Document.create(compilationSettings, filename, scriptSnapshot, byteOrderMark, version, isOpen, referencedFiles);
}
public updateDocument(
document: Document,
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
version: number,
isOpen: boolean,
textChangeRange: TextChangeRange
): TypeScript.Document {
return document.update(scriptSnapshot, version, isOpen, textChangeRange);
}
public releaseDocument(filename: string, compilationSettings: ts.CompilerOptions): void {
// no op since this class doesn't cache anything
}
}
export class DocumentRegistry implements IDocumentRegistry {
private buckets: IIndexable<StringHashTable<DocumentRegistryEntry>> = {};
private getKeyFromCompilationSettings(settings: ts.CompilerOptions): string {
return "_" + ts.ScriptTarget[settings.target]; // + "|" + settings.propagateEnumConstants.toString()
}
private getBucketForCompilationSettings(settings: ts.CompilerOptions, createIfMissing: boolean): StringHashTable<DocumentRegistryEntry> {
var key = this.getKeyFromCompilationSettings(settings);
var bucket = this.buckets[key];
if (!bucket && createIfMissing) {
this.buckets[key] = bucket = new StringHashTable<DocumentRegistryEntry>();
}
return bucket;
}
public reportStats() {
var bucketInfoArray = Object.keys(this.buckets).filter(name => name && name.charAt(0) === '_').map(name => {
var entries = this.buckets[name];
var documents = entries.getAllKeys().map((name) => {
var entry = entries.lookup(name);
return {
name: name,
refCount: entry.refCount,
references: entry.owners.slice(0)
};
});
documents.sort((x, y) => y.refCount - x.refCount);
return { bucket: name, documents: documents }
});
return JSON.stringify(bucketInfoArray, null, 2);
}
public acquireDocument(
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
byteOrderMark: ByteOrderMark,
version: number,
isOpen: boolean,
referencedFiles: string[]= []): TypeScript.Document {
var bucket = this.getBucketForCompilationSettings(compilationSettings, /*createIfMissing*/ true);
var entry = bucket.lookup(filename);
if (!entry) {
var document = Document.create(compilationSettings, filename, scriptSnapshot, byteOrderMark, version, isOpen, referencedFiles);
entry = new DocumentRegistryEntry(document);
bucket.add(filename, entry);
}
entry.refCount++;
return entry.document;
}
public updateDocument(
document: Document,
filename: string,
compilationSettings: ts.CompilerOptions,
scriptSnapshot: IScriptSnapshot,
version: number,
isOpen: boolean,
textChangeRange: TextChangeRange
): TypeScript.Document {
var bucket = this.getBucketForCompilationSettings(compilationSettings, /*createIfMissing*/ false);
Debug.assert(bucket);
var entry = bucket.lookup(filename);
Debug.assert(entry);
if (entry.document.isOpen === isOpen && entry.document.version === version) {
return entry.document;
}
entry.document = entry.document.update(scriptSnapshot, version, isOpen, textChangeRange);
return entry.document;
}
public releaseDocument(filename: string, compilationSettings: ts.CompilerOptions): void {
var bucket = this.getBucketForCompilationSettings(compilationSettings, false);
Debug.assert(bucket);
var entry = bucket.lookup(filename);
entry.refCount--;
Debug.assert(entry.refCount >= 0);
if (entry.refCount === 0) {
bucket.remove(filename);
}
}
}
export class LanguageService implements ILanguageService {
private logger: TypeScript.ILogger;
private _syntaxTreeCache: SyntaxTreeCache;
private formattingRulesProvider: Formatting.RulesProvider;
// A cache of all the information about the files on the host side.
private hostCache: HostCache = null;
private program: ts.Program;
private typeChecker: ts.TypeChecker;
private useCaseSensitivefilenames = false;
private documentsByName: ts.Map<Document> = {};
private documentRegistry: IDocumentRegistry
private cancellationToken: CancellationToken;
constructor(public host: ILanguageServiceHost, documentRegistry: IDocumentRegistry) {
this.logger = this.host;
this.cancellationToken = new CancellationToken(this.host.getCancellationToken());
this.documentRegistry = documentRegistry;
this._syntaxTreeCache = new SyntaxTreeCache(this.host);
// Check if the localized messages json is set, otherwise query the host for it
if (!TypeScript.LocalizedDiagnosticMessages) {
TypeScript.LocalizedDiagnosticMessages = this.host.getLocalizedDiagnosticMessages();
}
}
private createCompilerHost(): ts.CompilerHost {
return {
getSourceFile: (filename, languageVersion) => {
var document = this.documentsByName[filename];
Debug.assert(!!document, "document can not be undefined");
return document.sourceFile();
},
getCancellationToken: () => this.cancellationToken,
getCanonicalFileName: (filename) => this.useCaseSensitivefilenames ? filename : filename.toLowerCase(),
useCaseSensitiveFileNames: () => this.useCaseSensitivefilenames,
// Need something that doesn't depend on sys.ts here
getDefaultLibFilename: (): string => {
throw Error("TOD:: getDefaultLibfilename");
},
writeFile: (filename, data) => {
throw Error("TODO: write file");
},
getCurrentDirectory: (): string => {
throw Error("TODO: getCurrentDirectory");
}
};
}
private synchronizeHostData(): void {
// Reset the cache at start of every refresh
this.hostCache = new HostCache(this.host);
var compilationSettings = this.hostCache.compilationSettings();
// TODO: check if we need to create a new compiler to start with
// 1. files are identical
// 2. compilation settings are identical
// Now, remove any files from the compiler that are no longer in the host.
var oldDocumentsByName = this.documentsByName;
this.documentsByName = {};
var oldProgram = this.program;
if (oldProgram) {
var oldSettings = this.program.getCompilerOptions();
var changesInCompilationSettingsAffectSyntax =
oldSettings && compilationSettings && !compareDataObjects(oldSettings, compilationSettings) && settingsChangeAffectsSyntax(oldSettings, compilationSettings);
var oldSourceFiles = this.program.getSourceFiles();
for (var i = 0, n = oldSourceFiles.length; i < n; i++) {
this.cancellationToken.throwIfCancellationRequested();
var filename = oldSourceFiles[i].filename;
if (!this.hostCache.contains(filename) || changesInCompilationSettingsAffectSyntax) {
this.documentRegistry.releaseDocument(filename, oldSettings);
}
}
}
// Now, for every file the host knows about, either add the file (if the compiler
// doesn't know about it.). Or notify the compiler about any changes (if it does
// know about it.)
var hostfilenames = this.hostCache.getfilenames();
for (var i = 0, n = hostfilenames.length; i < n; i++) {
var filename = hostfilenames[i];
var version = this.hostCache.getVersion(filename);
var isOpen = this.hostCache.isOpen(filename);
var scriptSnapshot = this.hostCache.getScriptSnapshot(filename);
var document: Document = oldDocumentsByName[filename];
if (document) {
//
// If the document is the same, assume no update
//
if (document.version === version && document.isOpen === isOpen) {
continue;
}
// Only perform incremental parsing on open files that are being edited. If a file was
// open, but is now closed, we want to reparse entirely so we don't have any tokens that
// are holding onto expensive script snapshot instances on the host. Similarly, if a
// file was closed, then we always want to reparse. This is so our tree doesn't keep
// the old buffer alive that represented the file on disk (as the host has moved to a
// new text buffer).
var textChangeRange: TextChangeRange = null;
if (document.isOpen && isOpen) {
textChangeRange = this.hostCache.getScriptTextChangeRangeSinceVersion(filename, document.version);
}
document = this.documentRegistry.updateDocument(document, filename, compilationSettings, scriptSnapshot, version, isOpen, textChangeRange);
}
else {
document = this.documentRegistry.acquireDocument(filename, compilationSettings, scriptSnapshot, this.hostCache.getByteOrderMark(filename), version, isOpen, []);
}
// Remeber the new document
this.documentsByName[filename] = document;
}
// Now create a new compiler
this.program = ts.createProgram(hostfilenames, compilationSettings, this.createCompilerHost());
this.typeChecker = this.program.getTypeChecker();
}
dispose(): void {
if (this.program) {
ts.forEach(this.program.getSourceFiles(),
(f) => this.documentRegistry.releaseDocument(f.filename, this.program.getCompilerOptions()));
}
}
refresh() {
// No-op. Only kept around for compatability with the interface we shipped.
}
cleanupSemanticCache() { }
getSyntacticDiagnostics(filename: string) {
this.synchronizeHostData();
return this.program.getDiagnostics(this.program.getSourceFile(filename));
}
getSemanticDiagnostics(filename: string) {
this.synchronizeHostData();
return this.typeChecker.getDiagnostics(this.program.getSourceFile(filename));
}
getCompilerOptionsDiagnostics() {
this.synchronizeHostData();
return this.program.getGlobalDiagnostics();
}
getCompletionsAtPosition(filename: string, position: number, isMemberCompletion: boolean) {
return undefined;
}
getCompletionEntryDetails(filename: string, position: number, entryName: string) {
return undefined;
}
getTypeAtPosition(filename: string, position: number) {
return undefined;
}
getSignatureAtPosition(filename: string, position: number) {
return undefined;
}
getDefinitionAtPosition(filename: string, position: number) {
return [];
}
getReferencesAtPosition(filename: string, position: number) {
return [];
}
getOccurrencesAtPosition(filename: string, position: number) {
return [];
}
getImplementorsAtPosition(filename: string, position: number) {
return [];
}
getNavigateToItems(searchValue: string) {
return [];
}
getEmitOutput(filename: string) {
return undefined;
}
private getTypeInfoEligiblePath(filename: string, position: number, isConstructorValidPosition: boolean) {
var sourceUnit = this._syntaxTreeCache.getCurrentFileSyntaxTree(filename).sourceUnit();
var ast = TypeScript.ASTHelpers.getAstAtPosition(sourceUnit, position, /*useTrailingTriviaAsLimChar*/ false, /*forceInclusive*/ true);
if (ast === null) {
return null;
}
if (ast.kind() === SyntaxKind.ParameterList && ast.parent.kind() === SyntaxKind.CallSignature && ast.parent.parent.kind() === SyntaxKind.ConstructorDeclaration) {
ast = ast.parent.parent;
}
switch (ast.kind()) {
default:
return null;
case TypeScript.SyntaxKind.ConstructorDeclaration:
var constructorAST = <TypeScript.ConstructorDeclarationSyntax>ast;
if (!isConstructorValidPosition || !(position >= start(constructorAST) && position <= start(constructorAST) + "constructor".length)) {
return null;
}
else {
return ast;
}
case TypeScript.SyntaxKind.FunctionDeclaration:
return null;
case TypeScript.SyntaxKind.MemberAccessExpression:
case TypeScript.SyntaxKind.QualifiedName:
case TypeScript.SyntaxKind.SuperKeyword:
case TypeScript.SyntaxKind.StringLiteral:
case TypeScript.SyntaxKind.ThisKeyword:
case TypeScript.SyntaxKind.IdentifierName:
return ast;
}
}
getNameOrDottedNameSpan(filename: string, startPos: number, endPos: number) {
filename = TypeScript.switchToForwardSlashes(filename);
var node = this.getTypeInfoEligiblePath(filename, startPos, false);
if (!node) return null;
while (node) {
if (TypeScript.ASTHelpers.isNameOfMemberAccessExpression(node) ||
TypeScript.ASTHelpers.isRightSideOfQualifiedName(node)) {
node = node.parent;
}
else {
break;
}
}
var spanInfo = new SpanInfo(start(node), end(node));
return spanInfo;
}
getBreakpointStatementAtPosition(filename: string, position: number) {
// doesn't use compiler - no need to synchronize with host
filename = TypeScript.switchToForwardSlashes(filename);
var syntaxtree = this.getSyntaxTree(filename);
return TypeScript.Services.Breakpoints.getBreakpointLocation(syntaxtree, position);
}
getScriptLexicalStructure(filename: string) {
filename = TypeScript.switchToForwardSlashes(filename);
var syntaxTree = this.getSyntaxTree(filename);
var items: NavigateToItem[] = [];
GetScriptLexicalStructureWalker.getListsOfAllScriptLexicalStructure(items, filename, syntaxTree.sourceUnit());
return items;
}
getOutliningRegions(filename: string) {
// doesn't use compiler - no need to synchronize with host
filename = TypeScript.switchToForwardSlashes(filename);
var syntaxTree = this.getSyntaxTree(filename);
return OutliningElementsCollector.collectElements(syntaxTree.sourceUnit());
}
getBraceMatchingAtPosition(filename: string, position: number) {
filename = TypeScript.switchToForwardSlashes(filename);
var syntaxTree = this.getSyntaxTree(filename);
return BraceMatcher.getMatchSpans(syntaxTree, position);
}
getIndentationAtPosition(filename: string, position: number, editorOptions: EditorOptions) {
filename = TypeScript.switchToForwardSlashes(filename);
var syntaxTree = this.getSyntaxTree(filename);
var scriptSnapshot = this._syntaxTreeCache.getCurrentScriptSnapshot(filename);
var scriptText = TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot);
var textSnapshot = new TypeScript.Services.Formatting.TextSnapshot(scriptText);
var options = new FormattingOptions(!editorOptions.ConvertTabsToSpaces, editorOptions.TabSize, editorOptions.IndentSize, editorOptions.NewLineCharacter)
return TypeScript.Services.Formatting.SingleTokenIndenter.getIndentationAmount(position, syntaxTree.sourceUnit(), textSnapshot, options);
}
getFormattingEditsForRange(filename: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[] {
filename = TypeScript.switchToForwardSlashes(filename);
var manager = this.getFormattingManager(filename, options);
return manager.formatSelection(minChar, limChar);
}
getFormattingEditsForDocument(filename: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[] {
filename = TypeScript.switchToForwardSlashes(filename);
var manager = this.getFormattingManager(filename, options);
return manager.formatDocument(minChar, limChar);
}
getFormattingEditsOnPaste(filename: string, minChar: number, limChar: number, options: FormatCodeOptions): TextEdit[] {
filename = TypeScript.switchToForwardSlashes(filename);
var manager = this.getFormattingManager(filename, options);
return manager.formatOnPaste(minChar, limChar);
}
getFormattingEditsAfterKeystroke(filename: string, position: number, key: string, options: FormatCodeOptions): TextEdit[] {
filename = TypeScript.switchToForwardSlashes(filename);
var manager = this.getFormattingManager(filename, options);
if (key === "}") return manager.formatOnClosingCurlyBrace(position);
else if (key === ";") return manager.formatOnSemicolon(position);
else if (key === "\n") return manager.formatOnEnter(position);
else return [];
}
private getFormattingManager(filename: string, options: FormatCodeOptions) {
// Ensure rules are initialized and up to date wrt to formatting options
if (this.formattingRulesProvider == null) {
this.formattingRulesProvider = new TypeScript.Services.Formatting.RulesProvider(this.logger);
}
this.formattingRulesProvider.ensureUpToDate(options);
// Get the Syntax Tree
var syntaxTree = this.getSyntaxTree(filename);
// Convert IScriptSnapshot to ITextSnapshot
var scriptSnapshot = this._syntaxTreeCache.getCurrentScriptSnapshot(filename);
var scriptText = TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot);
var textSnapshot = new TypeScript.Services.Formatting.TextSnapshot(scriptText);
var manager = new TypeScript.Services.Formatting.FormattingManager(syntaxTree, textSnapshot, this.formattingRulesProvider, options);
return manager;
}
getSyntaxTree(filename: string): TypeScript.SyntaxTree {
filename = TypeScript.switchToForwardSlashes(filename);
return this._syntaxTreeCache.getCurrentFileSyntaxTree(filename);
}
}
}

View File

@@ -0,0 +1,26 @@
/////<reference path='es5compat.ts' />
/////<reference path='..\compiler\typescript.ts' />
//// document.ts depends on incrementalParser.ts being run first.
/////<reference path='..\compiler\syntax\incrementalParser.ts' />
/////<reference path='document.ts' />
/////<reference path='syntaxUtilities.generated.ts' />
/////<reference path='coreServices.ts' />
/////<reference path='classifier.ts' />
/////<reference path='compilerState.ts' />
/////<reference path='indentation.ts' />
/////<reference path='languageService.ts' />
/////<reference path='completionHelpers.ts' />
/////<reference path='keywordCompletions.ts' />
/////<reference path='signatureInfoHelpers.ts' />
/////<reference path='completionSession.ts' />
/////<reference path='pullLanguageService.ts' />
/////<reference path='findReferenceHelpers.ts' />
/////<reference path='shims.ts' />
/////<reference path='formatting\formatting.ts' />
/////<reference path='outliningElementsCollector.ts' />
/////<reference path='braceMatcher.ts' />
/////<reference path='indenter.ts' />
/////<reference path='breakpoints.ts' />
/////<reference path='getScriptLexicalStructureWalker.ts' />

View File

@@ -0,0 +1,445 @@
// <auto-generated />
module TypeScript {
export var DiagnosticCode = {
error_TS_0_1: "error TS{0}: {1}",
warning_TS_0_1: "warning TS{0}: {1}",
Unrecognized_escape_sequence: "Unrecognized escape sequence.",
Unexpected_character_0: "Unexpected character {0}.",
Missing_close_quote_character: "Missing close quote character.",
Identifier_expected: "Identifier expected.",
_0_keyword_expected: "'{0}' keyword expected.",
_0_expected: "'{0}' expected.",
Identifier_expected_0_is_a_keyword: "Identifier expected; '{0}' is a keyword.",
Automatic_semicolon_insertion_not_allowed: "Automatic semicolon insertion not allowed.",
Unexpected_token_0_expected: "Unexpected token; '{0}' expected.",
Trailing_comma_not_allowed: "Trailing comma not allowed.",
AsteriskSlash_expected: "'*/' expected.",
public_or_private_modifier_must_precede_static: "'public' or 'private' modifier must precede 'static'.",
Unexpected_token: "Unexpected token.",
Catch_clause_parameter_cannot_have_a_type_annotation: "Catch clause parameter cannot have a type annotation.",
A_rest_parameter_must_be_last_in_a_parameter_list: "A rest parameter must be last in a parameter list.",
Parameter_cannot_have_question_mark_and_initializer: "Parameter cannot have question mark and initializer.",
A_required_parameter_cannot_follow_an_optional_parameter: "A required parameter cannot follow an optional parameter.",
Index_signatures_cannot_have_rest_parameters: "Index signatures cannot have rest parameters.",
Index_signature_parameter_cannot_have_accessibility_modifiers: "Index signature parameter cannot have accessibility modifiers.",
Index_signature_parameter_cannot_have_a_question_mark: "Index signature parameter cannot have a question mark.",
Index_signature_parameter_cannot_have_an_initializer: "Index signature parameter cannot have an initializer.",
Index_signature_must_have_a_type_annotation: "Index signature must have a type annotation.",
Index_signature_parameter_must_have_a_type_annotation: "Index signature parameter must have a type annotation.",
Index_signature_parameter_type_must_be_string_or_number: "Index signature parameter type must be 'string' or 'number'.",
extends_clause_already_seen: "'extends' clause already seen.",
extends_clause_must_precede_implements_clause: "'extends' clause must precede 'implements' clause.",
Classes_can_only_extend_a_single_class: "Classes can only extend a single class.",
implements_clause_already_seen: "'implements' clause already seen.",
Accessibility_modifier_already_seen: "Accessibility modifier already seen.",
_0_modifier_must_precede_1_modifier: "'{0}' modifier must precede '{1}' modifier.",
_0_modifier_already_seen: "'{0}' modifier already seen.",
_0_modifier_cannot_appear_on_a_class_element: "'{0}' modifier cannot appear on a class element.",
Interface_declaration_cannot_have_implements_clause: "Interface declaration cannot have 'implements' clause.",
super_invocation_cannot_have_type_arguments: "'super' invocation cannot have type arguments.",
Only_ambient_modules_can_use_quoted_names: "Only ambient modules can use quoted names.",
Statements_are_not_allowed_in_ambient_contexts: "Statements are not allowed in ambient contexts.",
A_function_implementation_cannot_be_declared_in_an_ambient_context: "A function implementation cannot be declared in an ambient context.",
A_declare_modifier_cannot_be_used_in_an_already_ambient_context: "A 'declare' modifier cannot be used in an already ambient context.",
Initializers_are_not_allowed_in_ambient_contexts: "Initializers are not allowed in ambient contexts.",
_0_modifier_cannot_appear_on_a_module_element: "'{0}' modifier cannot appear on a module element.",
A_declare_modifier_cannot_be_used_with_an_interface_declaration: "A 'declare' modifier cannot be used with an interface declaration.",
A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file: "A 'declare' modifier is required for a top level declaration in a .d.ts file.",
A_rest_parameter_cannot_be_optional: "A rest parameter cannot be optional.",
A_rest_parameter_cannot_have_an_initializer: "A rest parameter cannot have an initializer.",
set_accessor_must_have_exactly_one_parameter: "'set' accessor must have exactly one parameter.",
set_accessor_parameter_cannot_be_optional: "'set' accessor parameter cannot be optional.",
set_accessor_parameter_cannot_have_an_initializer: "'set' accessor parameter cannot have an initializer.",
set_accessor_cannot_have_rest_parameter: "'set' accessor cannot have rest parameter.",
get_accessor_cannot_have_parameters: "'get' accessor cannot have parameters.",
Modifiers_cannot_appear_here: "Modifiers cannot appear here.",
Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher: "Accessors are only available when targeting ECMAScript 5 and higher.",
Enum_member_must_have_initializer: "Enum member must have initializer.",
Export_assignment_cannot_be_used_in_internal_modules: "Export assignment cannot be used in internal modules.",
Ambient_enum_elements_can_only_have_integer_literal_initializers: "Ambient enum elements can only have integer literal initializers.",
module_class_interface_enum_import_or_statement: "module, class, interface, enum, import or statement",
constructor_function_accessor_or_variable: "constructor, function, accessor or variable",
statement: "statement",
case_or_default_clause: "case or default clause",
identifier: "identifier",
call_construct_index_property_or_function_signature: "call, construct, index, property or function signature",
expression: "expression",
type_name: "type name",
property_or_accessor: "property or accessor",
parameter: "parameter",
type: "type",
type_parameter: "type parameter",
A_declare_modifier_cannot_be_used_with_an_import_declaration: "A 'declare' modifier cannot be used with an import declaration.",
Invalid_reference_directive_syntax: "Invalid 'reference' directive syntax.",
Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher: "Octal literals are not available when targeting ECMAScript 5 and higher.",
Accessors_are_not_allowed_in_ambient_contexts: "Accessors are not allowed in ambient contexts.",
_0_modifier_cannot_appear_on_a_constructor_declaration: "'{0}' modifier cannot appear on a constructor declaration.",
_0_modifier_cannot_appear_on_a_parameter: "'{0}' modifier cannot appear on a parameter.",
Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement: "Only a single variable declaration is allowed in a 'for...in' statement.",
Type_parameters_cannot_appear_on_a_constructor_declaration: "Type parameters cannot appear on a constructor declaration.",
Type_annotation_cannot_appear_on_a_constructor_declaration: "Type annotation cannot appear on a constructor declaration.",
Type_parameters_cannot_appear_on_an_accessor: "Type parameters cannot appear on an accessor.",
Type_annotation_cannot_appear_on_a_set_accessor: "Type annotation cannot appear on a 'set' accessor.",
Index_signature_must_have_exactly_one_parameter: "Index signature must have exactly one parameter.",
_0_list_cannot_be_empty: "'{0}' list cannot be empty.",
variable_declaration: "variable declaration",
type_argument: "type argument",
Invalid_use_of_0_in_strict_mode: "Invalid use of '{0}' in strict mode.",
with_statements_are_not_allowed_in_strict_mode: "'with' statements are not allowed in strict mode.",
delete_cannot_be_called_on_an_identifier_in_strict_mode: "'delete' cannot be called on an identifier in strict mode.",
Invalid_left_hand_side_in_for_in_statement: "Invalid left-hand side in 'for...in' statement.",
continue_statement_can_only_be_used_within_an_enclosing_iteration_statement: "'continue' statement can only be used within an enclosing iteration statement.",
break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement: "'break' statement can only be used within an enclosing iteration or switch statement.",
Jump_target_not_found: "Jump target not found.",
Jump_target_cannot_cross_function_boundary: "Jump target cannot cross function boundary.",
return_statement_must_be_contained_within_a_function_body: "'return' statement must be contained within a function body.",
Expression_expected: "Expression expected.",
Type_expected: "Type expected.",
Duplicate_identifier_0: "Duplicate identifier '{0}'.",
The_name_0_does_not_exist_in_the_current_scope: "The name '{0}' does not exist in the current scope.",
The_name_0_does_not_refer_to_a_value: "The name '{0}' does not refer to a value.",
super_can_only_be_used_inside_a_class_instance_method: "'super' can only be used inside a class instance method.",
The_left_hand_side_of_an_assignment_expression_must_be_a_variable_property_or_indexer: "The left-hand side of an assignment expression must be a variable, property or indexer.",
Value_of_type_0_is_not_callable_Did_you_mean_to_include_new: "Value of type '{0}' is not callable. Did you mean to include 'new'?",
Value_of_type_0_is_not_callable: "Value of type '{0}' is not callable.",
Value_of_type_0_is_not_newable: "Value of type '{0}' is not newable.",
An_index_expression_argument_must_be_string_number_or_any: "An index expression argument must be 'string', 'number', or 'any'.",
Operator_0_cannot_be_applied_to_types_1_and_2: "Operator '{0}' cannot be applied to types '{1}' and '{2}'.",
Type_0_is_not_assignable_to_type_1: "Type '{0}' is not assignable to type '{1}'.",
Type_0_is_not_assignable_to_type_1_NL_2: "Type '{0}' is not assignable to type '{1}':{NL}{2}",
Expected_var_class_interface_or_module: "Expected var, class, interface, or module.",
Getter_0_already_declared: "Getter '{0}' already declared.",
Setter_0_already_declared: "Setter '{0}' already declared.",
Exported_class_0_extends_private_class_1: "Exported class '{0}' extends private class '{1}'.",
Exported_class_0_implements_private_interface_1: "Exported class '{0}' implements private interface '{1}'.",
Exported_interface_0_extends_private_interface_1: "Exported interface '{0}' extends private interface '{1}'.",
Exported_class_0_extends_class_from_inaccessible_module_1: "Exported class '{0}' extends class from inaccessible module {1}.",
Exported_class_0_implements_interface_from_inaccessible_module_1: "Exported class '{0}' implements interface from inaccessible module {1}.",
Exported_interface_0_extends_interface_from_inaccessible_module_1: "Exported interface '{0}' extends interface from inaccessible module {1}.",
Public_static_property_0_of_exported_class_has_or_is_using_private_type_1: "Public static property '{0}' of exported class has or is using private type '{1}'.",
Public_property_0_of_exported_class_has_or_is_using_private_type_1: "Public property '{0}' of exported class has or is using private type '{1}'.",
Property_0_of_exported_interface_has_or_is_using_private_type_1: "Property '{0}' of exported interface has or is using private type '{1}'.",
Exported_variable_0_has_or_is_using_private_type_1: "Exported variable '{0}' has or is using private type '{1}'.",
Public_static_property_0_of_exported_class_is_using_inaccessible_module_1: "Public static property '{0}' of exported class is using inaccessible module {1}.",
Public_property_0_of_exported_class_is_using_inaccessible_module_1: "Public property '{0}' of exported class is using inaccessible module {1}.",
Property_0_of_exported_interface_is_using_inaccessible_module_1: "Property '{0}' of exported interface is using inaccessible module {1}.",
Exported_variable_0_is_using_inaccessible_module_1: "Exported variable '{0}' is using inaccessible module {1}.",
Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_type_1: "Parameter '{0}' of constructor from exported class has or is using private type '{1}'.",
Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_private_type_1: "Parameter '{0}' of public static property setter from exported class has or is using private type '{1}'.",
Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_private_type_1: "Parameter '{0}' of public property setter from exported class has or is using private type '{1}'.",
Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_type_1: "Parameter '{0}' of constructor signature from exported interface has or is using private type '{1}'.",
Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_type_1: "Parameter '{0}' of call signature from exported interface has or is using private type '{1}'.",
Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_type_1: "Parameter '{0}' of public static method from exported class has or is using private type '{1}'.",
Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_type_1: "Parameter '{0}' of public method from exported class has or is using private type '{1}'.",
Parameter_0_of_method_from_exported_interface_has_or_is_using_private_type_1: "Parameter '{0}' of method from exported interface has or is using private type '{1}'.",
Parameter_0_of_exported_function_has_or_is_using_private_type_1: "Parameter '{0}' of exported function has or is using private type '{1}'.",
Parameter_0_of_constructor_from_exported_class_is_using_inaccessible_module_1: "Parameter '{0}' of constructor from exported class is using inaccessible module {1}.",
Parameter_0_of_public_static_property_setter_from_exported_class_is_using_inaccessible_module_1: "Parameter '{0}' of public static property setter from exported class is using inaccessible module {1}.",
Parameter_0_of_public_property_setter_from_exported_class_is_using_inaccessible_module_1: "Parameter '{0}' of public property setter from exported class is using inaccessible module {1}.",
Parameter_0_of_constructor_signature_from_exported_interface_is_using_inaccessible_module_1: "Parameter '{0}' of constructor signature from exported interface is using inaccessible module {1}.",
Parameter_0_of_call_signature_from_exported_interface_is_using_inaccessible_module_1: "Parameter '{0}' of call signature from exported interface is using inaccessible module {1}",
Parameter_0_of_public_static_method_from_exported_class_is_using_inaccessible_module_1: "Parameter '{0}' of public static method from exported class is using inaccessible module {1}.",
Parameter_0_of_public_method_from_exported_class_is_using_inaccessible_module_1: "Parameter '{0}' of public method from exported class is using inaccessible module {1}.",
Parameter_0_of_method_from_exported_interface_is_using_inaccessible_module_1: "Parameter '{0}' of method from exported interface is using inaccessible module {1}.",
Parameter_0_of_exported_function_is_using_inaccessible_module_1: "Parameter '{0}' of exported function is using inaccessible module {1}.",
Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_private_type_0: "Return type of public static property getter from exported class has or is using private type '{0}'.",
Return_type_of_public_property_getter_from_exported_class_has_or_is_using_private_type_0: "Return type of public property getter from exported class has or is using private type '{0}'.",
Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_type_0: "Return type of constructor signature from exported interface has or is using private type '{0}'.",
Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_type_0: "Return type of call signature from exported interface has or is using private type '{0}'.",
Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_type_0: "Return type of index signature from exported interface has or is using private type '{0}'.",
Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_type_0: "Return type of public static method from exported class has or is using private type '{0}'.",
Return_type_of_public_method_from_exported_class_has_or_is_using_private_type_0: "Return type of public method from exported class has or is using private type '{0}'.",
Return_type_of_method_from_exported_interface_has_or_is_using_private_type_0: "Return type of method from exported interface has or is using private type '{0}'.",
Return_type_of_exported_function_has_or_is_using_private_type_0: "Return type of exported function has or is using private type '{0}'.",
Return_type_of_public_static_property_getter_from_exported_class_is_using_inaccessible_module_0: "Return type of public static property getter from exported class is using inaccessible module {0}.",
Return_type_of_public_property_getter_from_exported_class_is_using_inaccessible_module_0: "Return type of public property getter from exported class is using inaccessible module {0}.",
Return_type_of_constructor_signature_from_exported_interface_is_using_inaccessible_module_0: "Return type of constructor signature from exported interface is using inaccessible module {0}.",
Return_type_of_call_signature_from_exported_interface_is_using_inaccessible_module_0: "Return type of call signature from exported interface is using inaccessible module {0}.",
Return_type_of_index_signature_from_exported_interface_is_using_inaccessible_module_0: "Return type of index signature from exported interface is using inaccessible module {0}.",
Return_type_of_public_static_method_from_exported_class_is_using_inaccessible_module_0: "Return type of public static method from exported class is using inaccessible module {0}.",
Return_type_of_public_method_from_exported_class_is_using_inaccessible_module_0: "Return type of public method from exported class is using inaccessible module {0}.",
Return_type_of_method_from_exported_interface_is_using_inaccessible_module_0: "Return type of method from exported interface is using inaccessible module {0}.",
Return_type_of_exported_function_is_using_inaccessible_module_0: "Return type of exported function is using inaccessible module {0}.",
new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead: "'new T[]' cannot be used to create an array. Use 'new Array<T>()' instead.",
A_parameter_list_must_follow_a_generic_type_argument_list_expected: "A parameter list must follow a generic type argument list. '(' expected.",
Multiple_constructor_implementations_are_not_allowed: "Multiple constructor implementations are not allowed.",
Cannot_find_external_module_0: "Cannot find external module '{0}'.",
Module_cannot_be_aliased_to_a_non_module_type: "Module cannot be aliased to a non-module type.",
A_class_may_only_extend_another_class: "A class may only extend another class.",
A_class_may_only_implement_another_class_or_interface: "A class may only implement another class or interface.",
An_interface_may_only_extend_a_class_or_another_interface: "An interface may only extend a class or another interface.",
Unable_to_resolve_type: "Unable to resolve type.",
Unable_to_resolve_type_of_0: "Unable to resolve type of '{0}'.",
Unable_to_resolve_type_parameter_constraint: "Unable to resolve type parameter constraint.",
Type_parameter_constraint_cannot_be_a_primitive_type: "Type parameter constraint cannot be a primitive type.",
Supplied_parameters_do_not_match_any_signature_of_call_target: "Supplied parameters do not match any signature of call target.",
Supplied_parameters_do_not_match_any_signature_of_call_target_NL_0: "Supplied parameters do not match any signature of call target:{NL}{0}",
Cannot_use_new_with_an_expression_whose_type_lacks_a_signature: "Cannot use 'new' with an expression whose type lacks a signature.",
Only_a_void_function_can_be_called_with_the_new_keyword: "Only a void function can be called with the 'new' keyword.",
Could_not_select_overload_for_new_expression: "Could not select overload for 'new' expression.",
Type_0_does_not_satisfy_the_constraint_1: "Type '{0}' does not satisfy the constraint '{1}'.",
Could_not_select_overload_for_call_expression: "Could not select overload for 'call' expression.",
Cannot_invoke_an_expression_whose_type_lacks_a_call_signature: "Cannot invoke an expression whose type lacks a call signature.",
Calls_to_super_are_only_valid_inside_a_class: "Calls to 'super' are only valid inside a class.",
Generic_type_0_requires_1_type_argument_s: "Generic type '{0}' requires {1} type argument(s).",
Type_of_array_literal_cannot_be_determined_Best_common_type_could_not_be_found_for_array_elements: "Type of array literal cannot be determined. Best common type could not be found for array elements.",
Could_not_find_enclosing_symbol_for_dotted_name_0: "Could not find enclosing symbol for dotted name '{0}'.",
Property_0_does_not_exist_on_value_of_type_1: "Property '{0}' does not exist on value of type '{1}'.",
Cannot_find_name_0: "Cannot find name '{0}'.",
get_and_set_accessor_must_have_the_same_type: "'get' and 'set' accessor must have the same type.",
this_cannot_be_referenced_in_current_location: "'this' cannot be referenced in current location.",
Static_members_cannot_reference_class_type_parameters: "Static members cannot reference class type parameters.",
Type_0_recursively_references_itself_as_a_base_type: "Type '{0}' recursively references itself as a base type.",
super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class: "'super' property access is permitted only in a constructor, member function, or member accessor of a derived class.",
super_can_only_be_referenced_in_a_derived_class: "'super' can only be referenced in a derived class.",
A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties: "A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.",
Constructors_for_derived_classes_must_contain_a_super_call: "Constructors for derived classes must contain a 'super' call.",
Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors: "Super calls are not permitted outside constructors or in nested functions inside constructors.",
_0_1_is_inaccessible: "'{0}.{1}' is inaccessible.",
this_cannot_be_referenced_in_a_module_body: "'this' cannot be referenced in a module body.",
Invalid_expression_types_not_known_to_support_the_addition_operator: "Invalid '+' expression - types not known to support the addition operator.",
The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type: "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.",
The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type: "The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.",
An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type: "An arithmetic operand must be of type 'any', 'number' or an enum type.",
Variable_declarations_of_a_for_statement_cannot_use_a_type_annotation: "Variable declarations of a 'for' statement cannot use a type annotation.",
Variable_declarations_of_a_for_statement_must_be_of_types_string_or_any: "Variable declarations of a 'for' statement must be of types 'string' or 'any'.",
The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.",
The_left_hand_side_of_an_in_expression_must_be_of_types_any_string_or_number: "The left-hand side of an 'in' expression must be of types 'any', 'string' or 'number'.",
The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter: "The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.",
The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter: "The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.",
The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type: "The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.",
Setters_cannot_return_a_value: "Setters cannot return a value.",
Tried_to_query_type_of_uninitialized_module_0: "Tried to query type of uninitialized module '{0}'.",
Tried_to_set_variable_type_to_uninitialized_module_type_0: "Tried to set variable type to uninitialized module type '{0}'.",
Type_0_is_not_generic: "Type '{0}' is not generic.",
Getters_must_return_a_value: "Getters must return a value.",
Getter_and_setter_accessors_do_not_agree_in_visibility: "Getter and setter accessors do not agree in visibility.",
Invalid_left_hand_side_of_assignment_expression: "Invalid left-hand side of assignment expression.",
Function_declared_a_non_void_return_type_but_has_no_return_expression: "Function declared a non-void return type, but has no return expression.",
Cannot_resolve_return_type_reference: "Cannot resolve return type reference.",
Constructors_cannot_have_a_return_type_of_void: "Constructors cannot have a return type of 'void'.",
Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2: "Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'.",
All_symbols_within_a_with_block_will_be_resolved_to_any: "All symbols within a with block will be resolved to 'any'.",
Import_declarations_in_an_internal_module_cannot_reference_an_external_module: "Import declarations in an internal module cannot reference an external module.",
Class_0_declares_interface_1_but_does_not_implement_it_NL_2: "Class {0} declares interface {1} but does not implement it:{NL}{2}",
Class_0_declares_class_1_as_an_interface_but_does_not_implement_it_NL_2: "Class {0} declares class {1} as an interface but does not implement it:{NL}{2}",
The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer: "The operand of an increment or decrement operator must be a variable, property or indexer.",
this_cannot_be_referenced_in_a_static_property_initializer: "'this' cannot be referenced in a static property initializer.",
Class_0_cannot_extend_class_1_NL_2: "Class '{0}' cannot extend class '{1}':{NL}{2}",
Interface_0_cannot_extend_class_1_NL_2: "Interface '{0}' cannot extend class '{1}':{NL}{2}",
Interface_0_cannot_extend_interface_1_NL_2: "Interface '{0}' cannot extend interface '{1}':{NL}{2}",
Overload_signature_is_not_compatible_with_function_definition: "Overload signature is not compatible with function definition.",
Overload_signature_is_not_compatible_with_function_definition_NL_0: "Overload signature is not compatible with function definition:{NL}{0}",
Overload_signatures_must_all_be_public_or_private: "Overload signatures must all be public or private.",
Overload_signatures_must_all_be_exported_or_not_exported: "Overload signatures must all be exported or not exported.",
Overload_signatures_must_all_be_ambient_or_non_ambient: "Overload signatures must all be ambient or non-ambient.",
Overload_signatures_must_all_be_optional_or_required: "Overload signatures must all be optional or required.",
Specialized_overload_signature_is_not_assignable_to_any_non_specialized_signature: "Specialized overload signature is not assignable to any non-specialized signature.",
this_cannot_be_referenced_in_constructor_arguments: "'this' cannot be referenced in constructor arguments.",
Instance_member_cannot_be_accessed_off_a_class: "Instance member cannot be accessed off a class.",
Untyped_function_calls_may_not_accept_type_arguments: "Untyped function calls may not accept type arguments.",
Non_generic_functions_may_not_accept_type_arguments: "Non-generic functions may not accept type arguments.",
A_generic_type_may_not_reference_itself_with_a_wrapped_form_of_its_own_type_parameters: "A generic type may not reference itself with a wrapped form of its own type parameters.",
A_rest_parameter_must_be_of_an_array_type: "A rest parameter must be of an array type.",
Overload_signature_implementation_cannot_use_specialized_type: "Overload signature implementation cannot use specialized type.",
Export_assignments_may_only_be_used_at_the_top_level_of_external_modules: "Export assignments may only be used at the top-level of external modules.",
Export_assignments_may_only_be_made_with_variables_functions_classes_interfaces_enums_and_internal_modules: "Export assignments may only be made with variables, functions, classes, interfaces, enums and internal modules.",
Only_public_methods_of_the_base_class_are_accessible_via_the_super_keyword: "Only public methods of the base class are accessible via the 'super' keyword.",
Numeric_indexer_type_0_must_be_assignable_to_string_indexer_type_1: "Numeric indexer type '{0}' must be assignable to string indexer type '{1}'.",
Numeric_indexer_type_0_must_be_assignable_to_string_indexer_type_1_NL_2: "Numeric indexer type '{0}' must be assignable to string indexer type '{1}':{NL}{2}",
All_numerically_named_properties_must_be_assignable_to_numeric_indexer_type_0: "All numerically named properties must be assignable to numeric indexer type '{0}'.",
All_numerically_named_properties_must_be_assignable_to_numeric_indexer_type_0_NL_1: "All numerically named properties must be assignable to numeric indexer type '{0}':{NL}{1}",
All_named_properties_must_be_assignable_to_string_indexer_type_0: "All named properties must be assignable to string indexer type '{0}'.",
All_named_properties_must_be_assignable_to_string_indexer_type_0_NL_1: "All named properties must be assignable to string indexer type '{0}':{NL}{1}",
A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation: "A parameter initializer is only allowed in a function or constructor implementation.",
Function_expression_declared_a_non_void_return_type_but_has_no_return_expression: "Function expression declared a non-void return type, but has no return expression.",
Import_declaration_referencing_identifier_from_internal_module_can_only_be_made_with_variables_functions_classes_interfaces_enums_and_internal_modules: "Import declaration referencing identifier from internal module can only be made with variables, functions, classes, interfaces, enums and internal modules.",
Module_0_has_no_exported_member_1: "Module '{0}' has no exported member '{1}'.",
Unable_to_resolve_module_reference_0: "Unable to resolve module reference '{0}'.",
Could_not_find_module_0_in_module_1: "Could not find module '{0}' in module '{1}'.",
Exported_import_declaration_0_is_assigned_value_with_type_that_has_or_is_using_private_type_1: "Exported import declaration '{0}' is assigned value with type that has or is using private type '{1}'.",
Exported_import_declaration_0_is_assigned_value_with_type_that_is_using_inaccessible_module_1: "Exported import declaration '{0}' is assigned value with type that is using inaccessible module '{1}'.",
Exported_import_declaration_0_is_assigned_type_that_has_or_is_using_private_type_1: "Exported import declaration '{0}' is assigned type that has or is using private type '{1}'.",
Exported_import_declaration_0_is_assigned_type_that_is_using_inaccessible_module_1: "Exported import declaration '{0}' is assigned type that is using inaccessible module '{1}'.",
Exported_import_declaration_0_is_assigned_container_that_is_or_is_using_inaccessible_module_1: "Exported import declaration '{0}' is assigned container that is or is using inaccessible module '{1}'.",
Type_name_0_in_extends_clause_does_not_reference_constructor_function_for_1: "Type name '{0}' in extends clause does not reference constructor function for '{1}'.",
Internal_module_reference_0_in_import_declaration_does_not_reference_module_instance_for_1: "Internal module reference '{0}' in import declaration does not reference module instance for '{1}'.",
Module_0_cannot_merge_with_previous_declaration_of_1_in_a_different_file_2: "Module '{0}' cannot merge with previous declaration of '{1}' in a different file '{2}'.",
Interface_0_cannot_simultaneously_extend_types_1_and_2_NL_3: "Interface '{0}' cannot simultaneously extend types '{1}' and '{2}':{NL}{3}",
Initializer_of_parameter_0_cannot_reference_identifier_1_declared_after_it: "Initializer of parameter '{0}' cannot reference identifier '{1}' declared after it.",
Ambient_external_module_declaration_cannot_be_reopened: "Ambient external module declaration cannot be reopened.",
All_declarations_of_merged_declaration_0_must_be_exported_or_not_exported: "All declarations of merged declaration '{0}' must be exported or not exported.",
super_cannot_be_referenced_in_constructor_arguments: "'super' cannot be referenced in constructor arguments.",
Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: "Return type of constructor signature must be assignable to the instance type of the class.",
Ambient_external_module_declaration_must_be_defined_in_global_context: "Ambient external module declaration must be defined in global context.",
Ambient_external_module_declaration_cannot_specify_relative_module_name: "Ambient external module declaration cannot specify relative module name.",
Import_declaration_in_an_ambient_external_module_declaration_cannot_reference_external_module_through_relative_external_module_name: "Import declaration in an ambient external module declaration cannot reference external module through relative external module name.",
No_best_common_type_exists_among_return_expressions: "No best common type exists among return expressions.",
Import_declaration_cannot_refer_to_external_module_reference_when_noResolve_option_is_set: "Import declaration cannot refer to external module reference when --noResolve option is set.",
Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference: "Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.",
Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference: "Duplicate identifier '_super'. Compiler uses '_super' to capture base class reference.",
Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference: "Expression resolves to variable declaration '_this' that compiler uses to capture 'this' reference.",
Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference: "Expression resolves to '_super' that compiler uses to capture base class reference.",
TypeParameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_type_1: "TypeParameter '{0}' of constructor signature from exported interface has or is using private type '{1}'.",
TypeParameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_type_1: "TypeParameter '{0}' of call signature from exported interface has or is using private type '{1}'.",
TypeParameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_type_1: "TypeParameter '{0}' of public static method from exported class has or is using private type '{1}'.",
TypeParameter_0_of_public_method_from_exported_class_has_or_is_using_private_type_1: "TypeParameter '{0}' of public method from exported class has or is using private type '{1}'.",
TypeParameter_0_of_method_from_exported_interface_has_or_is_using_private_type_1: "TypeParameter '{0}' of method from exported interface has or is using private type '{1}'.",
TypeParameter_0_of_exported_function_has_or_is_using_private_type_1: "TypeParameter '{0}' of exported function has or is using private type '{1}'.",
TypeParameter_0_of_constructor_signature_from_exported_interface_is_using_inaccessible_module_1: "TypeParameter '{0}' of constructor signature from exported interface is using inaccessible module {1}.",
TypeParameter_0_of_call_signature_from_exported_interface_is_using_inaccessible_module_1: "TypeParameter '{0}' of call signature from exported interface is using inaccessible module {1}",
TypeParameter_0_of_public_static_method_from_exported_class_is_using_inaccessible_module_1: "TypeParameter '{0}' of public static method from exported class is using inaccessible module {1}.",
TypeParameter_0_of_public_method_from_exported_class_is_using_inaccessible_module_1: "TypeParameter '{0}' of public method from exported class is using inaccessible module {1}.",
TypeParameter_0_of_method_from_exported_interface_is_using_inaccessible_module_1: "TypeParameter '{0}' of method from exported interface is using inaccessible module {1}.",
TypeParameter_0_of_exported_function_is_using_inaccessible_module_1: "TypeParameter '{0}' of exported function is using inaccessible module {1}.",
TypeParameter_0_of_exported_class_has_or_is_using_private_type_1: "TypeParameter '{0}' of exported class has or is using private type '{1}'.",
TypeParameter_0_of_exported_interface_has_or_is_using_private_type_1: "TypeParameter '{0}' of exported interface has or is using private type '{1}'.",
TypeParameter_0_of_exported_class_is_using_inaccessible_module_1: "TypeParameter '{0}' of exported class is using inaccessible module {1}.",
TypeParameter_0_of_exported_interface_is_using_inaccessible_module_1: "TypeParameter '{0}' of exported interface is using inaccessible module {1}.",
Duplicate_identifier_i_Compiler_uses_i_to_initialize_rest_parameter: "Duplicate identifier '_i'. Compiler uses '_i' to initialize rest parameter.",
Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters: "Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.",
No_best_common_type_exists_between_0_and_1: "No best common type exists between '{0}' and '{1}'.",
No_best_common_type_exists_between_0_1_and_2: "No best common type exists between '{0}', '{1}', and '{2}'.",
Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_an_external_module: "Duplicate identifier '{0}'. Compiler reserves name '{1}' in top level scope of an external module.",
Constraint_of_a_type_parameter_cannot_reference_any_type_parameter_from_the_same_type_parameter_list: "Constraint of a type parameter cannot reference any type parameter from the same type parameter list.",
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor.",
Parameter_0_cannot_be_referenced_in_its_initializer: "Parameter '{0}' cannot be referenced in its initializer.",
Duplicate_string_index_signature: "Duplicate string index signature.",
Duplicate_number_index_signature: "Duplicate number index signature.",
All_declarations_of_an_interface_must_have_identical_type_parameters: "All declarations of an interface must have identical type parameters.",
Expression_resolves_to_variable_declaration_i_that_compiler_uses_to_initialize_rest_parameter: "Expression resolves to variable declaration '_i' that compiler uses to initialize rest parameter.",
Neither_type_0_nor_type_1_is_assignable_to_the_other: "Neither type '{0}' nor type '{1}' is assignable to the other.",
Neither_type_0_nor_type_1_is_assignable_to_the_other_NL_2: "Neither type '{0}' nor type '{1}' is assignable to the other:{NL}{2}",
Duplicate_function_implementation: "Duplicate function implementation.",
Function_implementation_expected: "Function implementation expected.",
Function_overload_name_must_be_0: "Function overload name must be '{0}'.",
Constructor_implementation_expected: "Constructor implementation expected.",
Class_name_cannot_be_0: "Class name cannot be '{0}'.",
Interface_name_cannot_be_0: "Interface name cannot be '{0}'.",
Enum_name_cannot_be_0: "Enum name cannot be '{0}'.",
A_module_cannot_have_multiple_export_assignments: "A module cannot have multiple export assignments.",
Export_assignment_not_allowed_in_module_with_exported_element: "Export assignment not allowed in module with exported element.",
A_parameter_property_is_only_allowed_in_a_constructor_implementation: "A parameter property is only allowed in a constructor implementation.",
Function_overload_must_be_static: "Function overload must be static.",
Function_overload_must_not_be_static: "Function overload must not be static.",
Type_0_is_missing_property_1_from_type_2: "Type '{0}' is missing property '{1}' from type '{2}'.",
Types_of_property_0_of_types_1_and_2_are_incompatible: "Types of property '{0}' of types '{1}' and '{2}' are incompatible.",
Types_of_property_0_of_types_1_and_2_are_incompatible_NL_3: "Types of property '{0}' of types '{1}' and '{2}' are incompatible:{NL}{3}",
Property_0_defined_as_private_in_type_1_is_defined_as_public_in_type_2: "Property '{0}' defined as private in type '{1}' is defined as public in type '{2}'.",
Property_0_defined_as_public_in_type_1_is_defined_as_private_in_type_2: "Property '{0}' defined as public in type '{1}' is defined as private in type '{2}'.",
Types_0_and_1_define_property_2_as_private: "Types '{0}' and '{1}' define property '{2}' as private.",
Call_signatures_of_types_0_and_1_are_incompatible: "Call signatures of types '{0}' and '{1}' are incompatible.",
Call_signatures_of_types_0_and_1_are_incompatible_NL_2: "Call signatures of types '{0}' and '{1}' are incompatible:{NL}{2}",
Type_0_requires_a_call_signature_but_type_1_lacks_one: "Type '{0}' requires a call signature, but type '{1}' lacks one.",
Construct_signatures_of_types_0_and_1_are_incompatible: "Construct signatures of types '{0}' and '{1}' are incompatible.",
Construct_signatures_of_types_0_and_1_are_incompatible_NL_2: "Construct signatures of types '{0}' and '{1}' are incompatible:{NL}{2}",
Type_0_requires_a_construct_signature_but_type_1_lacks_one: "Type '{0}' requires a construct signature, but type '{1}' lacks one.",
Index_signatures_of_types_0_and_1_are_incompatible: "Index signatures of types '{0}' and '{1}' are incompatible.",
Index_signatures_of_types_0_and_1_are_incompatible_NL_2: "Index signatures of types '{0}' and '{1}' are incompatible:{NL}{2}",
Call_signature_expects_0_or_fewer_parameters: "Call signature expects {0} or fewer parameters.",
Could_not_apply_type_0_to_argument_1_which_is_of_type_2: "Could not apply type '{0}' to argument {1} which is of type '{2}'.",
Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function: "Class '{0}' defines instance member accessor '{1}', but extended class '{2}' defines it as instance member function.",
Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function: "Class '{0}' defines instance member property '{1}', but extended class '{2}' defines it as instance member function.",
Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member accessor.",
Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member property.",
Types_of_static_property_0_of_class_1_and_class_2_are_incompatible: "Types of static property '{0}' of class '{1}' and class '{2}' are incompatible.",
Types_of_static_property_0_of_class_1_and_class_2_are_incompatible_NL_3: "Types of static property '{0}' of class '{1}' and class '{2}' are incompatible:{NL}{3}",
Type_reference_cannot_refer_to_container_0: "Type reference cannot refer to container '{0}'.",
Type_reference_must_refer_to_type: "Type reference must refer to type.",
In_enums_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_the_first_enum_element: "In enums with multiple declarations only one declaration can omit an initializer for the first enum element.",
_0_overload_s: " (+ {0} overload(s))",
Variable_declaration_cannot_have_the_same_name_as_an_import_declaration: "Variable declaration cannot have the same name as an import declaration.",
Signature_expected_0_type_arguments_got_1_instead: "Signature expected {0} type arguments, got {1} instead.",
Property_0_defined_as_optional_in_type_1_but_is_required_in_type_2: "Property '{0}' defined as optional in type '{1}', but is required in type '{2}'.",
Types_0_and_1_originating_in_infinitely_expanding_type_reference_do_not_refer_to_same_named_type: "Types '{0}' and '{1}' originating in infinitely expanding type reference do not refer to same named type.",
Types_0_and_1_originating_in_infinitely_expanding_type_reference_have_incompatible_type_arguments: "Types '{0}' and '{1}' originating in infinitely expanding type reference have incompatible type arguments.",
Types_0_and_1_originating_in_infinitely_expanding_type_reference_have_incompatible_type_arguments_NL_2: "Types '{0}' and '{1}' originating in infinitely expanding type reference have incompatible type arguments:{NL}{2}",
Named_properties_0_of_types_1_and_2_are_not_identical: "Named properties '{0}' of types '{1}' and '{2}' are not identical.",
Types_of_string_indexer_of_types_0_and_1_are_not_identical: "Types of string indexer of types '{0}' and '{1}' are not identical.",
Types_of_number_indexer_of_types_0_and_1_are_not_identical: "Types of number indexer of types '{0}' and '{1}' are not identical.",
Type_of_number_indexer_in_type_0_is_not_assignable_to_string_indexer_type_in_type_1_NL_2: "Type of number indexer in type '{0}' is not assignable to string indexer type in type '{1}'.{NL}{2}",
Type_of_property_0_in_type_1_is_not_assignable_to_string_indexer_type_in_type_2_NL_3: "Type of property '{0}' in type '{1}' is not assignable to string indexer type in type '{2}'.{NL}{3}",
Type_of_property_0_in_type_1_is_not_assignable_to_number_indexer_type_in_type_2_NL_3: "Type of property '{0}' in type '{1}' is not assignable to number indexer type in type '{2}'.{NL}{3}",
Static_property_0_defined_as_private_in_type_1_is_defined_as_public_in_type_2: "Static property '{0}' defined as private in type '{1}' is defined as public in type '{2}'.",
Static_property_0_defined_as_public_in_type_1_is_defined_as_private_in_type_2: "Static property '{0}' defined as public in type '{1}' is defined as private in type '{2}'.",
Types_0_and_1_define_static_property_2_as_private: "Types '{0}' and '{1}' define static property '{2}' as private.",
Current_host_does_not_support_0_option: "Current host does not support '{0}' option.",
ECMAScript_target_version_0_not_supported_Specify_a_valid_target_version_1_default_or_2: "ECMAScript target version '{0}' not supported. Specify a valid target version: '{1}' (default), or '{2}'",
Argument_for_0_option_must_be_1_or_2: "Argument for '{0}' option must be '{1}' or '{2}'",
Could_not_find_file_0: "Could not find file: '{0}'.",
A_file_cannot_have_a_reference_to_itself: "A file cannot have a reference to itself.",
Cannot_resolve_referenced_file_0: "Cannot resolve referenced file: '{0}'.",
Cannot_find_the_common_subdirectory_path_for_the_input_files: "Cannot find the common subdirectory path for the input files.",
Emit_Error_0: "Emit Error: {0}.",
Cannot_read_file_0_1: "Cannot read file '{0}': {1}",
Unsupported_file_encoding: "Unsupported file encoding.",
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.",
Unsupported_locale_0: "Unsupported locale: '{0}'.",
Execution_Failed_NL: "Execution Failed.{NL}",
Invalid_call_to_up: "Invalid call to 'up'",
Invalid_call_to_down: "Invalid call to 'down'",
Base64_value_0_finished_with_a_continuation_bit: "Base64 value '{0}' finished with a continuation bit.",
Unknown_compiler_option_0: "Unknown compiler option '{0}'",
Expected_0_arguments_to_message_got_1_instead: "Expected {0} arguments to message, got {1} instead.",
Expected_the_message_0_to_have_1_arguments_but_it_had_2: "Expected the message '{0}' to have {1} arguments, but it had {2}",
Could_not_delete_file_0: "Could not delete file '{0}'",
Could_not_create_directory_0: "Could not create directory '{0}'",
Error_while_executing_file_0: "Error while executing file '{0}': ",
Cannot_compile_external_modules_unless_the_module_flag_is_provided: "Cannot compile external modules unless the '--module' flag is provided.",
Option_mapRoot_cannot_be_specified_without_specifying_sourcemap_option: "Option mapRoot cannot be specified without specifying sourcemap option.",
Option_sourceRoot_cannot_be_specified_without_specifying_sourcemap_option: "Option sourceRoot cannot be specified without specifying sourcemap option.",
Options_mapRoot_and_sourceRoot_cannot_be_specified_without_specifying_sourcemap_option: "Options mapRoot and sourceRoot cannot be specified without specifying sourcemap option.",
Option_0_specified_without_1: "Option '{0}' specified without '{1}'",
codepage_option_not_supported_on_current_platform: "'codepage' option not supported on current platform.",
Concatenate_and_emit_output_to_single_file: "Concatenate and emit output to single file.",
Generates_corresponding_0_file: "Generates corresponding {0} file.",
Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations: "Specifies the location where debugger should locate map files instead of generated locations.",
Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations: "Specifies the location where debugger should locate TypeScript files instead of source locations.",
Watch_input_files: "Watch input files.",
Redirect_output_structure_to_the_directory: "Redirect output structure to the directory.",
Do_not_emit_comments_to_output: "Do not emit comments to output.",
Skip_resolution_and_preprocessing: "Skip resolution and preprocessing.",
Specify_ECMAScript_target_version_0_default_or_1: "Specify ECMAScript target version: '{0}' (default), or '{1}'",
Specify_module_code_generation_0_or_1: "Specify module code generation: '{0}' or '{1}'",
Print_this_message: "Print this message.",
Print_the_compiler_s_version_0: "Print the compiler's version: {0}",
Allow_use_of_deprecated_0_keyword_when_referencing_an_external_module: "Allow use of deprecated '{0}' keyword when referencing an external module.",
Specify_locale_for_errors_and_messages_For_example_0_or_1: "Specify locale for errors and messages. For example '{0}' or '{1}'",
Syntax_0: "Syntax: {0}",
options: "options",
file1: "file",
Examples: "Examples:",
Options: "Options:",
Insert_command_line_options_and_files_from_a_file: "Insert command line options and files from a file.",
Version_0: "Version {0}",
Use_the_0_flag_to_see_options: "Use the '{0}' flag to see options.",
NL_Recompiling_0: "{NL}Recompiling ({0}):",
STRING: "STRING",
KIND: "KIND",
file2: "FILE",
VERSION: "VERSION",
LOCATION: "LOCATION",
DIRECTORY: "DIRECTORY",
NUMBER: "NUMBER",
Specify_the_codepage_to_use_when_opening_source_files: "Specify the codepage to use when opening source files.",
Additional_locations: "Additional locations:",
This_version_of_the_Javascript_runtime_does_not_support_the_0_function: "This version of the Javascript runtime does not support the '{0}' function.",
Unknown_rule: "Unknown rule.",
Invalid_line_number_0: "Invalid line number ({0})",
Warn_on_expressions_and_declarations_with_an_implied_any_type: "Warn on expressions and declarations with an implied 'any' type.",
Variable_0_implicitly_has_an_any_type: "Variable '{0}' implicitly has an 'any' type.",
Parameter_0_of_1_implicitly_has_an_any_type: "Parameter '{0}' of '{1}' implicitly has an 'any' type.",
Parameter_0_of_function_type_implicitly_has_an_any_type: "Parameter '{0}' of function type implicitly has an 'any' type.",
Member_0_of_object_type_implicitly_has_an_any_type: "Member '{0}' of object type implicitly has an 'any' type.",
new_expression_which_lacks_a_constructor_signature_implicitly_has_an_any_type: "'new' expression, which lacks a constructor signature, implicitly has an 'any' type.",
_0_which_lacks_return_type_annotation_implicitly_has_an_any_return_type: "'{0}', which lacks return-type annotation, implicitly has an 'any' return type.",
Function_expression_which_lacks_return_type_annotation_implicitly_has_an_any_return_type: "Function expression, which lacks return-type annotation, implicitly has an 'any' return type.",
Parameter_0_of_lambda_function_implicitly_has_an_any_type: "Parameter '{0}' of lambda function implicitly has an 'any' type.",
Constructor_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type: "Constructor signature, which lacks return-type annotation, implicitly has an 'any' return type.",
Lambda_Function_which_lacks_return_type_annotation_implicitly_has_an_any_return_type: "Lambda Function, which lacks return-type annotation, implicitly has an 'any' return type.",
Array_Literal_implicitly_has_an_any_type_from_widening: "Array Literal implicitly has an 'any' type from widening.",
_0_which_lacks_get_accessor_and_parameter_type_annotation_on_set_accessor_implicitly_has_an_any_type: "'{0}', which lacks 'get' accessor and parameter type annotation on 'set' accessor, implicitly has an 'any' type.",
Index_signature_of_object_type_implicitly_has_an_any_type: "Index signature of object type implicitly has an 'any' type.",
Object_literal_s_property_0_implicitly_has_an_any_type_from_widening: "Object literal's property '{0}' implicitly has an 'any' type from widening.",
};
}

View File

@@ -0,0 +1,446 @@
// <auto-generated />
/// <reference path="..\core\diagnosticCategory.ts" />
module TypeScript {
export var diagnosticInformationMap: IIndexable<any> = {
"error TS{0}: {1}": { "code": 0, "category": DiagnosticCategory.NoPrefix },
"warning TS{0}: {1}": { "code": 1, "category": DiagnosticCategory.NoPrefix },
"Unrecognized escape sequence.": { "code": 1000, "category": DiagnosticCategory.Error },
"Unexpected character {0}.": { "code": 1001, "category": DiagnosticCategory.Error },
"Missing close quote character.": { "code": 1002, "category": DiagnosticCategory.Error },
"Identifier expected.": { "code": 1003, "category": DiagnosticCategory.Error },
"'{0}' keyword expected.": { "code": 1004, "category": DiagnosticCategory.Error },
"'{0}' expected.": { "code": 1005, "category": DiagnosticCategory.Error },
"Identifier expected; '{0}' is a keyword.": { "code": 1006, "category": DiagnosticCategory.Error },
"Automatic semicolon insertion not allowed.": { "code": 1007, "category": DiagnosticCategory.Error },
"Unexpected token; '{0}' expected.": { "code": 1008, "category": DiagnosticCategory.Error },
"Trailing comma not allowed.": { "code": 1009, "category": DiagnosticCategory.Error },
"'*/' expected.": { "code": 1010, "category": DiagnosticCategory.Error },
"'public' or 'private' modifier must precede 'static'.": { "code": 1011, "category": DiagnosticCategory.Error },
"Unexpected token.": { "code": 1012, "category": DiagnosticCategory.Error },
"Catch clause parameter cannot have a type annotation.": { "code": 1013, "category": DiagnosticCategory.Error },
"A rest parameter must be last in a parameter list.": { "code": 1014, "category": DiagnosticCategory.Error },
"Parameter cannot have question mark and initializer.": { "code": 1015, "category": DiagnosticCategory.Error },
"A required parameter cannot follow an optional parameter.": { "code": 1016, "category": DiagnosticCategory.Error },
"Index signatures cannot have rest parameters.": { "code": 1017, "category": DiagnosticCategory.Error },
"Index signature parameter cannot have accessibility modifiers.": { "code": 1018, "category": DiagnosticCategory.Error },
"Index signature parameter cannot have a question mark.": { "code": 1019, "category": DiagnosticCategory.Error },
"Index signature parameter cannot have an initializer.": { "code": 1020, "category": DiagnosticCategory.Error },
"Index signature must have a type annotation.": { "code": 1021, "category": DiagnosticCategory.Error },
"Index signature parameter must have a type annotation.": { "code": 1022, "category": DiagnosticCategory.Error },
"Index signature parameter type must be 'string' or 'number'.": { "code": 1023, "category": DiagnosticCategory.Error },
"'extends' clause already seen.": { "code": 1024, "category": DiagnosticCategory.Error },
"'extends' clause must precede 'implements' clause.": { "code": 1025, "category": DiagnosticCategory.Error },
"Classes can only extend a single class.": { "code": 1026, "category": DiagnosticCategory.Error },
"'implements' clause already seen.": { "code": 1027, "category": DiagnosticCategory.Error },
"Accessibility modifier already seen.": { "code": 1028, "category": DiagnosticCategory.Error },
"'{0}' modifier must precede '{1}' modifier.": { "code": 1029, "category": DiagnosticCategory.Error },
"'{0}' modifier already seen.": { "code": 1030, "category": DiagnosticCategory.Error },
"'{0}' modifier cannot appear on a class element.": { "code": 1031, "category": DiagnosticCategory.Error },
"Interface declaration cannot have 'implements' clause.": { "code": 1032, "category": DiagnosticCategory.Error },
"'super' invocation cannot have type arguments.": { "code": 1034, "category": DiagnosticCategory.Error },
"Only ambient modules can use quoted names.": { "code": 1035, "category": DiagnosticCategory.Error },
"Statements are not allowed in ambient contexts.": { "code": 1036, "category": DiagnosticCategory.Error },
"A function implementation cannot be declared in an ambient context.": { "code": 1037, "category": DiagnosticCategory.Error },
"A 'declare' modifier cannot be used in an already ambient context.": { "code": 1038, "category": DiagnosticCategory.Error },
"Initializers are not allowed in ambient contexts.": { "code": 1039, "category": DiagnosticCategory.Error },
"'{0}' modifier cannot appear on a module element.": { "code": 1044, "category": DiagnosticCategory.Error },
"A 'declare' modifier cannot be used with an interface declaration.": { "code": 1045, "category": DiagnosticCategory.Error },
"A 'declare' modifier is required for a top level declaration in a .d.ts file.": { "code": 1046, "category": DiagnosticCategory.Error },
"A rest parameter cannot be optional.": { "code": 1047, "category": DiagnosticCategory.Error },
"A rest parameter cannot have an initializer.": { "code": 1048, "category": DiagnosticCategory.Error },
"'set' accessor must have exactly one parameter.": { "code": 1049, "category": DiagnosticCategory.Error },
"'set' accessor parameter cannot be optional.": { "code": 1051, "category": DiagnosticCategory.Error },
"'set' accessor parameter cannot have an initializer.": { "code": 1052, "category": DiagnosticCategory.Error },
"'set' accessor cannot have rest parameter.": { "code": 1053, "category": DiagnosticCategory.Error },
"'get' accessor cannot have parameters.": { "code": 1054, "category": DiagnosticCategory.Error },
"Modifiers cannot appear here.": { "code": 1055, "category": DiagnosticCategory.Error },
"Accessors are only available when targeting ECMAScript 5 and higher.": { "code": 1056, "category": DiagnosticCategory.Error },
"Enum member must have initializer.": { "code": 1061, "category": DiagnosticCategory.Error },
"Export assignment cannot be used in internal modules.": { "code": 1063, "category": DiagnosticCategory.Error },
"Ambient enum elements can only have integer literal initializers.": { "code": 1066, "category": DiagnosticCategory.Error },
"module, class, interface, enum, import or statement": { "code": 1067, "category": DiagnosticCategory.NoPrefix },
"constructor, function, accessor or variable": { "code": 1068, "category": DiagnosticCategory.NoPrefix },
"statement": { "code": 1069, "category": DiagnosticCategory.NoPrefix },
"case or default clause": { "code": 1070, "category": DiagnosticCategory.NoPrefix },
"identifier": { "code": 1071, "category": DiagnosticCategory.NoPrefix },
"call, construct, index, property or function signature": { "code": 1072, "category": DiagnosticCategory.NoPrefix },
"expression": { "code": 1073, "category": DiagnosticCategory.NoPrefix },
"type name": { "code": 1074, "category": DiagnosticCategory.NoPrefix },
"property or accessor": { "code": 1075, "category": DiagnosticCategory.NoPrefix },
"parameter": { "code": 1076, "category": DiagnosticCategory.NoPrefix },
"type": { "code": 1077, "category": DiagnosticCategory.NoPrefix },
"type parameter": { "code": 1078, "category": DiagnosticCategory.NoPrefix },
"A 'declare' modifier cannot be used with an import declaration.": { "code": 1079, "category": DiagnosticCategory.Error },
"Invalid 'reference' directive syntax.": { "code": 1084, "category": DiagnosticCategory.Error },
"Octal literals are not available when targeting ECMAScript 5 and higher.": { "code": 1085, "category": DiagnosticCategory.Error },
"Accessors are not allowed in ambient contexts.": { "code": 1086, "category": DiagnosticCategory.Error },
"'{0}' modifier cannot appear on a constructor declaration.": { "code": 1089, "category": DiagnosticCategory.Error },
"'{0}' modifier cannot appear on a parameter.": { "code": 1090, "category": DiagnosticCategory.Error },
"Only a single variable declaration is allowed in a 'for...in' statement.": { "code": 1091, "category": DiagnosticCategory.Error },
"Type parameters cannot appear on a constructor declaration.": { "code": 1092, "category": DiagnosticCategory.Error },
"Type annotation cannot appear on a constructor declaration.": { "code": 1093, "category": DiagnosticCategory.Error },
"Type parameters cannot appear on an accessor.": { "code": 1094, "category": DiagnosticCategory.Error },
"Type annotation cannot appear on a 'set' accessor.": { "code": 1095, "category": DiagnosticCategory.Error },
"Index signature must have exactly one parameter.": { "code": 1096, "category": DiagnosticCategory.Error },
"'{0}' list cannot be empty.": { "code": 1097, "category": DiagnosticCategory.Error },
"variable declaration": { "code": 1098, "category": DiagnosticCategory.NoPrefix },
"type argument": { "code": 1099, "category": DiagnosticCategory.NoPrefix },
"Invalid use of '{0}' in strict mode.": { "code": 1100, "category": DiagnosticCategory.Error },
"'with' statements are not allowed in strict mode.": { "code": 1101, "category": DiagnosticCategory.Error },
"'delete' cannot be called on an identifier in strict mode.": { "code": 1102, "category": DiagnosticCategory.Error },
"Invalid left-hand side in 'for...in' statement.": { "code": 1103, "category": DiagnosticCategory.Error },
"'continue' statement can only be used within an enclosing iteration statement.": { "code": 1104, "category": DiagnosticCategory.Error },
"'break' statement can only be used within an enclosing iteration or switch statement.": { "code": 1105, "category": DiagnosticCategory.Error },
"Jump target not found.": { "code": 1106, "category": DiagnosticCategory.Error },
"Jump target cannot cross function boundary.": { "code": 1107, "category": DiagnosticCategory.Error },
"'return' statement must be contained within a function body.": { "code": 1108, "category": DiagnosticCategory.Error },
"Expression expected.": { "code": 1109, "category": DiagnosticCategory.Error },
"Type expected.": { "code": 1110, "category": DiagnosticCategory.Error },
"Duplicate identifier '{0}'.": { "code": 2000, "category": DiagnosticCategory.Error },
"The name '{0}' does not exist in the current scope.": { "code": 2001, "category": DiagnosticCategory.Error },
"The name '{0}' does not refer to a value.": { "code": 2002, "category": DiagnosticCategory.Error },
"'super' can only be used inside a class instance method.": { "code": 2003, "category": DiagnosticCategory.Error },
"The left-hand side of an assignment expression must be a variable, property or indexer.": { "code": 2004, "category": DiagnosticCategory.Error },
"Value of type '{0}' is not callable. Did you mean to include 'new'?": { "code": 2161, "category": DiagnosticCategory.Error },
"Value of type '{0}' is not callable.": { "code": 2006, "category": DiagnosticCategory.Error },
"Value of type '{0}' is not newable.": { "code": 2007, "category": DiagnosticCategory.Error },
"An index expression argument must be 'string', 'number', or 'any'.": { "code": 2008, "category": DiagnosticCategory.Error },
"Operator '{0}' cannot be applied to types '{1}' and '{2}'.": { "code": 2009, "category": DiagnosticCategory.Error },
"Type '{0}' is not assignable to type '{1}'.": { "code": 2011, "category": DiagnosticCategory.Error },
"Type '{0}' is not assignable to type '{1}':{NL}{2}": { "code": 2012, "category": DiagnosticCategory.Error },
"Expected var, class, interface, or module.": { "code": 2013, "category": DiagnosticCategory.Error },
"Getter '{0}' already declared.": { "code": 2015, "category": DiagnosticCategory.Error },
"Setter '{0}' already declared.": { "code": 2016, "category": DiagnosticCategory.Error },
"Exported class '{0}' extends private class '{1}'.": { "code": 2018, "category": DiagnosticCategory.Error },
"Exported class '{0}' implements private interface '{1}'.": { "code": 2019, "category": DiagnosticCategory.Error },
"Exported interface '{0}' extends private interface '{1}'.": { "code": 2020, "category": DiagnosticCategory.Error },
"Exported class '{0}' extends class from inaccessible module {1}.": { "code": 2021, "category": DiagnosticCategory.Error },
"Exported class '{0}' implements interface from inaccessible module {1}.": { "code": 2022, "category": DiagnosticCategory.Error },
"Exported interface '{0}' extends interface from inaccessible module {1}.": { "code": 2023, "category": DiagnosticCategory.Error },
"Public static property '{0}' of exported class has or is using private type '{1}'.": { "code": 2024, "category": DiagnosticCategory.Error },
"Public property '{0}' of exported class has or is using private type '{1}'.": { "code": 2025, "category": DiagnosticCategory.Error },
"Property '{0}' of exported interface has or is using private type '{1}'.": { "code": 2026, "category": DiagnosticCategory.Error },
"Exported variable '{0}' has or is using private type '{1}'.": { "code": 2027, "category": DiagnosticCategory.Error },
"Public static property '{0}' of exported class is using inaccessible module {1}.": { "code": 2028, "category": DiagnosticCategory.Error },
"Public property '{0}' of exported class is using inaccessible module {1}.": { "code": 2029, "category": DiagnosticCategory.Error },
"Property '{0}' of exported interface is using inaccessible module {1}.": { "code": 2030, "category": DiagnosticCategory.Error },
"Exported variable '{0}' is using inaccessible module {1}.": { "code": 2031, "category": DiagnosticCategory.Error },
"Parameter '{0}' of constructor from exported class has or is using private type '{1}'.": { "code": 2032, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public static property setter from exported class has or is using private type '{1}'.": { "code": 2033, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public property setter from exported class has or is using private type '{1}'.": { "code": 2034, "category": DiagnosticCategory.Error },
"Parameter '{0}' of constructor signature from exported interface has or is using private type '{1}'.": { "code": 2035, "category": DiagnosticCategory.Error },
"Parameter '{0}' of call signature from exported interface has or is using private type '{1}'.": { "code": 2036, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public static method from exported class has or is using private type '{1}'.": { "code": 2037, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public method from exported class has or is using private type '{1}'.": { "code": 2038, "category": DiagnosticCategory.Error },
"Parameter '{0}' of method from exported interface has or is using private type '{1}'.": { "code": 2039, "category": DiagnosticCategory.Error },
"Parameter '{0}' of exported function has or is using private type '{1}'.": { "code": 2040, "category": DiagnosticCategory.Error },
"Parameter '{0}' of constructor from exported class is using inaccessible module {1}.": { "code": 2041, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public static property setter from exported class is using inaccessible module {1}.": { "code": 2042, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public property setter from exported class is using inaccessible module {1}.": { "code": 2043, "category": DiagnosticCategory.Error },
"Parameter '{0}' of constructor signature from exported interface is using inaccessible module {1}.": { "code": 2044, "category": DiagnosticCategory.Error },
"Parameter '{0}' of call signature from exported interface is using inaccessible module {1}": { "code": 2045, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public static method from exported class is using inaccessible module {1}.": { "code": 2046, "category": DiagnosticCategory.Error },
"Parameter '{0}' of public method from exported class is using inaccessible module {1}.": { "code": 2047, "category": DiagnosticCategory.Error },
"Parameter '{0}' of method from exported interface is using inaccessible module {1}.": { "code": 2048, "category": DiagnosticCategory.Error },
"Parameter '{0}' of exported function is using inaccessible module {1}.": { "code": 2049, "category": DiagnosticCategory.Error },
"Return type of public static property getter from exported class has or is using private type '{0}'.": { "code": 2050, "category": DiagnosticCategory.Error },
"Return type of public property getter from exported class has or is using private type '{0}'.": { "code": 2051, "category": DiagnosticCategory.Error },
"Return type of constructor signature from exported interface has or is using private type '{0}'.": { "code": 2052, "category": DiagnosticCategory.Error },
"Return type of call signature from exported interface has or is using private type '{0}'.": { "code": 2053, "category": DiagnosticCategory.Error },
"Return type of index signature from exported interface has or is using private type '{0}'.": { "code": 2054, "category": DiagnosticCategory.Error },
"Return type of public static method from exported class has or is using private type '{0}'.": { "code": 2055, "category": DiagnosticCategory.Error },
"Return type of public method from exported class has or is using private type '{0}'.": { "code": 2056, "category": DiagnosticCategory.Error },
"Return type of method from exported interface has or is using private type '{0}'.": { "code": 2057, "category": DiagnosticCategory.Error },
"Return type of exported function has or is using private type '{0}'.": { "code": 2058, "category": DiagnosticCategory.Error },
"Return type of public static property getter from exported class is using inaccessible module {0}.": { "code": 2059, "category": DiagnosticCategory.Error },
"Return type of public property getter from exported class is using inaccessible module {0}.": { "code": 2060, "category": DiagnosticCategory.Error },
"Return type of constructor signature from exported interface is using inaccessible module {0}.": { "code": 2061, "category": DiagnosticCategory.Error },
"Return type of call signature from exported interface is using inaccessible module {0}.": { "code": 2062, "category": DiagnosticCategory.Error },
"Return type of index signature from exported interface is using inaccessible module {0}.": { "code": 2063, "category": DiagnosticCategory.Error },
"Return type of public static method from exported class is using inaccessible module {0}.": { "code": 2064, "category": DiagnosticCategory.Error },
"Return type of public method from exported class is using inaccessible module {0}.": { "code": 2065, "category": DiagnosticCategory.Error },
"Return type of method from exported interface is using inaccessible module {0}.": { "code": 2066, "category": DiagnosticCategory.Error },
"Return type of exported function is using inaccessible module {0}.": { "code": 2067, "category": DiagnosticCategory.Error },
"'new T[]' cannot be used to create an array. Use 'new Array<T>()' instead.": { "code": 2068, "category": DiagnosticCategory.Error },
"A parameter list must follow a generic type argument list. '(' expected.": { "code": 2069, "category": DiagnosticCategory.Error },
"Multiple constructor implementations are not allowed.": { "code": 2070, "category": DiagnosticCategory.Error },
"Cannot find external module '{0}'.": { "code": 2071, "category": DiagnosticCategory.Error },
"Module cannot be aliased to a non-module type.": { "code": 2072, "category": DiagnosticCategory.Error },
"A class may only extend another class.": { "code": 2073, "category": DiagnosticCategory.Error },
"A class may only implement another class or interface.": { "code": 2074, "category": DiagnosticCategory.Error },
"An interface may only extend a class or another interface.": { "code": 2075, "category": DiagnosticCategory.Error },
"Unable to resolve type.": { "code": 2077, "category": DiagnosticCategory.Error },
"Unable to resolve type of '{0}'.": { "code": 2078, "category": DiagnosticCategory.Error },
"Unable to resolve type parameter constraint.": { "code": 2079, "category": DiagnosticCategory.Error },
"Type parameter constraint cannot be a primitive type.": { "code": 2080, "category": DiagnosticCategory.Error },
"Supplied parameters do not match any signature of call target.": { "code": 2081, "category": DiagnosticCategory.Error },
"Supplied parameters do not match any signature of call target:{NL}{0}": { "code": 2082, "category": DiagnosticCategory.Error },
"Cannot use 'new' with an expression whose type lacks a signature.": { "code": 2083, "category": DiagnosticCategory.Error },
"Only a void function can be called with the 'new' keyword.": { "code": 2084, "category": DiagnosticCategory.Error },
"Could not select overload for 'new' expression.": { "code": 2085, "category": DiagnosticCategory.Error },
"Type '{0}' does not satisfy the constraint '{1}'.": { "code": 2086, "category": DiagnosticCategory.Error },
"Could not select overload for 'call' expression.": { "code": 2087, "category": DiagnosticCategory.Error },
"Cannot invoke an expression whose type lacks a call signature.": { "code": 2088, "category": DiagnosticCategory.Error },
"Calls to 'super' are only valid inside a class.": { "code": 2089, "category": DiagnosticCategory.Error },
"Generic type '{0}' requires {1} type argument(s).": { "code": 2090, "category": DiagnosticCategory.Error },
"Type of array literal cannot be determined. Best common type could not be found for array elements.": { "code": 2092, "category": DiagnosticCategory.Error },
"Could not find enclosing symbol for dotted name '{0}'.": { "code": 2093, "category": DiagnosticCategory.Error },
"Property '{0}' does not exist on value of type '{1}'.": { "code": 2094, "category": DiagnosticCategory.Error },
"Cannot find name '{0}'.": { "code": 2095, "category": DiagnosticCategory.Error },
"'get' and 'set' accessor must have the same type.": { "code": 2096, "category": DiagnosticCategory.Error },
"'this' cannot be referenced in current location.": { "code": 2097, "category": DiagnosticCategory.Error },
"Static members cannot reference class type parameters.": { "code": 2099, "category": DiagnosticCategory.Error },
"Type '{0}' recursively references itself as a base type.": { "code": 2100, "category": DiagnosticCategory.Error },
"'super' property access is permitted only in a constructor, member function, or member accessor of a derived class.": { "code": 2102, "category": DiagnosticCategory.Error },
"'super' can only be referenced in a derived class.": { "code": 2103, "category": DiagnosticCategory.Error },
"A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.": { "code": 2104, "category": DiagnosticCategory.Error },
"Constructors for derived classes must contain a 'super' call.": { "code": 2105, "category": DiagnosticCategory.Error },
"Super calls are not permitted outside constructors or in nested functions inside constructors.": { "code": 2106, "category": DiagnosticCategory.Error },
"'{0}.{1}' is inaccessible.": { "code": 2107, "category": DiagnosticCategory.Error },
"'this' cannot be referenced in a module body.": { "code": 2108, "category": DiagnosticCategory.Error },
"Invalid '+' expression - types not known to support the addition operator.": { "code": 2111, "category": DiagnosticCategory.Error },
"The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.": { "code": 2112, "category": DiagnosticCategory.Error },
"The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.": { "code": 2113, "category": DiagnosticCategory.Error },
"An arithmetic operand must be of type 'any', 'number' or an enum type.": { "code": 2114, "category": DiagnosticCategory.Error },
"Variable declarations of a 'for' statement cannot use a type annotation.": { "code": 2115, "category": DiagnosticCategory.Error },
"Variable declarations of a 'for' statement must be of types 'string' or 'any'.": { "code": 2116, "category": DiagnosticCategory.Error },
"The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.": { "code": 2117, "category": DiagnosticCategory.Error },
"The left-hand side of an 'in' expression must be of types 'any', 'string' or 'number'.": { "code": 2118, "category": DiagnosticCategory.Error },
"The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.": { "code": 2119, "category": DiagnosticCategory.Error },
"The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.": { "code": 2120, "category": DiagnosticCategory.Error },
"The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.": { "code": 2121, "category": DiagnosticCategory.Error },
"Setters cannot return a value.": { "code": 2122, "category": DiagnosticCategory.Error },
"Tried to query type of uninitialized module '{0}'.": { "code": 2123, "category": DiagnosticCategory.Error },
"Tried to set variable type to uninitialized module type '{0}'.": { "code": 2124, "category": DiagnosticCategory.Error },
"Type '{0}' is not generic.": { "code": 2125, "category": DiagnosticCategory.Error },
"Getters must return a value.": { "code": 2126, "category": DiagnosticCategory.Error },
"Getter and setter accessors do not agree in visibility.": { "code": 2127, "category": DiagnosticCategory.Error },
"Invalid left-hand side of assignment expression.": { "code": 2130, "category": DiagnosticCategory.Error },
"Function declared a non-void return type, but has no return expression.": { "code": 2131, "category": DiagnosticCategory.Error },
"Cannot resolve return type reference.": { "code": 2132, "category": DiagnosticCategory.Error },
"Constructors cannot have a return type of 'void'.": { "code": 2133, "category": DiagnosticCategory.Error },
"Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'.": { "code": 2134, "category": DiagnosticCategory.Error },
"All symbols within a with block will be resolved to 'any'.": { "code": 2135, "category": DiagnosticCategory.Error },
"Import declarations in an internal module cannot reference an external module.": { "code": 2136, "category": DiagnosticCategory.Error },
"Class {0} declares interface {1} but does not implement it:{NL}{2}": { "code": 2137, "category": DiagnosticCategory.Error },
"Class {0} declares class {1} as an interface but does not implement it:{NL}{2}": { "code": 2138, "category": DiagnosticCategory.Error },
"The operand of an increment or decrement operator must be a variable, property or indexer.": { "code": 2139, "category": DiagnosticCategory.Error },
"'this' cannot be referenced in a static property initializer.": { "code": 2140, "category": DiagnosticCategory.Error },
"Class '{0}' cannot extend class '{1}':{NL}{2}": { "code": 2141, "category": DiagnosticCategory.Error },
"Interface '{0}' cannot extend class '{1}':{NL}{2}": { "code": 2142, "category": DiagnosticCategory.Error },
"Interface '{0}' cannot extend interface '{1}':{NL}{2}": { "code": 2143, "category": DiagnosticCategory.Error },
"Overload signature is not compatible with function definition.": { "code": 2148, "category": DiagnosticCategory.Error },
"Overload signature is not compatible with function definition:{NL}{0}": { "code": 2149, "category": DiagnosticCategory.Error },
"Overload signatures must all be public or private.": { "code": 2150, "category": DiagnosticCategory.Error },
"Overload signatures must all be exported or not exported.": { "code": 2151, "category": DiagnosticCategory.Error },
"Overload signatures must all be ambient or non-ambient.": { "code": 2152, "category": DiagnosticCategory.Error },
"Overload signatures must all be optional or required.": { "code": 2153, "category": DiagnosticCategory.Error },
"Specialized overload signature is not assignable to any non-specialized signature.": { "code": 2154, "category": DiagnosticCategory.Error },
"'this' cannot be referenced in constructor arguments.": { "code": 2155, "category": DiagnosticCategory.Error },
"Instance member cannot be accessed off a class.": { "code": 2157, "category": DiagnosticCategory.Error },
"Untyped function calls may not accept type arguments.": { "code": 2158, "category": DiagnosticCategory.Error },
"Non-generic functions may not accept type arguments.": { "code": 2159, "category": DiagnosticCategory.Error },
"A generic type may not reference itself with a wrapped form of its own type parameters.": { "code": 2160, "category": DiagnosticCategory.Error },
"A rest parameter must be of an array type.": { "code": 2162, "category": DiagnosticCategory.Error },
"Overload signature implementation cannot use specialized type.": { "code": 2163, "category": DiagnosticCategory.Error },
"Export assignments may only be used at the top-level of external modules.": { "code": 2164, "category": DiagnosticCategory.Error },
"Export assignments may only be made with variables, functions, classes, interfaces, enums and internal modules.": { "code": 2165, "category": DiagnosticCategory.Error },
"Only public methods of the base class are accessible via the 'super' keyword.": { "code": 2166, "category": DiagnosticCategory.Error },
"Numeric indexer type '{0}' must be assignable to string indexer type '{1}'.": { "code": 2167, "category": DiagnosticCategory.Error },
"Numeric indexer type '{0}' must be assignable to string indexer type '{1}':{NL}{2}": { "code": 2168, "category": DiagnosticCategory.Error },
"All numerically named properties must be assignable to numeric indexer type '{0}'.": { "code": 2169, "category": DiagnosticCategory.Error },
"All numerically named properties must be assignable to numeric indexer type '{0}':{NL}{1}": { "code": 2170, "category": DiagnosticCategory.Error },
"All named properties must be assignable to string indexer type '{0}'.": { "code": 2171, "category": DiagnosticCategory.Error },
"All named properties must be assignable to string indexer type '{0}':{NL}{1}": { "code": 2172, "category": DiagnosticCategory.Error },
"A parameter initializer is only allowed in a function or constructor implementation.": { "code": 2174, "category": DiagnosticCategory.Error },
"Function expression declared a non-void return type, but has no return expression.": { "code": 2176, "category": DiagnosticCategory.Error },
"Import declaration referencing identifier from internal module can only be made with variables, functions, classes, interfaces, enums and internal modules.": { "code": 2177, "category": DiagnosticCategory.Error },
"Module '{0}' has no exported member '{1}'.": { "code": 2178, "category": DiagnosticCategory.Error },
"Unable to resolve module reference '{0}'.": { "code": 2179, "category": DiagnosticCategory.Error },
"Could not find module '{0}' in module '{1}'.": { "code": 2180, "category": DiagnosticCategory.Error },
"Exported import declaration '{0}' is assigned value with type that has or is using private type '{1}'.": { "code": 2181, "category": DiagnosticCategory.Error },
"Exported import declaration '{0}' is assigned value with type that is using inaccessible module '{1}'.": { "code": 2182, "category": DiagnosticCategory.Error },
"Exported import declaration '{0}' is assigned type that has or is using private type '{1}'.": { "code": 2183, "category": DiagnosticCategory.Error },
"Exported import declaration '{0}' is assigned type that is using inaccessible module '{1}'.": { "code": 2184, "category": DiagnosticCategory.Error },
"Exported import declaration '{0}' is assigned container that is or is using inaccessible module '{1}'.": { "code": 2185, "category": DiagnosticCategory.Error },
"Type name '{0}' in extends clause does not reference constructor function for '{1}'.": { "code": 2186, "category": DiagnosticCategory.Error },
"Internal module reference '{0}' in import declaration does not reference module instance for '{1}'.": { "code": 2187, "category": DiagnosticCategory.Error },
"Module '{0}' cannot merge with previous declaration of '{1}' in a different file '{2}'.": { "code": 2188, "category": DiagnosticCategory.Error },
"Interface '{0}' cannot simultaneously extend types '{1}' and '{2}':{NL}{3}": { "code": 2189, "category": DiagnosticCategory.Error },
"Initializer of parameter '{0}' cannot reference identifier '{1}' declared after it.": { "code": 2190, "category": DiagnosticCategory.Error },
"Ambient external module declaration cannot be reopened.": { "code": 2191, "category": DiagnosticCategory.Error },
"All declarations of merged declaration '{0}' must be exported or not exported.": { "code": 2192, "category": DiagnosticCategory.Error },
"'super' cannot be referenced in constructor arguments.": { "code": 2193, "category": DiagnosticCategory.Error },
"Return type of constructor signature must be assignable to the instance type of the class.": { "code": 2194, "category": DiagnosticCategory.Error },
"Ambient external module declaration must be defined in global context.": { "code": 2195, "category": DiagnosticCategory.Error },
"Ambient external module declaration cannot specify relative module name.": { "code": 2196, "category": DiagnosticCategory.Error },
"Import declaration in an ambient external module declaration cannot reference external module through relative external module name.": { "code": 2197, "category": DiagnosticCategory.Error },
"No best common type exists among return expressions.": { "code": 2198, "category": DiagnosticCategory.Error },
"Import declaration cannot refer to external module reference when --noResolve option is set.": { "code": 2199, "category": DiagnosticCategory.Error },
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": { "code": 2200, "category": DiagnosticCategory.Error },
"Duplicate identifier '_super'. Compiler uses '_super' to capture base class reference.": { "code": 2205, "category": DiagnosticCategory.Error },
"Expression resolves to variable declaration '_this' that compiler uses to capture 'this' reference.": { "code": 2206, "category": DiagnosticCategory.Error },
"Expression resolves to '_super' that compiler uses to capture base class reference.": { "code": 2207, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of constructor signature from exported interface has or is using private type '{1}'.": { "code": 2208, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of call signature from exported interface has or is using private type '{1}'.": { "code": 2209, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of public static method from exported class has or is using private type '{1}'.": { "code": 2210, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of public method from exported class has or is using private type '{1}'.": { "code": 2211, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of method from exported interface has or is using private type '{1}'.": { "code": 2212, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported function has or is using private type '{1}'.": { "code": 2213, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of constructor signature from exported interface is using inaccessible module {1}.": { "code": 2214, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of call signature from exported interface is using inaccessible module {1}": { "code": 2215, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of public static method from exported class is using inaccessible module {1}.": { "code": 2216, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of public method from exported class is using inaccessible module {1}.": { "code": 2217, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of method from exported interface is using inaccessible module {1}.": { "code": 2218, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported function is using inaccessible module {1}.": { "code": 2219, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported class has or is using private type '{1}'.": { "code": 2220, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported interface has or is using private type '{1}'.": { "code": 2221, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported class is using inaccessible module {1}.": { "code": 2222, "category": DiagnosticCategory.Error },
"TypeParameter '{0}' of exported interface is using inaccessible module {1}.": { "code": 2223, "category": DiagnosticCategory.Error },
"Duplicate identifier '_i'. Compiler uses '_i' to initialize rest parameter.": { "code": 2224, "category": DiagnosticCategory.Error },
"Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters.": { "code": 2225, "category": DiagnosticCategory.Error },
"No best common type exists between '{0}' and '{1}'.": { "code": 2226, "category": DiagnosticCategory.Error },
"No best common type exists between '{0}', '{1}', and '{2}'.": { "code": 2227, "category": DiagnosticCategory.Error },
"Duplicate identifier '{0}'. Compiler reserves name '{1}' in top level scope of an external module.": { "code": 2228, "category": DiagnosticCategory.Error },
"Constraint of a type parameter cannot reference any type parameter from the same type parameter list.": { "code": 2229, "category": DiagnosticCategory.Error },
"Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor.": { "code": 2230, "category": DiagnosticCategory.Error },
"Parameter '{0}' cannot be referenced in its initializer.": { "code": 2231, "category": DiagnosticCategory.Error },
"Duplicate string index signature.": { "code": 2232, "category": DiagnosticCategory.Error },
"Duplicate number index signature.": { "code": 2233, "category": DiagnosticCategory.Error },
"All declarations of an interface must have identical type parameters.": { "code": 2234, "category": DiagnosticCategory.Error },
"Expression resolves to variable declaration '_i' that compiler uses to initialize rest parameter.": { "code": 2235, "category": DiagnosticCategory.Error },
"Neither type '{0}' nor type '{1}' is assignable to the other.": { "code": 2236, "category": DiagnosticCategory.Error },
"Neither type '{0}' nor type '{1}' is assignable to the other:{NL}{2}": { "code": 2237, "category": DiagnosticCategory.Error },
"Duplicate function implementation.": { "code": 2237, "category": DiagnosticCategory.Error },
"Function implementation expected.": { "code": 2238, "category": DiagnosticCategory.Error },
"Function overload name must be '{0}'.": { "code": 2239, "category": DiagnosticCategory.Error },
"Constructor implementation expected.": { "code": 2240, "category": DiagnosticCategory.Error },
"Class name cannot be '{0}'.": { "code": 2241, "category": DiagnosticCategory.Error },
"Interface name cannot be '{0}'.": { "code": 2242, "category": DiagnosticCategory.Error },
"Enum name cannot be '{0}'.": { "code": 2243, "category": DiagnosticCategory.Error },
"A module cannot have multiple export assignments.": { "code": 2244, "category": DiagnosticCategory.Error },
"Export assignment not allowed in module with exported element.": { "code": 2245, "category": DiagnosticCategory.Error },
"A parameter property is only allowed in a constructor implementation.": { "code": 2246, "category": DiagnosticCategory.Error },
"Function overload must be static.": { "code": 2247, "category": DiagnosticCategory.Error },
"Function overload must not be static.": { "code": 2248, "category": DiagnosticCategory.Error },
"Type '{0}' is missing property '{1}' from type '{2}'.": { "code": 4000, "category": DiagnosticCategory.NoPrefix },
"Types of property '{0}' of types '{1}' and '{2}' are incompatible.": { "code": 4001, "category": DiagnosticCategory.NoPrefix },
"Types of property '{0}' of types '{1}' and '{2}' are incompatible:{NL}{3}": { "code": 4002, "category": DiagnosticCategory.NoPrefix },
"Property '{0}' defined as private in type '{1}' is defined as public in type '{2}'.": { "code": 4003, "category": DiagnosticCategory.NoPrefix },
"Property '{0}' defined as public in type '{1}' is defined as private in type '{2}'.": { "code": 4004, "category": DiagnosticCategory.NoPrefix },
"Types '{0}' and '{1}' define property '{2}' as private.": { "code": 4005, "category": DiagnosticCategory.NoPrefix },
"Call signatures of types '{0}' and '{1}' are incompatible.": { "code": 4006, "category": DiagnosticCategory.NoPrefix },
"Call signatures of types '{0}' and '{1}' are incompatible:{NL}{2}": { "code": 4007, "category": DiagnosticCategory.NoPrefix },
"Type '{0}' requires a call signature, but type '{1}' lacks one.": { "code": 4008, "category": DiagnosticCategory.NoPrefix },
"Construct signatures of types '{0}' and '{1}' are incompatible.": { "code": 4009, "category": DiagnosticCategory.NoPrefix },
"Construct signatures of types '{0}' and '{1}' are incompatible:{NL}{2}": { "code": 4010, "category": DiagnosticCategory.NoPrefix },
"Type '{0}' requires a construct signature, but type '{1}' lacks one.": { "code": 4011, "category": DiagnosticCategory.NoPrefix },
"Index signatures of types '{0}' and '{1}' are incompatible.": { "code": 4012, "category": DiagnosticCategory.NoPrefix },
"Index signatures of types '{0}' and '{1}' are incompatible:{NL}{2}": { "code": 4013, "category": DiagnosticCategory.NoPrefix },
"Call signature expects {0} or fewer parameters.": { "code": 4014, "category": DiagnosticCategory.NoPrefix },
"Could not apply type '{0}' to argument {1} which is of type '{2}'.": { "code": 4015, "category": DiagnosticCategory.NoPrefix },
"Class '{0}' defines instance member accessor '{1}', but extended class '{2}' defines it as instance member function.": { "code": 4016, "category": DiagnosticCategory.NoPrefix },
"Class '{0}' defines instance member property '{1}', but extended class '{2}' defines it as instance member function.": { "code": 4017, "category": DiagnosticCategory.NoPrefix },
"Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member accessor.": { "code": 4018, "category": DiagnosticCategory.NoPrefix },
"Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member property.": { "code": 4019, "category": DiagnosticCategory.NoPrefix },
"Types of static property '{0}' of class '{1}' and class '{2}' are incompatible.": { "code": 4020, "category": DiagnosticCategory.NoPrefix },
"Types of static property '{0}' of class '{1}' and class '{2}' are incompatible:{NL}{3}": { "code": 4021, "category": DiagnosticCategory.NoPrefix },
"Type reference cannot refer to container '{0}'.": { "code": 4022, "category": DiagnosticCategory.Error },
"Type reference must refer to type.": { "code": 4023, "category": DiagnosticCategory.Error },
"In enums with multiple declarations only one declaration can omit an initializer for the first enum element.": { "code": 4024, "category": DiagnosticCategory.Error },
" (+ {0} overload(s))": { "code": 4025, "category": DiagnosticCategory.Message },
"Variable declaration cannot have the same name as an import declaration.": { "code": 4026, "category": DiagnosticCategory.Error },
"Signature expected {0} type arguments, got {1} instead.": { "code": 4027, "category": DiagnosticCategory.Error },
"Property '{0}' defined as optional in type '{1}', but is required in type '{2}'.": { "code": 4028, "category": DiagnosticCategory.NoPrefix },
"Types '{0}' and '{1}' originating in infinitely expanding type reference do not refer to same named type.": { "code": 4029, "category": DiagnosticCategory.NoPrefix },
"Types '{0}' and '{1}' originating in infinitely expanding type reference have incompatible type arguments.": { "code": 4030, "category": DiagnosticCategory.NoPrefix },
"Types '{0}' and '{1}' originating in infinitely expanding type reference have incompatible type arguments:{NL}{2}": { "code": 4031, "category": DiagnosticCategory.NoPrefix },
"Named properties '{0}' of types '{1}' and '{2}' are not identical.": { "code": 4032, "category": DiagnosticCategory.NoPrefix },
"Types of string indexer of types '{0}' and '{1}' are not identical.": { "code": 4033, "category": DiagnosticCategory.NoPrefix },
"Types of number indexer of types '{0}' and '{1}' are not identical.": { "code": 4034, "category": DiagnosticCategory.NoPrefix },
"Type of number indexer in type '{0}' is not assignable to string indexer type in type '{1}'.{NL}{2}": { "code": 4035, "category": DiagnosticCategory.NoPrefix },
"Type of property '{0}' in type '{1}' is not assignable to string indexer type in type '{2}'.{NL}{3}": { "code": 4036, "category": DiagnosticCategory.NoPrefix },
"Type of property '{0}' in type '{1}' is not assignable to number indexer type in type '{2}'.{NL}{3}": { "code": 4037, "category": DiagnosticCategory.NoPrefix },
"Static property '{0}' defined as private in type '{1}' is defined as public in type '{2}'.": { "code": 4038, "category": DiagnosticCategory.NoPrefix },
"Static property '{0}' defined as public in type '{1}' is defined as private in type '{2}'.": { "code": 4039, "category": DiagnosticCategory.NoPrefix },
"Types '{0}' and '{1}' define static property '{2}' as private.": { "code": 4040, "category": DiagnosticCategory.NoPrefix },
"Current host does not support '{0}' option.": { "code": 5001, "category": DiagnosticCategory.Error },
"ECMAScript target version '{0}' not supported. Specify a valid target version: '{1}' (default), or '{2}'": { "code": 5002, "category": DiagnosticCategory.Error },
"Argument for '{0}' option must be '{1}' or '{2}'": { "code": 5003, "category": DiagnosticCategory.Error },
"Could not find file: '{0}'.": { "code": 5004, "category": DiagnosticCategory.Error },
"A file cannot have a reference to itself.": { "code": 5006, "category": DiagnosticCategory.Error },
"Cannot resolve referenced file: '{0}'.": { "code": 5007, "category": DiagnosticCategory.Error },
"Cannot find the common subdirectory path for the input files.": { "code": 5009, "category": DiagnosticCategory.Error },
"Emit Error: {0}.": { "code": 5011, "category": DiagnosticCategory.Error },
"Cannot read file '{0}': {1}": { "code": 5012, "category": DiagnosticCategory.Error },
"Unsupported file encoding.": { "code": 5013, "category": DiagnosticCategory.NoPrefix },
"Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.": { "code": 5014, "category": DiagnosticCategory.Error },
"Unsupported locale: '{0}'.": { "code": 5015, "category": DiagnosticCategory.Error },
"Execution Failed.{NL}": { "code": 5016, "category": DiagnosticCategory.Error },
"Invalid call to 'up'": { "code": 5019, "category": DiagnosticCategory.Error },
"Invalid call to 'down'": { "code": 5020, "category": DiagnosticCategory.Error },
"Base64 value '{0}' finished with a continuation bit.": { "code": 5021, "category": DiagnosticCategory.Error },
"Unknown compiler option '{0}'": { "code": 5023, "category": DiagnosticCategory.Error },
"Expected {0} arguments to message, got {1} instead.": { "code": 5024, "category": DiagnosticCategory.Error },
"Expected the message '{0}' to have {1} arguments, but it had {2}": { "code": 5025, "category": DiagnosticCategory.Error },
"Could not delete file '{0}'": { "code": 5034, "category": DiagnosticCategory.Error },
"Could not create directory '{0}'": { "code": 5035, "category": DiagnosticCategory.Error },
"Error while executing file '{0}': ": { "code": 5036, "category": DiagnosticCategory.Error },
"Cannot compile external modules unless the '--module' flag is provided.": { "code": 5037, "category": DiagnosticCategory.Error },
"Option mapRoot cannot be specified without specifying sourcemap option.": { "code": 5038, "category": DiagnosticCategory.Error },
"Option sourceRoot cannot be specified without specifying sourcemap option.": { "code": 5039, "category": DiagnosticCategory.Error },
"Options mapRoot and sourceRoot cannot be specified without specifying sourcemap option.": { "code": 5040, "category": DiagnosticCategory.Error },
"Option '{0}' specified without '{1}'": { "code": 5041, "category": DiagnosticCategory.Error },
"'codepage' option not supported on current platform.": { "code": 5042, "category": DiagnosticCategory.Error },
"Concatenate and emit output to single file.": { "code": 6001, "category": DiagnosticCategory.Message },
"Generates corresponding {0} file.": { "code": 6002, "category": DiagnosticCategory.Message },
"Specifies the location where debugger should locate map files instead of generated locations.": { "code": 6003, "category": DiagnosticCategory.Message },
"Specifies the location where debugger should locate TypeScript files instead of source locations.": { "code": 6004, "category": DiagnosticCategory.Message },
"Watch input files.": { "code": 6005, "category": DiagnosticCategory.Message },
"Redirect output structure to the directory.": { "code": 6006, "category": DiagnosticCategory.Message },
"Do not emit comments to output.": { "code": 6009, "category": DiagnosticCategory.Message },
"Skip resolution and preprocessing.": { "code": 6010, "category": DiagnosticCategory.Message },
"Specify ECMAScript target version: '{0}' (default), or '{1}'": { "code": 6015, "category": DiagnosticCategory.Message },
"Specify module code generation: '{0}' or '{1}'": { "code": 6016, "category": DiagnosticCategory.Message },
"Print this message.": { "code": 6017, "category": DiagnosticCategory.Message },
"Print the compiler's version: {0}": { "code": 6019, "category": DiagnosticCategory.Message },
"Allow use of deprecated '{0}' keyword when referencing an external module.": { "code": 6021, "category": DiagnosticCategory.Message },
"Specify locale for errors and messages. For example '{0}' or '{1}'": { "code": 6022, "category": DiagnosticCategory.Message },
"Syntax: {0}": { "code": 6023, "category": DiagnosticCategory.Message },
"options": { "code": 6024, "category": DiagnosticCategory.Message },
"file1": { "code": 6025, "category": DiagnosticCategory.Message },
"Examples:": { "code": 6026, "category": DiagnosticCategory.Message },
"Options:": { "code": 6027, "category": DiagnosticCategory.Message },
"Insert command line options and files from a file.": { "code": 6030, "category": DiagnosticCategory.Message },
"Version {0}": { "code": 6029, "category": DiagnosticCategory.Message },
"Use the '{0}' flag to see options.": { "code": 6031, "category": DiagnosticCategory.Message },
"{NL}Recompiling ({0}):": { "code": 6032, "category": DiagnosticCategory.Message },
"STRING": { "code": 6033, "category": DiagnosticCategory.Message },
"KIND": { "code": 6034, "category": DiagnosticCategory.Message },
"file2": { "code": 6035, "category": DiagnosticCategory.Message },
"VERSION": { "code": 6036, "category": DiagnosticCategory.Message },
"LOCATION": { "code": 6037, "category": DiagnosticCategory.Message },
"DIRECTORY": { "code": 6038, "category": DiagnosticCategory.Message },
"NUMBER": { "code": 6039, "category": DiagnosticCategory.Message },
"Specify the codepage to use when opening source files.": { "code": 6040, "category": DiagnosticCategory.Message },
"Additional locations:": { "code": 6041, "category": DiagnosticCategory.Message },
"This version of the Javascript runtime does not support the '{0}' function.": { "code": 7000, "category": DiagnosticCategory.Error },
"Unknown rule.": { "code": 7002, "category": DiagnosticCategory.Error },
"Invalid line number ({0})": { "code": 7003, "category": DiagnosticCategory.Error },
"Warn on expressions and declarations with an implied 'any' type.": { "code": 7004, "category": DiagnosticCategory.Message },
"Variable '{0}' implicitly has an 'any' type.": { "code": 7005, "category": DiagnosticCategory.Error },
"Parameter '{0}' of '{1}' implicitly has an 'any' type.": { "code": 7006, "category": DiagnosticCategory.Error },
"Parameter '{0}' of function type implicitly has an 'any' type.": { "code": 7007, "category": DiagnosticCategory.Error },
"Member '{0}' of object type implicitly has an 'any' type.": { "code": 7008, "category": DiagnosticCategory.Error },
"'new' expression, which lacks a constructor signature, implicitly has an 'any' type.": { "code": 7009, "category": DiagnosticCategory.Error },
"'{0}', which lacks return-type annotation, implicitly has an 'any' return type.": { "code": 7010, "category": DiagnosticCategory.Error },
"Function expression, which lacks return-type annotation, implicitly has an 'any' return type.": { "code": 7011, "category": DiagnosticCategory.Error },
"Parameter '{0}' of lambda function implicitly has an 'any' type.": { "code": 7012, "category": DiagnosticCategory.Error },
"Constructor signature, which lacks return-type annotation, implicitly has an 'any' return type.": { "code": 7013, "category": DiagnosticCategory.Error },
"Lambda Function, which lacks return-type annotation, implicitly has an 'any' return type.": { "code": 7014, "category": DiagnosticCategory.Error },
"Array Literal implicitly has an 'any' type from widening.": { "code": 7015, "category": DiagnosticCategory.Error },
"'{0}', which lacks 'get' accessor and parameter type annotation on 'set' accessor, implicitly has an 'any' type.": { "code": 7016, "category": DiagnosticCategory.Error },
"Index signature of object type implicitly has an 'any' type.": { "code": 7017, "category": DiagnosticCategory.Error },
"Object literal's property '{0}' implicitly has an 'any' type from widening.": { "code": 7018, "category": DiagnosticCategory.Error },
};
}

Some files were not shown because too many files have changed in this diff Show More