Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:sasstools/sass-lint into feature…
Browse files Browse the repository at this point in the history
…/error-codes

# Conflicts:
#	tests/cli.js
  • Loading branch information
DanPurdy committed Jan 25, 2016
2 parents 2a15719 + 6893d0f commit fda8715
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ Our AST is [Gonzales-PE](https://github.com/tonyganch/gonzales-pe/tree/dev). Eac

* [Atom](https://atom.io/packages/linter-sass-lint)
* [Sublime Text](https://github.com/skovhus/SublimeLinter-contrib-sass-lint)
* [Brackets](https://github.com/petetnt/brackets-sass-lint)
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,29 @@ sassLint.resultCount = function (results) {
*/
sassLint.lintText = function (file, options, configPath) {
var rules = slRules(this.getConfig(options, configPath)),
ast = groot(file.text, file.format, file.filename),
ast = {},
detects,
results = [],
errors = 0,
warnings = 0;

if (ast.content.length > 0) {
try {
ast = groot(file.text, file.format, file.filename);
}
catch (e) {
var line = e.line || 1;
errors++;

results = [{
ruleId: 'Fatal',
line: line,
column: 1,
message: e.message,
severity: 2
}];
}

if (ast.content && ast.content.length > 0) {
rules.forEach(function (rule) {
detects = rule.rule.detect(ast, rule);
results = results.concat(detects);
Expand All @@ -117,7 +133,6 @@ sassLint.lintText = function (file, options, configPath) {
});
}


results.sort(helpers.sortDetects);

return {
Expand Down
16 changes: 12 additions & 4 deletions lib/groot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
var gonzales = require('gonzales-pe');

module.exports = function (text, syntax, filename) {
var fileInfo = filename ? ' at ' + filename : '',
tree;
var tree;

// Run `.toString()` to allow Buffers to be passed in
text = text.toString();
Expand All @@ -18,11 +17,20 @@ module.exports = function (text, syntax, filename) {
});
}
catch (e) {
throw new Error('Parsing error' + fileInfo + ': ' + e.message);
throw {
message: e.message,
file: filename,
line: e.line
};
}

if (typeof tree === 'undefined') {
throw new Error('Undefined tree' + fileInfo + ': ' + text.toString() + ' => ' + tree.toString());
throw {
message: 'Undefined tree',
file: filename,
text: text.toString(),
tree: tree.toString()
};
}

return tree;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/extends-before-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
var lastDeclaration = null;

block.forEach(function (item, j) {
if (item.type === 'include' || item.type === 'extend') {
if ((item.type === 'include' || item.type === 'extend') &&
item.first('atkeyword')) {
if (item.first('atkeyword').first('ident').content === 'extend') {
if (j > lastDeclaration && lastDeclaration !== null) {
item.forEach('simpleSelector', function () {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/space-after-colon.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
if (delimiter.content === ':') {
var next = parent.content[i + 1];

if (next.is('space')) {
if (next && next.is('space')) {
if (!parser.options.include) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/space-around-operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var checkSpacing = function (node, i, parent, parser, result) {
return false;
}
}
// If we have a negative value without a preceeding value then we should
// return false
else {
return false;
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,57 @@ describe('cli', function () {
return done(new Error('Error code not 1'));
});
});

/**
* We disabled eslints handle callback err rule here as we are deliberately throwing errors that we don't care about
*/
it('parse errors should report as a lint error', function (done) {
var command = 'sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';

childProcess.exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0];

assert.equal(1, result.errorCount);
done();
});
});

it('parse errors should report as severity 2', function (done) {
var command = 'sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';

childProcess.exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
messages = result.messages[0],
severity = 2;

assert.equal(severity, messages.severity);
done();
});
});

it('parse errors should report the correct message', function (done) {
var command = 'sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';

childProcess.exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
message = result.messages[0].message,
expected = 'Please check validity of the block starting from line #5';

assert.equal(expected, message);
done();
});
});

it('parse errors rule Id should be \'Fatal\'', function (done) {
var command = 'sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';

childProcess.exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
messages = result.messages[0],
ruleId = 'Fatal';

assert.equal(ruleId, messages.ruleId);
done();
});
});
});
46 changes: 46 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ describe('sass lint', function () {
done();
});
});

//////////////////////////////
// Parse Errors should return as lint errors
//////////////////////////////
it('Parse Errors should return as lint errors', function (done) {
lintFile('parse.scss', function (data) {
assert.equal(1, data.errorCount);
done();
});
});

it('Parse Errors should not include warnings too', function (done) {
lintFile('parse.scss', function (data) {
assert.equal(0, data.warningCount);
done();
});
});

it('Parse Errors should return as severity 2', function (done) {
lintFile('parse.scss', function (data) {
var severity = data.messages[0].severity;

assert.equal(2, severity);
done();
});
});

it('Parse Errors should return the correct error message', function (done) {
lintFile('parse.scss', function (data) {
var message = data.messages[0].message,
expected = 'Please check validity of the block starting from line #5';

assert.equal(expected, message);
done();
});
});

it('Parse Errors should return the rule ID \'Fatal\'', function (done) {
lintFile('parse.scss', function (data) {
var ruleId = data.messages[0].ruleId,
expected = 'Fatal';

assert.equal(expected, ruleId);
done();
});
});
});

describe('sassLint detect counts', function () {
Expand Down
5 changes: 5 additions & 0 deletions tests/sass/extends-before-declarations.sass
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@
.bar
@extend %waldo
content: 'where'


.foo-noextend
+foo
content: "bar"
4 changes: 4 additions & 0 deletions tests/sass/extends-before-declarations.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
content: 'where';
}
}

.foo-noextend {
@include foo;
}
7 changes: 7 additions & 0 deletions tests/sass/parse.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.one {
color: red;
}

#test
color: blue;
}
3 changes: 3 additions & 0 deletions tests/sass/space-around-operator.sass
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,6 @@ $qux: (2 * 1) // FIXME: Gonzales - FIXED IN BETA

h1
+large-text

.foo
z-index: -1
4 changes: 4 additions & 0 deletions tests/sass/space-around-operator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,7 @@ $qux: (2 * 1); // FIXME: Gonzales - FIXED IN BETA
@if ($foo <= $bar) {
$baz: 1;
}

.foo {
z-index: -1;
}

0 comments on commit fda8715

Please sign in to comment.