Skip to content

Commit

Permalink
🐛 Preserve correct line numbers when front matter is stripped
Browse files Browse the repository at this point in the history
  • Loading branch information
flacerdk committed Apr 23, 2017
1 parent d81cc0e commit 7d1d8b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/groot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ==============================================================================
Expand Down

0 comments on commit 7d1d8b7

Please sign in to comment.