Repository: lesshint/lesshint
Pull request #360 (2017-03-11 06:52:07)

Handle both strings and error objects in custom errors
<!--
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?**
Point 3 of #357, kinda. Since we broke that functionality in `3.0` with our custom errors which this commit fixes.

**Is there anything in this PR that needs extra explaining or should something specific be focused on?**
Not really.


Patch:
diff --git a/lib/errors/lesshint-error.js b/lib/errors/lesshint-error.js
index 2bf01d4..8b442ff 100644
--- a/lib/errors/lesshint-error.js
+++ b/lib/errors/lesshint-error.js
@@ -8,7 +8,7 @@ class LesshintError extends Error {
 
         errorPath = errorPath || error.path;
 
-        this.message = error.message;
+        this.message = error.message || error;
         this.name = this.constructor.name;
         this.path = path.resolve(process.cwd(), errorPath);
         this.stack = error.stack;
diff --git a/lib/errors/runner-error.js b/lib/errors/runner-error.js
index e31df8c..8a6e38a 100644
--- a/lib/errors/runner-error.js
+++ b/lib/errors/runner-error.js
@@ -4,7 +4,7 @@ class RunnerError extends Error {
     constructor (error, status) {
         super(error);
 
-        this.message = error.message;
+        this.message = error.message || error;
         this.name = this.constructor.name;
         this.status = status;
         this.stack = error.stack;
diff --git a/lib/linters/space_around_operator.js b/lib/linters/space_around_operator.js
index 830763e..1038e13 100644
--- a/lib/linters/space_around_operator.js
+++ b/lib/linters/space_around_operator.js
@@ -24,12 +24,12 @@ module.exports = {
             const nextElement = child.parent.nodes[index + 1];
             const prevElement = child.parent.nodes[index - 1];
 
+
             // Ignore negative numbers
             if (child.value === '-' && (child.raws.before || node.raws.between) &&
-                nextElement.type === 'number' && !nextElement.raws.before) {
+                (nextElement.type === 'number' || nextElement.value === '(') && !nextElement.raws.before) {
                 return;
             }
-
             // Ignore variables
             if (child.value === '-' && (child.raws.before || node.raws.between) &&
                 nextElement.type === 'atword' && !nextElement.raws.before) {
diff --git a/package.json b/package.json
index 0899c06..5d49144 100644
--- a/package.json
+++ b/package.json
@@ -39,7 +39,7 @@
     "gulp-mocha": "^4.1.0",
     "nyc": "^10.1.2",
     "rimraf": "^2.4.2",
-    "sinon": "^1.14.1"
+    "sinon": "^2.0.0"
   },
   "bin": {
     "lesshint": "bin/lesshint"


Test patch:
diff --git a/test/specs/errors/lesshint-error.js b/test/specs/errors/lesshint-error.js
new file mode 100644
index 0000000..8489c5e
--- /dev/null
+++ b/test/specs/errors/lesshint-error.js
@@ -0,0 +1,33 @@
+'use strict';
+
+const LesshintError = require('../../../lib/errors/lesshint-error');
+const expect = require('chai').expect;
+const path = require('path');
+
+describe('LesshintError', function () {
+    it('should handle an Error object', function () {
+        const message = 'Something went wrong';
+        const error = new Error(message);
+
+        const lesshintError = new LesshintError(error, '/path/to/file.less');
+
+        expect(lesshintError.message).to.equal(message);
+    });
+
+    it('should handle a a string', function () {
+        const message = 'Something went wrong';
+
+        const lesshintError = new LesshintError(message, '/path/to/file.less');
+
+        expect(lesshintError.message).to.equal(message);
+    });
+
+    it('should resolve paths', function () {
+        const file = 'file.less';
+        const errorPath = path.resolve(process.cwd(), file);
+
+        const lesshintError = new LesshintError('Something went wrong', file);
+
+        expect(lesshintError.path).to.equal(errorPath);
+    });
+});
diff --git a/test/specs/errors/runner-error.js b/test/specs/errors/runner-error.js
new file mode 100644
index 0000000..2d47162
--- /dev/null
+++ b/test/specs/errors/runner-error.js
@@ -0,0 +1,30 @@
+'use strict';
+
+const RunnerError = require('../../../lib/errors/runner-error');
+const expect = require('chai').expect;
+
+describe('RunnerError', function () {
+    it('should handle an Error object', function () {
+        const message = 'Something went wrong';
+        const error = new Error(message);
+
+        const runnerError = new RunnerError(error, 1);
+
+        expect(runnerError.message).to.equal(message);
+    });
+
+    it('should handle a a string', function () {
+        const message = 'Something went wrong';
+
+        const runnerError = new RunnerError(message, 1);
+
+        expect(runnerError.message).to.equal(message);
+    });
+
+    it('should set the status', function () {
+        const status = 1;
+        const runnerError = new RunnerError('Something went wrong', status);
+
+        expect(runnerError.status).to.equal(status);
+    });
+});
diff --git a/test/specs/linters/space_around_operator.js b/test/specs/linters/space_around_operator.js
index d67819e..6578500 100644
--- a/test/specs/linters/space_around_operator.js
+++ b/test/specs/linters/space_around_operator.js
@@ -84,6 +84,16 @@ describe('lesshint', function () {
                 });
             });
 
+            it('should not report on negative values before a (', function () {
+                const source = 'margin-left: -(10px);';
+
+                return spec.parse(source, function (ast) {
+                    const result = spec.linter.lint(options, ast.root.first);
+
+                    expect(result).to.be.undefined;
+                });
+            });
+
             it('should not report on negative values in shorthands', function () {
                 const source = 'margin: -10px -1px 0px 20px;';