Skip to content

feat(42684): Override JSDoc for re-exported types / enum / classes / interfaces #47293

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

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,10 @@ namespace ts {
return diagnostic;
}

function isDeprecatedSymbol(symbol: Symbol) {
return !!(getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Deprecated);
}

function addDeprecatedSuggestion(location: Node, declarations: Node[], deprecatedEntity: string) {
const diagnostic = createDiagnosticForNode(location, Diagnostics._0_is_deprecated, deprecatedEntity);
return addDeprecatedSuggestionWorker(declarations, diagnostic);
Expand Down Expand Up @@ -15175,7 +15179,7 @@ namespace ts {
}
const prop = getPropertyOfType(objectType, propName);
if (prop) {
if (accessFlags & AccessFlags.ReportDeprecated && accessNode && prop.declarations && getDeclarationNodeFlagsFromSymbol(prop) & NodeFlags.Deprecated && isUncalledFunctionReference(accessNode, prop)) {
if (accessFlags & AccessFlags.ReportDeprecated && accessNode && prop.declarations && isDeprecatedSymbol(prop) && isUncalledFunctionReference(accessNode, prop)) {
const deprecatedNode = accessExpression?.argumentExpression ?? (isIndexedAccessTypeNode(accessNode) ? accessNode.indexType : accessNode);
addDeprecatedSuggestion(deprecatedNode, prop.declarations, propName as string);
}
Expand Down Expand Up @@ -25066,9 +25070,9 @@ namespace ts {
}

const localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol);
const sourceSymbol = localOrExportSymbol.flags & SymbolFlags.Alias ? resolveAlias(localOrExportSymbol) : localOrExportSymbol;
if (sourceSymbol.declarations && getDeclarationNodeFlagsFromSymbol(sourceSymbol) & NodeFlags.Deprecated && isUncalledFunctionReference(node, sourceSymbol)) {
addDeprecatedSuggestion(node, sourceSymbol.declarations, node.escapedText as string);
const targetSymbol = checkDeprecatedAliasedSymbol(localOrExportSymbol, node);
if (isDeprecatedSymbol(targetSymbol) && isUncalledFunctionReference(node, targetSymbol) && targetSymbol.declarations) {
addDeprecatedSuggestion(node, targetSymbol.declarations, node.escapedText as string);
}

let declaration = localOrExportSymbol.valueDeclaration;
Expand Down Expand Up @@ -28460,7 +28464,7 @@ namespace ts {
}
}
else {
if (prop.declarations && getDeclarationNodeFlagsFromSymbol(prop) & NodeFlags.Deprecated && isUncalledFunctionReference(node, prop)) {
if (isDeprecatedSymbol(prop) && isUncalledFunctionReference(node, prop) && prop.declarations) {
addDeprecatedSuggestion(right, prop.declarations, right.escapedText as string);
}
checkPropertyNotUsedBeforeDeclaration(prop, node, right);
Expand Down Expand Up @@ -39802,10 +39806,45 @@ namespace ts {
}
}

if (isImportSpecifier(node) && target.declarations?.every(d => !!(getCombinedNodeFlags(d) & NodeFlags.Deprecated))) {
addDeprecatedSuggestion(node.name, target.declarations, symbol.escapedName as string);
if (isImportSpecifier(node)) {
const targetSymbol = checkDeprecatedAliasedSymbol(symbol, node);
if (isDeprecatedAliasedSymbol(targetSymbol) && targetSymbol.declarations) {
addDeprecatedSuggestion(node, targetSymbol.declarations, targetSymbol.escapedName as string);
}
}
}
}

function isDeprecatedAliasedSymbol(symbol: Symbol) {
return !!symbol.declarations && every(symbol.declarations, d => !!(getCombinedNodeFlags(d) & NodeFlags.Deprecated));
}

function checkDeprecatedAliasedSymbol(symbol: Symbol, location: Node) {
if (!(symbol.flags & SymbolFlags.Alias)) return symbol;

const targetSymbol = resolveAlias(symbol);
if (targetSymbol === unknownSymbol) return targetSymbol;

while (symbol.flags & SymbolFlags.Alias) {
const target = getImmediateAliasedSymbol(symbol);
if (target) {
if (target === targetSymbol) break;
if (target.declarations && length(target.declarations)) {
if (isDeprecatedAliasedSymbol(target)) {
addDeprecatedSuggestion(location, target.declarations, target.escapedName as string);
break;
}
else {
if (symbol === targetSymbol) break;
symbol = target;
}
}
}
else {
break;
}
}
return targetSymbol;
}

function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7406,7 +7406,8 @@ namespace ts {
}

function parseExportSpecifier() {
return parseImportOrExportSpecifier(SyntaxKind.ExportSpecifier) as ExportSpecifier;
const hasJSDoc = hasPrecedingJSDocComment();
return withJSDoc(parseImportOrExportSpecifier(SyntaxKind.ExportSpecifier) as ExportSpecifier, hasJSDoc);
}

function parseImportSpecifier() {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ namespace ts {
| JSDocFunctionType
| ExportDeclaration
| NamedTupleMember
| ExportSpecifier
| EndOfFileToken
;

Expand Down Expand Up @@ -3117,7 +3118,7 @@ namespace ts {
readonly isTypeOnly: boolean;
}

export interface ExportSpecifier extends NamedDeclaration {
export interface ExportSpecifier extends NamedDeclaration, JSDocContainer {
readonly kind: SyntaxKind.ExportSpecifier;
readonly parent: NamedExports;
readonly isTypeOnly: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,8 @@ namespace ts {
node.kind === SyntaxKind.FunctionExpression ||
node.kind === SyntaxKind.ArrowFunction ||
node.kind === SyntaxKind.ParenthesizedExpression ||
node.kind === SyntaxKind.VariableDeclaration) ?
node.kind === SyntaxKind.VariableDeclaration ||
node.kind === SyntaxKind.ExportSpecifier) ?
concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) :
getLeadingCommentRanges(text, node.pos);
// True if the comment starts with '/**' but not if it is '/**/'
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ declare namespace ts {
}
export interface JSDocContainer {
}
export type HasJSDoc = ParameterDeclaration | CallSignatureDeclaration | ClassStaticBlockDeclaration | ConstructSignatureDeclaration | MethodSignature | PropertySignature | ArrowFunction | ParenthesizedExpression | SpreadAssignment | ShorthandPropertyAssignment | PropertyAssignment | FunctionExpression | EmptyStatement | DebuggerStatement | Block | VariableStatement | ExpressionStatement | IfStatement | DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement | BreakStatement | ContinueStatement | ReturnStatement | WithStatement | SwitchStatement | LabeledStatement | ThrowStatement | TryStatement | FunctionDeclaration | ConstructorDeclaration | MethodDeclaration | VariableDeclaration | PropertyDeclaration | AccessorDeclaration | ClassLikeDeclaration | InterfaceDeclaration | TypeAliasDeclaration | EnumMember | EnumDeclaration | ModuleDeclaration | ImportEqualsDeclaration | ImportDeclaration | NamespaceExportDeclaration | ExportAssignment | IndexSignatureDeclaration | FunctionTypeNode | ConstructorTypeNode | JSDocFunctionType | ExportDeclaration | NamedTupleMember | EndOfFileToken;
export type HasJSDoc = ParameterDeclaration | CallSignatureDeclaration | ClassStaticBlockDeclaration | ConstructSignatureDeclaration | MethodSignature | PropertySignature | ArrowFunction | ParenthesizedExpression | SpreadAssignment | ShorthandPropertyAssignment | PropertyAssignment | FunctionExpression | EmptyStatement | DebuggerStatement | Block | VariableStatement | ExpressionStatement | IfStatement | DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement | BreakStatement | ContinueStatement | ReturnStatement | WithStatement | SwitchStatement | LabeledStatement | ThrowStatement | TryStatement | FunctionDeclaration | ConstructorDeclaration | MethodDeclaration | VariableDeclaration | PropertyDeclaration | AccessorDeclaration | ClassLikeDeclaration | InterfaceDeclaration | TypeAliasDeclaration | EnumMember | EnumDeclaration | ModuleDeclaration | ImportEqualsDeclaration | ImportDeclaration | NamespaceExportDeclaration | ExportAssignment | IndexSignatureDeclaration | FunctionTypeNode | ConstructorTypeNode | JSDocFunctionType | ExportDeclaration | NamedTupleMember | ExportSpecifier | EndOfFileToken;
export type HasType = SignatureDeclaration | VariableDeclaration | ParameterDeclaration | PropertySignature | PropertyDeclaration | TypePredicateNode | ParenthesizedTypeNode | TypeOperatorNode | MappedTypeNode | AssertionExpression | TypeAliasDeclaration | JSDocTypeExpression | JSDocNonNullableType | JSDocNullableType | JSDocOptionalType | JSDocVariadicType;
export type HasTypeArguments = CallExpression | NewExpression | TaggedTemplateExpression | JsxOpeningElement | JsxSelfClosingElement;
export type HasInitializer = HasExpressionInitializer | ForStatement | ForInStatement | ForOfStatement | JsxAttribute;
Expand Down Expand Up @@ -1702,7 +1702,7 @@ declare namespace ts {
readonly name: Identifier;
readonly isTypeOnly: boolean;
}
export interface ExportSpecifier extends NamedDeclaration {
export interface ExportSpecifier extends NamedDeclaration, JSDocContainer {
readonly kind: SyntaxKind.ExportSpecifier;
readonly parent: NamedExports;
readonly isTypeOnly: boolean;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ declare namespace ts {
}
export interface JSDocContainer {
}
export type HasJSDoc = ParameterDeclaration | CallSignatureDeclaration | ClassStaticBlockDeclaration | ConstructSignatureDeclaration | MethodSignature | PropertySignature | ArrowFunction | ParenthesizedExpression | SpreadAssignment | ShorthandPropertyAssignment | PropertyAssignment | FunctionExpression | EmptyStatement | DebuggerStatement | Block | VariableStatement | ExpressionStatement | IfStatement | DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement | BreakStatement | ContinueStatement | ReturnStatement | WithStatement | SwitchStatement | LabeledStatement | ThrowStatement | TryStatement | FunctionDeclaration | ConstructorDeclaration | MethodDeclaration | VariableDeclaration | PropertyDeclaration | AccessorDeclaration | ClassLikeDeclaration | InterfaceDeclaration | TypeAliasDeclaration | EnumMember | EnumDeclaration | ModuleDeclaration | ImportEqualsDeclaration | ImportDeclaration | NamespaceExportDeclaration | ExportAssignment | IndexSignatureDeclaration | FunctionTypeNode | ConstructorTypeNode | JSDocFunctionType | ExportDeclaration | NamedTupleMember | EndOfFileToken;
export type HasJSDoc = ParameterDeclaration | CallSignatureDeclaration | ClassStaticBlockDeclaration | ConstructSignatureDeclaration | MethodSignature | PropertySignature | ArrowFunction | ParenthesizedExpression | SpreadAssignment | ShorthandPropertyAssignment | PropertyAssignment | FunctionExpression | EmptyStatement | DebuggerStatement | Block | VariableStatement | ExpressionStatement | IfStatement | DoStatement | WhileStatement | ForStatement | ForInStatement | ForOfStatement | BreakStatement | ContinueStatement | ReturnStatement | WithStatement | SwitchStatement | LabeledStatement | ThrowStatement | TryStatement | FunctionDeclaration | ConstructorDeclaration | MethodDeclaration | VariableDeclaration | PropertyDeclaration | AccessorDeclaration | ClassLikeDeclaration | InterfaceDeclaration | TypeAliasDeclaration | EnumMember | EnumDeclaration | ModuleDeclaration | ImportEqualsDeclaration | ImportDeclaration | NamespaceExportDeclaration | ExportAssignment | IndexSignatureDeclaration | FunctionTypeNode | ConstructorTypeNode | JSDocFunctionType | ExportDeclaration | NamedTupleMember | ExportSpecifier | EndOfFileToken;
export type HasType = SignatureDeclaration | VariableDeclaration | ParameterDeclaration | PropertySignature | PropertyDeclaration | TypePredicateNode | ParenthesizedTypeNode | TypeOperatorNode | MappedTypeNode | AssertionExpression | TypeAliasDeclaration | JSDocTypeExpression | JSDocNonNullableType | JSDocNullableType | JSDocOptionalType | JSDocVariadicType;
export type HasTypeArguments = CallExpression | NewExpression | TaggedTemplateExpression | JsxOpeningElement | JsxSelfClosingElement;
export type HasInitializer = HasExpressionInitializer | ForStatement | ForInStatement | ForOfStatement | JsxAttribute;
Expand Down Expand Up @@ -1702,7 +1702,7 @@ declare namespace ts {
readonly name: Identifier;
readonly isTypeOnly: boolean;
}
export interface ExportSpecifier extends NamedDeclaration {
export interface ExportSpecifier extends NamedDeclaration, JSDocContainer {
readonly kind: SyntaxKind.ExportSpecifier;
readonly parent: NamedExports;
readonly isTypeOnly: boolean;
Expand Down
33 changes: 33 additions & 0 deletions tests/cases/fourslash/jsdocDeprecated_suggestion14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference path="fourslash.ts" />

// @module: esnext
// @filename: /a.ts
////export const a = 1;
////export const b = 1;

// @filename: /b.ts
////export {
//// /** @deprecated a is deprecated */
//// a
////} from "./a";

// @filename: /c.ts
////import { [|a|] } from "./b";
////[|a|]

goTo.file("/c.ts")

verify.getSuggestionDiagnostics([
{
"code": 6385,
"message": "'a' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[0]
},
{
"code": 6385,
"message": "'a' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[1]
},
]);
38 changes: 38 additions & 0 deletions tests/cases/fourslash/jsdocDeprecated_suggestion15.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path="fourslash.ts" />

// @module: esnext
// @filename: /a.ts
////export const a = 1;
////export const b = 1;

// @filename: /b.ts
////export {
//// /** @deprecated a is deprecated */
//// a
////} from "./a";

// @filename: /c.ts
////export {
//// a
////} from "./b";

// @filename: /d.ts
////import { [|a|] } from "./c";
////[|a|]

goTo.file("/d.ts")

verify.getSuggestionDiagnostics([
{
"code": 6385,
"message": "'a' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[0]
},
{
"code": 6385,
"message": "'a' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[1]
},
]);
27 changes: 27 additions & 0 deletions tests/cases/fourslash/jsdocDeprecated_suggestion16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path="fourslash.ts" />

// @module: esnext
// @filename: /a.ts
////const a = 1;
////const b = 1;
////export { a, /** @deprecated b is deprecated */ b }

// @filename: /b.ts
////import { [|b|] } from "./a";
////[|b|]

goTo.file("/b.ts")
verify.getSuggestionDiagnostics([
{
"code": 6385,
"message": "'b' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[0]
},
{
"code": 6385,
"message": "'b' is deprecated.",
"reportsDeprecated": true,
"range": test.ranges()[1]
},
]);