Skip to content

Added greedy pattern matching when validating attribute separators #150

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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