A linter for stylus
npm install stylint




npm install stylint --save-devAs a cli tool: npm install stylint -g
NOTE: We are currently working on a 2.0 release. Bug fixing will not be a priority during this process, but we will try
to make sure every bug is squashed after that.
Follow the project,
milestone and issue for
progress. Please contribute if you have the time! Both with code, but also with ideas and features.
The goal of 2.0 is to make the project more stable:
- actual unit tests on the rules to avoid regressions, instead of coupling them to the tests of the core linter
- a clearly defined API surface, making Stylint easy to depend on for external tools
- a cleaner way of invoking Stylint programatically
- a way of linting a string of code, not just files
- simplify config (inspired by ESLint's config structure)
Sometime in the future:tm: we will also provide extension points for custom rules, and maybe even automatically fix some violations, but that's a change that might take some time. These goals are more long-term though, short term the focus is on stability and integration with tools.
If you have any problems with the linter just create a ticket there and I will respond.
Same thing if you have any feature requests.
I will gladly accept pull requests if you want to do the work yourself.
you can also ping me here
-h or --help Display list of commands-w or --watch Watch file or directory and run lint on change
-c or --config Pass in location of custom config file
-v or --version Display current version
stylint Run stylint on cwdstylint path/to/filename.styl Run stylint on a file
stylint path/to/dir --watch Watch dir, run stylint on file change
stylint --help Get list of commands
stylint --version Get version number
stylint --config path/to/config/.configrc Run stylint with custom config settings
stylint --reporter stylint-reporter-module Run stylint with custom reporter module
stylint styl/ --watch -c path/to/config/.configrc Watch dir, use custom config
javascript
const stylint = require('stylint')('path/to/stylus/', {
brackets: 'always',
namingConvention: 'BEM',
semicolons: 'always'
}, callbackFn).create();
`API docs are in the docs/ folder
Gulp
You can use the CLI with gulp-shell like below:
`javascript
gulp.task('stylint', shell.task([
'stylint path/to/styl/ -c .stylintrc'
]));
`Or just use gulp-stylint
`javascript
var gulp = require('gulp');
var stylint = require('gulp-stylint');gulp.task('default', function () {
return gulp.src('src/*.styl')
.pipe(stylint())
});
`Grunt
You can use grunt-stylint`javascript
grunt.initConfig({
stylint: {
options: {
config: {
colons: 'never'
}
},
src: ['src/*/.styl']
}
});
`Webpack
You can use stylint-loader`javascript
module.exports = {
// ...
module: {
preLoaders: [
{
test: /\.styl$/,
loader: 'stylint'
}
],
loaders: [
{
test: /\.styl$/,
loader: 'style!css!stylus'
}
]
}
// ...
}
`Rollup
You can use rollup-plugin-stylint` javascript
import { rollup } from 'rollup';
import stylint from 'rollup-plugin-stylint';rollup({
entry: 'index.js',
plugins: [
stylint()
]
});
`As Part of Your Workflow
Stylint integrations with multiple IDEs are available.* Atom
* Sublime Text
* WebStorm / PhpStorm / IntelliJ IDEA
* Visual Studio Code
Why Write This Tool?
Stylus is my CSS pre-processor of choice and the lack of a decent linter (or really, any linter) was an annoying pain point. So I thought I'd try my hand at writing what I thought my ideal linter would look like.
Why Use This Tool?
To catch little mistakes (duplication of rules for instance) and to enforce a code style guide. This is particularly important with Stylus, which is unopinionated when it comes to syntax. Like Stylus itself, this linter opts for flexibility over rigidity.
Options
The following is a list of all options available to stylint.Note that customProperties and mixins are aliases
`json
{
"blocks": false,
"brackets": "never",
"colons": "always",
"colors": "always",
"commaSpace": "always",
"commentSpace": "always",
"cssLiteral": "never",
"customProperties": [],
"depthLimit": false,
"duplicates": true,
"efficient": "always",
"exclude": [],
"extendPref": false,
"globalDupe": false,
"groupOutputByFile": true,
"indentPref": false,
"leadingZero": "never",
"maxErrors": false,
"maxWarnings": false,
"mixed": false,
"mixins": [],
"namingConvention": false,
"namingConventionStrict": false,
"none": "never",
"noImportant": true,
"parenSpace": false,
"placeholders": "always",
"prefixVarsWithDollar": "always",
"quotePref": false,
"reporterOptions": {
"columns": ["lineData", "severity", "description", "rule"],
"columnSplitter": " ",
"showHeaders": false,
"truncate": true
},
"semicolons": "never",
"sortOrder": "alphabetical",
"stackedProperties": "never",
"trailingWhitespace": "never",
"universal": false,
"valid": true,
"zeroUnits": "never",
"zIndexNormalize": false
}
`
#### Custom Configuration
Stylint will try to find a custom configuration in many different places before it falls back to the default config (see above). It goes in this order of importance:
1. Pass in via function parameter (ie, using stylint programmatically)
2. Passed in via command line flag
3. Passed in via package.json, either as an object or path to a
.stylintrc file
4. Via a .stylintrc file, starting from the cwd and working up to the home user directory
5. If all of the above fails, use the default configYou can also use the
-c or --config flags to pass in the location of your custom .stylintrc config file if it resides somewhere else.You can use the
stylintrc property in your package.json to point to a .stylintrc file, or just directly pass in your configIf requiring Stylint ( as opposed to using it via the cli ), the 2nd param is the config object.
#### Severity
Stylint has 2 levels of output, Warnings and Errors. All Stylint rules optionally take an object, allowing you to set it to be an Error. Combined with the maxWarnings and maxErrors properties, you can be very flexible about say, code style but strict on things like code duplication.
Example:
`json
{
"brackets": {
"expect": "never",
"error": true
},
"colons": {
"expect": "always",
"error": true
}
}
`
Custom Reporters
Stylint console output can be modified with the use of a reporter. Feel free to write your own (no matter how outlandish) and I'll add it here.* stylint-stylish
* stylint-json-reporter
Excluding files, directories, and code blocks from the linter
$3
Before linting Stylint will look for a .stylintignore file in the current working directory, and will ignore files listed there. The files should be formatted in the same way you would use .gitignore or .eslintignoreFor reference, it looks like this:
`
path/to/file.styl
path/to/directory/
`
$3
You can also exclude files/directories via your package.json by adding a stylintignore property.For reference:
`json
{
"stylintignore": ["path/to/file.styl", "path/to/directory/"],
}
`
$3
see config documentation below for how to use the exclude option via .stylintrc or your passed in config object
$3
Disable linting for a particular block of code by placing @stylint off in a line comment. Re-enable by placing @stylint on in a line comment further down. Stylint will not test any lines until turned back on. Use this to suppress warnings on a case-by-case basis. By default the linter will check every line except for @css blocks or places where certain rules have exceptions.For example, let's say you want to enforce
namingConvention: "lowercase_underscore", but you're also styling elements from the Bootstrap framework. You can use the @stylint off toggle to prevent warnings in places where you're referencing Bootstrap classes.Example:
`stylus
.button_block {
background: silver;
padding: 4px;
}
// @stylint off
.button_block .btn-default {
background-color: green;
color: white;
}
// @stylint on
`
$3
A toggle that works like @stylint off, but just for one line. Useful for cases where you want to include fallback css for browser support.Example:
`stylus
.button-zoom
background-image: url('path/to/png.png') // @stylint ignore
background-image: url('path/to/svg.svg')
cursor: pointer // @stylint ignore
cursor: zoom-in
`
$3
In Stylus you have the option of using mixins transparently, like css properties. Because of how Stylus' syntax works, this rule also allows you to add adhoc support for custom properties as needed by just added the name of the property to this array.`javascript
>>> config file
"customProperties": ['myCustomProperty']>>> example.styl
.className
myCustomProperty: 5px
`Where
myCustomProperty is a mixin that takes a px val as it's parameter.If you use nib,
size, absolute, and fixed are often used in this way. If you use css-modules, composes is another one.Because of nib's widespread use, and css-modules growing popularity, the above 4 custom properties are supported by Stylint by default.
$3
When 'always' expect the @block keyword when defining block variables.
When 'never', expect no @block keyword when defining block variables.
When false, do not check either way.Example if 'always': prefer
my-block = @block over my-block = Example if 'never': prefer
my-block = over my-block = @block
$3
When 'always', expect {} when declaring a selector.
When 'never', expect no brackets when declaring a selector.Example if 'always': prefer
.some-class-name { over .some-class-nameExample if 'never': prefer
.some-class-name over .some-class-name {
$3
When 'always', expect : when declaring a property.
When 'never', expect no : when declaring a property.Example if 'always': prefer
margin: 0 over margin 0Example if 'never: prefer
margin 0 over margin: 0
$3
When 'always', enforce variables when defining hex valuesExample if true: prefer
color $red over color #f00
$3
Enforce or disallow spaces after commas.Example if always: prefer
rgba(0, 0, 0, .18) over rgba(0,0,0,.18)Example if never: prefer
rgba(0,0,0,.18) over rgba(0, 0, 0, .18)
$3
Enforce or disallow spaces after line commentsExample if always: prefer
// comment over //commentExample if never: prefer
//comment over // comment
$3
By default Stylint ignores @css blocks. If set to true however, it will throw a warning if @css is used.Example if 'never':
@css will throw a warning
$3
Set the max selector depth. If set to 4, max selector depth will be 4 indents. Pseudo selectors like &:first-child or &:hover won't count towards the limit.Set to false if you don't want to check for this.
$3
Checks if selector or property duplicated unnecessarily. By default, only checks on a file-by-file basis, but if globalDupes: true is set, then it will also check for duplicates globally (for root values only).Example if true: the following will throw a warning:
`stylus
.test
margin 0
margin 5px
`
$3
Check for places where properties can be written more efficiently.Example if always: prefer
margin 0 over margin 0 0Example if never: prefer
margin 0 0 over margin 0
$3
Exclude certain file patterns from linting.Example:
["vendor//", "node_modules//"] will exclude all files in the vendor or node_modules directory.
$3
Pass in either @extend or @extends and then enforce that. Both are valid in Stylus. It doesn't really matter which one you use. I prefer @extends myself.Example if set to
@extends: prefer @extends $some-var over @extend $some-varExample if set to
@extend: prefer @extend $some-var over @extends $some-var
$3
Works in conjunction with duplicates. Does nothing on its own. If false, duplicates will check for dupes within individual files only. If true, duplicates will check for dupes across all files.Example if true: the following will throw a warning
`stylus
>>> file1.styl
.test
margin 0>>> file2.styl
.test
margin 5px
`
$3
Stylint's default setting groups errors and warnings by file when outputting. You can disable this if you wantExample if true:
`javascript
path/to/file.styl
73:32 warning mixed spaces and tabs mixed
78:30 error missing semicolon semicolonspath/to/file2.styl
16 warning prefer alphabetical when sorting properties sortOrder
`Example if false:
`javascript
path/to/file.styl
73:32 warning mixed spaces and tabs mixedpath/to/file.styl
78:30 error missing semicolon semicolons
path/to/file2.styl
16 warning prefer alphabetical when sorting properties sortOrder
`
$3
This works in conjunction with depthLimit. If you indent with spaces this is the number of spaces you indent with. If you use hard tabs, set this value to false.If you set this to a number, it will output warnings/errors if you the # of spaces used for indenting differs from the number set.
By default this value is false, so if you indent with spaces you'll need to manually set this value in a custom
.stylintrc file.Example if 2: prefer
/s/smargin: 0 over /s/s/smargin: 0
$3
Enforce or disallow unnecessary leading zeroes on decimal points.Example if always: prefer
rgba( 0, 0, 0, 0.5 ) over rgba( 0, 0, 0, .5 )Example if never: prefer
rgba( 0, 0, 0, .5 ) over rgba( 0, 0, 0, 0.5 )
$3
Set 'max' number of Errors.
$3
Set 'max' number of Warnings.
$3
Returns true if mixed spaces and tabs are found. If a number is passed to indentPref, it assumes soft tabs (ie, spaces), and if false is passed to indentPref it assumes hard tabs.If soft tabs, outputs warning/error if hard tabs used. If hard tabs, outputs warning/error if unnecessary extra spaces found.
Example if indentPref: 4 and mixed: true: prefer
\s\s\s\smargin\s0 over \tmargin\s0Example if indentPref: 2 and mixed: true: prefer
\s\smargin\s0 over \tmargin\s0Example if indentPref: false and mixed: true: prefer
\tmargin\s0 over \s\s\s\smargin\s0
$3
Enforce a particular naming convention when declaring classes, ids, and variables. Throws a warning if you don't follow the convention.Example if set to
'lowercase-dash': prefer $var-name over $var_name or $varNameExample if set to
'lowercase_underscore': prefer $var_name over $var-name or $varNameExample if set to
'camelCase': prefer $varName over $var_name or $var-nameExample if set to
'BEM': prefer $var__like--this over $var_name or $varName
$3
By default, namingConvention only looks at variable names. If namingConventionStrict is set to true, namingConvention will also look at class and id names.If you have a lot of 3rd party css you can't change you might want to leave this off.
$3
If 'always' check for places where none used instead of 0.
If 'never' check for places where 0 could be used instead of none.Example if 'always': prefer
border none over border 0Example if 'never': prefer
outline 0 over outline none
$3
If true, show warning when !important is found.Example if true: the following will throw a warning
`stylus
div
color red !important
`
$3
Enforce or disallow use of extra spaces inside parens.Example if always: prefer
my-mixin( $myParam ) over my-mixin($myParam)Example if never: prefer
my-mixin($myParam) over my-mixin( $myParam )
$3
Enforce extending placeholder vars when using @extend(s)Example if always: prefer
@extends $placeholder over $extends .some-classExample if never: prefer
@extends .some-class over $extends $placeholder
$3
Enforce use of $ when defining a variable. In Stylus using a $ when defining a variable is optional, but is a good idea if you want to prevent ambiguity. Not including the $ sets up situations where you wonder: "Is this a variable or a value?" For instance: padding $default is easier to understand than padding default.Yes,
default isn't an acceptable value for padding, but at first glance you might not catch that since it just looks like a value. And now if you try to set cursor default, it's not going to behave the way you expect. All this pain and heartache could've been avoided if you just used a $.Example if always: prefer
$my-var = 0 over my-var = 0Example if never: prefer
my-var = 0 over $my-var = 0
$3
Enforce consistent quotation style.Example if
'single': prefer $var = 'some string' over $var = "some string"Example if
'double': prefer $var = "some string" over $var = 'some string'
$3
If using the default reporter, Stylint uses columnify when outputting warnings and errors (only if you have groupOutputByFile set to true). See columnify for more details. Using this option, you can easily customize the output (to an extent) without having to install a separate reporter.Default options:
`
{
columns: ['lineData', 'severity', 'description', 'rule'],
columnSplitter: ' ',
showHeaders: false,
truncate: true
}
`
$3
Enforce or disallow semicolonsExample if always: prefer
margin 0; over margin 0Example if never: prefer
margin 0 over margin 0;
$3
Enforce a particular sort order when declaring properties. Throws a warning if you don't follow the order. If set to false, allow any order.Example if
'alphabetical':prefer this:
`stylus
.some-class
display block
float left
position absolute
right 10px
top 0
`over this:
`stylus
.some-class
position absolute
top 0
right 10px
display block
float left
`Example if
'grouped' (based on predefined grouped ordering):prefer this:
`stylus
.some-class
position absolute
top 0
right 10px
display block
float left
`over this:
`stylus
.some-class
display block
float left
position absolute
right 10px
top 0
`Example if
[ 'margin', 'padding', 'float', 'position' ]:prefer this:
`stylus
.some-class
margin 0
padding 0
float left
position absolute
right 10px
top 0
display block
`over this:
`stylus
.some-class
display block
float left
position absolute
right 10px
top 0
margin 0
padding 0
`When set to
'grouped' or {Array} throws a warning if properties that are not defined in the ordering array are not after those that should be ordered.
$3
No one-liners. Enforce putting properties on new lines.Example if
never: prefer`stylus
.className
padding 0
`over
.className { padding 0 }
$3
If false, ignores trailing white space. If 'never', trailing white space will throw a warning.
$3
Looks for instances of the inefficient * selector. Lots of resets use this, for good reason (resetting box model), but past that you really don't need this selector, and you should avoid it if possible.Example if never, disallow the following:
`stylus
div *
margin 0
`
$3
Check that a property is valid CSS or HTML.Example if true:
marg 0 will throw a warning, prefer margin 0Example if true:
divv will throw a warning, prefer div
$3
Looks for instances of 0px. You don't need the px. Checks all units, not just px.Example if always: prefer
margin-right 0px over margin-right 0Example if never: prefer
margin-right 0 over margin-right 0em
$3
Enforce some (very) basic z-index sanity. Any number passed in will be used as the base for your z-index values. Throws an error if your value is not normalized.Example if set to 5: prefer
z-index 10 over z-index 9
Example if set to 10: prefer z-index 20 over z-index 15
Example if set to 50: prefer z-index 100 over z-index 75`