Skip to content

[space-before-function-paren] this rule to abstract function not work #2192

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

Closed
adrielcodeco opened this issue Jun 8, 2020 · 8 comments
Closed
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@adrielcodeco
Copy link
Contributor

adrielcodeco commented Jun 8, 2020

Can anyone help to understand?
Im using vscode with eslint extension and the space-before-function-paren rule is not working for the abstract function

with this example code:

export abstract class CLASS {
  abstract ABSTRACT_FUNCTION(): void // not working here
  FUNCTION(): void { // working here
    // nothing
  }
}

with this config:
.eslintrc

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["@typescript-eslint", "jest"],
  "extends": [
    "plugin:jest/recommended",
    "plugin:@typescript-eslint/recommended",
    "standard-with-typescript"
  ],
  "env": {
    "jest/globals": true
  },
  "rules": {
    "space-before-function-paren": "off",
    "@typescript-eslint/space-before-function-paren": ["error", "always"]
  }
}

Expected Result
warn and fix space between function name and the opening paren

Actual Result
for abstract functions the behavior not working

Versions

package version
@typescript-eslint/eslint-plugin 3.2.0
@typescript-eslint/parser 3.2.0
TypeScript 3.9.5
ESLint 7.2.0
node 14.4.0
npm 6.14.5
@adrielcodeco adrielcodeco added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jun 8, 2020
@bradzacher
Copy link
Member

The rule doesn't currently check those functions.
Happy to accept a PR if you want to add it.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers and removed triage Waiting for team members to take a look labels Jun 8, 2020
@adrielcodeco
Copy link
Contributor Author

Ok I'll

@adrielcodeco
Copy link
Contributor Author

I'll need help to test it
I'm confused with some behaviors for the generator functions format
Where's the documentation for that? It has document?
For me it isn't consistent, sometimes the "*" are near to the function sometimes near to parentheses sometimes near to the function name
example :

    { code: 'function* foo () {}', parserOptions: { ecmaVersion: 6 } },
    { code: 'var foo = function *() {};', parserOptions: { ecmaVersion: 6 } },
    {
      code: 'var foo = function*() {};',
      options: ['never'],
      parserOptions: { ecmaVersion: 6 },
    },
    {
      code: [
        'function foo() {}',
        'var bar = function () {}',
        'function* baz() {}',
        'var bat = function*() {};',
        'var obj = { get foo() {}, set foo(val) {}, bar() {} };',
      ].join('\n'),
      options: [{ named: 'never', anonymous: 'always' }],
      parserOptions: { ecmaVersion: 6 },
    },
    {
      code: [
        'function foo () {}',
        'var bar = function() {}',
        'function* baz () {}',
        'var bat = function* () {};',
        'var obj = { get foo () {}, set foo (val) {}, bar () {} };',
      ].join('\n'),
      options: [{ named: 'always', anonymous: 'never' }],
      parserOptions: { ecmaVersion: 6 },
    },
    {
      code: 'class Foo { constructor() {} *method() {} }',
      options: [{ named: 'never', anonymous: 'always' }],
      parserOptions: { ecmaVersion: 6 },
    },
    {
      code: 'class Foo { constructor () {} *method () {} }',
      options: [{ named: 'always', anonymous: 'never' }],
      parserOptions: { ecmaVersion: 6 },
    },
  ],```

What should be the correct behavior?

@bradzacher
Copy link
Member

bradzacher commented Jun 9, 2020

the * goes between the function keyword, and the function name.
if there is no function keyword (i.e it's a method), then it just goes before the function name.
if there is no function name (i.e. it's an anonymous function expression) then it just goes after the function keyword.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*

For how the rule works, you'd best look at the base rule, which our rule extends:
https://github.com/eslint/eslint/blob/master/lib/rules/space-before-function-paren.js

It looks like if the function is a named generator function (function *foo() {}), then it treats it as a named function as normal, but if it's an anonymous generator function (const foo = function *() {}) then it completely ignores it.

@adrielcodeco
Copy link
Contributor Author

ok...

this i understood :

the * goes between the function keyword, and the function name.
if there is no function keyword (i.e it's a method), then it just goes before the function name.
if there is no function name (i.e. it's an anonymous function expression) then it just goes after the function keyword.

and thanks for it :

For how the rule works, you'd best look at the base rule, which our rule extends:
https://github.com/eslint/eslint/blob/master/lib/rules/space-before-function-paren.js

it help me to see more examples here :
https://github.com/eslint/eslint/blob/master/tests/lib/rules/space-before-function-paren.js

but my doubt is about these scenarios present in the test:

function* foo () {}
var foo = function *() {}; // not correct for me

why on first the * are near to function and on second are near to parentheses ?
it doesn't make sense to me.

for me...

if the method has the function keyword, the * must be next to the function keyword like:

function* () {}
function* foo () {}
var foo = function* () {}; // Correct for me

if the method hasn't the function keyword but has the function name, the * must be next to the function name like :

var obj = { *foo () {} };
var obj = { async *foo () {} };

what's wrong ?

@bradzacher
Copy link
Member

why on first the * are near to function and on second are near to parentheses ?
it doesn't make sense to me.

The grammar is intentionally ambiguous. As long as the * satisfies the conditions I mentioned above, it doesn't matter about the spacing - there may be spaces before, after, or both - parsers do not need to care.

Note that the exact spacing of the * is outside the scope of this rule.

This is why the const foo = function *() {} case is completely ignored by the rule. For enforcing the spacing of the *, see the generator-star-spacing rule.

@adrielcodeco
Copy link
Contributor Author

Makes sense. I was focusing on the generator and it doesn't apply here. that's my mistake. thank you very much for clarifying me.

@tadhgmister
Copy link
Contributor

PR was missing the "Fixes: #2192" so this didn't get automatically closed when the fix was merged, @adrielcodeco could you close this for house keeping?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants