Skip to content

Commit 7358b0f

Browse files
committed
take token kind from the tree in case if token kind from scanner is different
1 parent 9f0e85c commit 7358b0f

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/services/formatting/formattingScanner.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module ts.formatting {
146146
if (lastTokenInfo && expectedScanAction === lastScanAction) {
147147
// readTokenInfo was called before with the same expected scan action.
148148
// No need to re-scan text, return existing 'lastTokenInfo'
149-
return lastTokenInfo;
149+
return fixTokenKind(lastTokenInfo, n);
150150
}
151151

152152
if (scanner.getStartPos() !== savedPos) {
@@ -207,17 +207,30 @@ module ts.formatting {
207207
}
208208
}
209209

210-
return lastTokenInfo = {
210+
lastTokenInfo = {
211211
leadingTrivia: leadingTrivia,
212212
trailingTrivia: trailingTrivia,
213213
token: token
214214
}
215+
216+
return fixTokenKind(lastTokenInfo, n);
215217
}
216218

217219
function isOnToken(): boolean {
218220
var current = (lastTokenInfo && lastTokenInfo.token.kind) || scanner.getToken();
219221
var startPos = (lastTokenInfo && lastTokenInfo.token.pos) || scanner.getStartPos();
220222
return startPos < endPos && current !== SyntaxKind.EndOfFileToken && !isTrivia(current);
221223
}
224+
225+
// when containing node in the tree is token
226+
// but its kind differs from the kind that was returned by the scanner,
227+
// then kind needs to be fixed. This might happen in cases
228+
// when parser interprets token differently, i.e keyword treated as identifier
229+
function fixTokenKind(tokenInfo: TokenInfo, container: Node): TokenInfo {
230+
if (isToken(container) && tokenInfo.token.kind !== container.kind) {
231+
tokenInfo.token.kind = container.kind;
232+
}
233+
return tokenInfo;
234+
}
222235
}
223236
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////declare var module/*1*/
4+
5+
goTo.marker("1");
6+
edit.insert(";");
7+
verify.currentLineContentIs("declare var module;");

0 commit comments

Comments
 (0)