diff --git a/docs/rules/no-trailing-zero.md b/docs/rules/no-trailing-zero.md new file mode 100644 index 00000000..cb7589ba --- /dev/null +++ b/docs/rules/no-trailing-zero.md @@ -0,0 +1,21 @@ +# No Trailing Zero + +Rule `no-trailing-zero` will enforce that trailing zeros are not allowed. + +## Examples + +When enabled, the following are disallowed: + +```scss +.foo { + margin: 1.500rem; +} + +.foo { + margin: .500rem; +} + +.foo { + margin: 0.2500rem; +} +``` diff --git a/lib/config/sass-lint.yml b/lib/config/sass-lint.yml index be8f51ee..484d2a11 100644 --- a/lib/config/sass-lint.yml +++ b/lib/config/sass-lint.yml @@ -28,6 +28,7 @@ rules: no-important: 1 no-invalid-hex: 1 no-qualifying-elements: 1 + no-trailing-zero: 1 no-url-protocols: 1 no-vendor-prefixes: 1 no-warn: 1 diff --git a/lib/rules/no-trailing-zero.js b/lib/rules/no-trailing-zero.js new file mode 100644 index 00000000..f959d56f --- /dev/null +++ b/lib/rules/no-trailing-zero.js @@ -0,0 +1,30 @@ +'use strict'; + +var helpers = require('../helpers'); + +var trailingZeroRegex = /^(\d+\.|\.)+(\d*?[1-9])0+$/; + +module.exports = { + 'name': 'no-trailing-zero', + 'defaults': { + 'include': false + }, + 'detect': function (ast, parser) { + var result = []; + + ast.traverseByType('number', function (num) { + + if (num.content.match(trailingZeroRegex)) { + result = helpers.addUnique(result, { + 'ruleId': parser.rule.name, + 'line': num.start.line, + 'column': num.start.column, + 'message': 'Don\'t include trailing zeros on numbers', + 'severity': parser.severity + }); + } + }); + + return result; + } +}; diff --git a/tests/rules/no-trailing-zero.js b/tests/rules/no-trailing-zero.js new file mode 100644 index 00000000..7af4293a --- /dev/null +++ b/tests/rules/no-trailing-zero.js @@ -0,0 +1,16 @@ +'use strict'; + +var lint = require('./_lint'); + +var file = lint.file('no-trailing-zero.scss'); + +describe('no trailing zero', function () { + it('enforce', function (done) { + lint.test(file, { + 'trailing-zero': 1 + }, function (data) { + lint.assert.equal(3, data.warningCount); + done(); + }); + }); +}); diff --git a/tests/sass/no-trailing-zero.scss b/tests/sass/no-trailing-zero.scss new file mode 100644 index 00000000..f476fa28 --- /dev/null +++ b/tests/sass/no-trailing-zero.scss @@ -0,0 +1,27 @@ +.foo { + margin: 1.5rem; +} + +.foo { + margin: 1.500rem; +} + +.foo { + margin: .5rem; +} + +.foo { + margin: .500rem; +} + +.foo { + margin: 0.25rem; +} + +.foo { + margin: 0.2500rem; +} + +.foo { + margin: 0; +}