Skip to content

Commit c1f2afd

Browse files
authored
Add typedef declaration space, unify typedef name gathering (#18172)
* Add typedef declaration space, unify typedef name gathering, strengthen errorUnusedLocal * Bonus round: make jsdoc presence way mroe typesafe * Be exhaustive in nameForNamelessJSDocTypedef * Remove nonrequired casts * Replace more casts with guards * Cannot be internal * Debug.fail returns never, assert never no longer needs unreachable throw to satisfy checker * Rename type * Add replacement message as in 18287
1 parent 39d0590 commit c1f2afd

17 files changed

+279
-91
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,8 @@ namespace ts {
282282
const index = indexOf(functionType.parameters, node);
283283
return "arg" + index as __String;
284284
case SyntaxKind.JSDocTypedefTag:
285-
const parentNode = node.parent && node.parent.parent;
286-
let nameFromParentNode: __String;
287-
if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) {
288-
if ((<VariableStatement>parentNode).declarationList.declarations.length > 0) {
289-
const nameIdentifier = (<VariableStatement>parentNode).declarationList.declarations[0].name;
290-
if (isIdentifier(nameIdentifier)) {
291-
nameFromParentNode = nameIdentifier.escapedText;
292-
}
293-
}
294-
}
295-
return nameFromParentNode;
285+
const name = getNameOfJSDocTypedef(node as JSDocTypedefTag);
286+
return typeof name !== "undefined" ? name.escapedText : undefined;
296287
}
297288
}
298289

