How to use the same rules for js and ts in TSLint
Asked Answered
A

1

13

I want to use the same style in .js files and .ts files. I know there is jsRules property in tslint.json, but I see 2 problems with it:

  • Copying ant pasting exactly the same rules
  • When extending some configurations, e.g. tslint-react, you don't get the rules in jsRules, meaning that you have to go to the source code of the ruleset and copy it manually.

Any other way to have the same code style without having to maintain both eslint and tslint?

Amblyopia answered 18/10, 2017 at 12:44 Comment(0)
M
5

Not everyone knows that, but TSLint configuration doesn't have to come in a .json file. You can use the .js extension, and keep whatever logic you like inside.

In this case, we can split TSLint rules into two categories — universal rules and React-specific ones. Both rulesets will be used for TypeScript files, but only the first ones will be applied to JavaScript.

tslint.js

const UNIVERSAL_RULES = {
  "max-line-length": [true, 120]
}

const REACT_RULES = {
  "jsx-space-before-trailing-slash": true
}

module.exports = {
  extends: [
    "tslint:recommended"
  ],
  jsRules: UNIVERSAL_RULES,
  rules: {
    ...UNIVERSAL_RULES,
    ...REACT_RULES
  }
}

When running TSLint CLI, it may be required to point to your configuration file explicitly. You can do that by adding --config tslint.js to your tslint command.

Mauriciomaurie answered 9/9, 2018 at 22:56 Comment(1)
You can also import the *.js file into your root tslint.config file, as tslint uses NodeJS import resolution behind the scenes... so if you have a dynamiclint.js, it imports just fine in your root tsconfig.json: { "extends": [ "./dynamiclint" ] }Terracotta

© 2022 - 2024 — McMap. All rights reserved.