Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): new extended rule 'no-invalid-this' #1823

Merged
merged 11 commits into from May 4, 2020
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/no-invalid-this.md
Expand Up @@ -3,7 +3,7 @@
## Rule Details

This rule extends the base [`eslint/no-invalid-this`](https://eslint.org/docs/rules/no-invalid-this) rule.
It supports all options and features of the base rule.
It adds support for TypeScript's `this` parameters.

## How to use

Expand Down
44 changes: 18 additions & 26 deletions packages/eslint-plugin/src/rules/no-invalid-this.ts
@@ -1,29 +1,17 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/no-invalid-this';
import {
InferOptionsTypeFromRule,
InferMessageIdsTypeFromRule,
createRule,
deepMerge,
} from '../util';

export type Options = InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;

const schema = deepMerge(
Array.isArray(baseRule.meta.schema)
? baseRule.meta.schema[0]
: baseRule.meta.schema,
{
properties: {
capIsConstructor: {
type: 'boolean',
default: true,
},
},
},
);

export default createRule<Options, MessageIds>({
name: 'no-invalid-this',
meta: {
Expand All @@ -36,20 +24,22 @@ export default createRule<Options, MessageIds>({
extendsBaseRule: true,
},
messages: baseRule.meta.messages,
schema: [schema],
schema: baseRule.meta.schema,
},
defaultOptions: [{ capIsConstructor: true }],
create(context) {
const rules = baseRule.create(context);
let argList: Array<string[]> = [];
const argList: Array<string[]> = [];

return {
...rules,
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void {
const names = node?.params.map(
(param: TSESTree.Identifier) => param?.name,
argList.push(
node?.params.some(
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
(param: TSESTree.Identifier) =>
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
param.type === AST_NODE_TYPES.Identifier && param.name === 'this',
),
);
argList.push(names);
// baseRule's work
rules.FunctionDeclaration(node);
},
Expand All @@ -59,10 +49,12 @@ export default createRule<Options, MessageIds>({
rules['FunctionDeclaration:exit'](node);
},
FunctionExpression(node: TSESTree.FunctionExpression): void {
const names = node?.params.map(
(param: TSESTree.Identifier) => param.name,
argList.push(
node?.params.some(
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
(param: TSESTree.Identifier) =>
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
param.type === AST_NODE_TYPES.Identifier && param.name === 'this',
),
);
argList.push(names);
// baseRule's work
rules.FunctionExpression(node);
},
Expand All @@ -71,10 +63,10 @@ export default createRule<Options, MessageIds>({
// baseRule's work
rules['FunctionExpression:exit'](node);
},
ThisExpression(node: TSESTree.ThisExpression) {
ThisExpression(node: TSESTree.ThisExpression): void {
const lastFnArg = argList[argList.length - 1];

if (lastFnArg?.some((name: string) => name === 'this')) {
if (lastFnArg) {
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down