Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

bug in "fix" command #154

Open
dmitriyse opened this issue Feb 18, 2019 · 1 comment
Open

bug in "fix" command #154

dmitriyse opened this issue Feb 18, 2019 · 1 comment

Comments

@dmitriyse
Copy link

dmitriyse commented Feb 18, 2019

OS: Windows
Problem: Last CRLF replaced by LF

File that is processed by "fix" method (contains only CRLF):
Name = package.json

{
  "name": "clr-seed-scripts",
  "version": "12.12.0",
  "scripts": {
    "eclint-fix": "gulp eclint-fix",
    "eclint-check": "gulp eclint-check"
  },
  "license": "MIT",
  "devDependencies": {
    "@types/gulp": "^4.0.5",
    "@types/gulp-debug": "^2.0.31",
    "@types/find-up": "^2.1.1",
    "@types/through2": "^2.0.34",
    "@types/vinyl": "^2.0.2",
    "@types/yargs": "^12.0.9",
    "eclint": "^2.8.1",
    "find-up": "^3.0.0",
    "gulp": "^4.0.0",
    "gulp-debug": "^4.0.0",
    "gulp-exclude-gitignore": "^1.2.0",
    "gulp-reporter": "^2.10.0",
    "through2": "^3.0.0",
    "ts-node": "^8.0.2",
    "typescript": "^3.3.3",
    "yargs": "^13.2.1"
  }
}

Command that executes "fix"

pipe(eclint.fix({ settings: { insert_final_newline: true, trim_trailing_whitespace: true } }));

Content of .editorconfig:

; EditorConfig to support per-solution formatting.
; Use the EditorConfig VS add-in to make this work.
; http://editorconfig.org/

; This is the default for the codeline.
root = true

[*]    
indent_style = space
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.sln]
indent_style = tab
charset = utf-8-bom

[*.*proj]
charset = utf-8-bom

[*.cs]
indent_size = 4
dotnet_sort_system_directives_first = true:warning

[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}]
indent_size = 2

[*.json]
indent_size = 2

[*.{ps1,psm1}]
indent_size = 4

[*.sh]
indent_size = 4
end_of_line = lf
@dmitriyse
Copy link
Author

This is an ugly workaround.

      .pipe(
        through.obj((file: VinylFile, _enc: string, done: Done) => {
          fs.readFile(file.path, (err, data) => {
            try {
              if (err == null) {
                var modifiedBuffer = <Buffer>file.contents;

                // This is workaround for bug https://github.com/jedmao/eclint/issues/154
                // ------------------------------------------------------------------------
                // if modified buffer ends with 'lf'
                if ((modifiedBuffer.length > 1
                  && modifiedBuffer[modifiedBuffer.length - 2] != 0x0D
                  && modifiedBuffer[modifiedBuffer.length - 1] == 0x0A)
                  || (modifiedBuffer.length == 1
                    && modifiedBuffer[modifiedBuffer.length - 1] == 0x0A)) {

                  // Checking that modified buffer contains 'crlf'
                  var contains_crlf = false;
                  var contains_lf = false;
                  for (var i = 0; i < modifiedBuffer.length - 1; i++) {
                    if (modifiedBuffer[i + 1] == 0x0A) {
                      if (modifiedBuffer[i] == 0x0D) {
                        contains_crlf = true;
                      }
                      else {
                        contains_lf = true;
                      }
                    }
                  }

                  if (contains_crlf || !contains_lf) {
                    // Fixing last lf to crlf
                    //modifiedBuffer[modifiedBuffer.length - 1] = 0x0D;
                    modifiedBuffer[modifiedBuffer.length - 1] = 0x0D;
                    modifiedBuffer = Buffer.concat([modifiedBuffer, Buffer.from([0x0A])])
                  }
                }

                if (modifiedBuffer.compare(data) == 0) {
                  done(null, file);
                }
                else {
                  // Overwriting file only if it has been fixed.
                  fs.writeFile(file.path, modifiedBuffer, (err) => {
                    if (err == null) {
                      console.log(`File updated: ${file.path}`);
                    }
                    done(null, file)
                  });
                }
              }
              else {
                done(null, file);
              }
            }
            catch{
              done(null, file);
              return;
            }
          }
          )
        }));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant