Skip to content

Instantly share code, notes, and snippets.

@awk
Created July 29, 2019 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awk/fed5266c44c929d6a28ac762b1a9c324 to your computer and use it in GitHub Desktop.
Save awk/fed5266c44c929d6a28ac762b1a9c324 to your computer and use it in GitHub Desktop.
WIP: no-patched rule for eslint-plugin-rxjs
import { ESLintUtils, TSESTree, AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import * as es from "estree";
import * as ts from "typescript";
import { isPropertyAccessExpression } from "tsutils";
import { getParserServices } from "../utils";
const createRule = ESLintUtils.RuleCreator(name => name);
type MessageIds = 'forbidden';
type Config = {
allowedObservables?: string[];
allowAllObservables?: boolean;
allowedOperators?: string[];
allowAllOperators?: boolean;
};
type Options = [Config];
export default createRule<Options, MessageIds>({
name: 'no-patched',
meta: {
docs: {
description: "Forbids the calling of patched methods.",
category: "Best Practices",
recommended: false
},
fixable: null,
messages: {
forbidden: "Calling patched methods is forbidden.",
},
schema: [
{
properties: {
allowAllObservables: { type: "boolean" },
allowedObservables: { type: "array", items: { type: "string"}},
allowAllOperators: { type: "boolean" },
allowedOperators: { type: "array", items: { type: "string"}}
},
type: "object"
}
],
type: "suggestion"
},
defaultOptions: [{allowAllObservables: false, allowedObservables: [], allowAllOperators: false, allowedOperators: []}],
create(context, options: Options) {
const service = getParserServices(<any> context);
const sourceCode = context.getSourceCode();
const typeChecker = service.program.getTypeChecker();
return {
CallExpression(node: TSESTree.CallExpression) {
const { callee } = node;
const { expression } = service.esTreeNodeToTSNodeMap.get(node as es.CallExpression) as ts.PropertyAccessExpression;
if (isPropertyAccessExpression(expression)) {
const type = typeChecker.getTypeAtLocation(expression);
// TODO: Establish that the type of left hand side of the expression is 'Observable' and then look for the right hand side in a list of known operators
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment