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

Rule Proposal: func-name-matching #6065

Closed
mysticatea opened this issue May 4, 2016 · 22 comments
Closed

Rule Proposal: func-name-matching #6065

mysticatea opened this issue May 4, 2016 · 22 comments
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Comments

@mysticatea
Copy link
Member

mysticatea commented May 4, 2016

From requireMatchingFunctionName.

This rule will enforce function expression names with the variable name (or property name) which is the destination of the assignment.

Options

{
    "func-name-matching": ["error", {"module.exports": "ignore"}]
}
  • module.exports - The setting for module.exports = function foo() {};
    • "ignore" (default) - The function is ignored.
    • "propertyName" - The function's name should be exports. Original JSCS rule has this option.
    • (in my head, in future) "fileName" - The function's name should match camelcase of the file name.

Examples:

/*eslint func-name-matching: "error"*/

// ✔ GOOD ---------------------------------------------------------------------
let foo = function foo() {};
foo = function foo() {};
obj.foo = function foo() {};
let obj = {foo: function foo() {}};

// ignore anonymous function expressions.
// it's warned by `func-names` rule.
let foo = function() {};
foo = function() {};
obj.foo = function() {};
let obj = {foo: function() {}};

// ignore it if it's not assigned
foo(function bar() {});

// ignore 'module.exports' by default.
module.exports = function foo() {};


// ✘ BAD ----------------------------------------------------------------------
let foo = function bar() {};
foo = function bar() {};
obj.foo = function bar() {};
let obj = {foo: function bar() {}};

// Should warn simple computed properties from original JSCS rule.
obj["foo"] = function bar() {};
let obj = {["foo"]: function bar() {}};
@mysticatea mysticatea added rule Relates to ESLint's core rules feature This change adds a new feature to ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels May 4, 2016
@mysticatea mysticatea added this to the JSCS Compatibility milestone May 4, 2016
@alberto
Copy link
Member

alberto commented May 4, 2016

Should this be added as an option for func-names instead?

Also there is an additional option to also check module exports

@mysticatea
Copy link
Member Author

In the first proposal, this had been merged to func-names rule: #6059
But I got aware of those 2 rules are orthogonal. This rule checks names if function expression names exist, otherwise does nothing.

I thought the name exports has no mean.
But original JSCS rule has the option, so we can add the option.

@alberto
Copy link
Member

alberto commented May 6, 2016

That's why I proposed an option. Values could be always, never and matching.

@mysticatea
Copy link
Member Author

mysticatea commented May 6, 2016

I think 4 options are needed in that case: always, never, matching-if-exist, matching-always
So, I think the separation is proper. 2 x 2.

@alberto
Copy link
Member

alberto commented May 6, 2016

Oh, now I see. It could also be the 3 options mentioned and a refinement for always. I was proposing adding them to func-names to avoid conflicts between the rules, but it would be more of a misconfiguration to enable both this rule and func-names: ["error", "never"].

@mysticatea
Copy link
Member Author

mysticatea commented May 6, 2016

Separated version:

func-names func-name-matching
off off allows anything
off on requires matching if name exists
always off requires any names
always on requires matched names (in callback, any names)
never off disallow names
never on disallow names (func-name-matching has no effect)

And an option -- includeModuleExports in func-name-matching?


Merged version, maybe:

func-names
off allows anything
matching requires matching if name exists
always requires any names
always-matching requires matched names (in callback, any names)
never disallow names

And an option -- matchingIncludeModuleExports?

@mysticatea
Copy link
Member Author

@eslint/eslint-team Any insights about combining?

@alberto
Copy link
Member

alberto commented May 14, 2016

Now that I read this again I am not sure it is a big problem if the rules are separated.
I was thinking there would be a conflict between func-names: never and func-name-matching: on

Given this code:

let foo = function bar() {};

We would get two errors on bar, one saying "Unexpected named function expression bar" and the other "Function name does not match property name". But it's less of a conflict than I tought at first, so I'm ok with separate rules.

@mikesherov
Copy link
Contributor

This seems like two rules to me, like @alberto said.

@alberto
Copy link
Member

alberto commented May 14, 2016

heh, it was actually @mysticatea who was proposing that. I just backed out 😅

@nzakas
Copy link
Member

nzakas commented May 15, 2016

I also think having two rules makes the most sense.

@mysticatea
Copy link
Member Author

OK, I saw consensus.
I updated an option of this rule to fit to original JSCS rule.

In future, I hope the option to match the containing file's name for module.exports and export default. Of course, I'm happy if it's now.

@alberto
Copy link
Member

alberto commented May 27, 2016

@eslint/eslint-team opinions?

I can champion this one

@alberto alberto self-assigned this May 27, 2016
@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jul 21, 2016
@nzakas
Copy link
Member

nzakas commented Jul 21, 2016

Per TSC meeting (7/21/16), marking as accepted.

@annie
Copy link
Contributor

annie commented Sep 1, 2016

i can work on this!

@alberto
Copy link
Member

alberto commented Sep 1, 2016

Go ahead @azhang496!

@kaicataldo
Copy link
Member

Thanks for all the recent contributions to ESLint, @azhang496!

@vitorbal
Copy link
Member

vitorbal commented Sep 2, 2016

Yeah, thanks, @azhang496! Great job :)

@annie
Copy link
Contributor

annie commented Sep 2, 2016

thanks everyone for being so welcoming! :)

@annie
Copy link
Contributor

annie commented Sep 2, 2016

should there be an option for other reserved/ignored words in addition to module.exports?

@nzakas
Copy link
Member

nzakas commented Sep 2, 2016

I think we can just special case module exports for now and leave it open in case other special cases pop up later.

I would suggest, though a slightly different format for the option, as I think the original is unclear:

{
    "func-name-matching": ["error", { includeCommonJSExports: false  }]
}

Where includeCommonJSExports is false by default and true to enable. I think that's a lot clearer and also disambiguates between CommonJS modules and ES modules.

Sorry @mysticatea , I think the filename match doesn't belong in this rule. :)

@mysticatea
Copy link
Member Author

Sorry @mysticatea , I think the filename match doesn't belong in this rule. :)

I'm OK.
Just one point, includeCommonJSExports sounds that it will change the behavior of both module.exports and exports.foo. What about includeCommonJSDefaultExports or includeCommonJSModuleExports?

annie added a commit to annie/eslint that referenced this issue Sep 6, 2016
annie added a commit to annie/eslint that referenced this issue Sep 7, 2016
annie added a commit to annie/eslint that referenced this issue Sep 18, 2016
annie added a commit to annie/eslint that referenced this issue Sep 20, 2016
annie added a commit to annie/eslint that referenced this issue Sep 26, 2016
annie added a commit to annie/eslint that referenced this issue Oct 7, 2016
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
No open projects
Development

No branches or pull requests

7 participants