Skip to content

Feature/property count #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions docs/rules/property-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Property Count

Rule `property-count` will enforce the maximum number of properties allowed within a ruleset. This can include or exclude a cumulative count of nested properties also.

## Options

* `max-properties`: number (defaults to `0`)
* `include-nested`: `true`/`false` (defaults to `false`)

## Examples

When `max-properties: '2'`, the following are allowed.

```scss
.foo {
content: '';
display: block;
}

.nest {
content: '';
display: block;

.nest-child {
content: '';
display: block;
}
}
```
When `max-properties: '2'`, the following are disallowed.

```scss
.foo { // lint reported
content: '';
display: block;
color: red;
}

.nest { // lint reported
content: '';
display: block;
color: red;

.nest-child { // lint reported
content: '';
display: block;
color: red;
}
}
```

When `max-properties: 2` and `include-nested: true`, the following are allowed.

```scss
.foo {
content: '';
display: block;
}

.nest { // nested property count of 2
content: '';

.nest-child {
content: '';
}
}
```

When `max-properties: 2` and `include-nested: true`, the following are disallowed.

```scss
.foo { // lint reported
content: '';
display: block;
color: red;
}

.nest { // lint reported - nested property count of 3
content: '';

.nest-child {
content: '';
color: red
}
}

.nest-other { // lint reported - nested property count of 3
content: '';

.nest-other-child {
content: '';
}

.nest-other-child-sibling {
display: block;
}
}
```

#### Special Case - `max-properties: 0`

As there is no perceived best practice for a maximum number of properties per ruleset the default for `max-properties` is 0. This effectively leaves the rule disabled and should therefore always be customised by the end user.


When `max-properties: '0'`, the following are allowed.

```scss
.foo {
content: '';
display: block;
}

.nest {
content: '';
display: block;

.nest-child {
content: '';
display: block;
}
}
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rules:
leading-zero: 1
nesting-depth: 1
property-sort-order: 1
property-count: 0
quotes: 1
shorthand-values: 1
url-quotes: 1
Expand Down
83 changes: 83 additions & 0 deletions lib/rules/property-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

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

/**
* Traverses a ruleset node for declarations and rulesets.
* Counts the properties per nest depth and handles the parser options
* for excluding nested values. Also used to skip over rulesets in the main rule
* if they've already been parsed here.
* @param {object} node - the current ruleset node
* @param {bool} includeNested - user defined option on whether to parse nested blocks
* @returns {array} the property count and the number of child rulesets parsed
*/
var countProps = function (node, includeNested) {
var propertyCount = 0,
nestCount = 1;

if (node.contains('block')) {
node.forEach('block', function (block) {
block.forEach('declaration', function (declaration) {
if (declaration.contains('property')) {
propertyCount++;
}
});
if (includeNested && block.contains('ruleset')) {
block.forEach('ruleset', function (childRule) {
var outcome = countProps(childRule, true);
propertyCount += outcome[0];
nestCount += outcome[1];
});
}
});
}

return [propertyCount, nestCount];
};

module.exports = {
'name': 'property-count',
'defaults': {
'max-properties': 0,
'include-nested': false
},
'detect': function (ast, parser) {
var result = [],
propCount = 0,
nestedRules = 1,
outcome,
errProps,
propSuffix;

ast.traverseByType('ruleset', function (ruleset) {
var errMessage = 'Rule set contains ';

if (nestedRules > 1) {
nestedRules--;

return false;
}

outcome = countProps(ruleset, parser.options['include-nested']);
propCount = outcome[0];
nestedRules = outcome[1];

if (parser.options['max-properties'] >= 1 && propCount > parser.options['max-properties']) {
errProps = parseInt(propCount - parser.options['max-properties'], 10);
propSuffix = errProps === 1 ? 'property' : 'properties';
errMessage += errProps + ' ' + propSuffix;
errMessage += ' more than the specified maximum of ' + parser.options['max-properties'];
errMessage += parser.options['include-nested'] ? '(includes properties in nested rules)' : '';

result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'line': ruleset.start.line,
'column': ruleset.start.column,
'message': errMessage,
'severity': parser.severity
});
}
});
return result;
}
};
181 changes: 181 additions & 0 deletions tests/rules/property-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
'use strict';

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

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

it('[default]', function (done) {
lint.test(file, {
'property-count': 1
}, function (data) {
lint.assert.equal(0, data.warningCount);
done();
});
});

it('[max-properties: 2]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 2
}
]
}, function (data) {
lint.assert.equal(12, data.warningCount);
done();
});
});

it('[max-properties: 2, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 2,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(8, data.warningCount);
done();
});
});

it('[max-properties: 0, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 0,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(0, data.warningCount);
done();
});
});

it('[max-properties: 4]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 4
}
]
}, function (data) {
lint.assert.equal(2, data.warningCount);
done();
});
});

it('[max-properties: 4, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 4,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(6, data.warningCount);
done();
});
});
});

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

it('[default]', function (done) {
lint.test(file, {
'property-count': 1
}, function (data) {
lint.assert.equal(0, data.warningCount);
done();
});
});

it('[max-properties: 2]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 2
}
]
}, function (data) {
lint.assert.equal(12, data.warningCount);
done();
});
});

it('[max-properties: 2, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 2,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(8, data.warningCount);
done();
});
});

it('[max-properties: 0, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 0,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(0, data.warningCount);
done();
});
});

it('[max-properties: 4]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 4
}
]
}, function (data) {
lint.assert.equal(2, data.warningCount);
done();
});
});

it('[max-properties: 4, include-nested: true]', function (done) {
lint.test(file, {
'property-count': [
1,
{
'max-properties': 4,
'include-nested': true
}
]
}, function (data) {
lint.assert.equal(6, data.warningCount);
done();
});
});
});
Loading