standard / ts-standard

Typescript style guide, linter, and formatter using StandardJS
https://www.npmjs.com/package/ts-standard
MIT License
464 stars 36 forks source link

Allow overriding or disabling rules for certain files/directories. #126

Closed tarruda closed 3 years ago

tarruda commented 3 years ago

What version of this package are you using? 10.0.0

What problem do you want to solve? Override/disable rules for certain files/directories

What do you think is the correct solution to this problem? Reading per-directories eslintrc configuration files to override default rules. In my case, I have tests in the "test" directory and would like to disable (@typescript-eslint/no-unused-expressions) or I get lint errors for expect() assertions.

Are you willing to submit a pull request to implement this change? Yes, though I would appreciate some guidance on where this change should be made.

theoludwig commented 3 years ago

Hey! @tarruda Thanks for your report!

You don't need to disable any rules for tests for example. You can provide the envs array options according to your test framework. See: https://github.com/standard/ts-standard#-packagejson-options

tarruda commented 3 years ago

Hi @Divlo , thanks for the reply

I saw the "env" option, but it is not clear how it would help in this case. From what I understood "env" is about defining globals that are environment-specific (such as "process" in node.js). In my case, I have no problem with any test-specific globals, but with the @typescript-eslint/no-unused-expressions rule which disallows expressions that return a value to be unsued (which is the case of "expect" assertions).

lorenzogrv commented 3 years ago

Hi @Divlo , thanks for the reply

I saw the "env" option, but it is not clear how it would help in this case. From what I understood "env" is about defining globals that are environment-specific (such as "process" in node.js). In my case, I have no problem with any test-specific globals, but with the @typescript-eslint/no-unused-expressions rule which disallows expressions that return a value to be unsued (which is the case of "expect" assertions).

Could you provide a Code example of what you are refering to?

tarruda commented 3 years ago

Could you provide a Code example of what you are refering to?

Sure, here's an example "jest" test file:

describe('jest suite', function() {
  it('should pass', function() {
    expect(1 + 1).toEqual(2)
  })
})

The above does not pass the @typescript-eslint/no-unused-expressions rule because expect().toEqual() returns a value which is not used.

theoludwig commented 3 years ago

Could you provide a reproductible repo, please ? @tarruda I can't reproduce, here's what I did :

I ran again ts-standard and I've got no linting issues.

tarruda commented 3 years ago

@Divlo TBH I just found out that the error was caused by me using the jest API in the wrong way. In one of my tests I had

expect(someExpr).toBeUndefined;

While the correct way would be

expect(someExpr).toBeUndefined();

The first is an unused expression because the matcher function was not called. I somehow assumed it would just work because many years ago I used mocha + chai, which allowed me to do this:

expect(someExpr).to.be.undefined;

Sorry for the noise.

lumaxis commented 3 years ago

Hey! I'd like this to be reopened.

As @tarruda alluded to in their last comment, this can happen with mocha and chai with something like expect(true).to.be.true which is a valid chai assertion: https://www.chaijs.com/api/bdd/#method_true

Instead of littering my code with // eslint-disable-line no-unused-expressions statements, I'd love to be able to use something like this:

  "ts-standard": {
    "env": [
      "mocha"
    ],
    "overrides": [{
      "files": ["test/**/*.js"],
      "rules": {
        "no-unused-expressions": "off"
      }
    }]
  }

which seems to be the standard (hah!) way of getting around this problem with regular ESLint setups.

LuisEnMarroquin commented 3 years ago

Did you figured out any way to override certain rule? @lumaxis

theoludwig commented 3 years ago

Did you figured out any way to override certain rule? @lumaxis

If you want to override rules, you can always use eslint and extends the config from ,eslint-config-standard-with-typescript.

RiccardoManzan commented 1 year ago

@lumaxis that "overrides" key is not working in my setup. I took a look to the lib code, and i didn't find any match. However, from last comment of @theoludwig, it looks like if i need to change any rule, i need to use directly eslint and give my own configuration. But this replaces completely the behaviour of ts-standard, making it useless. isn't it?

ZandercraftGames commented 6 months ago

+1 On this :) I've run into the same roadblock. It would be nice if it respected a .eslintrc file or had some other way of disabling rules.