-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make JSDoc skipping public, plus more configurable #55739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
442b467
9119d2f
ba9dcab
8a4837c
66a0b09
46bbf0e
88bfd9e
2abd21b
b691830
9e2f8c1
3d60c92
fb22a3e
626ae3e
b6379cb
a273a91
a430b30
3ebdfb6
3673f9e
f393631
40b0cc4
12f5903
735c17c
b3e23a9
db14c33
517f289
9392280
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
DiagnosticMessage, | ||
Diagnostics, | ||
identity, | ||
JSDocParsingMode, | ||
JSDocSyntaxKind, | ||
JsxTokenSyntaxKind, | ||
KeywordSyntaxKind, | ||
|
@@ -21,6 +22,7 @@ import { | |
parsePseudoBigInt, | ||
positionIsSynthesized, | ||
PunctuationOrKeywordSyntaxKind, | ||
ScriptKind, | ||
ScriptTarget, | ||
SourceFileLike, | ||
SyntaxKind, | ||
|
@@ -95,6 +97,8 @@ export interface Scanner { | |
setOnError(onError: ErrorCallback | undefined): void; | ||
setScriptTarget(scriptTarget: ScriptTarget): void; | ||
setLanguageVariant(variant: LanguageVariant): void; | ||
setScriptKind(scriptKind: ScriptKind): void; | ||
setJSDocParsingMode(kind: JSDocParsingMode): void; | ||
/** @deprecated use {@link resetTokenState} */ | ||
setTextPos(textPos: number): void; | ||
resetTokenState(pos: number): void; | ||
|
@@ -114,8 +118,6 @@ export interface Scanner { | |
// callback returns something truthy, then the scanner state is not rolled back. The result | ||
// of invoking the callback is returned from this function. | ||
tryScan<T>(callback: () => T): T; | ||
/** @internal */ | ||
setSkipNonSemanticJSDoc(skip: boolean): void; | ||
} | ||
|
||
/** @internal */ | ||
|
@@ -345,10 +347,7 @@ const commentDirectiveRegExSingleLine = /^\/\/\/?\s*@(ts-expect-error|ts-ignore) | |
*/ | ||
const commentDirectiveRegExMultiLine = /^(?:\/|\*)*\s*@(ts-expect-error|ts-ignore)/; | ||
|
||
/** | ||
* Test for whether a comment contains a JSDoc tag needed by the checker when run in tsc. | ||
*/ | ||
const semanticJSDocTagRegEx = /@(?:see|link)/i; | ||
const jsDocSeeOrLink = /@(?:see|link)/i; | ||
|
||
function lookupInUnicodeMap(code: number, map: readonly number[]): boolean { | ||
// Bail out quickly if it couldn't possibly be in the map. | ||
|
@@ -1008,7 +1007,8 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean | |
var commentDirectives: CommentDirective[] | undefined; | ||
var inJSDocType = 0; | ||
|
||
var skipNonSemanticJSDoc = false; | ||
var scriptKind = ScriptKind.Unknown; | ||
var jsDocParsingMode = JSDocParsingMode.ParseAll; | ||
|
||
setText(text, start, length); | ||
|
||
|
@@ -1054,14 +1054,15 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean | |
setText, | ||
setScriptTarget, | ||
setLanguageVariant, | ||
setScriptKind, | ||
setJSDocParsingMode, | ||
setOnError, | ||
resetTokenState, | ||
setTextPos: resetTokenState, | ||
setInJSDocType, | ||
tryScan, | ||
lookAhead, | ||
scanRange, | ||
setSkipNonSemanticJSDoc, | ||
}; | ||
/* eslint-enable no-var */ | ||
|
||
|
@@ -2003,7 +2004,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean | |
} | ||
} | ||
|
||
if (isJSDoc && (!skipNonSemanticJSDoc || semanticJSDocTagRegEx.test(text.slice(fullStartPos, pos)))) { | ||
if (isJSDoc && shouldParseJSDoc()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏼 |
||
tokenFlags |= TokenFlags.PrecedingJSDocComment; | ||
} | ||
|
||
|
@@ -2288,6 +2289,28 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean | |
} | ||
} | ||
|
||
function shouldParseJSDoc() { | ||
switch (jsDocParsingMode) { | ||
case JSDocParsingMode.ParseAll: | ||
return true; | ||
case JSDocParsingMode.ParseNone: | ||
return false; | ||
} | ||
|
||
if (scriptKind !== ScriptKind.TS && scriptKind !== ScriptKind.TSX) { | ||
// If outside of TS, we need JSDoc to get any type info. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we ever actually make it to this check at all for those file types, so I felt safe not checking for them (and choosing the backwards-compatible option). But, I'll verify. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flipping this to be "=== JS, === JSX" doesn't change any tests, but truthfully I don't know when the others are even used. I would feel safest with the conservative I have now, but am not strongly attached if someone knows better. |
||
return true; | ||
} | ||
|
||
if (jsDocParsingMode === JSDocParsingMode.ParseForTypeInfo) { | ||
// If we're in TS, but we don't need to produce reliable errors, | ||
// we don't need to parse to find @see or @link. | ||
return false; | ||
} | ||
|
||
return jsDocSeeOrLink.test(text.slice(fullStartPos, pos)); | ||
} | ||
|
||
function reScanInvalidIdentifier(): SyntaxKind { | ||
Debug.assert(token === SyntaxKind.Unknown, "'reScanInvalidIdentifier' should only be called when the current token is 'SyntaxKind.Unknown'."); | ||
pos = tokenStart = fullStartPos; | ||
|
@@ -2788,8 +2811,12 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean | |
languageVariant = variant; | ||
} | ||
|
||
function setSkipNonSemanticJSDoc(skip: boolean) { | ||
skipNonSemanticJSDoc = skip; | ||
function setScriptKind(kind: ScriptKind) { | ||
scriptKind = kind; | ||
} | ||
|
||
function setJSDocParsingMode(kind: JSDocParsingMode) { | ||
jsDocParsingMode = kind; | ||
} | ||
|
||
function resetTokenState(position: number) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.