Skip to content

Commit

Permalink
chore(utils): Use tsutils-etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Feb 23, 2019
1 parent e645fa2 commit ba68ff0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 121 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"@phenomnomnominal/tsquery": "^3.0.0",
"tslib": "^1.8.0",
"tsutils": "^3.0.0"
"tsutils": "^3.0.0",
"tsutils-etc": "^1.0.0"
},
"description": "More rules for TSLint",
"devDependencies": {
Expand Down
121 changes: 1 addition & 120 deletions source/support/util.ts
Expand Up @@ -3,123 +3,4 @@
* can be found in the LICENSE file at https://github.com/cartant/tslint-etc
*/

import * as ts from "typescript";
import * as tsutils from "tsutils";
import { InternalSymbolName } from "typescript";

export function couldBeFunction(type: ts.Type): boolean {
return (type.getCallSignatures().length > 0) ||
couldBeType(type, "Function") ||
couldBeType(type, "ArrowFunction") ||
couldBeType(type, InternalSymbolName.Function);
}

export function couldBeType(type: ts.Type, name: string | RegExp): boolean {

if (isReferenceType(type)) {
type = type.target;
}

if (isType(type, name)) {
return true;
}

if (isUnionType(type)) {
return type.types.some((t) => couldBeType(t, name));
}

const baseTypes = type.getBaseTypes();
return Boolean(baseTypes) && baseTypes.some((t) => couldBeType(t, name));
}

export function findDeclaration(node: ts.Node, typeChecker: ts.TypeChecker): ts.Declaration | undefined {

const symbol = typeChecker.getSymbolAtLocation(node);
if (!symbol) {
return undefined;
}
const declarations = symbol.getDeclarations();
if (!declarations || (declarations.length === 0)) {
return undefined;
}
const [declaration] = declarations;
return declaration;
}

export function isConstDeclaration(declaration: ts.Declaration): boolean {

let variableDeclarationList: ts.VariableDeclarationList | null = null;

if (tsutils.isVariableDeclaration(declaration)) {
if (tsutils.isVariableDeclarationList(declaration.parent)) {
variableDeclarationList = declaration.parent;
}
} else if (tsutils.isBindingElement(declaration)) {
let parent: ts.Node = declaration.parent;
while (tsutils.isBindingPattern(parent) || tsutils.isVariableDeclaration(parent)) {
parent = parent.parent;
}
if (tsutils.isVariableDeclarationList(parent)) {
variableDeclarationList = parent;
}
}

if (variableDeclarationList) {
return tsutils.getVariableDeclarationKind(variableDeclarationList) === tsutils.VariableDeclarationKind.Const;
}
return false;
}

export function isInstanceofCtor(node: ts.Node): boolean {

const { parent } = node;
return tsutils.isBinaryExpression(parent) &&
(node === parent.right) &&
(parent.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword);
}

export function isReferenceType(type: ts.Type): type is ts.TypeReference {

return tsutils.isTypeFlagSet(type, ts.TypeFlags.Object) &&
tsutils.isObjectFlagSet(type as ts.ObjectType, ts.ObjectFlags.Reference);
}

export function isThis(node: ts.Node): boolean {

return node.kind === ts.SyntaxKind.ThisKeyword;
}

export function isType(type: ts.Type, name: string | RegExp): boolean {

if (!type.symbol) {
return false;
}
return (typeof name === "string") ?
(type.symbol.name === name) :
Boolean(type.symbol.name.match(name));
}

export function isUnionType(type: ts.Type): type is ts.UnionType {

return tsutils.isTypeFlagSet(type, ts.TypeFlags.Union);
}

export function isWithinCallExpressionExpression(node: ts.Node): boolean {

let parent = node.parent;
while (parent && tsutils.isPropertyAccessExpression(parent)) {
node = parent;
parent = node.parent;
}
return parent && tsutils.isCallExpression(parent) && (node === parent.expression);
}

export function isWithinParameterDeclaration(node: ts.Node): boolean {

if (tsutils.isParameterDeclaration(node)) {
return true;
}
return tsutils.isBindingElement(node) &&
tsutils.isBindingPattern(node.parent) &&
tsutils.isParameterDeclaration(node.parent.parent);
}
export * from "tsutils-etc";
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -241,6 +241,10 @@ tslint@^5.1.0:
tslib "^1.8.0"
tsutils "^2.27.2"

tsutils-etc@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/tsutils-etc/-/tsutils-etc-1.0.0.tgz#0e98230aad1e5d488556b67312b7b395b74de7a5"

tsutils@^2.27.2:
version "2.29.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.29.0.tgz#32b488501467acbedd4b85498673a0812aca0b99"
Expand Down

0 comments on commit ba68ff0

Please sign in to comment.