Skip to content

Commit ae17c61

Browse files
Merge pull request microsoft#3565 from Microsoft/incrementalParserReuse
Fix incremental parsing issue.
2 parents 3cb44fb + 38a54bc commit ae17c61

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/compiler/parser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,12 +1491,20 @@ namespace ts {
14911491
switch (node.kind) {
14921492
case SyntaxKind.Constructor:
14931493
case SyntaxKind.IndexSignature:
1494-
case SyntaxKind.MethodDeclaration:
14951494
case SyntaxKind.GetAccessor:
14961495
case SyntaxKind.SetAccessor:
14971496
case SyntaxKind.PropertyDeclaration:
14981497
case SyntaxKind.SemicolonClassElement:
14991498
return true;
1499+
case SyntaxKind.MethodDeclaration:
1500+
// Method declarations are not necessarily reusable. An object-literal
1501+
// may have a method calls "constructor(...)" and we must reparse that
1502+
// into an actual .ConstructorDeclaration.
1503+
let methodDeclaration = <MethodDeclaration>node;
1504+
let nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier &&
1505+
(<Identifier>methodDeclaration.name).originalKeywordKind === SyntaxKind.ConstructorKeyword;
1506+
1507+
return !nameIsConstructor;
15001508
}
15011509
}
15021510

tests/cases/unittests/incrementalParser.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,24 @@ module m3 { }\
713713
compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 4);
714714
});
715715

716+
it('Do not move constructors from class to object-literal.', () => {
717+
var source = "class C { public constructor() { } public constructor() { } public constructor() { } }"
718+
719+
var oldText = ScriptSnapshot.fromString(source);
720+
var newTextAndChange = withChange(oldText, 0, "class C".length, "var v =");
721+
722+
compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 0);
723+
});
724+
725+
it('Do not move methods called "constructor" from object literal to class', () => {
726+
var source = "var v = { public constructor() { } public constructor() { } public constructor() { } }"
727+
728+
var oldText = ScriptSnapshot.fromString(source);
729+
var newTextAndChange = withChange(oldText, 0, "var v =".length, "class C");
730+
731+
compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 0);
732+
});
733+
716734
it('Moving index signatures from class to interface',() => {
717735
var source = "class C { public [a: number]: string; public [a: number]: string; public [a: number]: string }"
718736

0 commit comments

Comments
 (0)