Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e76ccb8

Browse files
Merge pull request microsoft#1535 from Microsoft/mergeMarkers1
Provide better error recovery when we encounter merge markers in the source.
2 parents c9594c3 + 12cb284 commit e76ccb8

File tree

12 files changed

+325
-155
lines changed

12 files changed

+325
-155
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ module ts {
2323

2424
export interface StringSet extends Map<any> { }
2525

26-
export function forEach<T, U>(array: T[], callback: (element: T) => U): U {
26+
export function forEach<T, U>(array: T[], callback: (element: T, index: number) => U): U {
2727
if (array) {
2828
for (var i = 0, len = array.length; i < len; i++) {
29-
var result = callback(array[i]);
29+
var result = callback(array[i], i);
3030
if (result) {
3131
return result;
3232
}

src/compiler/scanner.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ module ts {
77
(message: DiagnosticMessage, length: number): void;
88
}
99

10-
export interface CommentCallback {
11-
(pos: number, end: number): void;
12-
}
13-
1410
export interface Scanner {
1511
getStartPos(): number;
1612
getToken(): SyntaxKind;
@@ -396,8 +392,10 @@ module ts {
396392
var mergeConflictMarkerLength = "<<<<<<<".length;
397393

398394
function isConflictMarkerTrivia(text: string, pos: number) {
395+
Debug.assert(pos >= 0);
396+
399397
// Conflict markers must be at the start of a line.
400-
if (pos > 0 && isLineBreak(text.charCodeAt(pos - 1))) {
398+
if (pos === 0 || isLineBreak(text.charCodeAt(pos - 1))) {
401399
var ch = text.charCodeAt(pos);
402400

403401
if ((pos + mergeConflictMarkerLength) < text.length) {
@@ -415,10 +413,31 @@ module ts {
415413
return false;
416414
}
417415

418-
function scanConflictMarkerTrivia(text: string, pos: number) {
416+
function scanConflictMarkerTrivia(text: string, pos: number, error?: ErrorCallback) {
417+
if (error) {
418+
error(Diagnostics.Merge_conflict_marker_encountered, mergeConflictMarkerLength);
419+
}
420+
421+
var ch = text.charCodeAt(pos);
419422
var len = text.length;
420-
while (pos < len && !isLineBreak(text.charCodeAt(pos))) {
421-
pos++;
423+
424+
if (ch === CharacterCodes.lessThan || ch === CharacterCodes.greaterThan) {
425+
while (pos < len && !isLineBreak(text.charCodeAt(pos))) {
426+
pos++;
427+
}
428+
}
429+
else {
430+
Debug.assert(ch === CharacterCodes.equals);
431+
// Consume everything from the start of the mid-conlict marker to the start of the next
432+
// end-conflict marker.
433+
while (pos < len) {
434+
var ch = text.charCodeAt(pos);
435+
if (ch === CharacterCodes.greaterThan && isConflictMarkerTrivia(text, pos)) {
436+
break;
437+
}
438+
439+
pos++;
440+
}
422441
}
423442

424443
return pos;
@@ -1057,8 +1076,7 @@ module ts {
10571076
return pos++, token = SyntaxKind.SemicolonToken;
10581077
case CharacterCodes.lessThan:
10591078
if (isConflictMarkerTrivia(text, pos)) {
1060-
mergeConflictError();
1061-
pos = scanConflictMarkerTrivia(text, pos);
1079+
pos = scanConflictMarkerTrivia(text, pos, error);
10621080
if (skipTrivia) {
10631081
continue;
10641082
}
@@ -1079,8 +1097,7 @@ module ts {
10791097
return pos++, token = SyntaxKind.LessThanToken;
10801098
case CharacterCodes.equals:
10811099
if (isConflictMarkerTrivia(text, pos)) {
1082-
mergeConflictError();
1083-
pos = scanConflictMarkerTrivia(text, pos);
1100+
pos = scanConflictMarkerTrivia(text, pos, error);
10841101
if (skipTrivia) {
10851102
continue;
10861103
}
@@ -1101,8 +1118,7 @@ module ts {
11011118
return pos++, token = SyntaxKind.EqualsToken;
11021119
case CharacterCodes.greaterThan:
11031120
if (isConflictMarkerTrivia(text, pos)) {
1104-
mergeConflictError();
1105-
pos = scanConflictMarkerTrivia(text, pos);
1121+
pos = scanConflictMarkerTrivia(text, pos, error);
11061122
if (skipTrivia) {
11071123
continue;
11081124
}
@@ -1171,10 +1187,6 @@ module ts {
11711187
}
11721188
}
11731189

1174-
function mergeConflictError() {
1175-
error(Diagnostics.Merge_conflict_marker_encountered, mergeConflictMarkerLength);
1176-
}
1177-
11781190
function reScanGreaterToken(): SyntaxKind {
11791191
if (token === SyntaxKind.GreaterThanToken) {
11801192
if (text.charCodeAt(pos) === CharacterCodes.greaterThan) {

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module ts {
1616
MultiLineCommentTrivia,
1717
NewLineTrivia,
1818
WhitespaceTrivia,
19+
// We detect and provide better error recovery when we encounter a git merge marker. This
20+
// allows us to edit files with git-conflict markers in them in a much more pleasant manner.
1921
ConflictMarkerTrivia,
2022
// Literals
2123
NumericLiteral,
@@ -272,8 +274,6 @@ module ts {
272274
LastLiteralToken = NoSubstitutionTemplateLiteral,
273275
FirstTemplateToken = NoSubstitutionTemplateLiteral,
274276
LastTemplateToken = TemplateTail,
275-
FirstOperator = SemicolonToken,
276-
LastOperator = CaretEqualsToken,
277277
FirstBinaryOperator = LessThanToken,
278278
LastBinaryOperator = CaretEqualsToken,
279279
FirstNode = QualifiedName,

src/services/formatting/tokenRange.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ module ts.formatting {
125125
static Any: TokenRange = TokenRange.AllTokens();
126126
static AnyIncludingMultilineComments = TokenRange.FromTokens(TokenRange.Any.GetTokens().concat([SyntaxKind.MultiLineCommentTrivia]));
127127
static Keywords = TokenRange.FromRange(SyntaxKind.FirstKeyword, SyntaxKind.LastKeyword);
128-
static Operators = TokenRange.FromRange(SyntaxKind.FirstOperator, SyntaxKind.LastOperator);
129128
static BinaryOperators = TokenRange.FromRange(SyntaxKind.FirstBinaryOperator, SyntaxKind.LastBinaryOperator);
130129
static BinaryKeywordOperators = TokenRange.FromTokens([SyntaxKind.InKeyword, SyntaxKind.InstanceOfKeyword]);
131-
static ReservedKeywords = TokenRange.FromRange(SyntaxKind.FirstFutureReservedWord, SyntaxKind.LastFutureReservedWord);
132130
static UnaryPrefixOperators = TokenRange.FromTokens([SyntaxKind.PlusPlusToken, SyntaxKind.MinusMinusToken, SyntaxKind.TildeToken, SyntaxKind.ExclamationToken]);
133131
static UnaryPrefixExpressions = TokenRange.FromTokens([SyntaxKind.NumericLiteral, SyntaxKind.Identifier, SyntaxKind.OpenParenToken, SyntaxKind.OpenBracketToken, SyntaxKind.OpenBraceToken, SyntaxKind.ThisKeyword, SyntaxKind.NewKeyword]);
134132
static UnaryPreincrementExpressions = TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.OpenParenToken, SyntaxKind.ThisKeyword, SyntaxKind.NewKeyword]);

0 commit comments

Comments
 (0)