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 Change: Make max-lines-per-function only reports lines that exceed the limit, not all #15098
Comments
I’d say this is a bug given the different behavior in max-lines, so I’m in favor of bringing this rule into alignment. |
Fair enough. @alvis would you be interested in submitting a pull request for this? |
I would love to take this on as first contribution if possible! |
@LilCthulu I'd appreciate it very much if you can help ❤️ |
@LilCthulu happy to have your help. Please post back here if you have any questions or need some pointers. |
This is the main file to look at: You’ll also need to update the tests: The pull request making a similar change to max-lines might be useful as an example: |
@nzakas Thanks! I'll get started right away and keep you posted! |
I'd like to make my first open source contribution by updating this rule. 😄 |
@The-x-Theorist thanks. We already have @LilCthulu volunteering to work on this, so let’s give them the chance to complete the work first. |
@nzakas it's okay, I will wait. 😃 |
@LilCthulu are you still planning on working on this? |
@nzakas Can I work on this issue? |
@nzakas I am indeed sorry for being slow! Had a bunch of stuff going on. |
@LilCthulu thats okay, we just need regular updates because there are others interested in implementing this. If you can’t get to this soon, please let us know so we can let someone else work on it. @Gautam-Arora24 right now @LilCthulu is working on this and @The-x-Theorist is next line. |
@nzakas Sorry for the delay it is fine if someone else grabs this |
@LilCthulu Can I grab this one? |
@Gautam-Arora24 @The-x-Theorist has already submitted a pull request. |
Here it is: #15140 |
No worries @nzakas. I'll pick some another issue :) |
I have concerns about this change. The starting location of the reported error will be floating inside the function, so it will be basically impossible to use Would it make more sense to report just the header? |
Hmm. Not sure if putting |
I also don’t think the ability to use disable comments should be the deciding factor here. I’d suggest we try out the pull request and see what the UX feels like. |
This change may bring UX improvements in some cases, but I still have the following concerns:
|
These all seem to be the way that max-lines currently works. Am I missing something? |
Indeed, for the last two points. For the same reasons, I'm not sure if reporting extra lines in The difference is in disabling errors. With |
If I understand the code correctly, instead of reporting eslint/lib/rules/max-lines-per-function.js Lines 195 to 199 in 0d3f95b
we can report Lines 180 to 187 in 0d3f95b
Since the input is |
Location of For example, this disable comment: /* eslint max-lines-per-function: ["error", 5] */
function one() { // eslint-disable-line max-lines-per-function
two;
three;
four;
five;
six;
} would have to be relocated here: /* eslint max-lines-per-function: ["error", 5] */
function one() {
two;
three;
four;
five;
six; // eslint-disable-line max-lines-per-function
} Then, if anything new is added to the function before that line, the location of the comment must be changed: /* eslint max-lines-per-function: ["error", 5] */
function one() {
two;
three;
somethingNew;
four;
five; // eslint-disable-line max-lines-per-function
six;
} |
Can’t people just use a disable comment before the function and then enable after? I don’t think we need to guarantee that every disable directive works in every situation. |
Yes, that will work. A downside is that it also disables this rule in inner functions. Anyway, since everyone is in favor of this proposal, I agree that we try it out. Question: do we want to change the error message (as in #15140), or shall we keep the existing one? |
I’m fine either way. There’s something nice about doing the math for users, though. |
Thinking about this from the user's perspective, seeing editor squigglies only on the excess lines would be much nicer than squigglying the whole function. The tradeoff that disable-(next-)line comments will have to be moved might be seen as a feature: previously, disabling this rule for a function was binary. Now, someone could disable it on just the n+1th line, but adding an n+2nd line would still error. Block comments are still available to disable the rule for an entire function. |
…on (#15140) * Update: reports exceeded lines (fixes #15098) * Update lib/rules/max-lines-per-function.js Co-authored-by: Nitin Kumar <snitin315@gmail.com> * Update: added loc property to context.report() * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update: loc property improved * higlights only the exceeded lines * highlight only exceeded line with skipSomeLines too * skipComments working fine * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> Co-authored-by: Nitin Kumar <snitin315@gmail.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
(basically, what's described in #15098 (comment)) Can it be reverted please? I expect any rule named "per-function" to always apply to the entire function. |
Can you move the Or, add |
I can, but “i have to move code” means it’s a breaking change. |
Should we reopen this issue to track it for v9? The change was reverted in #15397 |
It seems better not to implement this change at all, for the reasons described in #15390 and in this issue. |
What rule do you want to change?
max-lines-per-function
What change to do you want to make?
Generate fewer warnings
How do you think the change should be implemented?
A new default behavior
Example code
What does the rule currently do for this code?
Currently it will report the range of line numbers from the beginning of the function to the very last line of it.
What will the rule do after it's changed?
Following the same spirit of
max-lines
implemented in #13318, we should only report those lines which exceed the limit.With this example, it should only report line 4 and 5.
Participation
Additional comments
The current default is annoying because once the function exceeds even a single line, the whole function is highlighted by the editor and other important errors may be suppressed as a result.
The text was updated successfully, but these errors were encountered: