-
Notifications
You must be signed in to change notification settings - Fork 200
void-0-rule #770
void-0-rule #770
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💪 good step in the right direction, but I think it can be simplified.
const FAILURE_STRING: string = 'Replace void 0 with undefined'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: ExtendedMetadata = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not immediately clear why void 0
is undesirable (many folks won't know what it does), so I think this should include a rationale
field. Maybe something around the lines of...
`void 0`, which resolves to `undefined`, can be confusing to newcomers. Exclusively use `undefined` to reduce ambiguity.
...and also in the README.md table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Truth. 👍
src/noVoidZeroRule.ts
Outdated
public static metadata: ExtendedMetadata = { | ||
ruleName: 'no-void-zero', | ||
type: 'maintainability', | ||
description: 'Avoid using void 0, prefer undefined', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README.md table's description is a little nicer:
Avoid using `void 0`; use `undefined` instead.
README.md
Outdated
<code>no-void-zero</code> | ||
</td> | ||
<td> | ||
Avoid using <code>void 0</code>; use undefined instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: missing <code>
wrapping around the undefined
src/tests/NoVoidZeroRuleTests.ts
Outdated
it('should fail when function is given argument of void 0', (): void => { | ||
const inputScript: string = ` | ||
const baz = (void 0) => {}; | ||
function (void 0) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't valid JS: a void
expression can't be in place of a function parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙃 😬 Ah meant to declare then use? Can't remember.
src/noVoidZeroRule.ts
Outdated
tsutils.isArrowFunction(node) || | ||
tsutils.isFunctionDeclaration(node) || | ||
tsutils.isFunctionExpression(node) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not following why this second if
statement is necessary. Void expressions can't be function parameters, so I think you can remove this whole block and just have the first if
statement checking for a void 0
.
The first block should find any void 0
expression anyway, since this walker will recurse through all nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Void expressions can't be function parameters
gotcha -- thought I saw it used as so. Makes sense.
src/tests/NoVoidZeroRuleTests.ts
Outdated
@@ -0,0 +1,62 @@ | |||
import { Utils } from '../utils/Utils'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind switching this to the newer test format, actually? Under the test/
directory, copy any one that doesn't have sub-directories (which would be necessary if this rule had options) such as non-literal-fs-path
, modify the tslint.json
to only include no-void-0
, and add your test cases there.
Tests in general are moving to that format (#489) because it's quite a bit nicer to read & write.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Good to know -- thank you!
src/noVoidZeroRule.ts
Outdated
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: ExtendedMetadata = { | ||
ruleName: 'no-void-zero', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`ruleName: 'no-void-zero',
IMO it's better to move away from prefixing rule names with no-
for a couple of reasons:
- It discourages rules from later being modified to support the opposite case. That's not so relevant to this rule, but does for others (e.g.
parameter-reassignment
; what if we want prefer reassigning them over making unnecessary new variables?) - It messes with sorting of rules, since some have
no-
and others don't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Makes total sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think just void-zero
(void-0 ?) is sufficient or prefer-undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void-zero
. names around preferring undefined could get confused with rules in the null
vs. undefined
area.
Re your edge cases - I can't think of too many others right now 😄 but it seems fine to go in with just a few. Maybe the different ways variables/properties can be assigned, such as parameters, variables, class members, and static class members? |
Updated. The tests are an interesting format. Would there be no need need for a test runner if these were all this format or would you still need mocha/chai set up? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So close!
There are tests for utility methods that are used in rules implementation, so mocha/chai setup will be still used, but only those utility methods. |
This is pretty simple rule with concrete instruction what to do. May be it make sense to do add fixer? @lizzzp1 would you prefer to add fixer in this PR or should I create separate issue for enhancement? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only two minor notes from me. Otherwise looks great 👍
README.md
Outdated
<td> | ||
<code>void 0</code>, which resolves to <code>undefined</code>, can be confusing newcomers. Exclusively use <code>undefined</code> to reduce ambiguity. | ||
</td> | ||
<td>6.0.0</td> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6.0.0
is current verstion. This rule will be added in next minor version - 6.1.0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I thought you were linking to this when you said one more question. Define fixer
:)
@IllusionMH updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌 thanks @lizzzp1!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was fast! 🏎
Thank you!
I will post here instead of comment for line Fixer is code that will fix lint error (in this case replace Example in docs (no direct link to paragraph, example in the end): https://palantir.github.io/tslint/develop/custom-rules/ |
@IllusionMH 💡ah, yes the |
@lizzzp1 since fixer for this rule should be easy to implement, I'd prefer to have everything in this PR. Please note that fixer behavior should be tested with adding If you'll ahve any problems do not hesitate to ask here.
I've created #779 to track this suggestion. |
@IllusionMH sounds good, thanks! |
Unfortunately I forgot that #665 is not merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lizzzp1 Code, tests, and fixer looks great! Thank you for prompt responses and fixer implementation! 🙌
@JoshuaKGoldberg since only problem with line endings on Travis CI on Windows - I'm in favor of mergin this PR as is and adding line to .gitattributes
in separate PR. What do you think?
Yeah SGTM! I can work on the other, bigger PRs in a few minutes. |
* add rule and test * feeback - simplify rule, update to new test format, fix descriptions * feedback - fix typo, version, rationale on one line * feedback -add fix for rule * Remove extra line
PR checklist
Overview of change:
Attempts to add a no void 0 rule.
Is there anything you'd like reviewers to focus on?
@JoshuaKGoldberg unsure if this is the right implementation. It seems to evaluate void and zero individually as parameters (the test confirms it gives 2 errors). Not sure if looking for the specific string in sourceFile is brittle. Also.. I may have missed some cases?