Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Conversation

lizzzp1
Copy link

@lizzzp1 lizzzp1 commented Jan 7, 2019

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?

@lizzzp1 lizzzp1 changed the title add rule and test no-void-rule Jan 7, 2019
@lizzzp1 lizzzp1 changed the title no-void-rule no-void-0-rule Jan 7, 2019
Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a 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 = {

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truth. 👍

public static metadata: ExtendedMetadata = {
ruleName: 'no-void-zero',
type: 'maintainability',
description: 'Avoid using void 0, prefer undefined',

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.

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

it('should fail when function is given argument of void 0', (): void => {
const inputScript: string = `
const baz = (void 0) => {};
function (void 0) { }

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.

Copy link
Author

@lizzzp1 lizzzp1 Jan 7, 2019

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.

tsutils.isArrowFunction(node) ||
tsutils.isFunctionDeclaration(node) ||
tsutils.isFunctionExpression(node)
) {

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.

Copy link
Author

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.

@@ -0,0 +1,62 @@
import { Utils } from '../utils/Utils';
Copy link

@JoshuaKGoldberg JoshuaKGoldberg Jan 7, 2019

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.

Copy link
Author

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!


export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'no-void-zero',

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes total sense

Copy link
Author

@lizzzp1 lizzzp1 Jan 8, 2019

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?

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.

@JoshuaKGoldberg
Copy link

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?

@lizzzp1 lizzzp1 changed the title no-void-0-rule void-0-rule Jan 9, 2019
@lizzzp1
Copy link
Author

lizzzp1 commented Jan 9, 2019

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?

Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So close!

@IllusionMH
Copy link
Contributor

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 are tests for utility methods that are used in rules implementation, so mocha/chai setup will be still used, but only those utility methods.

@IllusionMH
Copy link
Contributor

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?

Copy link
Contributor

@IllusionMH IllusionMH left a 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>
Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Thanks

Copy link
Author

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 IllusionMH added the PR: Waiting for Author Changes have been requested that the pull request author should address. label Jan 9, 2019
@lizzzp1
Copy link
Author

lizzzp1 commented Jan 9, 2019

@IllusionMH updated.

Copy link

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 thanks @lizzzp1!

Copy link
Contributor

@IllusionMH IllusionMH left a 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!

@IllusionMH IllusionMH removed the PR: Waiting for Author Changes have been requested that the pull request author should address. label Jan 9, 2019
@IllusionMH
Copy link
Contributor

Only one question left above about fixer for this rule see above

Otherwise I think this PR is good to go unless @lizzzp1 would prefer implement fix in scope of current PR.

@IllusionMH
Copy link
Contributor

I will post here instead of comment for line

Fixer is code that will fix lint error (in this case replace void 0 expression to just undefind) if user runs tslint with --fix option.
Example in this repo: https://github.com/Microsoft/tslint-microsoft-contrib/blob/59fa06e239f0a84da9d55113006ab0e8a6a7553a/src/importNameRule.ts#L175-L176
and 1 line in meta for consistency: https://github.com/Microsoft/tslint-microsoft-contrib/blob/59fa06e239f0a84da9d55113006ab0e8a6a7553a/src/importNameRule.ts#L14

Example in docs (no direct link to paragraph, example in the end): https://palantir.github.io/tslint/develop/custom-rules/

@lizzzp1
Copy link
Author

lizzzp1 commented Jan 9, 2019

@IllusionMH 💡ah, yes the --fix flag, got it. I'm happy to do that here or if you think it's easier to do it in another issues, that's fine too. Would it be helpful for me to add this to the contributor read me? This might be common knowledge, but if you think it would be helpful, let me know.

@IllusionMH
Copy link
Contributor

I'm happy to do that here or if you think it's easier to do it in another issues, that's fine too.

@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 tests/void-zero/test.ts.fix file that has all replacements in place.
You can check test.ts.lint and corresponding test.ts.fix for example (links to PR).

If you'll ahve any problems do not hesitate to ask here.

Would it be helpful for me to add this to the contributor read me? This might be common knowledge, but if you think it would be helpful, let me know.

I've created #779 to track this suggestion.

@lizzzp1
Copy link
Author

lizzzp1 commented Jan 9, 2019

@IllusionMH sounds good, thanks!

@IllusionMH IllusionMH added the PR: Waiting for Author Changes have been requested that the pull request author should address. label Jan 9, 2019
@IllusionMH IllusionMH added PR: Waiting for Author Changes have been requested that the pull request author should address. and removed PR: Waiting for Author Changes have been requested that the pull request author should address. labels Jan 10, 2019
@IllusionMH
Copy link
Contributor

Unfortunately I forgot that #665 is not merged.
You'll need to add *.fix eol=lf to .gitattributes (see .gitattributes diff to avoid conflicts) and ensure that tests/void-zero/test.ts.fix has only LF line endings (should have correct already)

Copy link
Contributor

@IllusionMH IllusionMH left a 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?

@JoshuaKGoldberg
Copy link

Yeah SGTM! I can work on the other, bigger PRs in a few minutes.

@IllusionMH IllusionMH merged commit 07567bf into microsoft:master Jan 10, 2019
@IllusionMH IllusionMH added this to the 6.1.0-beta milestone Feb 19, 2019
apawast pushed a commit to lupine86/tslint-microsoft-contrib that referenced this pull request Feb 26, 2019
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

PR: Waiting for Author Changes have been requested that the pull request author should address.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule suggestion: void-zero

3 participants