Skip to content

Commit

Permalink
🔍 Add unnecessary-mantissa and accompanying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Hays committed Dec 3, 2015
1 parent 0c90429 commit a7db54b
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/rules/unnecessary-mantissa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Unnecessary Mantissa

Rule `unnecessary-mantissa` will enforce that trailing decimal points should be removed if they are unnecessary (e.g. removing `.0` from `4.0`).

## Examples

When enabled, the following are allowed:

```scss
$margin-size: 4.7px;

.foo {
border-size: 4.3px 2% .8rem 9ex;
}
```

When enabled, the following are disallowed:

```scss
$margin-size: 4.0px;

.foo {
border-size: 10.0px 2.0% .0rem 9.0ex;
}
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rules:
no-vendor-prefixes: 1
no-warn: 1
property-units: 0
unnecessary-mantissa: 1

# Nesting
force-attribute-nesting: 1
Expand Down
34 changes: 34 additions & 0 deletions lib/rules/unnecessary-mantissa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

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

var hasUnnecessaryMantissa = function (mantissa) {
return !mantissa.match(/[^0]/);
};

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

ast.traverseByType('number', function (item) {
var match = item.content.match(/(\d*)\.(\d+)/);
if (match && hasUnnecessaryMantissa(match[2])) {
// if the number was written without an integer
// (e.g. '.0'), it needs to be replaced with 0
var correction = match[1] || '0';
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'severity': parser.severity,
'line': item.end.line,
'column': item.end.column,
// TODO: include unit in result?
'message': '`' + item.content + '` should be written without the mantissa as `' + correction + '`'
});
}
});

return result;
}
};
35 changes: 35 additions & 0 deletions tests/rules/unnecessary-mantissa.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('unnecessary mantissa - scss', function () {
var file = lint.file('unnecessary-mantissa.scss');

it('enforce', function (done) {
lint.test(file, {
'unnecessary-mantissa': 1
}, function (data) {
lint.assert.equal(6, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('unnecessary mantissa - scss', function () {
var file = lint.file('unnecessary-mantissa.sass');

it('enforce', function (done) {
lint.test(file, {
'unnecessary-mantissa': 1
}, function (data) {
lint.assert.equal(6, data.warningCount);
done();
});
});
});
28 changes: 28 additions & 0 deletions tests/sass/unnecessary-mantissa.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.foo
width: 50.0%
height: 70.2%

.bar
padding: 5px 3.0 10.2px 4.0%

.baz
line-height: .7em
margin-top: .0px

$foo: 4.0px
$bar: '10.0px'
$baz: 11.0
$quux: '12.0'

.foo
@if $bar == 10
content: 'bar'

@if $foo == '4.0px'
content: 'bar'

@if $baz == '12.0'
content: 'baz'

@if $quux == 11
content: 'quux'
36 changes: 36 additions & 0 deletions tests/sass/unnecessary-mantissa.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.foo {
width: 50.0%;
height: 70.2%;
}

.bar {
padding: 5px 3.0 10.2px 4.0%;
}

.baz {
line-height: .7em;
margin-top: .0px;
}

$foo: 4.0px;
$bar: '10.0px';
$baz: 11.0;
$quux: '12.0';

.foo {
@if $bar == 10 {
content: 'bar';
}

@if $foo == '4.0px' {
content: 'foo';
}

@if $baz == '12.0' {
content: 'baz';
}

@if $quux == 11 {
content: 'quux';
}
}

0 comments on commit a7db54b

Please sign in to comment.