|
| 1 | +import json |
| 2 | + |
| 3 | +from coalib.bearlib.abstractions.Linter import linter |
| 4 | +from coalib.settings.Setting import typed_list |
| 5 | + |
| 6 | +from dependency_management.requirements.NpmRequirement import NpmRequirement |
| 7 | + |
| 8 | + |
| 9 | +@linter(executable='pug-lint', |
| 10 | + output_format='regex', |
| 11 | + output_regex=r'(?P<line>\d+):?(?P<column>\d+)? (?P<message>.+)', |
| 12 | + use_stdout=False, |
| 13 | + use_stderr=True) |
| 14 | +class PugLintBear: |
| 15 | + """ |
| 16 | + A configurable linter and style checker for ``Pug`` (formerly ``Jade``) |
| 17 | + that is a clean, whitespace-sensitive template language for writing HTML. |
| 18 | + """ |
| 19 | + |
| 20 | + LANGUAGES = {'Pug'} |
| 21 | + REQUIREMENTS = {NpmRequirement('pug-lint', '2.4.0')} |
| 22 | + AUTHORS = {'The coala developers'} |
| 23 | + AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} |
| 24 | + LICENSE = 'AGPL-3.0' |
| 25 | + CAN_DETECT = {'Formatting', 'Syntax', 'Redundancy'} |
| 26 | + SEE_MORE = 'https://github.com/pugjs/pug-lint' |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def generate_config(filename, file, |
| 30 | + prohibit_block_expansion: bool=True, |
| 31 | + prohibit_class_attribute_with_static_value: bool=True, |
| 32 | + prohibit_class_literals_before_attributes: bool=True, |
| 33 | + prohibit_class_literals_before_id_literals: bool=True, |
| 34 | + prohibit_class_literals: bool=True, |
| 35 | + prohibit_duplicate_attributes: bool=True, |
| 36 | + prohibit_html_text: bool=True, |
| 37 | + prohibit_id_attribute_with_static_value: bool=True, |
| 38 | + prohibit_id_literals_before_attributes: bool=True, |
| 39 | + prohibit_id_literals: bool=True, |
| 40 | + prohibit_legacy_mixin_call: bool=True, |
| 41 | + prohibit_multiple_line_breaks: bool=False, |
| 42 | + prohibit_spaces_inside_attribute_brackets: bool=True, |
| 43 | + prohibit_string_interpolation: bool=True, |
| 44 | + prohibit_tag_interpolation: bool=True, |
| 45 | + prohibit_specific_attributes: typed_list(str)=None, |
| 46 | + prohibit_specific_tags: typed_list(str)=None, |
| 47 | + enforce_class_literals_before_attributes: bool=False, |
| 48 | + enforce_class_literals_before_id_literals: bool=False, |
| 49 | + enforce_id_literals_before_attributes: bool=False, |
| 50 | + enforce_lower_case_attributes: bool=True, |
| 51 | + enforce_lower_case_tags: bool=True, |
| 52 | + enforce_spaces_inside_attribute_brackets: bool=False, |
| 53 | + enforce_strict_equality_operators: bool=True, |
| 54 | + validate_div_tags: bool=True, |
| 55 | + validate_extensions: bool=True, |
| 56 | + validate_self_closing_tags: bool=True, |
| 57 | + preferred_quotation: str="'", |
| 58 | + max_lines_per_file: int=None, |
| 59 | + puglint_config: str=''): |
| 60 | + """ |
| 61 | + :param prohibit_block_expansion: |
| 62 | + When ``True``, disallow any block expansion operators. |
| 63 | + For example: If set to ``True``, this will throw a warning:: |
| 64 | +
|
| 65 | + p: strong text |
| 66 | + table: tr: td text |
| 67 | +
|
| 68 | + :param prohibit_class_attribute_with_static_value: |
| 69 | + When ``True``, prefer class literals over class attributes with |
| 70 | + static values. |
| 71 | + For example: If set to ``True``, prefer ``span.foo`` over |
| 72 | + ``span(class='foo')``. |
| 73 | + :param prohibit_class_literals_before_attributes: |
| 74 | + When ``True``, prefer all attribute blocks to be written before |
| 75 | + any class literals. |
| 76 | + For example: If set to ``True``, prefer |
| 77 | + ``input(type='text').class`` over ``input.class(type='text')``. |
| 78 | + :param prohibit_class_literals_before_id_literals: |
| 79 | + When ``True``, prefer all ID literals to be written before any |
| 80 | + class literals. |
| 81 | + For example: If set to ``True``, prefer |
| 82 | + ``input#id.class(type='text')`` over |
| 83 | + ``input.class#id(type='text')``. |
| 84 | + :param prohibit_class_literals: |
| 85 | + When ``True``, disallow any class literals. |
| 86 | + For example: If set to ``True``, prefer ``div(class='class')`` |
| 87 | + over ``.class``. |
| 88 | + :param prohibit_duplicate_attributes: |
| 89 | + When ``True``, attribute blocks should not contain any duplicates. |
| 90 | + For example: If set to ``True``, this will throw a warning:: |
| 91 | +
|
| 92 | + div(a='a' a='b') |
| 93 | + #id(id='id') |
| 94 | +
|
| 95 | + :param prohibit_html_text: |
| 96 | + When ``True``, disallow any HTML text. |
| 97 | + For example: If set to ``True``, this will throw a warning:: |
| 98 | +
|
| 99 | + <strong>html text</strong> |
| 100 | + p this is <strong>html</strong> text |
| 101 | +
|
| 102 | + :param prohibit_id_attribute_with_static_value: |
| 103 | + When ``True``, prefer ID literals over ``id`` attributes with |
| 104 | + static values. |
| 105 | + For example: If set to ``True``, prefer ``span#id`` over |
| 106 | + ``span(id='foo')``. |
| 107 | + :param prohibit_id_literals_before_attributes: |
| 108 | + When ``True``, prefer all attribute blocks to be written before |
| 109 | + any ID literals. |
| 110 | + For example: If set to ``True``, prefer ``input(type='text')#id`` |
| 111 | + over ``input#id(type='text')``. |
| 112 | + :param prohibit_id_literals: |
| 113 | + When ``True``, disallow any ID literals. |
| 114 | + For example: If set to ``True``, ``#id`` will throw a warning. |
| 115 | + :param prohibit_legacy_mixin_call: |
| 116 | + When ``True``, disallow any legacy mixin call. |
| 117 | + When ``True``, prefer ``+myMixin(arg)`` over |
| 118 | + ``mixin myMixin(arg)``. |
| 119 | + :param prohibit_multiple_line_breaks: |
| 120 | + When ``True``, disallow multiple blank lines in a row. |
| 121 | + :param prohibit_spaces_inside_attribute_brackets: |
| 122 | + When ``True``, disallow space after opening attribute bracket and |
| 123 | + before closing attribute bracket. |
| 124 | + For example: If set to ``True``, prefer |
| 125 | + ``input(type='text' name='name' value='value')`` over |
| 126 | + ``input( type='text' name='name' value='value' )``. |
| 127 | + :param prohibit_string_interpolation: |
| 128 | + When ``True``, disallow any string interpolation operators. |
| 129 | + For example: If set to ``True``, ``h1 #{title} text`` will throw |
| 130 | + a warning. |
| 131 | + :param prohibit_tag_interpolation: |
| 132 | + When ``True``, disallow any tag interpolation operators. |
| 133 | + For example: If set to ``True``, this will throw a warning:: |
| 134 | +
|
| 135 | + | #[strong html] text |
| 136 | + p #[strong html] text |
| 137 | +
|
| 138 | + :param prohibit_specific_attributes: |
| 139 | + Disallow any of the attributes specified. |
| 140 | + :param prohibit_specific_tags: |
| 141 | + Disallow any of the tags specified. |
| 142 | + :param enforce_class_literals_before_attributes: |
| 143 | + When ``True``, all class literals must be written before any |
| 144 | + attribute blocks. |
| 145 | + :param enforce_class_literals_before_id_literals: |
| 146 | + When ``True``, all class literals should be written before any |
| 147 | + ID literals. |
| 148 | + :param enforce_id_literals_before_attributes: |
| 149 | + When ``True``, all ID literals must be written before any |
| 150 | + attribute blocks. |
| 151 | + :param enforce_lower_case_attributes: |
| 152 | + When ``True``, all attributes should be written in lower case. |
| 153 | + For example: If set to ``True``, prefer ``div(class='class')`` |
| 154 | + over ``div(Class='class')``. |
| 155 | + :param enforce_lower_case_tags: |
| 156 | + When ``True``, all tags must be written in lower case. |
| 157 | + For example: If set to ``True``, prefer ``div(class='class')`` |
| 158 | + over ``Div(class='class')``. |
| 159 | + :param enforce_spaces_inside_attribute_brackets: |
| 160 | + When ``True``, enforce space after opening attribute bracket and |
| 161 | + before closing attribute bracket. |
| 162 | + :param enforce_strict_equality_operators: |
| 163 | + When ``True``, enforce the use of ``===`` and ``!==`` instead of |
| 164 | + ``==`` and ``!=``. |
| 165 | + :param validate_div_tags: |
| 166 | + When ``True``, disallow any unnecessary ``div`` tags. |
| 167 | + :param validate_extensions: |
| 168 | + When ``True``, enforce proper file extensions with inclusion and |
| 169 | + inheritance. |
| 170 | + :param validate_self_closing_tags: |
| 171 | + When ``True``, disallow any unnecessary self closing tags. |
| 172 | + :param preferred_quotation: |
| 173 | + Your preferred quotation character, e.g.``"`` or ``'``. |
| 174 | + :param max_lines_per_file: |
| 175 | + Number of lines allowed per file. |
| 176 | + """ |
| 177 | + if puglint_config: |
| 178 | + return None |
| 179 | + else: |
| 180 | + options = { |
| 181 | + 'disallowBlockExpansion': prohibit_block_expansion, |
| 182 | + 'disallowClassAttributeWithStaticValue': |
| 183 | + prohibit_class_attribute_with_static_value, |
| 184 | + 'disallowClassLiteralsBeforeAttributes': |
| 185 | + prohibit_class_literals_before_attributes, |
| 186 | + 'disallowClassLiteralsBeforeIdLiterals': |
| 187 | + prohibit_class_literals_before_id_literals, |
| 188 | + 'disallowClassLiterals': prohibit_class_literals, |
| 189 | + 'disallowDuplicateAttributes': prohibit_duplicate_attributes, |
| 190 | + 'disallowHtmlText': prohibit_html_text, |
| 191 | + 'disallowIdAttributeWithStaticValue': |
| 192 | + prohibit_id_attribute_with_static_value, |
| 193 | + 'disallowIdLiteralsBeforeAttributes': |
| 194 | + prohibit_id_literals_before_attributes, |
| 195 | + 'disallowIdLiterals': prohibit_id_literals, |
| 196 | + 'disallowLegacyMixinCall': prohibit_legacy_mixin_call, |
| 197 | + 'disallowMultipleLineBreaks': prohibit_multiple_line_breaks, |
| 198 | + 'disallowSpacesInsideAttributeBrackets': |
| 199 | + prohibit_spaces_inside_attribute_brackets, |
| 200 | + 'disallowStringInterpolation': prohibit_string_interpolation, |
| 201 | + 'disallowTagInterpolation': prohibit_tag_interpolation, |
| 202 | + 'disallowSpecificAttributes': prohibit_specific_attributes, |
| 203 | + 'disallowSpecificTags': prohibit_specific_tags, |
| 204 | + 'requireClassLiteralsBeforeAttributes': |
| 205 | + enforce_class_literals_before_attributes, |
| 206 | + 'requireClassLiteralsBeforeIdLiterals': |
| 207 | + enforce_class_literals_before_id_literals, |
| 208 | + 'requireIdLiteralsBeforeAttributes': |
| 209 | + enforce_id_literals_before_attributes, |
| 210 | + 'requireLowerCaseAttributes': enforce_lower_case_attributes, |
| 211 | + 'requireLowerCaseTags': enforce_lower_case_tags, |
| 212 | + 'requireSpacesInsideAttributeBrackets': |
| 213 | + enforce_spaces_inside_attribute_brackets, |
| 214 | + 'requireStrictEqualityOperators': |
| 215 | + enforce_strict_equality_operators, |
| 216 | + 'validateDivTags': validate_div_tags, |
| 217 | + 'validateExtensions': validate_extensions, |
| 218 | + 'validateSelfClosingTags': validate_self_closing_tags, |
| 219 | + 'validateAttributeQuoteMarks': preferred_quotation, |
| 220 | + 'maximumNumberOfLines': max_lines_per_file |
| 221 | + } |
| 222 | + |
| 223 | + for k, v in options.items(): |
| 224 | + options[k] = v if v else None |
| 225 | + |
| 226 | + return json.dumps(options) |
| 227 | + |
| 228 | + @staticmethod |
| 229 | + def create_arguments(filename, file, config_file, puglint_config: str=''): |
| 230 | + """ |
| 231 | + :param puglint_config: |
| 232 | + The location of a custom ``.pug-lintrc`` config file. |
| 233 | + """ |
| 234 | + return ('--config', |
| 235 | + puglint_config if puglint_config else config_file, |
| 236 | + '--reporter', 'inline', filename) |
0 commit comments