Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 substitute = DISCOURAGED_APIS.get(name) | 277 substitute = DISCOURAGED_APIS.get(name) |
278 if substitute: | 278 if substitute: |
279 self.errors.append((node, 'A301 use {}() instead of ' | 279 self.errors.append((node, 'A301 use {}() instead of ' |
280 '{}()'.format(substitute, name))) | 280 '{}()'.format(substitute, name))) |
281 | 281 |
282 def visit_Call(self, node): | 282 def visit_Call(self, node): |
283 func = get_identifier(node.func) | 283 func = get_identifier(node.func) |
284 arg = next(iter(node.args), None) | 284 arg = next(iter(node.args), None) |
285 redundant_literal = False | 285 redundant_literal = False |
286 | 286 |
287 if isinstance(arg, ast.Lambda) and func in {'map', 'filter', | 287 if isinstance(arg, ast.Lambda): |
288 'imap', 'ifilter', | 288 if len(node.args) == 2 and func in {'map', 'filter', |
289 'itertools.imap', | 289 'imap', 'ifilter', |
290 'itertools.ifilter'}: | 290 'itertools.imap', |
291 self.errors.append((node, 'A104 use a comprehension ' | 291 'itertools.ifilter'}: |
292 'instead of calling {}() with ' | 292 self.errors.append((node, 'A104 use a comprehension ' |
293 'lambda function'.format(func))) | 293 'instead of calling {}() with ' |
294 'lambda function'.format(func))) | |
294 elif isinstance(arg, (ast.List, ast.Tuple)): | 295 elif isinstance(arg, (ast.List, ast.Tuple)): |
295 if func == 'dict': | 296 if func == 'dict': |
296 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List)) | 297 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List)) |
297 for elt in arg.elts) | 298 for elt in arg.elts) |
298 else: | 299 else: |
299 redundant_literal = func in {'list', 'set', 'tuple'} | 300 redundant_literal = func in {'list', 'set', 'tuple'} |
300 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)): | 301 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)): |
301 if func == 'dict': | 302 if func == 'dict': |
302 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List)) | 303 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List)) |
303 else: | 304 else: |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 | 369 |
369 | 370 |
370 def check_non_default_encoding(physical_line, line_number): | 371 def check_non_default_encoding(physical_line, line_number): |
371 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line): | 372 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line): |
372 return (0, 'A303 non-default file encoding') | 373 return (0, 'A303 non-default file encoding') |
373 | 374 |
374 check_non_default_encoding.name = 'abp-non-default-encoding' | 375 check_non_default_encoding.name = 'abp-non-default-encoding' |
375 check_non_default_encoding.version = __version__ | 376 check_non_default_encoding.version = __version__ |
376 | 377 |
377 | 378 |
378 def check_quotes(logical_line, tokens, previous_logical): | 379 def check_quotes(logical_line, tokens, previous_logical, checker_state): |
379 first_token = True | 380 first_token = True |
380 is_unicode_literals = False | 381 |
Sebastian Noack
2016/05/20 00:04:34
While I didnt test this change yet I have a hard t
Vasily Kuznetsov
2016/05/20 08:46:24
Funnily enough, the test does pass in the first ve
| |
382 token_strings = [t[1] for t in tokens] | |
383 future_import = token_strings[:3] == ['from', '__future__', 'import'] | |
384 | |
385 if future_import and 'unicode_literals' in token_strings: | |
386 checker_state['has_unicode_literals'] = True | |
381 | 387 |
382 for kind, token, start, end, _ in tokens: | 388 for kind, token, start, end, _ in tokens: |
383 if kind == tokenize.INDENT or kind == tokenize.DEDENT: | 389 if kind == tokenize.INDENT or kind == tokenize.DEDENT: |
384 continue | 390 continue |
385 | 391 |
386 if token is 'unicode_literals': | |
Sebastian Noack
2016/05/20 00:04:34
It's not as simple as that. Following examples wil
Jon Sonesen
2016/05/20 02:00:01
Yes I should be checking the logical line object w
| |
387 is_unicode_literals = True | |
388 | |
389 if kind == tokenize.STRING: | 392 if kind == tokenize.STRING: |
390 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', | 393 match = re.search(r'^([rub]*)([\'"]{1,3})(.*)\2$', |
391 token, re.IGNORECASE | re.DOTALL) | 394 token, re.IGNORECASE | re.DOTALL) |
392 (is_unicode, is_bytes, is_raw, | 395 prefixes, quote, text = match.groups() |
393 literal, has_doc_quotes) = match.groups() | 396 prefixes = prefixes.lower() |
397 | |
398 if 'u' in prefixes: | |
399 yield (start, 'A112 use "from __future__ import ' | |
400 'unicode_literals" instead of ' | |
401 'prefixing literals with "u"') | |
394 | 402 |
395 if first_token and re.search(r'^(?:(?:def|class)\s|$)', | 403 if first_token and re.search(r'^(?:(?:def|class)\s|$)', |
396 previous_logical): | 404 previous_logical): |
397 if not has_doc_quotes: | 405 if quote != '"""': |
398 yield (start, 'A109 use triple double ' | 406 yield (start, 'A109 use triple double ' |
399 'quotes for docstrings') | 407 'quotes for docstrings') |
400 elif is_unicode or is_bytes or is_raw: | 408 elif start[0] != end[0]: |
401 yield (start, "A109 don't use u'', b'' " | 409 pass |
402 "or r'' for doc strings") | 410 elif 'r' in prefixes: |
403 elif start[0] == end[0]: | 411 if quote != "'" and not (quote == '"' and "'" in text): |
404 if is_raw: | 412 yield (start, 'A110 use single quotes for raw string') |
405 literal = re.sub(r'\\(?!{})'.format(literal[0]), | 413 else: |
406 '\\\\\\\\', literal) | 414 prefix = '' |
407 | |
408 if sys.version_info[0] >= 3: | 415 if sys.version_info[0] >= 3: |
409 if is_bytes: | 416 if 'b' in prefixes: |
410 literal = 'b' + literal | 417 prefix = 'b' |
411 elif is_unicode: | 418 else: |
412 literal = 'u' + literal | 419 u_literals = checker_state.get('has_unicode_literals') |
413 elif not is_unicode_literals: | 420 if 'u' in prefixes or u_literals and 'b' not in prefixes: |
Sebastian Noack
2016/05/20 00:04:34
Just add it to the condition above using "or" rath
Jon Sonesen
2016/05/20 02:00:01
Will do.
| |
414 literal = 'u' + literal | 421 prefix = 'u' |
415 | 422 |
423 literal = '{0}{1}{2}{1}'.format(prefix, quote, text) | |
416 if ascii(eval(literal)) != literal: | 424 if ascii(eval(literal)) != literal: |
417 yield (start, "A110 string literal doesn't match " | 425 yield (start, "A110 string literal doesn't match " |
418 '{}()'.format(ascii.__name__)) | 426 '{}()'.format(ascii.__name__)) |
419 | 427 |
420 first_token = False | 428 first_token = False |
421 | 429 |
422 check_quotes.name = 'abp-quotes' | 430 check_quotes.name = 'abp-quotes' |
423 check_quotes.version = __version__ | 431 check_quotes.version = __version__ |
424 | 432 |
425 | 433 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 if tokens[i + 1][:2] != (tokenize.OP, ':'): | 476 if tokens[i + 1][:2] != (tokenize.OP, ':'): |
469 break | 477 break |
470 | 478 |
471 return [(pos, 'A111 redundant parenthesis for {} ' | 479 return [(pos, 'A111 redundant parenthesis for {} ' |
472 'statement'.format(statement))] | 480 'statement'.format(statement))] |
473 | 481 |
474 return [] | 482 return [] |
475 | 483 |
476 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' | 484 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' |
477 check_redundant_parenthesis.version = __version__ | 485 check_redundant_parenthesis.version = __version__ |
LEFT | RIGHT |