typescript-formatter icon indicating copy to clipboard operation
typescript-formatter copied to clipboard

If no-consecutive-blank-lines is set to false or [true, <n>] in tslint.json blank lines are always collapsed to one

Open InExtremaRes opened this issue 7 years ago • 1 comments

Having this file (with 4 empty/blank lines):

const x = 'hello';




console.log(x);

and tslint.json:

{
  "rules": {
    "no-consecutive-blank-lines": false
  }
}

Running tsfmt -r replaces the file and removes consecutive blank lines as this:

const x = 'hello';

console.log(x);

Expected behavior

I expect the output file sill has consecutive blank lines (as the original) since no-consecutive-blank-lines is explicitly set to false. If tslint.json contains "no-consecutive-blank-lines": [true, 2] I would expect the output file gets removed some blank lines but keeps two of them. Instead every time no-consecutive-blank-lines is set to something all blank lines are collapsed to one.

If I run npx tsfmt --no-tslint -r then blank lines are not removed. The same if I take off the option of tslint.json at all.

By the way, why that option needs to be addressed here in a postprocessing? Isn't that on the tslint realm? Users can just run tslint --fix and let it take care of this.

InExtremaRes avatar Jul 01 '18 09:07 InExtremaRes

I believe this is due to tslintjson.ts ignoring the value of no-consecutive-blank-lines.

    if (rules.has("no-consecutive-blank-lines")) {
        formattedCode = formattedCode.replace(/\n+^$/mg, "\n");
    }

Looking at the block of code, it could probably be a bit more robust, since it fails for Windows newlines (CRLF) and also doesn't account for "whitespace" blank lines.

aserra54 avatar Sep 13 '18 18:09 aserra54