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 a4c1cc8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lib/rules/validate-attribute-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ 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 regexNewLines = new RegExp('(\\r\\n|\\r|\\n)[ \\t]{' + current._indent + '}', 'g');

source = source.replace(regexReplace, function (val) {
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/invalid.pug
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
div
|
div=
div
13 changes: 6 additions & 7 deletions test/fixtures/reporters/expected-invalid.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
%dirname%invalid.pug:2:1
1| div
> 2| |
-------^
3| div
4|
%dirname%invalid.pug:1:4
> 1| div=
----------^
2| div
3|

unexpected text "|
unexpected text "=
div"
3 changes: 1 addition & 2 deletions test/fixtures/rules/invalid.pug
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
div
|
div=
div
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")
8 changes: 4 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

0 comments on commit a4c1cc8

Please sign in to comment.