-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1142 from Asana/no-disallowed-property-values
New rule: no-disallowed-property-values
- Loading branch information
Showing
5 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |