Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

deprecation: properly check destructuring #3318

Merged
merged 1 commit into from
Oct 19, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/rules/deprecationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
isIdentifier,
isNewExpression,
isPropertyAccessExpression,
isPropertyAssignment,
isReassignmentTarget,
isShorthandPropertyAssignment,
isTaggedTemplateExpression,
isVariableDeclaration,
isVariableDeclarationList,
Expand Down Expand Up @@ -102,10 +105,12 @@ function isDeclaration(identifier: ts.Identifier): boolean {
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertyAssignment:
case ts.SyntaxKind.EnumMember:
case ts.SyntaxKind.ImportEqualsDeclaration:
return (parent as ts.NamedDeclaration).name === identifier;
case ts.SyntaxKind.PropertyAssignment:
return (parent as ts.PropertyAssignment).name === identifier &&
!isReassignmentTarget(identifier.parent!.parent as ts.ObjectLiteralExpression);
case ts.SyntaxKind.BindingElement:
// return true for `b` in `const {a: b} = obj"`
return (parent as ts.BindingElement).name === identifier &&
Expand Down Expand Up @@ -134,7 +139,17 @@ function getDeprecation(node: ts.Identifier, tc: ts.TypeChecker): string | undef
return result;
}
}
let symbol = tc.getSymbolAtLocation(node);
let symbol: ts.Symbol | undefined;
const parent = node.parent!;
if (parent.kind === ts.SyntaxKind.BindingElement) {
symbol = tc.getTypeAtLocation(parent.parent!).getProperty(node.text);
} else if (isPropertyAssignment(parent) && parent.name === node ||
isShorthandPropertyAssignment(parent) && parent.name === node && isReassignmentTarget(node)) {
symbol = tc.getPropertySymbolOfDestructuringAssignment(node);
} else {
symbol = tc.getSymbolAtLocation(node);
}

if (symbol !== undefined && Lint.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
symbol = tc.getAliasedSymbol(symbol);
}
Expand Down
25 changes: 23 additions & 2 deletions test/rules/deprecation/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,30 @@ import {DeprecatedClass, DeprecatedConstructorClass, PartiallyDeprecatedClass} f
~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('PartiallyDeprecatedClass')]
}

// TODO: those should be an error
let {f, g, h} = p;
(function ({f, g}: I) {})
~ [err % ('f')]
~ [errmsg % ('h', 'Use g instead.')]
({f, g, h} = p);
~ [err % ('f')]
~ [errmsg % ('h', 'Use g instead.')]

({f: g, g: h, h: f} = p);
~ [err % ('f')]
~ [errmsg % ('h', 'Use g instead.')]

{
/** @deprecated Just don't use. */
let tmp;
({f: tmp} = p);
~ [err % ('f')]
~~~ [errmsg % ('tmp', "Just don't use.")]
}

(function ({f, g, h: tmp}: I) {})
~ [err % ('f')]
~ [errmsg % ('h', 'Use g instead.')]
(function ({foo: {f, g}}: {foo: I}) {})
~ [err % ('f')]

[err]: %s is deprecated.
[errmsg]: %s is deprecated: %s