Skip to content

Commit a242cfa

Browse files
refactor: remove getType and typeChecker from getTypeServices
1 parent cc439b1 commit a242cfa

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

src/etc/get-type-services.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function getTypeServices<
1818
name: string | RegExp,
1919
qualified?: { name: RegExp },
2020
): boolean => {
21-
const type = getType(node);
21+
const type = getTypeAtLocation(node);
2222
return tsutilsEtcCouldBeType(
2323
type,
2424
name,
@@ -56,10 +56,6 @@ export function getTypeServices<
5656
);
5757
};
5858

59-
const getType = (node: TSESTree.Node): ts.Type => {
60-
return getTypeAtLocation(node);
61-
};
62-
6359
return {
6460
couldBeBehaviorSubject: (node: TSESTree.Node) =>
6561
couldBeType(node, 'BehaviorSubject'),
@@ -68,7 +64,7 @@ export function getTypeServices<
6864
if (isArrowFunctionExpression(node) || isFunctionDeclaration(node)) {
6965
return true;
7066
}
71-
return couldBeFunction(getType(node));
67+
return couldBeFunction(getTypeAtLocation(node));
7268
},
7369
couldBeMonoTypeOperatorFunction: (node: TSESTree.Node) =>
7470
couldBeType(node, 'MonoTypeOperatorFunction'),
@@ -79,10 +75,8 @@ export function getTypeServices<
7975
couldReturnObservable: (node: TSESTree.Node) =>
8076
couldReturnType(node, 'Observable'),
8177
couldReturnType,
82-
getType,
83-
isAny: (node: TSESTree.Node) => tsutils.isIntrinsicAnyType(getType(node)),
84-
isReferenceType: (node: TSESTree.Node) => tsutils.isTypeReference(getType(node)),
85-
isUnknown: (node: TSESTree.Node) => tsutils.isIntrinsicUnknownType(getType(node)),
86-
typeChecker,
78+
isAny: (node: TSESTree.Node) => tsutils.isIntrinsicAnyType(getTypeAtLocation(node)),
79+
isReferenceType: (node: TSESTree.Node) => tsutils.isTypeReference(getTypeAtLocation(node)),
80+
isUnknown: (node: TSESTree.Node) => tsutils.isIntrinsicUnknownType(getTypeAtLocation(node)),
8781
};
8882
}

src/rules/no-cyclic-action.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { TSESTree as es } from '@typescript-eslint/utils';
1+
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
22
import { stripIndent } from 'common-tags';
33
import ts from 'typescript';
44
import { defaultObservable } from '../constants';
5-
import { getTypeServices, isCallExpression, isIdentifier } from '../etc';
5+
import { isCallExpression, isIdentifier } from '../etc';
66
import { ruleCreator } from '../utils';
77

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

47-
const { getType, typeChecker } = getTypeServices(context);
47+
const { getTypeAtLocation, program } = ESLintUtils.getParserServices(context);
48+
const typeChecker = program.getTypeChecker();
4849

4950
function checkNode(pipeCallExpression: es.CallExpression) {
5051
const operatorCallExpression = pipeCallExpression.arguments.find(
@@ -56,7 +57,7 @@ export const noCyclicActionRule = ruleCreator({
5657
if (!operatorCallExpression) {
5758
return;
5859
}
59-
const operatorType = getType(operatorCallExpression);
60+
const operatorType = getTypeAtLocation(operatorCallExpression);
6061
const [signature] = typeChecker.getSignaturesOfType(
6162
operatorType,
6263
ts.SignatureKind.Call,
@@ -75,7 +76,7 @@ export const noCyclicActionRule = ruleCreator({
7576
return;
7677
}
7778

78-
const pipeType = getType(pipeCallExpression);
79+
const pipeType = getTypeAtLocation(pipeCallExpression);
7980
if (!isTypeReference(pipeType)) {
8081
return;
8182
}

src/rules/no-unbound-methods.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TSESTree as es } from '@typescript-eslint/utils';
1+
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
22
import {
33
getTypeServices,
44
isCallExpression,
@@ -21,13 +21,13 @@ export const noUnboundMethodsRule = ruleCreator({
2121
},
2222
name: 'no-unbound-methods',
2323
create: (context) => {
24-
const { couldBeObservable, couldBeSubscription, getType }
25-
= getTypeServices(context);
24+
const { getTypeAtLocation } = ESLintUtils.getParserServices(context);
25+
const { couldBeObservable, couldBeSubscription } = getTypeServices(context);
2626
const nodeMap = new WeakMap<es.Node, void>();
2727

2828
function mapArguments(node: es.CallExpression | es.NewExpression) {
2929
node.arguments.filter(isMemberExpression).forEach((arg) => {
30-
const argType = getType(arg);
30+
const argType = getTypeAtLocation(arg);
3131
if (argType.getCallSignatures().length > 0) {
3232
nodeMap.set(arg);
3333
}

src/rules/no-unsafe-subject-next.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { TSESTree as es } from '@typescript-eslint/utils';
1+
import { TSESTree as es, ESLintUtils } from '@typescript-eslint/utils';
22
import * as tsutils from 'ts-api-utils';
33
import ts from 'typescript';
44
import {
55
couldBeType,
6-
getTypeServices,
7-
isMemberExpression } from '../etc';
6+
isMemberExpression,
7+
} from '../etc';
88
import { ruleCreator } from '../utils';
99

1010
export const noUnsafeSubjectNext = ruleCreator({
@@ -23,13 +23,15 @@ export const noUnsafeSubjectNext = ruleCreator({
2323
},
2424
name: 'no-unsafe-subject-next',
2525
create: (context) => {
26-
const { getType, typeChecker } = getTypeServices(context);
26+
const { getTypeAtLocation, program } = ESLintUtils.getParserServices(context);
27+
const typeChecker = program.getTypeChecker();
28+
2729
return {
2830
[`CallExpression[callee.property.name='next']`]: (
2931
node: es.CallExpression,
3032
) => {
3133
if (node.arguments.length === 0 && isMemberExpression(node.callee)) {
32-
const type = getType(node.callee.object);
34+
const type = getTypeAtLocation(node.callee.object);
3335
if (tsutils.isTypeReference(type) && couldBeType(type, 'Subject')) {
3436
const [typeArg] = typeChecker.getTypeArguments(type);
3537
if (tsutils.isTypeFlagSet(typeArg, ts.TypeFlags.Any)) {

src/rules/throw-error.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export const throwErrorRule = ruleCreator({
2020
},
2121
name: 'throw-error',
2222
create: (context) => {
23-
const { esTreeNodeToTSNodeMap, program } = ESLintUtils.getParserServices(context);
24-
const { couldBeObservable, getType } = getTypeServices(context);
23+
const { esTreeNodeToTSNodeMap, program, getTypeAtLocation } = ESLintUtils.getParserServices(context);
24+
const { couldBeObservable } = getTypeServices(context);
2525

2626
function checkNode(node: es.Node) {
27-
let type = getType(node);
27+
let type = getTypeAtLocation(node);
2828
if (couldBeFunction(type)) {
2929
const tsNode = esTreeNodeToTSNodeMap.get(node);
3030
const annotation = (tsNode as ts.ArrowFunction).type;

0 commit comments

Comments
 (0)