Skip to content

Commit 2842d0c

Browse files
committed
Better/faster checks for shouldParseJSDoc
1. Set a global boolean when invoked from tsc. 2. Use a regex with sourceText.slice to check for @see/@link.
1 parent ebcbd11 commit 2842d0c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,6 @@ namespace Parser {
14131413
let IdentifierConstructor: new (kind: SyntaxKind.Identifier, pos: number, end: number) => Identifier;
14141414
let PrivateIdentifierConstructor: new (kind: SyntaxKind.PrivateIdentifier, pos: number, end: number) => PrivateIdentifier;
14151415
let SourceFileConstructor: new (kind: SyntaxKind.SourceFile, pos: number, end: number) => SourceFile;
1416-
let isInTsserver = true;
14171416

14181417
function countNode(node: Node) {
14191418
nodeCount++;
@@ -1660,10 +1659,10 @@ namespace Parser {
16601659
IdentifierConstructor = objectAllocator.getIdentifierConstructor();
16611660
PrivateIdentifierConstructor = objectAllocator.getPrivateIdentifierConstructor();
16621661
SourceFileConstructor = objectAllocator.getSourceFileConstructor();
1663-
isInTsserver = (new NodeConstructor(0, 0, 0).constructor.name !== "Node");
16641662

16651663
fileName = normalizePath(_fileName);
16661664
sourceText = _sourceText;
1665+
16671666
languageVersion = _languageVersion;
16681667
syntaxCursor = _syntaxCursor;
16691668
scriptKind = _scriptKind;
@@ -1764,19 +1763,17 @@ namespace Parser {
17641763
return hasJSDoc ? addJSDocComment(node) : node;
17651764
}
17661765

1767-
function shouldCheckJSDoc<T extends HasJSDoc>(node: T, comment: ts.CommentRange) {
1768-
if (isInTsserver) return true;
1766+
const seeLink = /@(?:see|link)/;
1767+
function shouldParseJSDoc<T extends HasJSDoc>(node: T, comment: ts.CommentRange) {
1768+
if (!(globalThis as any).isTSC) return true;
17691769
if (node.flags & NodeFlags.JavaScriptFile) return true;
1770-
const link = sourceText.indexOf("@link", comment.pos);
1771-
const see = sourceText.indexOf("@see", comment.pos);
1772-
if (comment.pos < link && link < comment.end) return true;
1773-
if (comment.pos < see && see < comment.end) return true;
1770+
if (seeLink.test(sourceText.slice(comment.pos, comment.end))) return true;
17741771
}
17751772

17761773
let hasDeprecatedTag = false;
17771774
function addJSDocComment<T extends HasJSDoc>(node: T): T {
17781775
Debug.assert(!node.jsDoc); // Should only be called once per node
1779-
const jsDoc = mapDefined(getJSDocCommentRanges(node, sourceText), comment => shouldCheckJSDoc(node, comment) && JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos));
1776+
const jsDoc = mapDefined(getJSDocCommentRanges(node, sourceText), comment => shouldParseJSDoc(node, comment) && JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos));
17801777
if (jsDoc.length) node.jsDoc = jsDoc;
17811778
if (hasDeprecatedTag) {
17821779
hasDeprecatedTag = false;

src/tsc/tsc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnviron
2020
if (ts.sys.setBlocking) {
2121
ts.sys.setBlocking();
2222
}
23+
declare var console: any;
24+
console.log("I am over here")
25+
;(globalThis as any)["isTSC"] = true;
2326

2427
ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args);

0 commit comments

Comments
 (0)