SCSS-lint Regex
I got tired of cleaning Sass codebases by hand to make them compliant with SCSS-lint so I wrote a set of regular expressions to do just that.
The point is to be able to perform a quick search and replace to make the whole codebase compliant to a specific rule from SCSS-lint — when possible of course. Turned out to be extremely easy in Sublime Text for instance (see following screenshot).
If you suspect a regular expression to be incorrect, dangerous or improvable, please by all mean open a new issue so we can discuss it!
- Search:
([^\s])!important
- Replace:
\1 !important
- Search:
(border(-bottom|-left|-right|-top)?):\s*none
- Replace:
\1: 0
A bit complex, but could probably be done.
- Search:
^@debug.+$
- Replace: nothing
Hard, but could be done I suppose.
- Search:
}\s*@else
- Replace:
} @else
Could be done.
- Search:
^([^\n{]+){(?:|\s+)}\n
- Replace: nothing
Could be done I suppose.
- Search:
#(?:([a-fA-F0-9])\1)(?:([a-fA-F0-9])\2)(?:([a-fA-F0-9])\3)
- Replace:
#\1\2\3
- Search:
(#[a-zA-Z0-9]{6}|[a-zA-Z0-9]{3})
- Replace:
\L\1\E
(to lowercase) or\U\1\E
(to uppercase)
- Search:
^@import\s+('|")_?(.*?)(?:.scss)?\1;
- Replace:
@import \1\2\1;
Could theoretically be possible I guess.
Remove zeros:
- Search:
\s+0\.(\d+)
- Replace:
\ .\1
Include zeros:
- Search:
\s+\.(\d+)
- Replace:
\ 0.\1
- Search:
([a-z])([A-Z])
- Replace:
\1-\L\2\E
Note: remember to enable case sensitiveness.
Note: will also affect selectors. Could be optimised to only affect variables, placeholders, mixins and functions.
- Search:
([^:]):(after|backdrop|before|content|first-letter|first-line|placeholder|selection|shadow)
- Replace:
\1::\2
Could be doable.
- Search:
;(?!\n)\s*
- Replace:
;\n
Note: currently does not apply indentation.
Could be doable.
- Search:
,([^\s])
- Replace:
, \1
- Search:
:(?:|\s{2,})([^\s])
- Replace:
: \1
Could be doable.
Could be doable.
Could be doable.
Could be doable.
- Search
([^\s]){
- Replace:
\1 {
- Search:
\(\s+([^()]+)\s+\)
- Replace:
(\1)
Note: does not deal with nested parentheses.
- Search:
"
- Replace:
'
Could be doable.
- Search:
[ \t]+$
- Replace: nothing
Could be doable.
Could be doable.
- Search:
url\(([^\s'"]+)\)
- Replace:
url('\1')
(orurl("\1")
)
Could be doable.
- Search:
0(?!s|ms)(\w+)
- Replace:
0