mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 19:27:35 -06:00
take token kind from the tree in case if token kind from scanner is different
This commit is contained in:
parent
9f0e85cb8e
commit
7358b0f7d3
@ -146,7 +146,7 @@ module ts.formatting {
|
||||
if (lastTokenInfo && expectedScanAction === lastScanAction) {
|
||||
// readTokenInfo was called before with the same expected scan action.
|
||||
// No need to re-scan text, return existing 'lastTokenInfo'
|
||||
return lastTokenInfo;
|
||||
return fixTokenKind(lastTokenInfo, n);
|
||||
}
|
||||
|
||||
if (scanner.getStartPos() !== savedPos) {
|
||||
@ -207,11 +207,13 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
return lastTokenInfo = {
|
||||
lastTokenInfo = {
|
||||
leadingTrivia: leadingTrivia,
|
||||
trailingTrivia: trailingTrivia,
|
||||
token: token
|
||||
}
|
||||
|
||||
return fixTokenKind(lastTokenInfo, n);
|
||||
}
|
||||
|
||||
function isOnToken(): boolean {
|
||||
@ -219,5 +221,16 @@ module ts.formatting {
|
||||
var startPos = (lastTokenInfo && lastTokenInfo.token.pos) || scanner.getStartPos();
|
||||
return startPos < endPos && current !== SyntaxKind.EndOfFileToken && !isTrivia(current);
|
||||
}
|
||||
|
||||
// when containing node in the tree is token
|
||||
// but its kind differs from the kind that was returned by the scanner,
|
||||
// then kind needs to be fixed. This might happen in cases
|
||||
// when parser interprets token differently, i.e keyword treated as identifier
|
||||
function fixTokenKind(tokenInfo: TokenInfo, container: Node): TokenInfo {
|
||||
if (isToken(container) && tokenInfo.token.kind !== container.kind) {
|
||||
tokenInfo.token.kind = container.kind;
|
||||
}
|
||||
return tokenInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
tests/cases/fourslash/formattingKeywordAsIdentifier.ts
Normal file
7
tests/cases/fourslash/formattingKeywordAsIdentifier.ts
Normal file
@ -0,0 +1,7 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////declare var module/*1*/
|
||||
|
||||
goTo.marker("1");
|
||||
edit.insert(";");
|
||||
verify.currentLineContentIs("declare var module;");
|
||||
Loading…
x
Reference in New Issue
Block a user