Skip to content

Commit

Permalink
Merge pull request sasstools#144 from DanPurdy/feature/vendor-prefix
Browse files Browse the repository at this point in the history
Add rule: no-vendor-prefix
  • Loading branch information
Snugug committed Sep 9, 2015
2 parents 9ea096d + c1ab77a commit db1270e
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 0 deletions.
97 changes: 97 additions & 0 deletions docs/rules/no-vendor-prefixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# No Vendor Prefixes

Rule `no-vendor-prefixes` will enforce that vendor prefixes are not allowed to be used.

List of prefixes affected by default:
* webkit
* moz
* ms

## Options

* `additional-identifiers`: `[array of additional prefixes to check for]` (defaults to empty array `[]`)
* `excluded-identifiers`: `[array of prefixes to exclude checking for]` (defaults to empty array `[]`)

## Examples

When enabled, the following are disallowed:

```scss
@-webkit-keyframes anim {
0% { opacity: 0; }
}

.ms-block {
-ms-hyphenate-limit-lines: no-limit;
}

::-moz-placeholder {
content: '';
}

.foo {
-webkit-transition: none;
}

.bar {
position: -moz-sticky;
}
```

When `additional-identifiers` contains a custom prefix value of `test` as show below

```yaml
no-vendor-prefix:
- 1
-
'additional-identifiers':
- 'khtml'
```

The following would now also be disallowed

```scss
.baz {
position: -khtml-sticky;
}
```

When `excluded-identifiers` contains currently disallowed prefix values such as `webkit` and `moz` as show below

```yaml
no-vendor-prefix:
- 1
-
'excluded-identifiers':
- 'webkit'
- 'moz'
```

The following would now be allowed

```scss
@-webkit-keyframes anim {
0% { opacity: 0; }
}

::-moz-placeholder {
content: '';
}

.foo {
-webkit-transition: none;
}

.bar {
position: -moz-sticky;
}
```

While the following would remain disallowed

```scss

.ms-block {
-ms-hyphenate-limit-lines: no-limit;
}
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rules:
no-css-comments: 1
no-color-literals: 1
no-url-protocols: 1
no-vendor-prefixes: 1

# Style Guide
border-zero: 1
Expand Down
61 changes: 61 additions & 0 deletions lib/rules/no-vendor-prefixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

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

var prefixes = ['webkit', 'moz', 'ms'];

var handleExcludes = function (excludes) {
excludes.forEach(function (item) {
var index = prefixes.indexOf(item);

if (index > -1) {
prefixes.splice(index, 1);
}
});
};

var handleIncludes = function (includes) {
includes.forEach(function (item) {
if (prefixes.indexOf(item) === -1) {
prefixes.push(item);
}
});
};

var precompileRegEx = function (includes, excludes) {

if (includes.length) {
handleIncludes(includes);
}

if (excludes.length) {
handleExcludes(excludes);
}

return new RegExp('-(' + prefixes.join('|') + ')-');
};

module.exports = {
'name': 'no-vendor-prefixes',
'defaults': {
'additional-identifiers': [],
'excluded-identifiers': []
},
'detect': function (ast, parser) {
var result = [],
statement = precompileRegEx(parser.options['additional-identifiers'], parser.options['excluded-identifiers']);

ast.traverseByType('ident', function (value) {
if (statement.test(value.content)) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': value.start.line,
'column': value.start.column,
'message': 'Vendor prefixes should not be used',
'severity': parser.severity
});
}
});
return result;
}
};
88 changes: 88 additions & 0 deletions tests/rules/no-vendor-prefixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

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

var file = lint.file('no-vendor-prefixes.scss');

describe('no vendor prefix', function () {
it('enforce', function (done) {
lint.test(file, {
'no-vendor-prefixes': 1
}, function (data) {
lint.assert.equal(5, data.warningCount);
done();
});
});

it('[excluded-identifiers: webkit]', function (done) {
lint.test(file, {
'no-vendor-prefixes': [
1,
{
'excluded-identifiers':
[
'webkit'
]
}
]
}, function (data) {
lint.assert.equal(3, data.warningCount);
done();
});
});

it('[excluded-identifiers: webkit, moz]', function (done) {
lint.test(file, {
'no-vendor-prefixes': [
1,
{
'excluded-identifiers':
[
'webkit',
'moz'
]
}
]
}, function (data) {
lint.assert.equal(1, data.warningCount);
done();
});
});

it('[included-identifiers: khtml]', function (done) {
lint.test(file, {
'no-vendor-prefixes': [
1,
{
'additional-identifiers':
[
'khtml'
]
}
]
}, function (data) {
lint.assert.equal(2, data.warningCount);
done();
});
});

it('[included-identifiers: khtml, webkit, moz]', function (done) {
lint.test(file, {
'no-vendor-prefixes': [
1,
{
'excluded-identifiers': [],
'additional-identifiers':
[
'khtml',
'webkit',
'moz'
]
}
]
}, function (data) {
lint.assert.equal(6, data.warningCount);
done();
});
});
});
23 changes: 23 additions & 0 deletions tests/sass/no-vendor-prefixes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@-webkit-keyframes anim {
0% { opacity: 0; }
}

::-moz-placeholder {
color: red;
}

.foo {
-webkit-transition: none;
}

.bar {
position: -moz-sticky;
}

.baz {
position: -khtml-sticky;
}

.ms-block {
-ms-hyphenate-limit-lines: no-limit;
}

0 comments on commit db1270e

Please sign in to comment.