Indent rule not reporting or fixing indent size violations #2814
Comments
@adidahiya See this comment by @nchen63. |
From @nchen63:
It SHOULD do |
My project uses both 2 and 4 spaces. Fix rule |
I also ran into this issue expecting it to fix size violations, or at least report them as errors so that I can fix them. At the moment it seems the configuration for the indent size documented on the site (https://palantir.github.io/tslint/rules/indent/) doesn't actually do anything. |
Yes, please fix this bug. Angular CLI uses 2 spaces for an indentation level when generating a file or a project, and all quotes are single quotes. Then it runs tslint to fix these according to the user's tslint.json. Quotes work well (transforming to double quotes as per my preference), but indentation stays at 2 spaces (while I prefer 4). Tslint only reports an error if it sees an actual TAB character, but seems reasonable that it should also check the number of spaces |
It seems to me like the implementation being used in https://github.com/palantir/tslint/blob/master/src/rules/indentRule.ts is quite naive, and I wouldn't expect it to work for Say, for instance, an indentation width of 2 spaces was chosen, and consider this code:
How would our current implementation know to fail this at all? Each sequence of spaces passes the regex I think any solution would need to traverse the AST, similar to what eslint does (https://github.com/eslint/eslint/blob/master/lib/rules/indent.js). The downside of that would be that we'd take a slight performance hit for I think that at least some of the AST work done in the eslint implementation can be handled by typescript's library directly, so the solution shouldn't be too hard to write up. |
It doesn't technically need to. The |
Nice! I missed that when I was examining the source code. I'll take a peak over there too. TBH, I kinda like the idea of leveraging existing rules to keep others as naive as possible. |
I'm experiencing the same issue. The rule below will catch tabs being used instead of spaces, but won't catch an incorrect number of spaces. I can change 2 to any other number, and I still get no errors. I'm using tslint 5.5.0.
|
TSLint currently fails to detect this. See: palantir/tslint/issues/2814
TSLint currently fails to detect this. See: palantir/tslint/issues/2814
TSLint currently fails to detect this. See: palantir/tslint/issues/2814
Any updates on this? (If someone isn't working on a fix already, I'm willling to give it a go. ) |
@mDibyo go for it |
@mDibyo Have you made any progress on this? |
I have a partial implementation. Will try to complete it and put up a PR over the weekend |
Just wanted to give a heads up that this issue has become a lower priority for me since I've started to use prettier formatting with tslint-plugin-prettier for my projects. We might try to add more official support for prettier in the built-in configs in the future and correspondingly adjust focus to core rules that are not formatting-related. |
What if you don't want that?
We use 120 characters in our projects. Also, it's yet another dependency. I think that I'd prefer to just use regular TSLint rules. |
@glen-84 yeah, that's fine, which is why i'm not saying we should remove all formatting rules from TSLint and delegate completely to an external formatter. Prettier is obviously opinionated and not everyone is going to choose to use it. This issue is still open for PRs. |
This is a functionality that almost everybody uses on a daily basis. Would appreciate if this issue gets more attention. |
Guys, You can use ter-indent option provided by tslint-eslint-rules. "ter-indent": [ true, 4, { "SwitchCase": 1 }] It worked for me. Cheers! |
@hiteshaleriya that's what I've had in my project for a while now, but it isn't actually fixing the errors, just silencing them... here's my
and here's a relevant example that finished without causing warnings or errors:
It's also not showing errors when tabs are used (although that might be by design when 4 spaces are present?). |
@SpencerKaiser Can you please update your ter-indent rule as shown below and then try:
I tried your example and it is working as expected at my end (causing errors). |
@hiteshaleriya thanks for getting back to me so quickly! So it's now throwing errors as expected (👍) but it's not fixing any of them with |
@SpencerKaiser Can you try running the --fix command twice. First time it will indent first line only then second time it will indent the rest (for your example code). Seems weird but please report the issue if it doesn't work. |
@hiteshaleriya so a couple of observations... I didn't need to run it again, I needed to run it roughly After finally finishing them all, it seems it's skipping over basic indentation errors like this:
If I mess up the indentation level of the
With
And the second pass:
Thoughts?? |
This file uses tabs instead of 4 spaces, which our tslint.json file specifies. Since this file was copied from the vscode repo, I chose to leave the tabs there and just disable the rule for this file. This will make it easier to diff it with the original file, in case we need to update it. The warnings are not shown by tslint (probably because of palantir/tslint#2814), but they show up in our Sonarqube project. Change-Id: Idc7536e5e80be4d7e0d32b529a442d99365f7a19 Signed-off-by: Simon Marchi <simon.marchi@ericsson.com> Signed-off-by: Bogdan Stolojan <petre.stolojan@arm.com>
@shubich I ended up doing the same thing... |
Is there any update on this? |
@MaKCbIMKo as far as I understand, the entire team will be moving to integrate eslint visit typescript-eslint, and in near future tslint will be deprecated, so it's as good as ignoring this rule for now (or use the tslint-config-prettier) |
Closing due to the complexity of this task and the change in project direction: #4534 |
ESLint rule from typescript-eslint works perfectly for me (see #4534): module.exports = {
"env": {
"browser": true,
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"project": "./tsconfig.json",
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/indent": ["error", 2] // or ["error", "tab"]
}
} |
tslint is lacking several rules from eslint [1], and fails to correctly detect the indention level [2]. Using vanilla eslint allows us to properly reuse the wikimedia eslint config [3] and be more future-proof since tslint is going to be phased out in favor of eslint [4]. [1] https://github.com/buzinas/tslint-eslint-rules [2] palantir/tslint#2814 [3] https://github.com/wikimedia/eslint-config-wikimedia [4] https://medium.com/palantir/tslint-in-2019-1a144c2317a9 Change-Id: I6fb762eb384f817b78c7ca0a5b3905d799e3572b
Link not found. |
🤖 Beep boop! 👉 TSLint is deprecated 👈 and you should switch to typescript-eslint! 🤖 🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋 |
P.S. tslint-config-prettier - please stop using linters such as TSLint to format your TypeScript. That's better done by a formatter such as Prettier. |
Bug Report
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
No errors with
tslint
. No fixes withtslint --fix
.Expected behavior
Errors reported with
tslint
, fixes applied withtslint --fix
so that the resulting file looks like:#2723 did not quite work like I expected. The problem seems to be that a failure is only reported if using the wrong whitespace character, not if the indent size is off (like in my example). relevant source.
The text was updated successfully, but these errors were encountered: