Skip to content

Commit 794b0f3

Browse files
committed
Only slice once, and tokenise leading * as whitespace
From my other experiment. Maybe together they'll be fast!
1 parent 6d792e7 commit 794b0f3

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/compiler/parser.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,9 @@ namespace Parser {
21612161
return nextTokenWithoutCheck();
21622162
}
21632163

2164+
function nextTokenJSDocInitialIndent(): JSDocSyntaxKind {
2165+
return currentToken = scanner.scanJSDocInitialIndent();
2166+
}
21642167
function nextTokenJSDoc(): JSDocSyntaxKind {
21652168
return currentToken = scanner.scanJsDocToken();
21662169
}
@@ -8641,7 +8644,7 @@ namespace Parser {
86418644
comments.push(scanner.getTokenText());
86428645
}
86438646
else if (margin !== undefined && indent + whitespaceLength > margin) {
8644-
comments.push(scanner.getTokenText().slice(margin - indent));
8647+
comments.push(scanner.getText().slice(scanner.getTokenPos() + margin - indent, scanner.getTextPos()));
86458648
}
86468649
indent += whitespaceLength;
86478650
break;
@@ -8671,7 +8674,12 @@ namespace Parser {
86718674
pushComment(scanner.getTokenText());
86728675
break;
86738676
}
8674-
nextTokenJSDoc();
8677+
if (state === JSDocState.BeginningOfLine) {
8678+
nextTokenJSDocInitialIndent();
8679+
}
8680+
else {
8681+
nextTokenJSDoc();
8682+
}
86758683
}
86768684
removeTrailingWhitespace(comments);
86778685
if (parts.length && comments.length) {
@@ -8891,7 +8899,7 @@ namespace Parser {
88918899
const whitespaceLength = scanner.getTextPos() - scanner.getTokenPos();
88928900
// if the whitespace crosses the margin, take only the whitespace that passes the margin
88938901
if (margin !== undefined && indent + whitespaceLength > margin) {
8894-
comments.push(scanner.getTokenText().slice(margin - indent));
8902+
comments.push(scanner.getText().slice(scanner.getTokenPos() + margin - indent, scanner.getTextPos()));
88958903
}
88968904
indent += whitespaceLength;
88978905
}
@@ -8937,7 +8945,12 @@ namespace Parser {
89378945
break;
89388946
}
89398947
previousWhitespace = token() === SyntaxKind.WhitespaceTrivia;
8940-
tok = nextTokenJSDoc();
8948+
if (state === JSDocState.BeginningOfLine) {
8949+
tok = nextTokenJSDocInitialIndent();
8950+
}
8951+
else {
8952+
tok = nextTokenJSDoc();
8953+
}
89418954
}
89428955

89438956
removeLeadingNewlines(comments);

src/compiler/scanner.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export interface Scanner {
7474
reScanQuestionToken(): SyntaxKind;
7575
reScanInvalidIdentifier(): SyntaxKind;
7676
scanJsxToken(): JsxTokenSyntaxKind;
77+
/** @internal */
78+
scanJSDocInitialIndent(): JSDocSyntaxKind;
7779
scanJsDocToken(): JSDocSyntaxKind;
7880
scan(): SyntaxKind;
7981

@@ -1019,6 +1021,7 @@ export function createScanner(languageVersion: ScriptTarget,
10191021
reScanQuestionToken,
10201022
reScanInvalidIdentifier,
10211023
scanJsxToken,
1024+
scanJSDocInitialIndent,
10221025
scanJsDocToken,
10231026
scan,
10241027
getText,
@@ -2455,6 +2458,22 @@ export function createScanner(languageVersion: ScriptTarget,
24552458
return scanJsxAttributeValue();
24562459
}
24572460

2461+
function scanJSDocInitialIndent(): JSDocSyntaxKind {
2462+
startPos = tokenPos = pos;
2463+
tokenFlags = TokenFlags.None;
2464+
if (pos >= end) {
2465+
return token = SyntaxKind.EndOfFileToken;
2466+
}
2467+
for (let ch = codePointAt(text, pos); // TODO: Only allow 1 (one) asterisk!
2468+
pos < end && (isWhiteSpaceSingleLine(ch) || ch === CharacterCodes.asterisk);
2469+
ch = codePointAt(text, pos += charSize(ch))) {
2470+
}
2471+
if (pos === tokenPos) {
2472+
return scanJsDocToken();
2473+
}
2474+
return token = SyntaxKind.WhitespaceTrivia;
2475+
}
2476+
24582477
function scanJsDocToken(): JSDocSyntaxKind {
24592478
startPos = tokenPos = pos;
24602479
tokenFlags = TokenFlags.None;

0 commit comments

Comments
 (0)