Мне сложно написать собственное правило, которое проверяло бы имя метода и тип объекта.Мне нужно проверить, что имя метода 'include' и объект типа Array.Как я могу достичь этого?Я нашел некоторые подсказки здесь , но я понятия не имею, как добраться до типа объекта.
Это все, что я придумал, но я получаю только имя метода с этим.
import * as ts from 'typescript';
import * as Lint from 'tslint';
/**
* Rule for checking if project uses non IE supportive functions and errors them
* To build run command 'tsc src\rules\noIeFunctionsRule.ts --lib 'es6,dom''
*/
export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: 'no-ie-functions',
description: 'Disallow use of Internet Explorer uncompactible functions',
optionsDescription: 'Not configurable.',
options: null,
optionExamples: [true],
rationale: Lint.Utils.dedent`
When function or constructor may be called with a type parameter but one isn't supplied or inferrable,
TypeScript defaults to \`{}\`.
This is often undesirable as the call is meant to be of a more specific type.
`,
type: 'functionality',
typescriptOnly: true,
requiresTypeInfo: true,
};
public static METHOD_INCLUDES = 'Internet Explorer does not support this method, use "indexOf(value) > 0" instead';
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(new IeWalker(sourceFile, this.ruleName, program.getTypeChecker()));
}
}
class IeWalker extends Lint.AbstractWalker<void> {
constructor(sourceFile: ts.SourceFile, ruleName: string, private readonly checker: ts.TypeChecker) {
super(sourceFile, ruleName, undefined);
}
public walk(sourceFile: ts.SourceFile): void {
const cb = (node: ts.Node): void => {
if (node.kind === ts.SyntaxKind.CallExpression) {
this.checkCallExpression(node as ts.CallExpression);
}
return ts.forEachChild(node, cb);
};
return ts.forEachChild(sourceFile, cb);
}
private checkCallExpression(node: ts.CallExpression) {
if (node.expression.getText().includes('includes')) {
this.addFailureAtNode(node, Rule.METHOD_INCLUDES);
}
}
}