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): added allowSuper option for unbound-method #1925

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/eslint-plugin/docs/rules/unbound-method.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const innerLog = () => instance.logBound();
The rule accepts an options object with the following property:

- `ignoreStatic` to not check whether `static` methods are correctly bound
- `allowSuper` allows `Super` class methods to bound with base class.

### `ignoreStatic`

Expand All @@ -70,14 +71,33 @@ const { log } = OtherClass;
log();
```

### `allowSuper`

Examples of **correct** code for this rule with `{ allowSuper: true }`:

```ts
class SuperClass {
method1(){ ... }
}

class baseClass extend SuperClass {
constructor(){
super();
this.baseVar = super.method1;
}
}

```

### Example

```json
{
"@typescript-eslint/unbound-method": [
"error",
{
"ignoreStatic": true
"ignoreStatic": true,
"allowSuper": false
}
]
}
Expand Down
19 changes: 15 additions & 4 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as util from '../util';

interface Config {
ignoreStatic: boolean;
allowSuper?: boolean;
}

export type Options = [Config];
Expand Down Expand Up @@ -138,6 +139,9 @@ export default util.createRule<Options, MessageIds>({
ignoreStatic: {
type: 'boolean',
},
allowSuper: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -147,9 +151,10 @@ export default util.createRule<Options, MessageIds>({
defaultOptions: [
{
ignoreStatic: false,
allowSuper: false,
},
],
create(context, [{ ignoreStatic }]) {
create(context, [{ ignoreStatic, allowSuper }]) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const currentSourceFile = parserServices.program.getSourceFile(
Expand All @@ -160,7 +165,7 @@ export default util.createRule<Options, MessageIds>({
'MemberExpression, OptionalMemberExpression'(
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression,
): void {
if (isSafeUse(node)) {
if (isSafeUse(node, allowSuper)) {
return;
}

Expand Down Expand Up @@ -217,7 +222,7 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean {
return false;
}

function isSafeUse(node: TSESTree.Node): boolean {
function isSafeUse(node: TSESTree.Node, allowSuper = false): boolean {
const parent = node.parent;

switch (parent?.type) {
Expand Down Expand Up @@ -250,7 +255,13 @@ function isSafeUse(node: TSESTree.Node): boolean {
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);

case AST_NODE_TYPES.AssignmentExpression:
return parent.operator === '=' && node === parent.left;
return (
parent.operator === '=' &&
(node === parent.left ||
(allowSuper &&
(node as TSESTree.MemberExpression) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe - prefer an explicit check.

Suggested change
(node as TSESTree.MemberExpression) &&
node.type === AST_NODE_TYPES.MemberExpression &&

node.object.type === AST_NODE_TYPES.Super))
);

case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
Expand Down
59 changes: 59 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ ruleTester.run('unbound-method', rule, {
"['1', '2', '3'].map(Number.parseInt);",
'[5.2, 7.1, 3.6].map(Math.floor);',
'const x = console.log;',
{
code: `
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
}

class OtherClass extends BaseClass {
superLogThis: any;
constructor() {
super();
this.superLogThis = super.logThis;
}
}

const oc = new OtherClass();
oc.superLogThis();
`,
options: [
{
allowSuper: true,
},
],
},
...[
'instance.bound();',
'instance.unbound();',
Expand Down Expand Up @@ -384,5 +410,38 @@ const unbound = new Foo().unbound;
},
],
},
{
code: `
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
}

class OtherClass extends BaseClass {
superLogThis: any;
constructor() {
super();
this.superLogThis = super.logThis;
}
}

const oc = new OtherClass();
oc.superLogThis();
`,
options: [
{
allowSuper: false,
},
],
errors: [
{
line: 13,
column: 25,
messageId: 'unbound',
},
],
},
],
Comment on lines +445 to 446
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a sanity check, this should report an error

class BaseClass {
  x: number = 42;
  logThis() {
    console.log('x is ');
  }
}
class OtherClass extends BaseClass {
  superLogThis: any;
  constructor() {
    super();
    const x = super.logThis; // ERROR - unbound method
  }
}

});