diff --git a/lib/rules/disallow-attribute-concatenation.js b/lib/rules/disallow-attribute-concatenation.js index 00416ea..45e8177 100644 --- a/lib/rules/disallow-attribute-concatenation.js +++ b/lib/rules/disallow-attribute-concatenation.js @@ -22,9 +22,11 @@ module.exports.prototype = , lint: function (file, errors) { - var regex = /\+(?=([^']*'[^']*')*[^']*$)/ - - file.addErrorForAllLinesByType('attribute', regex, true, errors, 'Attribute concatenation must not be used') + file.iterateTokensByType('attribute', function (token) { + if (utils.concatenationRegex.test(token.val)) { + errors.add('Attribute concatenation must not be used', token.line) + } + }) } } diff --git a/lib/rules/disallow-string-concatenation.js b/lib/rules/disallow-string-concatenation.js index 3b045b5..1c6a2ea 100644 --- a/lib/rules/disallow-string-concatenation.js +++ b/lib/rules/disallow-string-concatenation.js @@ -22,11 +22,13 @@ module.exports.prototype = , lint: function (file, errors) { - var regex = /['"]\s*\+|\+\s*['"]/ - - file.addErrorForAllLinesByFilter(function (token) { + file.iterateTokensByFilter(function (token) { return (token.type === 'code' && token.buffer) - }, regex, true, errors, 'String concatenation must not be used') + }, function (token) { + if (utils.concatenationRegex.test(token.val)) { + errors.add('String concatenation must not be used', token.line) + } + }) } } diff --git a/lib/utils.js b/lib/utils.js index 0403913..ea69564 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -34,3 +34,5 @@ exports.validateTrueOptions = function (name, options) { } exports.htmlTagBoundaryTypes = [ ':', 'newline', 'indent', 'outdent', 'eos' ] + +exports.concatenationRegex = /.['"]\s*\+|\+\s*['"]./ diff --git a/test/rules/disallow-attribute-concatenation.test.js b/test/rules/disallow-attribute-concatenation.test.js index 50dae22..21dec07 100644 --- a/test/rules/disallow-attribute-concatenation.test.js +++ b/test/rules/disallow-attribute-concatenation.test.js @@ -15,14 +15,16 @@ function createTest (linter, fixturesPath) { it('should report attribute concatenation', function () { assert.equal(linter.checkString('a(href=\'text \' + title) Link').length, 1) assert.equal(linter.checkString('a(href=title + \'text \') Link').length, 1) + assert.equal(linter.checkString('a(href=title + "text ") Link').length, 1) + assert.equal(linter.checkString('a(href=title + title)').length, 0) + assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'+G Logo\')').length, 0) + assert.equal(linter.checkString('img(src="logo.png", alt="G+ Logo")').length, 0) + assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'G Logo+\')').length, 0) + assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'+\')').length, 0) }) it('should not report attribute interpolation', function () { assert.equal(linter.checkString('a(href=\'#{title}\') Link').length, 0) - assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'+G Logo\')').length, 0) - assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'G+ Logo\')').length, 0) - assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'G Logo+\')').length, 0) - assert.equal(linter.checkString('img(src=\'logo.png\', alt=\'+\')').length, 0) }) it('should report multiple errors found in file', function () { diff --git a/test/rules/disallow-string-concatenation.test.js b/test/rules/disallow-string-concatenation.test.js index 8c2146e..25bf849 100644 --- a/test/rules/disallow-string-concatenation.test.js +++ b/test/rules/disallow-string-concatenation.test.js @@ -14,6 +14,9 @@ function createTest (linter, fixturesPath) { it('should report string concatenation', function () { assert.equal(linter.checkString('h1= title + \'text\'').length, 1) + assert.equal(linter.checkString('h1(class="test" + "test")= title + \'text\'').length, 1) + assert.equal(linter.checkString('h1= \'text+\'').length, 0) + assert.equal(linter.checkString('h1= test + test').length, 0) }) it('should not report string interpolation', function () {