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

Variant of no-unused-expressions that allows type assertions? #271

Closed
mkscrg opened this issue Sep 16, 2017 · 4 comments
Closed

Variant of no-unused-expressions that allows type assertions? #271

mkscrg opened this issue Sep 16, 2017 · 4 comments

Comments

@mkscrg
Copy link
Contributor

mkscrg commented Sep 16, 2017

It's sometimes helpful to write a bare type assertion with no side-effects. For example when discriminating a disjoint union in a switch:

type Action
  = { type: 'FOO', doFoo(_: number): void }
  | { type: 'BAR', doBar(_: string): void };

function handleAction(act: Action): void {
  switch (act.type) {
    case 'FOO':
      doFoo(10);
      break;
    case 'BAR':
      doBar('hello');
      break;
    default:
      (act: empty); // type error unless we've exhausted the possibilities for `act.type`
  }
}

Unfortunately these are caught by ESLint's no-unused-expressions rule.

Is there a clean way to "fix" this with eslint-plugin-flowtype? Maybe we could expose a flowtype/no-unused-expressions that additionally ignores type assertions?

@mkscrg
Copy link
Contributor Author

mkscrg commented Sep 16, 2017

Scratched this itch with #272

@bradennapier
Copy link

Love this one! Thanks!

@mkscrg mkscrg closed this as completed Nov 2, 2017
@cinic
Copy link

cinic commented Feb 16, 2018

Not working for me.

Eslint: 4.14
Eslint-plugin-flowtype: 2.44.0
babel-eslint: 8.2.1

Eslint error: Expected an assignment or function call and instead saw an expression. (no-unused-expressions).

const reducer = (state: ConfigState = initialState, action: ConfigActions): ConfigState => {
  switch (action.type) {
    case APP_CONFIG_SUCCESS:
      return {
        ...state,
        config: action.config,
      };
    default:
      (action: empty); // eslint caught error
      return state;
  }
};

@pronebird
Copy link

pronebird commented Aug 8, 2018

Hey, this doesn't work for me either, my eslintrc:

{
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:promise/recommended",
    "plugin:flowtype/recommended",
    "prettier"
  ],
  "plugins": ["react", "flowtype", "promise"],
  "rules": {
    "no-unused-expressions": "error",
  }
}

How do you guys disable no-unused-expressions because ESLint picks up the ones that belong to Flow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants