|
21 | 21 | ``[yes, FALSE, Off]`` into ``[true, false, false]`` or
|
22 | 22 | ``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1, true: 5}``.
|
23 | 23 |
|
| 24 | +Depending on the YAML specification version used by the YAML document, the list |
| 25 | +of truthy values can differ. In YAML 1.2, only capitalized / uppercased |
| 26 | +combinations of ``true`` and ``false`` are considered truthy, whereas in YAML |
| 27 | +1.1 combinations of ``yes``, ``no``, ``on`` and ``off`` are too. To make the |
| 28 | +YAML specification version explicit in a YAML document, a ``%YAML 1.2`` |
| 29 | +directive can be used (see example below). |
| 30 | +
|
24 | 31 | .. rubric:: Options
|
25 | 32 |
|
26 | 33 | * ``allowed-values`` defines the list of truthy values which will be ignored
|
|
80 | 87 | the following code snippet would **FAIL**:
|
81 | 88 | ::
|
82 | 89 |
|
| 90 | + %YAML 1.1 |
| 91 | + --- |
83 | 92 | yes: 1
|
84 | 93 | on: 2
|
85 | 94 | True: 3
|
86 | 95 |
|
| 96 | + the following code snippet would **PASS**: |
| 97 | + :: |
| 98 | +
|
| 99 | + %YAML 1.2 |
| 100 | + --- |
| 101 | + yes: 1 |
| 102 | + on: 2 |
| 103 | + true: 3 |
| 104 | +
|
87 | 105 | #. With ``truthy: {allowed-values: ["yes", "no"]}``
|
88 | 106 |
|
89 | 107 | the following code snippet would **PASS**:
|
|
125 | 143 |
|
126 | 144 | from yamllint.linter import LintProblem
|
127 | 145 |
|
128 |
| -TRUTHY = ['YES', 'Yes', 'yes', |
129 |
| - 'NO', 'No', 'no', |
130 |
| - 'TRUE', 'True', 'true', |
131 |
| - 'FALSE', 'False', 'false', |
132 |
| - 'ON', 'On', 'on', |
133 |
| - 'OFF', 'Off', 'off'] |
| 146 | +TRUTHY_1_1 = ['YES', 'Yes', 'yes', |
| 147 | + 'NO', 'No', 'no', |
| 148 | + 'TRUE', 'True', 'true', |
| 149 | + 'FALSE', 'False', 'false', |
| 150 | + 'ON', 'On', 'on', |
| 151 | + 'OFF', 'Off', 'off'] |
| 152 | +TRUTHY_1_2 = ['TRUE', 'True', 'true', |
| 153 | + 'FALSE', 'False', 'false'] |
134 | 154 |
|
135 | 155 |
|
136 | 156 | ID = 'truthy'
|
137 | 157 | TYPE = 'token'
|
138 |
| -CONF = {'allowed-values': TRUTHY.copy(), 'check-keys': bool} |
| 158 | +CONF = {'allowed-values': TRUTHY_1_1.copy(), 'check-keys': bool} |
139 | 159 | DEFAULT = {'allowed-values': ['true', 'false'], 'check-keys': True}
|
140 | 160 |
|
141 | 161 |
|
| 162 | +def yaml_spec_version_for_document(context): |
| 163 | + if 'yaml_spec_version' in context: |
| 164 | + return context['yaml_spec_version'] |
| 165 | + return (1, 1) |
| 166 | + |
| 167 | + |
142 | 168 | def check(conf, token, prev, next, nextnext, context):
|
| 169 | + if isinstance(token, yaml.tokens.DirectiveToken) and token.name == 'YAML': |
| 170 | + context['yaml_spec_version'] = token.value |
| 171 | + elif isinstance(token, yaml.tokens.DocumentEndToken): |
| 172 | + context.pop('yaml_spec_version', None) |
| 173 | + context.pop('bad_truthy_values', None) |
| 174 | + |
143 | 175 | if prev and isinstance(prev, yaml.tokens.TagToken):
|
144 | 176 | return
|
145 | 177 |
|
146 | 178 | if (not conf['check-keys'] and isinstance(prev, yaml.tokens.KeyToken) and
|
147 | 179 | isinstance(token, yaml.tokens.ScalarToken)):
|
148 | 180 | return
|
149 | 181 |
|
150 |
| - if isinstance(token, yaml.tokens.ScalarToken): |
151 |
| - if (token.value in (set(TRUTHY) - set(conf['allowed-values'])) and |
152 |
| - token.style is None): |
| 182 | + if isinstance(token, yaml.tokens.ScalarToken) and token.style is None: |
| 183 | + if 'bad_truthy_values' not in context: |
| 184 | + context['bad_truthy_values'] = set( |
| 185 | + TRUTHY_1_2 if yaml_spec_version_for_document(context) == (1, 2) |
| 186 | + else TRUTHY_1_1) |
| 187 | + context['bad_truthy_values'] -= set(conf['allowed-values']) |
| 188 | + |
| 189 | + if token.value in context['bad_truthy_values']: |
153 | 190 | yield LintProblem(token.start_mark.line + 1,
|
154 | 191 | token.start_mark.column + 1,
|
155 | 192 | "truthy value should be one of [" +
|
|
0 commit comments