Skip to content

Commit

Permalink
Merge pull request #1142 from Asana/no-disallowed-property-values
Browse files Browse the repository at this point in the history
New rule: no-disallowed-property-values
  • Loading branch information
DanPurdy committed Nov 13, 2017
2 parents fad4e51 + 16407a6 commit 3043155
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/rules/no-disallowed-property-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# No Disallowed Property Values

Rule `no-disallowed-property-values` will warn against the use of certain values for properties. Either a single string can be specified for a single disallowed value, or an array of disallowed property values.

## Options

* `properties`: `{dictionary of properties mapped to string or array of strings representing values}` (defaults to empty dictionary `{}`).

## Examples

When `properties` contains a the following options for disallowed values of `text-transform` and `white-space`:

```yaml
no-disallowed-property-values:
- 1
-
properties:
text-transform: capitalize
white-space: ['pre', 'pre-wrap']
```

The following would not be allowed:

```scss

// the capitalize value for text-transform is not allowed
.foo {
text-transform: capitalize;
}

// the pre and pre-wrap values for white-space are not allowed
.bar {
white-space: pre;
}
.baz {
white-space: pre-wrap;
}
```
45 changes: 45 additions & 0 deletions lib/rules/no-disallowed-property-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

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

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

ast.traverseByType('value', function (valueNode, idx, parent) {
var propertyNode = parent.first('property');
if (!propertyNode) {
return;
}
var propertyNameNode = propertyNode.first();
if (!propertyNameNode.is('ident')) {
return;
}
var propertyName = propertyNameNode.content;
var disallowedPropertyValues = parser.options.properties[propertyName];
if (!disallowedPropertyValues) {
return;
}
if (typeof disallowedPropertyValues === 'string') {
disallowedPropertyValues = [disallowedPropertyValues];
}
valueNode.forEach('ident', function (node) {
if (disallowedPropertyValues.indexOf(node.content) === -1) {
return;
}
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': node.start.line,
'column': node.start.column,
'message': 'Property `' + propertyName + '` should not have value `' + node.content + '`',
'severity': parser.severity
});
});
});
return result;
}
};
113 changes: 113 additions & 0 deletions tests/rules/no-disallowed-property-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';

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

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

it('properties: { display: [block, inline] }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': ['block', 'inline']
}
}
]
}, function (data) {
lint.assert.equal(4, data.warningCount);
done();
});
});

it('properties: { display: [inline] }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': ['inline']
}
}
]
}, function (data) {
lint.assert.equal(1, data.warningCount);
done();
});
});

it('properties: { display: inline }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': 'inline'
}
}
]
}, function (data) {
lint.assert.equal(1, data.warningCount);
done();
});
});
});

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

it('properties: { display: [block, inline] }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': ['block', 'inline']
}
}
]
}, function (data) {
lint.assert.equal(4, data.warningCount);
done();
});
});

it('properties: { display: [inline] }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': ['inline']
}
}
]
}, function (data) {
lint.assert.equal(1, data.warningCount);
done();
});
});

it('properties: { display: inline }', function (done) {
lint.test(file, {
'no-disallowed-property-values': [
1,
{
properties: {
'display': 'inline'
}
}
]
}, function (data) {
lint.assert.equal(1, data.warningCount);
done();
});
});
});
25 changes: 25 additions & 0 deletions tests/sass/no-disallowed-property-values.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.foo
z-index: 10
display: block
.bar
.baz
z-index: 20

.test
@media (max-width: 100px)
z-index: 10

=test ($level)
z-index: $level
display: block

@function myFunc($level)
@return $level * 2

.func-res
z-index: myFunc(10)
display: block

interp
z-index: #{20}
display: inline
36 changes: 36 additions & 0 deletions tests/sass/no-disallowed-property-values.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.foo {
z-index: 10;
display: block;

.bar {
.baz {
z-index: 20;
}
}
}

.test {
@media (max-width: 100px) {
z-index: 10;
}
}

@mixin test ($level) {
z-index: $level;
display: block;
}


@function myFunc($level) {
@return $level * 2;
}

.func-res {
z-index: myFunc(10);
display: block;
}

interp {
z-index: #{20};
display: inline;
}

0 comments on commit 3043155

Please sign in to comment.