Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/etc/get-type-services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ESLintUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import ts from 'typescript';
import { couldBeFunction } from './could-be-function';
import { couldBeType as tsutilsEtcCouldBeType } from './could-be-type';
Expand All @@ -18,7 +17,7 @@ export function getTypeServices<
name: string | RegExp,
qualified?: { name: RegExp },
): boolean => {
const type = getType(node);
const type = getTypeAtLocation(node);
return tsutilsEtcCouldBeType(
type,
name,
Expand Down Expand Up @@ -56,19 +55,14 @@ export function getTypeServices<
);
};

const getType = (node: TSESTree.Node): ts.Type => {
return getTypeAtLocation(node);
};

return {
couldBeBehaviorSubject: (node: TSESTree.Node) =>
couldBeType(node, 'BehaviorSubject'),
couldBeError: (node: TSESTree.Node) => couldBeType(node, 'Error'),
couldBeFunction: (node: TSESTree.Node) => {
if (isArrowFunctionExpression(node) || isFunctionDeclaration(node)) {
return true;
}
return couldBeFunction(getType(node));
return couldBeFunction(getTypeAtLocation(node));
},
couldBeMonoTypeOperatorFunction: (node: TSESTree.Node) =>
couldBeType(node, 'MonoTypeOperatorFunction'),
Expand All @@ -79,10 +73,5 @@ export function getTypeServices<
couldReturnObservable: (node: TSESTree.Node) =>
couldReturnType(node, 'Observable'),
couldReturnType,
getType,
isAny: (node: TSESTree.Node) => tsutils.isIntrinsicAnyType(getType(node)),
isReferenceType: (node: TSESTree.Node) => tsutils.isTypeReference(getType(node)),
isUnknown: (node: TSESTree.Node) => tsutils.isIntrinsicUnknownType(getType(node)),
typeChecker,
};
}
11 changes: 6 additions & 5 deletions src/rules/no-cyclic-action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TSESTree as es } from '@typescript-eslint/utils';
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
import { stripIndent } from 'common-tags';
import ts from 'typescript';
import { defaultObservable } from '../constants';
import { getTypeServices, isCallExpression, isIdentifier } from '../etc';
import { isCallExpression, isIdentifier } from '../etc';
import { ruleCreator } from '../utils';

function isTypeReference(type: ts.Type): type is ts.TypeReference {
Expand Down Expand Up @@ -44,7 +44,8 @@ export const noCyclicActionRule = ruleCreator({
const { observable = defaultObservable } = config;
const observableRegExp = new RegExp(observable);

const { getType, typeChecker } = getTypeServices(context);
const { getTypeAtLocation, program } = ESLintUtils.getParserServices(context);
const typeChecker = program.getTypeChecker();

function checkNode(pipeCallExpression: es.CallExpression) {
const operatorCallExpression = pipeCallExpression.arguments.find(
Expand All @@ -56,7 +57,7 @@ export const noCyclicActionRule = ruleCreator({
if (!operatorCallExpression) {
return;
}
const operatorType = getType(operatorCallExpression);
const operatorType = getTypeAtLocation(operatorCallExpression);
const [signature] = typeChecker.getSignaturesOfType(
operatorType,
ts.SignatureKind.Call,
Expand All @@ -75,7 +76,7 @@ export const noCyclicActionRule = ruleCreator({
return;
}

const pipeType = getType(pipeCallExpression);
const pipeType = getTypeAtLocation(pipeCallExpression);
if (!isTypeReference(pipeType)) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/rules/no-unbound-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESTree as es } from '@typescript-eslint/utils';
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
import {
getTypeServices,
isCallExpression,
Expand All @@ -21,13 +21,13 @@ export const noUnboundMethodsRule = ruleCreator({
},
name: 'no-unbound-methods',
create: (context) => {
const { couldBeObservable, couldBeSubscription, getType }
= getTypeServices(context);
const { getTypeAtLocation } = ESLintUtils.getParserServices(context);
const { couldBeObservable, couldBeSubscription } = getTypeServices(context);
const nodeMap = new WeakMap<es.Node, void>();

function mapArguments(node: es.CallExpression | es.NewExpression) {
node.arguments.filter(isMemberExpression).forEach((arg) => {
const argType = getType(arg);
const argType = getTypeAtLocation(arg);
if (argType.getCallSignatures().length > 0) {
nodeMap.set(arg);
}
Expand Down
12 changes: 7 additions & 5 deletions src/rules/no-unsafe-subject-next.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TSESTree as es } from '@typescript-eslint/utils';
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import ts from 'typescript';
import {
couldBeType,
getTypeServices,
isMemberExpression } from '../etc';
isMemberExpression,
} from '../etc';
import { ruleCreator } from '../utils';

export const noUnsafeSubjectNext = ruleCreator({
Expand All @@ -23,13 +23,15 @@ export const noUnsafeSubjectNext = ruleCreator({
},
name: 'no-unsafe-subject-next',
create: (context) => {
const { getType, typeChecker } = getTypeServices(context);
const { getTypeAtLocation, program } = ESLintUtils.getParserServices(context);
const typeChecker = program.getTypeChecker();

return {
[`CallExpression[callee.property.name='next']`]: (
node: es.CallExpression,
) => {
if (node.arguments.length === 0 && isMemberExpression(node.callee)) {
const type = getType(node.callee.object);
const type = getTypeAtLocation(node.callee.object);
if (tsutils.isTypeReference(type) && couldBeType(type, 'Subject')) {
const [typeArg] = typeChecker.getTypeArguments(type);
if (tsutils.isTypeFlagSet(typeArg, ts.TypeFlags.Any)) {
Expand Down
6 changes: 3 additions & 3 deletions src/rules/throw-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const throwErrorRule = ruleCreator({
},
name: 'throw-error',
create: (context) => {
const { esTreeNodeToTSNodeMap, program } = ESLintUtils.getParserServices(context);
const { couldBeObservable, getType } = getTypeServices(context);
const { esTreeNodeToTSNodeMap, program, getTypeAtLocation } = ESLintUtils.getParserServices(context);
const { couldBeObservable } = getTypeServices(context);

function checkNode(node: es.Node) {
let type = getType(node);
let type = getTypeAtLocation(node);
if (couldBeFunction(type)) {
const tsNode = esTreeNodeToTSNodeMap.get(node);
const annotation = (tsNode as ts.ArrowFunction).type;
Expand Down