Skip to content

Commit

Permalink
Cherry-pick PR #39702 into release-4.0 (#39985)
Browse files Browse the repository at this point in the history
Component commits:
cd5dde5 Improve deprecated suggestion node position

6e39ee7 fix typo

a593fd0 Simplify code

58f6f3d merge helper function

Co-authored-by: kingwl <[email protected]>
  • Loading branch information
TypeScript Bot and Kingwl authored Aug 12, 2020
1 parent 3055018 commit 7558de6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
34 changes: 29 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24698,7 +24698,7 @@ namespace ts {
if (isNodeOpeningLikeElement) {
const jsxOpeningLikeNode = node as JsxOpeningLikeElement;
const sig = getResolvedSignature(jsxOpeningLikeNode);
checkDeprecatedSignature(sig, node);
checkDeprecatedSignature(sig, <JsxOpeningLikeElement>node);
checkJsxReturnAssignableToAppropriateBound(getJsxReferenceKind(jsxOpeningLikeNode), getReturnTypeOfSignature(sig), jsxOpeningLikeNode);
}
}
Expand Down Expand Up @@ -27581,9 +27581,34 @@ namespace ts {
return returnType;
}

function checkDeprecatedSignature(signature: Signature, node: Node) {
function checkDeprecatedSignature(signature: Signature, node: CallLikeExpression) {
if (signature.declaration && signature.declaration.flags & NodeFlags.Deprecated) {
errorOrSuggestion(/*isError*/ false, node, Diagnostics._0_is_deprecated, signatureToString(signature));
const suggestionNode = getDeprecatedSuggestionNode(node);
errorOrSuggestion(/*isError*/ false, suggestionNode, Diagnostics._0_is_deprecated, signatureToString(signature));
}
}

function getDeprecatedSuggestionNode(node: Node): Node {
node = skipParentheses(node);
switch (node.kind) {
case SyntaxKind.CallExpression:
case SyntaxKind.Decorator:
case SyntaxKind.NewExpression:
return getDeprecatedSuggestionNode((<Decorator | CallExpression | NewExpression>node).expression);
case SyntaxKind.TaggedTemplateExpression:
return getDeprecatedSuggestionNode((<TaggedTemplateExpression>node).tag);
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxSelfClosingElement:
return getDeprecatedSuggestionNode((<JsxOpeningLikeElement>node).tagName);
case SyntaxKind.ElementAccessExpression:
return (<ElementAccessExpression>node).argumentExpression;
case SyntaxKind.PropertyAccessExpression:
return (<PropertyAccessExpression>node).name;
case SyntaxKind.TypeReference:
const typeReference = <TypeReferenceNode>node;
return isQualifiedName(typeReference.typeName) ? typeReference.typeName.right : typeReference;
default:
return node;
}
}

Expand Down Expand Up @@ -30978,8 +31003,7 @@ namespace ts {
const symbol = getNodeLinks(node).resolvedSymbol;
if (symbol) {
if (some(symbol.declarations, d => isTypeDeclaration(d) && !!(d.flags & NodeFlags.Deprecated))) {
const diagLocation = isTypeReferenceNode(node) && isQualifiedName(node.typeName) ? node.typeName.right : node;
errorOrSuggestion(/* isError */ false, diagLocation, Diagnostics._0_is_deprecated, symbol.escapedName as string);
errorOrSuggestion(/* isError */ false, getDeprecatedSuggestionNode(node), Diagnostics._0_is_deprecated, symbol.escapedName as string);
}
if (type.flags & TypeFlags.Enum && symbol.flags & SymbolFlags.EnumMember) {
error(node, Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type));
Expand Down
31 changes: 15 additions & 16 deletions tests/cases/fourslash/jsdocDeprecated_suggestion1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
//// export namespace foo {
//// /** @deprecated */
//// export function faff () { }
//// [|faff()|]
//// [|faff|]()
//// }
//// const [|a|] = [|foo.faff()|]
//// const [|a|] = foo.[|faff|]()
//// foo[[|"faff"|]]
//// const { [|faff|] } = foo
//// [|faff()|]
//// [|faff|]()
//// /** @deprecated */
//// export function bar () {
//// [|foo?.faff()|]
//// foo?.[|faff|]()
//// }
//// [|foo?.["faff"]?.()|]
//// [|bar()|];
//// foo?.[[|"faff"|]]?.()
//// [|bar|]();
//// /** @deprecated */
//// export interface Foo {
//// /** @deprecated */
Expand All @@ -37,8 +37,8 @@
//// constructor() {
//// }
//// }
//// var c = [|new C()|]
//// [|c.m()|]
//// var c = new [|C|]()
//// c.[|m|]()
//// c.[|m|]
//// new [|D|]()
//// C
Expand All @@ -51,26 +51,26 @@
//// return <div></div>
//// }
//// [|Compi|];
//// [|<Compi />|];
//// [|<Compi {...props}>|]<div></div></[|Compi|]>;
//// <[|Compi|] />;
//// <[|Compi|] {...props}><div></div></[|Compi|]>;
//// /** @deprecated */
//// function ttf(_x: unknown) {
//// }
//// [|ttf``|]
//// [|ttf|]``
//// [|ttf|]
//// /** @deprecated */
//// function dec(_c: unknown) { }
//// [|dec|]
//// [|@dec|]
//// @[|dec|]
//// class K { }

// @Filename: b.ts
//// // imports and aliases
//// import * as f from './a';
//// import { [|bar|], [|QW|] } from './a';
//// [|f.bar()|];
//// [|f.foo.faff()|];
//// [|bar()|];
//// f.[|bar|]();
//// f.foo.[|faff|]();
//// [|bar|]();
//// type Z = [|QW|];
//// type A = f.[|Foo|];
//// type B = f.[|QW|];
Expand All @@ -79,7 +79,6 @@

goTo.file('a.ts')
const ranges = test.ranges();

verify.getSuggestionDiagnostics([
{
"code": 6385,
Expand Down
16 changes: 8 additions & 8 deletions tests/cases/fourslash/jsdocDeprecated_suggestion2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
//// /** @deprecated */
//// declare function foo(): undefined;
//// declare function foo (a?: string): number | undefined;
//// [|foo()|];
//// [|foo|]();
//// foo('');
//// foo;

//// /** @deprecated */
//// declare function bar(): number;
//// [|bar()|];
//// [|bar|]();
//// [|bar|];

//// /** @deprecated */
//// declare function baz(): number;
//// /** @deprecated */
//// declare function baz(): number | undefined;
//// [|baz()|];
//// [|baz|]();
//// [|baz|];

//// interface Foo {
Expand All @@ -27,7 +27,7 @@
//// (a: number): void
//// }
//// declare const f: Foo;
//// [|f()|];
//// [|f|]();
//// f(1);

//// interface T {
Expand All @@ -37,15 +37,15 @@
//// }
//// declare const t: T;
//// t.createElement();
//// [|t.createElement('xmp')|];
//// t.[|createElement|]('xmp');

//// declare class C {
//// /** @deprecated */
//// constructor ();
//// constructor(v: string)
//// }
//// C;
//// const c = [|new C()|];
//// const c = new [|C|]();

//// interface Ca {
//// /** @deprecated */
Expand All @@ -61,10 +61,10 @@
//// declare const cb: Cb;
//// ca;
//// cb;
//// [|ca()|];
//// [|ca|]();
//// cb();
//// new ca();
//// [|new cb()|];
//// new [|cb|]();

const ranges = test.ranges();
verify.getSuggestionDiagnostics([
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/jsdocDeprecated_suggestion3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
//// declare function b(): void
//// declare const tb: b;
//// [|b|]
//// [|b()|];
//// [|b|]();

//// interface c { }
//// /** @deprecated */
//// declare function c(): void
//// declare function c(a: number): void
//// declare const tc: c;
//// c;
//// [|c()|];
//// [|c|]();
//// c(1);

//// /** @deprecated */
Expand All @@ -38,8 +38,8 @@
//// /** @deprecated */
//// declare function e(a: number): void
//// [|e|];
//// [|e()|];
//// [|e(1)|];
//// [|e|]();
//// [|e|](1);

//// /** @deprecated */
//// interface f { a: number }
Expand Down

0 comments on commit 7558de6

Please sign in to comment.