-
Notifications
You must be signed in to change notification settings - Fork 198
max-func-body-length fails to count body length correctly #468
Comments
Ah, but now I'm confused! Why is that an important definition for you? What's your use case for that particular interpretation? Agreed that this should be a one-line function: (x: number): number => x + 1 What about this? (x: number): number => {
return x + 1;
} Perhaps the rule's |
Hi @JoshuaKGoldberg, thanks for your reply!
Without this kind of definition, we cannot discuss which implementation is correct. Fortunately, we have a good real example in https://github.com/Microsoft/tslint-microsoft-contrib/blob/master/src/tests/MaxFuncBodyLengthRuleTests.ts#L274:
Without that kind of definition I've given, we cannot say confidently whether this is four-line, five-line or six-line. (BTW, the current implementation considers it a five-line function.)
Hmmmmmm... Good point! I personally consider that this is a three-line function, but I would say that it is reasonable to name it a one-line function by considering "the line difference between the start of the first statement to the end of the last statement" as you say. Frankly speaking, I don't have a definite answer for this at this timing. But I would say that should not be a two-line, whilst the current impelementation does say it is: |
That makes sense, thanks! Accepting PRs to have the rule consider both the To be clear, this should be considered to have 3 lines: (x: number): number => {
return x + 1;
} ...and this should have 1 line: (x: number): number => x + 1 |
* Proposed fix for issue #468 * Revision of a test script responding to a fix of #468 * Avoid using tsutils v2.29.0, which breaks tests * Revert "Avoid using tsutils v2.29.0, which breaks tests" This reverts commit f1e963f. See also: #469 (comment)
Fixed by #469! 🎉✨ |
Prerequisite
I assume that the following is a 3-line function:
// I seriously think that it is an important definition. Without it, we (at least, I) can get confused. :P
Repro
git clone https://github.com/makotom/tslint-test-20180724.git
npm i
npm run lint
OR
"max-func-body-length": [true, 2]
. This intends to raise an error if a function has more than 2 lines.tslint
Expected Result
An error should be raised to a function having more than 2 lines.
Actual Result
No error.
Note that the test case I've provided (which is also referred in Repro) contains a 4-line function as well, to which an error is fired as expected.
Considerations
As I wrote in my testcase, this happens because it does not add
1
toendLine - startLine
to calculate body length. I believe that it should add1
- if not, it yields 0-line function ifstartLine === endLine
satisfies (e.g.(x: number): number => x + 1
).Apparently, this can be fixed very easily. However, I found out that fixing it may break several tests which depends on error messages. I'm also afraid that the fix, which could be a single line, may break many other things. This is why I'm opning a new issue, rather than a PR, to call for discussions as the first step.
The text was updated successfully, but these errors were encountered: