Include function name in report message if possible / relevant
See original GitHub issueI have these two warning about the same function in my code.
- complexity Function ‘formatLog’ has a complexity of 12.
- max-statements This function has too many statements (25). Maximum allowed is 16.
Notice that the latter says This function
, where the former says Function 'formatLog'
. This named notation I find really helpful.
I did a quick search in the rules and found a few more mentioning a function that could potentially be named.
What rule do you want to change?
- array-callback-return
- complexity
- consistent-return
- func-names
- max-params
- max-statements
- no-empty-functions
- strict
I excluded
1. rules where the function should not be the focus of the feedback message. Eg. no-dupe-args
gives Duplicate param '{{name}}'
. Since we already name the param, I don’t find it more useful to include a named function as well.
2. rules regarding function calls (Eg. func-call-spacing
)
3. deprecated rules
Does this change cause the rule to produce more or fewer warnings?
Same.
How will the change be implemented?
this program
if global return${getFunctionNameWithKind(node)}
if function
array-callback-return
Before
Expected to return a value at the end of this function.
Expected to return a value in this function.
Expected a return value.
After
Expected to return a value at the end of
${getFunctionNameWithKind(node)}
.
Expected to return a value in
${getFunctionNameWithKind(node)}
.
${getFunctionNameWithKind(node)}
expected a return value.
complexity
Before
Function ‘{{name}}’ has a complexity of {{complexity}}.
After
${getFunctionNameWithKind(node)}
has a complexity of {{complexity}}.
consistent-return
Before
Expected {{which}} return value.
Expected to return a value at the end of this {{type}}.
After
${getFunctionNameWithKind(node)}
expected {{which}} return value.
Expected to return a value at the end of
${getFunctionNameWithKind(node)}
.
func-names
Before
Unexpected function expression name.
After
Unexpected named
${getFunctionNameWithKind(node)}
.
max-params
Before
This function has too many parameters ({{count}}). Maximum allowed is {{max}}.
After
${getFunctionNameWithKind(node)}
has too many parameters ({{count}}). Maximum allowed is {{max}}.
max-statements
Before
This function has too many statements ({{count}}). Maximum allowed is {{max}}.
After
${getFunctionNameWithKind(node)}
has too many statements ({{count}}). Maximum allowed is {{max}}.
no-empty-functions
Before
Unexpected empty {{kind}}.
After
Unexpected empty
${getFunctionNameWithKind(node)}
.
strict
Before
Wrap this function in a function with ‘use strict’ directive.
After
Wrap
${getFunctionNameWithKind(node)}
in a function with ‘use strict’ directive.
CHANGELOG
- 30/12
- Added
${getFunctionNameWithKind(node)}
. - Rewrote func-names feedback message.
- Added
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:17 (17 by maintainers)
Top GitHub Comments
Looks good to me! (I prefer “this function” over “function ‘anonymous’” in the unlikely event someone uses “anonymous” as a function name.)
I like the idea. That said, since we are also discussing a rule change elsewhere about using a function’s inferred name to suppress warnings, I would like to make sure we consolidate all that logic in one place (ast-utils maybe?).