8000 Check the text change range before and after we expand it. · nycdotnet/TypeScript@ad7c77e · GitHub
[go: up one dir, main page]

Skip to content

Commit ad7c77e

Browse files
Check the text change range before and after we expand it.
1 parent 1a17fd1 commit ad7c77e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/compiler/parser.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -683,24 +683,12 @@ module ts {
683683
}
684684
}
685685

686-
687-
// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
688-
// indicates what changed between the 'text' that this SourceFile has and the 'newText'.
689-
// The SourceFile will be created with the compiler attempting to reuse as many nodes from
690-
// this file as possible.
691-
//
692-
// Note: this function mutates nodes from this SourceFile. That means any existing nodes
693-
// from this SourceFile that are being held onto may change as a result (including
694-
// becoming detached from any SourceFile). It is recommended that this SourceFile not
695-
// be used once 'update' is called on it.
696-
export function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile {
697-
aggressiveChecks = aggressiveChecks || Debug.shouldAssert(AssertionLevel.Aggressive);
698-
686+
function checkChangeRange(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks: boolean) {
699687
var oldText = sourceFile.text;
700688
if (textChangeRange) {
701689
Debug.assert((oldText.length - textChangeRange.span.length + textChangeRange.newLength) === newText.length);
702690

703-
if (Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
691+
if (aggressiveChecks || Debug.shouldAssert(AssertionLevel.VeryAggressive)) {
704692
var oldTextPrefix = oldText.substr(0, textChangeRange.span.start);
705693
var newTextPrefix = newText.substr(0, textChangeRange.span.start);
706694
Debug.assert(oldTextPrefix === newTextPrefix);
@@ -710,7 +698,21 @@ module ts {
710698
Debug.assert(oldTextSuffix === newTextSuffix);
711699
}
712700
}
701+
}
713702

703+
// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
704+
// indicates what changed between the 'text' that this SourceFile has and the 'newText'.
705+
// The SourceFile will be created with the compiler attempting to reuse as many nodes from
706+
// this file as possible.
707+
//
708+
// Note: this function mutates nodes from this SourceFile. That means any existing nodes
709+
// from this SourceFile that are being held onto may change as a result (including
710+
// becoming detached from any SourceFile). It is recommended that this SourceFile not
711+
// be used once 'update' is called on it.
712+
export function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile {
713+
aggressiveChecks = aggressiveChecks || Debug.shouldAssert(AssertionLevel.Aggressive);
714+
715+
checkChangeRange(sourceFile, newText, textChangeRange, aggressiveChecks);
714716
if (textChangeRangeIsUnchanged(textChangeRange)) {
715717
// if the text didn't change, then we can just return our current source file as-is.
716718
return sourceFile;
@@ -722,11 +724,13 @@ module ts {
722724
return parseSourceFile(sourceFile.fileName, newText, sourceFile.languageVersion,/*syntaxCursor*/ undefined, /*setNodeParents*/ true)
723725
}
724726

727+
var oldText = sourceFile.text;
725728
var syntaxCursor = createSyntaxCursor(sourceFile);
726729

727730
// Make the actual change larger so that we know to reparse anything whose lookahead
728731
// might have intersected the change.
729732
var changeRange = extendToAffectedRange(sourceFile, textChangeRange);
733+
checkChangeRange(sourceFile, newText, changeRange, aggressiveChecks);
730734

731735
// Ensure that extending the affected range only moved the start of the change range
732736
// earlier in the file.

0 commit comments

Comments
 (0)
0