@@ -598,7 +589,7 @@ namespace ts {
598589
// Binding of JsDocComment should be done before the current block scope container changes.
599590
// because the scope of JsDocComment should not be affected by whether the current node is a
600591
// container or not.
601-
if (node.jsDoc) {
592+
if (hasJSDocNodes(node)) {
602593
if (isInJavaScriptFile(node)) {
603594
for (const j of node.jsDoc) {
604595
bind(j);
@@ -1931,7 +1922,7 @@ namespace ts {
19311922
}
19321923

19331924
function bindJSDocTypedefTagIfAny(node: Node) {
1934-
if (!node.jsDoc) {
1925+
if (!hasJSDocNodes(node)) {
19351926
return;
19361927
}
19371928

src/compiler/checker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19158,6 +19158,8 @@ namespace ts {
1915819158
switch (d.kind) {
1915919159
case SyntaxKind.InterfaceDeclaration:
1916019160
case SyntaxKind.TypeAliasDeclaration:
19161+
// A jsdoc typedef is, by definition, a type alias
19162+
case SyntaxKind.JSDocTypedefTag:
1916119163
return DeclarationSpaces.ExportType;
1916219164
case SyntaxKind.ModuleDeclaration:
1916319165
return isAmbientModule(d) || getModuleInstanceState(d) !== ModuleInstanceState.NonInstantiated
@@ -19827,7 +19829,7 @@ namespace ts {
1982719829
}
1982819830
}
1982919831
else if (compilerOptions.noUnusedLocals) {
19830-
forEach(local.declarations, d => errorUnusedLocal(getNameOfDeclaration(d) || d, unescapeLeadingUnderscores(local.escapedName)));
19832+
forEach(local.declarations, d => errorUnusedLocal(d, unescapeLeadingUnderscores(local.escapedName)));
1983119833
}
1983219834
}
1983319835
});
@@ -19842,7 +19844,8 @@ namespace ts {
1984219844
return false;
1984319845
}
1984419846

19845-
function errorUnusedLocal(node: Node, name: string) {
19847+
function errorUnusedLocal(declaration: Declaration, name: string) {
19848+
const node = getNameOfDeclaration(declaration) || declaration;
1984619849
if (isIdentifierThatStartsWithUnderScore(node)) {
1984719850
const declaration = getRootDeclaration(node.parent);
1984819851
if (declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) {
@@ -19909,7 +19912,7 @@ namespace ts {
1990919912
if (!local.isReferenced && !local.exportSymbol) {
1991019913
for (const declaration of local.declarations) {
1991119914
if (!isAmbientModule(declaration)) {
19912-
errorUnusedLocal(getNameOfDeclaration(declaration), unescapeLeadingUnderscores(local.escapedName));
19915+
errorUnusedLocal(declaration, unescapeLeadingUnderscores(local.escapedName));
1991319916
}
1991419917
}
1991519918
}

src/compiler/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,7 @@ namespace ts {
24412441
}
24422442
}
24432443

2444-
export function fail(message?: string, stackCrawlMark?: Function): void {
2444+
export function fail(message?: string, stackCrawlMark?: Function): never {
24452445
debugger;
24462446
const e = new Error(message ? `Debug Failure. ${message}` : "Debug Failure.");
24472447
if ((<any>Error).captureStackTrace) {
@@ -2450,6 +2450,10 @@ namespace ts {
24502450
throw e;
24512451
}
24522452

2453+
export function assertNever(member: never, message?: string, stackCrawlMark?: Function): never {
2454+
return fail(message || `Illegal value: ${member}`, stackCrawlMark || assertNever);
2455+
}
2456+
24532457
export function getFunctionName(func: Function) {
24542458
if (typeof func !== "function") {
24552459
return "";

src/compiler/parser.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ namespace ts {
729729
}
730730

731731

732-
function addJSDocComment<T extends Node>(node: T): T {
732+
function addJSDocComment<T extends HasJSDoc>(node: T): T {
733733
const comments = getJSDocCommentRanges(node, sourceFile.text);
734734
if (comments) {
735735
for (const comment of comments) {
@@ -768,7 +768,7 @@ namespace ts {
768768
const saveParent = parent;
769769
parent = n;
770770
forEachChild(n, visitNode);
771-
if (n.jsDoc) {
771+
if (hasJSDocNodes(n)) {
772772
for (const jsDoc of n.jsDoc) {
773773
jsDoc.parent = n;
774774
parent = jsDoc;
@@ -2158,7 +2158,7 @@ namespace ts {
21582158
const result = <JSDocFunctionType>createNode(SyntaxKind.JSDocFunctionType);
21592159
nextToken();
21602160
fillSignature(SyntaxKind.ColonToken, SignatureFlags.Type | SignatureFlags.JSDoc, result);
2161-
return finishNode(result);
2161+
return addJSDocComment(finishNode(result));
21622162
}
21632163
const node = <TypeReferenceNode>createNode(SyntaxKind.TypeReference);
21642164
node.typeName = parseIdentifierName();
@@ -2365,7 +2365,7 @@ namespace ts {
23652365
parseSemicolon();
23662366
}
23672367

2368-
function parseSignatureMember(kind: SyntaxKind): CallSignatureDeclaration | ConstructSignatureDeclaration {
2368+
function parseSignatureMember(kind: SyntaxKind.CallSignature | SyntaxKind.ConstructSignature): CallSignatureDeclaration | ConstructSignatureDeclaration {
23692369
const node = <CallSignatureDeclaration | ConstructSignatureDeclaration>createNode(kind);
23702370
if (kind === SyntaxKind.ConstructSignature) {
23712371
parseExpected(SyntaxKind.NewKeyword);
@@ -2445,7 +2445,7 @@ namespace ts {
24452445
node.parameters = parseBracketedList(ParsingContext.Parameters, parseParameter, SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken);
24462446
node.type = parseTypeAnnotation();
24472447
parseTypeMemberSemicolon();
2448-
return finishNode(node);
2448+
return addJSDocComment(finishNode(node));
24492449
}
24502450

24512451
function parsePropertyOrMethodSignature(fullStart: number, modifiers: NodeArray<Modifier>): PropertySignature | MethodSignature {
@@ -2605,7 +2605,7 @@ namespace ts {
26052605
parseExpected(SyntaxKind.NewKeyword);
26062606
}
26072607
fillSignature(SyntaxKind.EqualsGreaterThanToken, SignatureFlags.Type, node);
2608-
return finishNode(node);
2608+
return addJSDocComment(finishNode(node));
26092609
}
26102610

26112611
function parseKeywordAndNoDot(): TypeNode | undefined {
@@ -6182,7 +6182,7 @@ namespace ts {
61826182
return jsDoc ? { jsDoc, diagnostics } : undefined;
61836183
}
61846184

6185-
export function parseJSDocComment(parent: Node, start: number, length: number): JSDoc {
6185+
export function parseJSDocComment(parent: HasJSDoc, start: number, length: number): JSDoc {
61866186
const saveToken = currentToken;
61876187
const saveParseDiagnosticsLength = parseDiagnostics.length;
61886188
const saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode;
@@ -6997,7 +6997,7 @@ namespace ts {
69976997
}
69986998

69996999
forEachChild(node, visitNode, visitArray);
7000-
if (node.jsDoc) {
7000+
if (hasJSDocNodes(node)) {
70017001
for (const jsDocComment of node.jsDoc) {
70027002
forEachChild(jsDocComment, visitNode, visitArray);
70037003
}

0 commit comments

Comments
 (0)