Skip to content

Commit

Permalink
Added greedy pattern matching when validating attribute separators
Browse files Browse the repository at this point in the history
Fixes pugjs#149
  • Loading branch information
Joona Laamanen committed Apr 12, 2018
1 parent 5f8fdff commit 6c7282f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/rules/validate-attribute-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ module.exports.prototype = {

function getParsedSource(current, start, end, patterns) {
var source = file.getSourceBetweenTokens(current, end).trim();
var regexReplace = new RegExp(patterns.join('|'), 'g');
var regexReplace = new RegExp(patterns.sort(function (a, b) {
// Patterns need to be sorted longest first to make the replace greedy
return b.length - a.length;
}).join('|'), 'g');
// var regexReplace = new RegExp(patterns.join('|'), 'g');
var regexNewLines = new RegExp('(\\r\\n|\\r|\\n)[ \\t]{' + current._indent + '}', 'g');

source = source.replace(regexReplace, function (val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ div
div
div(a="foo", b="bar",
c="batz")

div(
foo,
foo-bar="baz")

div(
foo,
foo-foo="bar")
4 changes: 4 additions & 0 deletions test/fixtures/rules/validate-attribute-separator.pug
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ div
class='class'
name='name'
)

div(foo foo-bar="baz")

div(foo foo-foo="bar")
9 changes: 5 additions & 4 deletions test/rules/validate-attribute-separator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createTest(linter, fixturesPath) {
it('should report multiple errors found in file', function () {
var result = linter.checkFile(fixturePath);

assert.equal(result.length, 32);
assert.equal(result.length, 34);
assert.equal(result[0].code, 'PUG:LINT_VALIDATEATTRIBUTESEPARATOR');
assert.equal(result[0].line, 2);
assert.equal(result[0].column, 18);
Expand All @@ -76,7 +76,7 @@ function createTest(linter, fixturesPath) {
it('should report multiple errors found in file', function () {
var result = linter.checkFile(fixturePath);

assert.equal(result.length, 31);
assert.equal(result.length, 33);
assert.equal(result[0].code, 'PUG:LINT_VALIDATEATTRIBUTESEPARATOR');
assert.equal(result[0].line, 2);
assert.equal(result[0].column, 18);
Expand All @@ -99,7 +99,7 @@ function createTest(linter, fixturesPath) {
it('should report multiple errors found in file', function () {
var result = linter.checkFile(fixturePath);

assert.equal(result.length, 32);
assert.equal(result.length, 34);
assert.equal(result[0].code, 'PUG:LINT_VALIDATEATTRIBUTESEPARATOR');
assert.equal(result[0].line, 2);
assert.equal(result[0].column, 18);
Expand All @@ -122,7 +122,7 @@ function createTest(linter, fixturesPath) {
it('should report multiple errors found in file', function () {
var result = linter.checkFile(fixturePath);

assert.equal(result.length, 33);
assert.equal(result.length, 35);
assert.equal(result[0].code, 'PUG:LINT_VALIDATEATTRIBUTESEPARATOR');
assert.equal(result[0].line, 2);
assert.equal(result[0].column, 18);
Expand Down Expand Up @@ -175,6 +175,7 @@ function createTest(linter, fixturesPath) {
assert.equal(result.length, 0);

result = linter.checkFile(fixturesPath + 'validate-attribute-separator--multiline.pug');
console.log('resultsit', result);

assert.equal(result.length, 0);
});
Expand Down

0 comments on commit 6c7282f

Please sign in to comment.