eslint: `func-names` error on "inferrable names"

Hi, Excuse me for bringing issue #7235 up again, but why Foo.prototype.bar = function() {}; is incorrect when using the as-needed option? while I understand that the function’s name property would be empty, in callstacks this function will have the name Foo.bar, and I think this goes along with the TSC decision of “see the rule as ensuring names are present for debugging”. Also this is the behavior of JSCS requireNamedUnassignedFunctions (which is actually what I’m trying to restore)… Thx

— EDIT — I propose a rule change for func-names to have option to error only for unassigned functions, I am not sure whether this should be a change or a whole new rule.

What rule do you want to change? func-names

Does this change cause the rule to produce more or fewer warnings? Fewer

How will the change be implemented? (New option, new default behavior, etc.)? New option

Please provide some example code that this change will affect: Currently func-names require all function expressions to have names, even if the name can be inferred from the context. The new option will enforce the rule only if the function is unassigned, like callbacks:

setTimeout(function() {
    throw new Error('I am truly anonymous');
}, 1000);

The code below is currently “incorrect” using the func-names rule, adding an option (say: unassigned-only) will make it “correct”

function Foo() {}
Foo.prototype.baz = function() {
    throw new Error('I am not so anonymous, at least V8 knows who I am');
};

What does the rule currently do for this code? Errors - saying a name is required

What will the rule do after it’s changed? Will not error.

As I said, maybe a new rule is more appropriate here (I am currently using a custom rule I wrote to achieve that, can use it as a base for a new rule).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

In case anyone is interested in JSCS requireNamedUnassignedFunctions behavior I created an ESLint plugin: https://github.com/ValYouW/eslint-plugin-named-unassigned-functions

@ljharb That form doesn’t get an inferred names in the ES6 spec AFAIK.