Skip to content
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

feat(eslint-plugin): [no-unused-expressions] extend for optional chaining #1175

Merged
merged 11 commits into from Nov 11, 2019

Conversation

Nizarius
Copy link
Contributor

@Nizarius Nizarius commented Nov 5, 2019

Fixes #1138

To note: this PR depends on #1169, so it's better to watch after first one

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @Nizarius!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@bradzacher bradzacher added the bug Something isn't working label Nov 5, 2019
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

it looks like you've copied most of the code from the existing rule implementation.
May I suggest that you instead build on top of the existing implementation?

Duplicating the implementation means we have to keep it in sync with eslint, which is a huge maintenance burden.

You could do something similar to
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/rules/camelcase.ts

I.e. - defer most of the processing to the base rule's handler, but add handling which will handle optional chaining:

const rules = baseRule.create(context);
ExpressionStatement(node) {
  if (isValidOptionalChain(node)) {
    return;
  }
  
  rules.ExpressionStatement(node);
}

@Nizarius
Copy link
Contributor Author

Nizarius commented Nov 5, 2019

@bradzacher yeah, great idea! I will do it, but it looks like I would still need a lot of duplicating code as I can't get private functions from eslint rule.

@Nizarius
Copy link
Contributor Author

Nizarius commented Nov 8, 2019

@bradzacher done. Can you tell me how I should add docs to that rule (just copy it from eslint or other way?)

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

Don't forget to add a doc for the new rule.
Have a look at the docs for semi
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/semi.md

Essentially with rule extensions now, we like our extension rule docs to just mention the changes it makes to the rule, and then link out to the original eslint docs

@bradzacher bradzacher changed the title Fix: no-unused-expressions rule and OptionalCallExpression parsing feat(eslint-plugin): extend no-unused-expressions rule to support optional chain Nov 8, 2019
@bradzacher bradzacher added enhancement: new base rule extension New base rule extension required to handle a TS specific case and removed bug Something isn't working labels Nov 8, 2019
@Nizarius
Copy link
Contributor Author

Nizarius commented Nov 9, 2019

@bradzacher added docs and new tests. Could you explain to me how I should update documentation parsing to avoid 'Incorrect line number for' errors?

Also, should I add this rule to recommended ones to prevent errors with base configuration?

@bradzacher
Copy link
Member

Could you explain to me how I should update documentation parsing to avoid 'Incorrect line number for' errors?

The rule needs to be listed in the plugin readme. The table should be sorted alphabetically.

Also, should I add this rule to recommended ones to prevent errors with base configuration?

Changes to the recommended config are considered breaking changes, so we cannot do that for now.
Feel free to raise a separate tracking issue so we can keep track of doing this when we start on a 3.0.0 release.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for this.

@codecov
Copy link

codecov bot commented Nov 11, 2019

Codecov Report

Merging #1175 into master will increase coverage by 0.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master    #1175      +/-   ##
==========================================
+ Coverage   93.96%   93.98%   +0.01%     
==========================================
  Files         120      121       +1     
  Lines        5207     5217      +10     
  Branches     1443     1444       +1     
==========================================
+ Hits         4893     4903      +10     
  Misses        179      179              
  Partials      135      135
Impacted Files Coverage Δ
...s/eslint-plugin/src/rules/no-unused-expressions.ts 100% <100%> (ø)
packages/eslint-plugin/src/rules/index.ts 100% <100%> (ø) ⬆️

@bradzacher bradzacher changed the title feat(eslint-plugin): extend no-unused-expressions rule to support optional chain feat(eslint-plugin): [no-unused-expressions] extend for optional chaining Nov 11, 2019
@bradzacher bradzacher merged commit 57d63b7 into typescript-eslint:master Nov 11, 2019
@glen-84
Copy link
Contributor

glen-84 commented Nov 19, 2019

@JamesHenry This isn't in the release notes?

@bradzacher
Copy link
Member

@glen-84
Copy link
Contributor

glen-84 commented Nov 19, 2019

🤦‍♂ I must have missed that.

etaoins added a commit to seek-oss/eslint-config-seek that referenced this pull request Dec 22, 2019
The default ESLint `no-unused-expressions` rule cannot handle optional
chaining in TypeScript, for example:

```
onChangeHook?.(nextValue);
```

In typescript-eslint/typescript-eslint#1175 the base rule was duplicated
in to a TypeScript-specific one that handles optional chaining
correctly. Presumably this will eventually be merged back to the base
rule as this is now part of the ECMAScript standard.
etaoins added a commit to seek-oss/eslint-config-seek that referenced this pull request Dec 23, 2019
The default ESLint `no-unused-expressions` rule cannot handle optional
chaining in TypeScript, for example:

```
onChangeHook?.(nextValue);
```

In typescript-eslint/typescript-eslint#1175 the base rule was duplicated
in to a TypeScript-specific one that handles optional chaining
correctly. Presumably this will eventually be merged back to the base
rule as this is now part of the ECMAScript standard.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new base rule extension New base rule extension required to handle a TS specific case
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-unused-expressions] False positive with optional chaining
4 participants