UNPKG

eslint-plugin-dtslint

Version:
179 lines (178 loc) 6.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDeprecation = exports.isDeclaration = void 0; const tslib_1 = require("tslib"); const tsutils = tslib_1.__importStar(require("tsutils")); const ts = tslib_1.__importStar(require("typescript")); function isDeclaration(identifier) { const parent = identifier.parent; switch (parent.kind) { case ts.SyntaxKind.ClassDeclaration: case ts.SyntaxKind.ClassExpression: case ts.SyntaxKind.InterfaceDeclaration: case ts.SyntaxKind.TypeParameter: case ts.SyntaxKind.FunctionExpression: case ts.SyntaxKind.FunctionDeclaration: case ts.SyntaxKind.LabeledStatement: case ts.SyntaxKind.JsxAttribute: case ts.SyntaxKind.MethodDeclaration: case ts.SyntaxKind.MethodSignature: case ts.SyntaxKind.PropertySignature: case ts.SyntaxKind.TypeAliasDeclaration: case ts.SyntaxKind.GetAccessor: case ts.SyntaxKind.SetAccessor: case ts.SyntaxKind.EnumDeclaration: case ts.SyntaxKind.ModuleDeclaration: return true; case ts.SyntaxKind.VariableDeclaration: case ts.SyntaxKind.Parameter: case ts.SyntaxKind.PropertyDeclaration: case ts.SyntaxKind.EnumMember: case ts.SyntaxKind.ImportEqualsDeclaration: return parent.name === identifier; case ts.SyntaxKind.PropertyAssignment: return (parent.name === identifier && !tsutils.isReassignmentTarget(identifier.parent.parent)); case ts.SyntaxKind.BindingElement: return (parent.name === identifier && parent.propertyName !== undefined); default: return false; } } exports.isDeclaration = isDeclaration; function getCallExpresion(node) { let parent = node.parent; if (tsutils.isPropertyAccessExpression(parent) && parent.name === node) { node = parent; parent = node.parent; } return tsutils.isTaggedTemplateExpression(parent) || ((tsutils.isCallExpression(parent) || tsutils.isNewExpression(parent)) && parent.expression === node) ? parent : undefined; } function getDeprecation(node, tc) { const callExpression = getCallExpresion(node); if (callExpression !== undefined) { const result = getSignatureDeprecation(tc.getResolvedSignature(callExpression)); if (result !== undefined) { return result; } } let symbol; const parent = node.parent; if (parent.kind === ts.SyntaxKind.BindingElement) { symbol = tc.getTypeAtLocation(parent.parent).getProperty(node.text); } else if ((tsutils.isPropertyAssignment(parent) && parent.name === node) || (tsutils.isShorthandPropertyAssignment(parent) && parent.name === node && tsutils.isReassignmentTarget(node))) { symbol = tc.getPropertySymbolOfDestructuringAssignment(node); } else { symbol = tc.getSymbolAtLocation(node); } if (symbol !== undefined && tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) { symbol = tc.getAliasedSymbol(symbol); } if (symbol === undefined || (callExpression !== undefined && isFunctionOrMethod(symbol.declarations))) { return undefined; } return getSymbolDeprecation(symbol); } exports.getDeprecation = getDeprecation; function findDeprecationTag(tags) { for (const tag of tags) { if (tag.name === "deprecated") { if (tag.text === undefined) { return ""; } else if (typeof tag.text === "string") { return tag.text; } else { return tag.text.reduce((text, part) => text.concat(part.text), ""); } } } return undefined; } function getSymbolDeprecation(symbol) { if (symbol.getJsDocTags !== undefined) { return findDeprecationTag(symbol.getJsDocTags()); } return getDeprecationFromDeclarations(symbol.declarations); } function getSignatureDeprecation(signature) { if (signature === undefined) { return undefined; } if (signature.getJsDocTags !== undefined) { return findDeprecationTag(signature.getJsDocTags()); } return signature.declaration === undefined ? undefined : getDeprecationFromDeclaration(signature.declaration); } function getDeprecationFromDeclarations(declarations) { if (declarations === undefined) { return undefined; } let declaration; for (declaration of declarations) { if (tsutils.isBindingElement(declaration)) { declaration = tsutils.getDeclarationOfBindingElement(declaration); } if (tsutils.isVariableDeclaration(declaration)) { declaration = declaration.parent; } if (tsutils.isVariableDeclarationList(declaration)) { declaration = declaration.parent; } const result = getDeprecationFromDeclaration(declaration); if (result !== undefined) { return result; } } return undefined; } function getDeprecationFromDeclaration(declaration) { for (const comment of tsutils.getJsDoc(declaration)) { if (comment.tags === undefined) { continue; } for (const tag of comment.tags) { if (tag.tagName.text === "deprecated") { if (tag.comment === undefined) { return ""; } else if (typeof tag.comment === "string") { return tag.comment; } else { return tag.comment.reduce((text, part) => text.concat(part.text), ""); } } } } 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; } }