Skip to content

Commit

Permalink
Merge branch 'develop' into feature/tidy-mergeable-selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
bgriffith authored Aug 15, 2016
2 parents e43b349 + 91dfe0b commit dc2d59d
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 14 deletions.
33 changes: 33 additions & 0 deletions docs/rules/no-color-hex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# No Color Hex

Rule `no-color-hex` will disallow the use of hexadecimal colors

## Examples

When enabled the following are disallowed.

```scss
$foo-color: #456;

.bar {
background: linear-gradient(top, #3ff, #ddd);
}

.baz {
color: #fff;
}
```

When enabled the following are allowed:

```scss
$foo-color: red;

.bar {
background: linear-gradient(top, blue, green);
}

.baz {
color: white;
}
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rules:

# Disallows
no-attribute-selectors: 0
no-color-hex: 0
no-color-keywords: 1
no-color-literals: 1
no-combinators: 0
Expand Down
22 changes: 22 additions & 0 deletions lib/rules/no-color-hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var helpers = require('../helpers');

module.exports = {
'name': 'no-color-hex',
'defaults': {},
'detect': function (ast, parser) {
var result = [];

ast.traverseByType('color', function (value) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': value.start.line,
'column': value.start.column,
'message': 'Hexadecimal colors should not be used',
'severity': parser.severity
});
});
return result;
}
};
47 changes: 33 additions & 14 deletions lib/rules/single-line-per-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

var helpers = require('../helpers');

/**
* Checks a ruleset for selectors or EOL characters. If a selector is found before an EOL
* then it returns the selector node for reporting or returns false
*
* @param {Object} ruleset - The ruleset node
* @param {number} index - The current index of the delimiter
* @returns {Object|boolean} Either the selector node or false
*/
var checkLineForSelector = function (ruleset, index) {
var curIndex = index += 1;
if (ruleset.content[curIndex]) {
for (; curIndex < ruleset.content.length; curIndex++) {
var curType = ruleset.content[curIndex].type;
if (curType === 'space' && helpers.hasEOL(ruleset.content[curIndex])) {
return false;
}
if (curType === 'selector' || curType === 'typeSelector') {
return ruleset.content[curIndex];
}
}
}

return false;
};

module.exports = {
'name': 'single-line-per-selector',
'defaults': {},
Expand All @@ -10,22 +35,16 @@ module.exports = {

ast.traverseByType('ruleset', function (ruleset) {
ruleset.forEach('delimiter', function (delimiter, j) {
var next = ruleset.content[j + 1] || false;
var next = checkLineForSelector(ruleset, j);

if (next) {
if (next.is('selector')) {
next = next.content[0];
}

if (!(next.is('space') && helpers.hasEOL(next.content))) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': next.start.line,
'column': next.start.column,
'message': 'Selectors must be placed on new lines',
'severity': parser.severity
});
}
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': next.start.line,
'column': next.start.column,
'message': 'Selectors must be placed on new lines',
'severity': parser.severity
});
}
});
});
Expand Down
35 changes: 35 additions & 0 deletions tests/rules/no-color-hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var lint = require('./_lint');

//////////////////////////////
// SCSS syntax tests
//////////////////////////////
describe('no color hex - scss', function () {
var file = lint.file('no-color-hex.scss');

it('enforce', function (done) {
lint.test(file, {
'no-color-hex': 1
}, function (data) {
lint.assert.equal(9, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('no color hex - sass', function () {
var file = lint.file('no-color-hex.sass');

it('enforce', function (done) {
lint.test(file, {
'no-color-hex': 1
}, function (data) {
lint.assert.equal(9, data.warningCount);
done();
});
});
});
25 changes: 25 additions & 0 deletions tests/sass/no-color-hex.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$foo-color: #123

.foo
background: linear-gradient(top, #cc2, #44d)
color: #fff


$bar-color: #112233

.bar
background: linear-gradient(top, #cccc22, #4444dd)
color: #ffffff

.baz
border-color: #123456


// color literals, rgb and hsl values currently don't get returned
// by the AST's color type
$qux-color: red
$rgb-color: rgb(255, 255, 255)
$rgba-color: rgba(0, 0, 0, .1)
$hsl-color: hsl(40, 50%, 50%)
$hsla-color: hsla(40, 50%, 50%, .3)
26 changes: 26 additions & 0 deletions tests/sass/no-color-hex.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$foo-color: #123;

.foo {
background: linear-gradient(top, #cc2, #44d);
color: #fff;
}

$bar-color: #112233;

.bar {
background: linear-gradient(top, #cccc22, #4444dd);
color: #ffffff;
}

.baz {
border-color: #123456;
}

// color literals, rgb and hsl values currently don't get returned
// by the AST's color type

$qux-color: red;
$rgb-color: rgb(255, 255, 255);
$rgba-color: rgba(0, 0, 0, .1);
$hsl-color: hsl(40, 50%, 50%);
$hsla-color: hsla(40, 50%, 50%, .3);
9 changes: 9 additions & 0 deletions tests/sass/single-line-per-selector.sass
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@
.foo
.bar &, .baz &
content: 'foo'

// Issue #789 - Issue with comments being warned as selector content on single lines
button,
html,
html input[type='button'], // 6
input[type='reset'],
input[type='submit']
-webkit-appearance: button; // 7
cursor: pointer; // 8
10 changes: 10 additions & 0 deletions tests/sass/single-line-per-selector.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@
content: 'foo';
}
}

// Issue #789 - Issue with comments being warned as selector content on single lines
button,
html,
html input[type='button'], // 6
input[type='reset'],
input[type='submit'] {
-webkit-appearance: button; // 7
cursor: pointer; // 8
}

0 comments on commit dc2d59d

Please sign in to comment.