Trying my hand at creating a custom TypeScript-eslint rule that requires the callee's object of a node to be of a specific type, which I'll refer to as MyType
. Utilizing the CallExpression from typescript-eslint in the code snippet below:
Source code:
interface MyType {
...
}
const someVariable: MyType = ...;
Custom TypeScript-eslint rule:
export const rule = createRule({
name: 'specific-rule',
meta: {
type: 'problem',
docs: {
description: 'detailed description',
recommended: 'error',
},
schema: [],
messages,
},
defaultOptions: [],
create: (context) => {
return {
CallExpression(node: TSESTree.CallExpression) {
const services = ESLintUtils.getParserServices(context);
const checker = services.program.getTypeChecker();
const callee = node.callee;
const object = callee.type === 'MemberExpression' ? callee.object : null;
if (object === null) {
return;
}
const type = checker.getTypeAtLocation(services.esTreeNodeToTSNodeMap.get(object));
const isMyType = (
object.type === 'Identifier' &&
(type as any)?.symbol?.escapedName === 'MyType'
);
if (isMyType) {
[... perform necessary actions here]
}
},
};
},
});
Encountering an issue where the symbol or escapedName is undefined, preventing it from being equal to 'MyType'. Is there a more effective method to verify if a callee's object matches a specific type?