forked from sasstools/sass-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔍 Add unnecessary-mantissa and accompanying tests
- Loading branch information
Drew Hays
committed
Dec 3, 2015
1 parent
0c90429
commit a7db54b
Showing
6 changed files
with
159 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,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; | ||
} | ||
``` |
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
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,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; | ||
} | ||
}; |
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,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(); | ||
}); | ||
}); | ||
}); |
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,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' |
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 { | ||
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'; | ||
} | ||
} |