forked from coala/coala-bears
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSSCombBear is a coding stye formatter for CSS. It is based on .csscomb.json file written by the user. Closes coala#634
- Loading branch information
1 parent
4926dc0
commit 24db134
Showing
2 changed files
with
210 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,85 @@ | ||
from coalib.bearlib.abstractions.Linter import linter | ||
from coalib.bears.requirements.NpmRequirement import NpmRequirement | ||
import json | ||
|
||
|
||
@linter(executable='csscomb', | ||
output_format='corrected', | ||
use_stdout=True, | ||
use_stdin=False, | ||
result_message='Change coding style based on CSS rules written in ' + | ||
'.csscomb.json', | ||
) | ||
class CSSCombBear: | ||
""" | ||
CSScomb is a coding style formatter for CSS. | ||
For more information, consult <https://github.com/csscomb/csscomb.js>. | ||
""" | ||
|
||
LANGUAGES = {"CSS"} | ||
REQUIREMENTS = {NpmRequirement('csscomb')} | ||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} | ||
LICENSE = 'AGPL-3.0' | ||
ASCIINEMA_URL = 'https://asciinema.org/a/bxpke7nizyxdf6mlx6ss4h405' | ||
CAN_FIX = {'Formatting'} | ||
|
||
@staticmethod | ||
def create_arguments(filename, file, config_file, csscomb_json: str = ""): | ||
args = (filename,) | ||
if csscomb_json: | ||
args += (csscomb_json,) | ||
else: | ||
args += (config_file,) | ||
return args | ||
|
||
def generate_config(filename, file, | ||
verbose: bool = True, | ||
remove_empty_rulesets: bool = True, | ||
always_semicolon: bool = True, | ||
color_case: str = "upper", | ||
block_indent: str = "\\t", | ||
color_shorthand: bool = False, | ||
eof_newline: bool = True, | ||
leading_zero: bool = False, | ||
quotes: str = "single", | ||
sort_order_fallback: str = "abc", | ||
space_before_colon: str = " ", | ||
space_after_colon: str = " ", | ||
space_between_declar: str = "\\n", | ||
space_before_o_brace: str = "\\n", | ||
space_after_o_brace: str = "\\n", | ||
space_after_s_delim: str = "\\n", | ||
space_before_s_delim: str = " ", | ||
space_before_c_brace: str = "\\n", | ||
strip_spaces: bool = True, | ||
tab_size: bool = True, | ||
vendor_prefix_align: bool = True, | ||
csscomb_json: str = ""): | ||
|
||
if not csscomb_json: | ||
options = {"verbose": verbose, | ||
"remove-empty-rulesets": remove_empty_rulesets, | ||
"always-semicolon": always_semicolon, | ||
"color-case": color_case, | ||
"block-indent": block_indent, | ||
"eof-newline": eof_newline, | ||
"leading-zero": leading_zero, | ||
"quotes": quotes, | ||
"sort-order-fallback": sort_order_fallback, | ||
"space-before-colon": space_before_colon, | ||
"space-after-colon": space_after_colon, | ||
"space-between-declarations": space_between_declar, | ||
"space-before-opening-brace": space_before_o_brace, | ||
"space-after-opening-brace": space_after_o_brace, | ||
"space-after-selector-delimiter": space_after_s_delim, | ||
"space-before-selector-delimiter": space_before_s_delim, | ||
"space-before-closing-brace": space_before_c_brace, | ||
"strip-spaces": strip_spaces, | ||
"vendor-prefix-align": vendor_prefix_align | ||
} | ||
|
||
return json.dumps(options) | ||
else: | ||
return None |
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,125 @@ | ||
from bears.css.CSSCombBear import CSSCombBear | ||
from tests.LocalBearTestHelper import verify_local_bear | ||
import os | ||
|
||
test_file1 = """ | ||
example { | ||
color: red; | ||
} | ||
""" | ||
|
||
test_file2 = """ | ||
example{color: red} | ||
""" | ||
|
||
test_file3 = """ | ||
example { | ||
color: red; | ||
margin-bottom: 100px; | ||
margin-right: 150px; | ||
margin-left: 80px; | ||
background-color: lightblue} | ||
""" | ||
|
||
test_file4 = """ | ||
example{ | ||
color: red; | ||
margin-bottom: 100px; | ||
margin-right: 150px; | ||
margin-left: 80px} | ||
""" | ||
|
||
test_file5 = """ | ||
example{ | ||
color: red; | ||
margin-bottom: 100px; | ||
margin-right: 150px; | ||
} | ||
""" | ||
|
||
test_file_always_semicolon = """ | ||
example{ | ||
color:red | ||
} | ||
""" | ||
|
||
test_file_space_after = """ | ||
example { | ||
margin-botttom: 100px; | ||
} | ||
""" | ||
|
||
csscombconfig = os.path.join(os.path.dirname(__file__), | ||
"test_files", | ||
"csscomb.json") | ||
|
||
settings = { | ||
"always-semicolon": "True", | ||
"color-case": "upper", | ||
"eof-newline": "True", | ||
"leading-zero": "False", | ||
"sort-order-fallback": "abc", | ||
"space-before-colon": " ", | ||
"space-after-colon": " ", | ||
"strip-spaces": "True"} | ||
|
||
CSSCombBearTest = verify_local_bear(CSSCombBear, | ||
valid_files=(test_file1,), | ||
invalid_files=(test_file2, | ||
test_file3, test_file4)) | ||
|
||
|
||
CSSCombBearConfigFileTest = verify_local_bear( | ||
CSSCombBear, | ||
valid_files=(test_file5,), | ||
invalid_files=(test_file2, test_file3), | ||
settings={"csscomb_json": csscombconfig}) | ||
|
||
|
||
CSSCombBearConfigFileTest2 = verify_local_bear( | ||
CSSCombBear, | ||
valid_files=(test_file1,), | ||
invalid_files=(test_file2, test_file3, test_file4), | ||
settings={"csscomb_json": csscombconfig}) | ||
|
||
|
||
CSSCombBearConfigFileTest2 = verify_local_bear( | ||
CSSCombBear, | ||
valid_files=(test_file1,), | ||
invalid_files=(test_file2, test_file3, test_file4), | ||
settings={"csscomb_json": csscombconfig}) | ||
|
||
|
||
CSSCombBearCoafileTest = verify_local_bear( | ||
CSSCombBear, | ||
invalid_files=(), | ||
valid_files=(test_file5,), | ||
settings=settings) | ||
|
||
|
||
CSSCombBearCoafileTest2 = verify_local_bear( | ||
CSSCombBear, | ||
invalid_files=(test_file2, test_file3, test_file4), | ||
valid_files=(test_file1,), | ||
settings=settings) | ||
|
||
|
||
CSSCombBearSemicolonTest = verify_local_bear( | ||
CSSCombBear, | ||
invalid_files = (test_file_always_semicolon,), | ||
valid_files = (test_file1,), | ||
settings = {"always-semicolon": "True"}) | ||
|
||
|
||
CSSCombBearSpaceBefore = verify_local_bear( | ||
CSSCombBear, | ||
invalid_files = (test_file4, test_file2), | ||
valid_files = (test_file1,), | ||
settings = {"space-before-colon": " "}) | ||
|
||
|
||
CSSCombBearSpaceAfter = verify_local_bear( | ||
CSSCombBear, | ||
invalid_files = (test_file2, test_file4), | ||
valid_files = (test_file_space_after), | ||
settings = {"space-after-colon": " "}) |