Repository: sasstools/sass-lint (js)

Issue:
Wrong line numbers when stripping front matter
Per #897, sass-lint strips the front matter used in packages such as jekyll. However this causes the line numbers reported by the linter to be incorrect.

For example, running `sass-lint -v` (version 1.10.2) on `tests/sass/front-matter/front-matter.scss` will give:

```
front-matter.scss
  3:10  warning  Color 'red' should be written in its hexadecimal form #ff0000              no-color-keywords
  3:10  warning  Color literals such as 'red' should only be used in variable declarations  no-color-literals
```

Where the relevant line is actually line 6.

Pull request description:
Preserve correct line numbers when front matter is stripped
This change corrects the line numbers reported by sass-lint when front matter (such as used by jekyll) is stripped. Fixes #1059.

`<DCO 1.1 Signed-off-by: Felipe Lacerda fegolac@gmail.com>`


Patch:
diff --git a/lib/groot.js b/lib/groot.js
index 1d2ee52..60a057e 100644
--- a/lib/groot.js
+++ b/lib/groot.js
@@ -15,7 +15,14 @@ module.exports = function (text, syntax, filename) {
 
   // if we're skipping front matter do it here, fall back to just our text in case it fails
   if (fm.test(text)) {
-    text = fm(text).body || text;
+    var withFrontMatter = fm(text);
+    // Replace front matter by empty lines, so that line numbers are preserved
+    var numEmptyLines = 2;
+    if (withFrontMatter.frontmatter !== '') {
+      numEmptyLines += withFrontMatter.frontmatter.split('\r\n|\r|\n').length;
+    }
+    var emptyLines = new Array(numEmptyLines + 1).join('\n');
+    text = emptyLines + withFrontMatter.body || text;
   }
 
   try {


Test patch:
diff --git a/tests/main.js b/tests/main.js
index 28131df..0a23191 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -173,6 +173,13 @@ describe('sass lint', function () {
       });
     });
 
+    it('should preserve line numbers after parsing a file with front matter', function (done) {
+      lintFile('front-matter/front-matter.scss', function (data) {
+        assert.equal(6, data.messages[0].line);
+        done();
+      });
+    });
+
     // ==============================================================================
     //  Parse Errors should return as lint errors
     // ==============================================================================