Repository: lesshint/lesshint
Pull request #412 (2017-07-21 04:22:31)

Refactor url* rules position reporting
<!--
If you're submitting a PR for a new feature, please open an issue about it first so we can discuss it.
If you're submitting a PR for something else, for example a documentation fix, go right ahead!
-->

**Which issue, if any, does this resolve?**
No issue.

**Is there anything in this PR that needs extra explaining or should something specific be focused on?**
No, it should be straight forward.


Patch:
diff --git a/docs/developer-guide/api/lesshint.md b/docs/developer-guide/api/lesshint.md
index 9db4d91..5acce58 100644
--- a/docs/developer-guide/api/lesshint.md
+++ b/docs/developer-guide/api/lesshint.md
@@ -71,6 +71,8 @@ Setup the options to use. This method must always be called before doing anythin
 
 The `config` argument is optional, but if a object is passed it'll be merged with the defaults.
 
+The merged, final, config object will be returned.
+
 ```js
 const config = {
     emptyRule: false
diff --git a/lib/lesshint.js b/lib/lesshint.js
index 367515a..92a1dec 100644
--- a/lib/lesshint.js
+++ b/lib/lesshint.js
@@ -136,6 +136,8 @@ class Lesshint {
         const defaultConfig = configLoader(__dirname + '/config/defaults.json');
 
         this.config = merge(defaultConfig, config);
+
+        return this.config;
     }
 
     hasAllowedExtension (file) {
diff --git a/lib/linters/url_format.js b/lib/linters/url_format.js
index f8322e2..b64e4c2 100644
--- a/lib/linters/url_format.js
+++ b/lib/linters/url_format.js
@@ -9,7 +9,7 @@ module.exports = {
     nodeTypes: ['decl'],
     message: 'URL "%s" should be %s.',
 
-    lint: function urlFormatLinter (config, decl) {
+    lint: function urlFormatLinter (config, node) {
         const style = {
             absolute: isAbsoluteURL,
             relative: (url) => {
@@ -25,7 +25,7 @@ module.exports = {
          * Traverse all child value-nodes
          * https://github.com/lesshint/lesshint/issues/225
          */
-        const ast = parseValue(decl.params || decl.value);
+        const ast = parseValue(node.params || node.value);
         const results = [];
 
         ast.first.nodes.forEach((child) => {
@@ -35,16 +35,16 @@ module.exports = {
 
             // Skip over the 'paren' node; the following node is the value node.
             const uri = child.first.next();
+            const value = uri.value.trim();
 
-            if (!style[config.style](uri.value)) {
-                const column = (decl.raws.between ? decl.raws.between.length : 0) +
-                         decl.source.start.column +
-                         decl.prop.length +
-                         uri.source.start.column - 1;
+            if (!style[config.style](value)) {
+                const position = node.positionBy({
+                    word: value
+                });
 
                 results.push({
-                    column: column,
-                    line: decl.source.start.line,
+                    column: position.column,
+                    line: position.line,
                     message: util.format(this.message, uri.value, config.style)
                 });
             }
diff --git a/lib/linters/url_quotes.js b/lib/linters/url_quotes.js
index db91e68..4e06314 100644
--- a/lib/linters/url_quotes.js
+++ b/lib/linters/url_quotes.js
@@ -57,14 +57,13 @@ module.exports = {
                  * Tracking: https://github.com/lesshint/postcss-values-parser/issues/1
                  */
                 if ((uri.type === 'word' && !(rSingle.test(value) || rDouble.test(value))) && uri.type !== 'string') {
-                    const column = (node.raws.between ? node.raws.between.length : 0) +
-                             node.source.start.column +
-                             (node.prop || node.name).length +
-                             uri.source.start.column - 1;
+                    const position = node.positionBy({
+                        word: value
+                    });
 
                     results.push({
-                        column: column,
-                        line: node.source.start.line,
+                        column: position.column,
+                        line: position.line,
                         message: this.message
                     });
                 }


Test patch:
diff --git a/test/specs/lesshint.js b/test/specs/lesshint.js
index 8b1172b..5cc1404 100644
--- a/test/specs/lesshint.js
+++ b/test/specs/lesshint.js
@@ -273,6 +273,13 @@ describe('lesshint', function () {
 
             expect(lesshint.config).to.deep.equal(expected);
         });
+
+        it('should return the final config object', function () {
+            const expected = configLoader(path.resolve(process.cwd() + '/lib/config/defaults.json'));
+            const lesshint = new Lesshint();
+
+            expect(lesshint.configure(expected)).to.deep.equal(expected);
+        });
     });
 
     describe('getConfig', function () {
diff --git a/test/specs/linters/url_quotes.js b/test/specs/linters/url_quotes.js
index 2a5640b..950de75 100644
--- a/test/specs/linters/url_quotes.js
+++ b/test/specs/linters/url_quotes.js
@@ -156,7 +156,7 @@ describe('lesshint', function () {
         it('should not allow unquoted URLs strings surrounded by spaces (#22)', function () {
             const source = '.foo { background-image: url( img/image.jpg ); }';
             const expected = [{
-                column: 30,
+                column: 31,
                 line: 1,
                 message: 'URLs should be enclosed in quotes.'
             }];