Add additional aggressive checks during incremental parsing.

This commit is contained in:
Cyrus Najmabadi 2015-02-08 16:10:16 -08:00
parent ff31b96533
commit f1cb97b692
11 changed files with 80 additions and 34 deletions

View file

@ -357,21 +357,41 @@ module ts {
forEachChild(sourceFile, walk);
}
function moveElementEntirelyPastChangeRange(element: IncrementalElement, delta: number) {
function shouldCheckNode(node: Node) {
switch (node.kind) {
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.Identifier:
return true;
}
return false;
}
function moveElementEntirelyPastChangeRange(element: IncrementalElement, delta: number, oldText: string, newText: string, aggressiveChecks: boolean) {
if (element.length) {
visitArray(<IncrementalNodeArray>element);
}
else {
visitNode(<IncrementalNode>element);
}
return;
function visitNode(node: IncrementalNode) {
if (aggressiveChecks && shouldCheckNode(node)) {
var text = oldText.substring(node.pos, node.end);
}
// Ditch any existing LS children we may have created. This way we can avoid
// moving them forward.
node._children = undefined;
node.pos += delta;
node.end += delta;
if (aggressiveChecks && shouldCheckNode(node)) {
Debug.assert(text === newText.substring(node.pos, node.end));
}
forEachChild(node, visitNode, visitArray);
}
@ -459,14 +479,24 @@ module ts {
}
}
function updateTokenPositionsAndMarkElements(node: IncrementalNode, changeStart: number, changeRangeOldEnd: number, changeRangeNewEnd: number, delta: number): void {
function updateTokenPositionsAndMarkElements(
node: IncrementalNode,
changeStart: number,
changeRangeOldEnd: number,
changeRangeNewEnd: number,
delta: number,
oldText: string,
newText: string,
aggressiveChecks: boolean): void {
visitNode(node);
return;
function visitNode(child: IncrementalNode) {
if (child.pos > changeRangeOldEnd) {
// Node is entirely past the change range. We need to move both its pos and
// end, forward or backward appropriately.
moveElementEntirelyPastChangeRange(child, delta);
moveElementEntirelyPastChangeRange(child, delta, oldText, newText, aggressiveChecks);
return;
}
@ -490,7 +520,7 @@ module ts {
if (array.pos > changeRangeOldEnd) {
// Array is entirely after the change range. We need to move it, and move any of
// its children.
moveElementEntirelyPastChangeRange(array, delta);
moveElementEntirelyPastChangeRange(array, delta, oldText, newText, aggressiveChecks);
}
else {
// Check if the element intersects the change range. If it does, then it is not
@ -513,7 +543,6 @@ module ts {
}
}
function extendToAffectedRange(sourceFile: SourceFile, changeRange: TextChangeRange): TextChangeRange {
// Consider the following code:
// void foo() { /; }
@ -650,7 +679,9 @@ module ts {
// from this SourceFile that are being held onto may change as a result (including
// becoming detached from any SourceFile). It is recommended that this SourceFile not
// be used once 'update' is called on it.
export function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile {
export function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile {
aggressiveChecks = aggressiveChecks || Debug.shouldAssert(AssertionLevel.Aggressive);
if (textChangeRangeIsUnchanged(textChangeRange)) {
// if the text didn't change, then we can just return our current source file as-is.
return sourceFile;
@ -662,12 +693,19 @@ module ts {
return parseSourceFile(sourceFile.fileName, newText, sourceFile.languageVersion,/*syntaxCursor*/ undefined, /*setNodeParents*/ true)
}
var oldText = sourceFile.text;
var syntaxCursor = createSyntaxCursor(sourceFile);
// Make the actual change larger so that we know to reparse anything whose lookahead
// might have intersected the change.
var changeRange = extendToAffectedRange(sourceFile, textChangeRange);
// Ensure that extending the affected range only moved the start of the change range
// earlier in the file.
Debug.assert(changeRange.span.start <= textChangeRange.span.start);
Debug.assert(textSpanEnd(changeRange.span) === textSpanEnd(textChangeRange.span));
Debug.assert(textSpanEnd(textChangeRangeNewSpan(changeRange)) === textSpanEnd(textChangeRangeNewSpan(textChangeRange)));
// The is the amount the nodes after the edit range need to be adjusted. It can be
// positive (if the edit added characters), negative (if the edit deleted characters)
// or zero (if this was a pure overwrite with nothing added/removed).
@ -693,7 +731,7 @@ module ts {
// Also, mark any syntax elements that intersect the changed span. We know, up front,
// that we cannot reuse these elements.
updateTokenPositionsAndMarkElements(<IncrementalNode><Node>sourceFile,
changeRange.span.start, textSpanEnd(changeRange.span), textSpanEnd(textChangeRangeNewSpan(changeRange)), delta);
changeRange.span.start, textSpanEnd(changeRange.span), textSpanEnd(textChangeRangeNewSpan(changeRange)), delta, oldText, newText, aggressiveChecks);
// Now that we've set up our internal incremental state just proceed and parse the
// source file in the normal fashion. When possible the parser will retrieve and

View file

@ -115,7 +115,7 @@ module Harness.LanguageService {
version: string,
textChangeRange: ts.TextChangeRange
): ts.SourceFile {
var result = ts.updateLanguageServiceSourceFile(document, scriptSnapshot, version, textChangeRange);
var result = ts.updateLanguageServiceSourceFile(document, scriptSnapshot, version, textChangeRange, /*aggressiveChecks:*/ true);
Utils.assertInvariants(result, /*parent:*/ undefined);
return result;
}

View file

@ -1624,7 +1624,7 @@ module ts {
export var disableIncrementalParsing = false;
export function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile {
export function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile {
if (textChangeRange && Debug.shouldAssert(AssertionLevel.Normal)) {
var oldText = sourceFile.scriptSnapshot;
var newText = scriptSnapshot;
@ -1648,7 +1648,7 @@ module ts {
if (version !== sourceFile.version) {
// Once incremental parsing is ready, then just call into this function.
if (!disableIncrementalParsing) {
var newSourceFile = updateSourceFile(sourceFile, scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange);
var newSourceFile = updateSourceFile(sourceFile, scriptSnapshot.getText(0, scriptSnapshot.getLength()), textChangeRange, aggressiveChecks);
setSourceFileFields(newSourceFile, scriptSnapshot, version);
// after incremental parsing nameTable might not be up-to-date
// drop it so it can be lazily recreated later

View file

@ -1404,7 +1404,7 @@ declare module "typescript" {
function createNode(kind: SyntaxKind): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function modifierToFlag(token: SyntaxKind): NodeFlags;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function isEvalOrArgumentsIdentifier(node: Node): boolean;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
function isLeftHandSideExpression(expr: Expression): boolean;
@ -1895,7 +1895,7 @@ declare module "typescript" {
}
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile;
var disableIncrementalParsing: boolean;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createDocumentRegistry(): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;

View file

@ -4484,13 +4484,14 @@ declare module "typescript" {
>SyntaxKind : SyntaxKind
>NodeFlags : NodeFlags
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange) => SourceFile
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>newText : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function isEvalOrArgumentsIdentifier(node: Node): boolean;
@ -5895,8 +5896,8 @@ declare module "typescript" {
var disableIncrementalParsing: boolean;
>disableIncrementalParsing : boolean
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange) => SourceFile
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>scriptSnapshot : IScriptSnapshot
@ -5904,6 +5905,7 @@ declare module "typescript" {
>version : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function createDocumentRegistry(): DocumentRegistry;

View file

@ -1435,7 +1435,7 @@ declare module "typescript" {
function createNode(kind: SyntaxKind): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function modifierToFlag(token: SyntaxKind): NodeFlags;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function isEvalOrArgumentsIdentifier(node: Node): boolean;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
function isLeftHandSideExpression(expr: Expression): boolean;
@ -1926,7 +1926,7 @@ declare module "typescript" {
}
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile;
var disableIncrementalParsing: boolean;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createDocumentRegistry(): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;

View file

@ -4628,13 +4628,14 @@ declare module "typescript" {
>SyntaxKind : SyntaxKind
>NodeFlags : NodeFlags
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange) => SourceFile
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>newText : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function isEvalOrArgumentsIdentifier(node: Node): boolean;
@ -6039,8 +6040,8 @@ declare module "typescript" {
var disableIncrementalParsing: boolean;
>disableIncrementalParsing : boolean
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange) => SourceFile
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>scriptSnapshot : IScriptSnapshot
@ -6048,6 +6049,7 @@ declare module "typescript" {
>version : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function createDocumentRegistry(): DocumentRegistry;

View file

@ -1436,7 +1436,7 @@ declare module "typescript" {
function createNode(kind: SyntaxKind): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function modifierToFlag(token: SyntaxKind): NodeFlags;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function isEvalOrArgumentsIdentifier(node: Node): boolean;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
function isLeftHandSideExpression(expr: Expression): boolean;
@ -1927,7 +1927,7 @@ declare module "typescript" {
}
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile;
var disableIncrementalParsing: boolean;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createDocumentRegistry(): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;

View file

@ -4580,13 +4580,14 @@ declare module "typescript" {
>SyntaxKind : SyntaxKind
>NodeFlags : NodeFlags
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange) => SourceFile
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>newText : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function isEvalOrArgumentsIdentifier(node: Node): boolean;
@ -5991,8 +5992,8 @@ declare module "typescript" {
var disableIncrementalParsing: boolean;
>disableIncrementalParsing : boolean
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange) => SourceFile
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>scriptSnapshot : IScriptSnapshot
@ -6000,6 +6001,7 @@ declare module "typescript" {
>version : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function createDocumentRegistry(): DocumentRegistry;

View file

@ -1473,7 +1473,7 @@ declare module "typescript" {
function createNode(kind: SyntaxKind): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function modifierToFlag(token: SyntaxKind): NodeFlags;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function isEvalOrArgumentsIdentifier(node: Node): boolean;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
function isLeftHandSideExpression(expr: Expression): boolean;
@ -1964,7 +1964,7 @@ declare module "typescript" {
}
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile;
var disableIncrementalParsing: boolean;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createDocumentRegistry(): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;

View file

@ -4753,13 +4753,14 @@ declare module "typescript" {
>SyntaxKind : SyntaxKind
>NodeFlags : NodeFlags
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange) => SourceFile
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateSourceFile : (sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>newText : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function isEvalOrArgumentsIdentifier(node: Node): boolean;
@ -6164,8 +6165,8 @@ declare module "typescript" {
var disableIncrementalParsing: boolean;
>disableIncrementalParsing : boolean
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange) => SourceFile
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
>updateLanguageServiceSourceFile : (sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean) => SourceFile
>sourceFile : SourceFile
>SourceFile : SourceFile
>scriptSnapshot : IScriptSnapshot
@ -6173,6 +6174,7 @@ declare module "typescript" {
>version : string
>textChangeRange : TextChangeRange
>TextChangeRange : TextChangeRange
>aggressiveChecks : boolean
>SourceFile : SourceFile
function createDocumentRegistry(): DocumentRegistry;