Skip to content

Commit

Permalink
deprecation: properly check destructuring (palantir#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent 2b9d9db commit f056d04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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

0 comments on commit f056d04

Please sign in to comment.