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:
Any other way to have the same code style without having to maintain both eslint and tslint?
Tslint is a tool that checks your JavaScript code for errors and code formatting issues.
TSLint's internal rules are itself written to be pluggable, so adding a new rule is as simple as creating a new rule file named by convention. New rules can be written in either TypeScript or JavaScript; if written in TypeScript, the code must be compiled to JavaScript before invoking TSLint.
tslint accepts the following command-line options: -c, --config: The location of the configuration file that tslint will use to determine which rules are activated and what options to provide to the rules. If no option is specified, the config file named tslint.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With