diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-10-14 18:40:54 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-10-14 18:40:54 +0200 |
commit | 9df98e65f842cf3acae09cbdd969966f42d64469 (patch) | |
tree | f071d3e09a342c208fb8e1cd3f5241d64fbfbaf3 /node_modules/tslint/lib/rules/deprecationRule.js | |
parent | 008926b18470e7f394cd640302957b29728a9803 (diff) |
update dependencies
Diffstat (limited to 'node_modules/tslint/lib/rules/deprecationRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/deprecationRule.js | 114 |
1 files changed, 86 insertions, 28 deletions
diff --git a/node_modules/tslint/lib/rules/deprecationRule.js b/node_modules/tslint/lib/rules/deprecationRule.js index 40b58e96b..ef6180125 100644 --- a/node_modules/tslint/lib/rules/deprecationRule.js +++ b/node_modules/tslint/lib/rules/deprecationRule.js @@ -20,7 +20,7 @@ var tslib_1 = require("tslib"); var tsutils_1 = require("tsutils"); var ts = require("typescript"); var Lint = require("../index"); -var Rule = (function (_super) { +var Rule = /** @class */ (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; @@ -30,7 +30,7 @@ var Rule = (function (_super) { return name + " is deprecated" + (message === "" ? "." : ": " + message.trim()); }; Rule.prototype.applyWithProgram = function (sourceFile, program) { - return this.applyWithFunction(sourceFile, function (ctx) { return walk(ctx, program.getTypeChecker()); }); + return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker()); }; /* tslint:disable:object-literal-sort-keys */ Rule.metadata = { @@ -59,6 +59,13 @@ function walk(ctx, tc) { } } else { + switch (node.kind) { + case ts.SyntaxKind.ImportDeclaration: + case ts.SyntaxKind.ImportEqualsDeclaration: + case ts.SyntaxKind.ExportDeclaration: + case ts.SyntaxKind.ExportAssignment: + return; + } return ts.forEachChild(node, cb); } }); @@ -89,40 +96,70 @@ function isDeclaration(identifier) { case ts.SyntaxKind.PropertyDeclaration: case ts.SyntaxKind.PropertyAssignment: case ts.SyntaxKind.EnumMember: + case ts.SyntaxKind.ImportEqualsDeclaration: return parent.name === identifier; case ts.SyntaxKind.BindingElement: - case ts.SyntaxKind.ExportSpecifier: - case ts.SyntaxKind.ImportSpecifier: - // return true for `b` in `import {a as b} from "foo"` + // return true for `b` in `const {a: b} = obj"` return parent.name === identifier && parent.propertyName !== undefined; default: return false; } } +function getCallExpresion(node) { + var parent = node.parent; + if (tsutils_1.isPropertyAccessExpression(parent) && parent.name === node) { + node = parent; + parent = node.parent; + } + return tsutils_1.isTaggedTemplateExpression(parent) || tsutils_1.isCallExpression(parent) && parent.expression === node ? parent : undefined; +} function getDeprecation(node, tc) { + var callExpression = getCallExpresion(node); + if (callExpression !== undefined) { + var result = getSignatureDeprecation(tc.getResolvedSignature(callExpression)); + if (result !== undefined) { + return result; + } + } var symbol = tc.getSymbolAtLocation(node); if (symbol !== undefined && Lint.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) { symbol = tc.getAliasedSymbol(symbol); } - if (symbol !== undefined) { - return getDeprecationValue(symbol); + if (symbol === undefined || + // if this is a CallExpression and the declaration is a function or method, + // stop here to avoid collecting JsDoc of all overload signatures + callExpression !== undefined && isFunctionOrMethod(symbol.declarations)) { + return undefined; + } + return getSymbolDeprecation(symbol); +} +function findDeprecationTag(tags) { + for (var _i = 0, tags_1 = tags; _i < tags_1.length; _i++) { + var tag = tags_1[_i]; + if (tag.name === "deprecated") { + return tag.text; + } } return undefined; } -function getDeprecationValue(symbol) { +function getSymbolDeprecation(symbol) { if (symbol.getJsDocTags !== undefined) { - for (var _i = 0, _a = symbol.getJsDocTags(); _i < _a.length; _i++) { - var tag = _a[_i]; - if (tag.name === "deprecated") { - return tag.text; - } - } - return undefined; + return findDeprecationTag(symbol.getJsDocTags()); } // for compatibility with typescript@<2.3.0 return getDeprecationFromDeclarations(symbol.declarations); } +function getSignatureDeprecation(signature) { + if (signature === undefined) { + return undefined; + } + if (signature.getJsDocTags !== undefined) { + return findDeprecationTag(signature.getJsDocTags()); + } + // for compatibility with typescript@<2.3.0 + return signature.declaration === undefined ? undefined : getDeprecationFromDeclaration(signature.declaration); +} function getDeprecationFromDeclarations(declarations) { if (declarations === undefined) { return undefined; @@ -139,22 +176,43 @@ function getDeprecationFromDeclarations(declarations) { if (tsutils_1.isVariableDeclarationList(declaration)) { declaration = declaration.parent; } - for (var _a = 0, _b = declaration.getChildren(); _a < _b.length; _a++) { - var child = _b[_a]; - if (!tsutils_1.isJsDoc(child)) { - break; - } - if (child.tags === undefined) { - continue; - } - for (var _c = 0, _d = child.tags; _c < _d.length; _c++) { - var tag = _d[_c]; - if (tag.tagName.text === "deprecated") { - return tag.comment === undefined ? "" : tag.comment; - } + var result = getDeprecationFromDeclaration(declaration); + if (result !== undefined) { + return result; + } + } + return undefined; +} +function getDeprecationFromDeclaration(declaration) { + for (var _i = 0, _a = declaration.getChildren(); _i < _a.length; _i++) { + var child = _a[_i]; + if (!tsutils_1.isJsDoc(child)) { + break; + } + if (child.tags === undefined) { + continue; + } + for (var _b = 0, _c = child.tags; _b < _c.length; _b++) { + var tag = _c[_b]; + if (tag.tagName.text === "deprecated") { + return tag.comment === undefined ? "" : tag.comment; } } } return undefined; } +function isFunctionOrMethod(declarations) { + if (declarations === undefined || declarations.length === 0) { + return false; + } + switch (declarations[0].kind) { + case ts.SyntaxKind.MethodDeclaration: + case ts.SyntaxKind.FunctionDeclaration: + case ts.SyntaxKind.FunctionExpression: + case ts.SyntaxKind.MethodSignature: + return true; + default: + return false; + } +} var _a; |