Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some edge cases in preprocess logic #3

Merged
merged 5 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/processors/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ module.exports = {
preprocess: function(text, filename) {
// Trim leading and trailing whitespace
text = text.replace(/^\s+|\s+$/g, '');

// Check if the file begins with frontmatter declaration
if (text.substring(0,3) === '---') {
// Remove the frontmatter
var split = text.split('---');
text= split.slice(2).join('---');
// Find end of frontmatter
var matchedDashes = /^---$/m.exec(text.slice(3));

if (matchedDashes) {
var dashStartIndex = matchedDashes.index;
var sliceIndex = dashStartIndex + matchedDashes[0].length;
return [text.slice(sliceIndex + 3).replace(/^\s+/, '')];
}
}

return [text]; // return an array of strings to lint
},

Expand All @@ -43,4 +50,4 @@ module.exports = {
// Don't autofix lint errors
supportsAutofix: false
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "Douglas Duhaime",
"main": "lib/index.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha ./tests/*.js"
"test": "mocha ./tests/*.js"
},
"dependencies": {
"lodash": "^4.17.4",
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
process_with_jekyll: true
---

// We shouldn't remove this: ---
var plusOne = function(num) {
return num+1;
};
4 changes: 4 additions & 0 deletions tests/fixtures/dashesInCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
frontmatter: true
---
x --- y;
4 changes: 4 additions & 0 deletions tests/fixtures/withDashesInFrontMatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
propertyKey: "Value with ---"
---
var x;
39 changes: 27 additions & 12 deletions tests/plugin.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
var CLIEngine = require('eslint').CLIEngine;
var plugin = require('../');
var chai = require('chai');
var fs = require('fs');
var path = require('path');
var should = chai.should();
var assert = chai.assert;

function getFixturePath(relativePath) {
return path.normalize(path.join(__dirname, relativePath));
}

describe('Tests for frontmatter ESLint processor', function() {

// Specify a sample input file
var sample = `
---
process_with_jekyll: true
---

// We shouldn't remove this: ---
var plusOne = function(num) {
return num+1;
};
`
var sample = fs.readFileSync(getFixturePath("./fixtures/basic.js"), "utf8");

before(function() {
cli = new CLIEngine({
Expand All @@ -36,15 +33,33 @@ describe('Tests for frontmatter ESLint processor', function() {

describe('the preprocess function', function() {
it('should remove frontmatter', function() {
var report = cli.executeOnText(sample, 'sample.js');
var processed = plugin['processors']['.js']['preprocess'](sample)
var tripleDashes = processed[0].split('---').length;
tripleDashes.should.equal(2);
});

it('should not raise a linting error', function() {
var report = cli.executeOnText(sample, 'sample.js');
var errors = report.results[0].messages;
errors.length.should.equal(0);
});

it('should remove all frontmatter even if --- is present in frontmatter', function() {
debugger;
var withDashes = fs.readFileSync(getFixturePath("./fixtures/withDashesInFrontMatter.js"), "utf8");

var processed = plugin.processors['.js'].preprocess(withDashes);

processed[0].should.equal('var x;');
});

it('should not change JS even if --- present in code', function() {
debugger;
var dashesInCode = fs.readFileSync(getFixturePath("./fixtures/dashesInCode.js"), "utf8");

var processed = plugin.processors['.js'].preprocess(dashesInCode);

processed[0].should.equal('x --- y;');
});
});
});
});