summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--checkers/format.py30
-rw-r--r--test/functional/line_endings.args2
-rw-r--r--test/functional/line_endings.py4
-rw-r--r--test/functional/line_endings.txt2
5 files changed, 42 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index d4f7c86..26cb291 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,10 @@ ChangeLog for Pylint
--
+ * Added new checks for line endings if they are mixed (LF vs CRLF)
+ or if they are not as expected. New messages: mixed-line-endings,
+ unexpected-line-ending-format. New option: expected-line-ending-format.
+
* Allow hanging continued indentation for implicitly concatenated
strings. Closes issue #232.
@@ -19,7 +23,7 @@ ChangeLog for Pylint
Python 3 `metaclass=` argument.
* Checkers respect priority now. Close issue #229.
-
+
* Fix a false positive regarding W0511. Closes issue #149.
* Fix unused-import false positive with Python 3 metaclasses (#143).
@@ -79,9 +83,6 @@ ChangeLog for Pylint
* Don't emit 'unnecessary-lambda' if the body of the lambda call contains
call chaining. Closes issue #243.
- * Don't emit 'missing-docstring' when the actual docstring uses `.format`.
- Closes issue #281.
-
2014-04-30 -- 1.2.1
* Restore the ability to specify the init-hook option via the
diff --git a/checkers/format.py b/checkers/format.py
index cb31256..2c0e215 100644
--- a/checkers/format.py
+++ b/checkers/format.py
@@ -121,6 +121,12 @@ MSGS = {
'Used when the deprecated "``" (backtick) operator is used '
'instead of the str() function.',
{'scope': WarningScope.NODE, 'maxversion': (3, 0)}),
+ 'C0327': ('Mixed line endings LF and CRLF',
+ 'mixed-line-endings',
+ 'Used when there are mixed (LF and CRLF) newline signs in a file.'),
+ 'C0328': ('Unexpected line ending format. There is \'%s\' while it should be \'%s\'.',
+ 'unexpected-line-ending-format',
+ 'Used when there is different newline than expected.'),
}
@@ -442,13 +448,18 @@ class FormatChecker(BaseTokenChecker):
{'type': 'int', 'metavar': '<int>', 'default': 4,
'help': 'Number of spaces of indent required inside a hanging '
' or continued line.'}),
- )
+ ('expected-line-ending-format',
+ {'type': 'choice', 'metavar': '<empty or LF or CRLF>', 'default': '',
+ 'choices': ['', 'LF', 'CRLF'],
+ 'help': 'Expected format of line ending, e.g. empty (any line ending), LF or CRLF.'}),
+ )
def __init__(self, linter=None):
BaseTokenChecker.__init__(self, linter)
self._lines = None
self._visited_lines = None
self._bracket_stack = [None]
+ self._last_line_ending = None
def _pop_token(self):
self._bracket_stack.pop()
@@ -737,6 +748,7 @@ class FormatChecker(BaseTokenChecker):
check_equal = True
self._process_retained_warnings(TokenWrapper(tokens), idx)
self._current_line.next_logical_line()
+ self._check_line_ending(token, line_num)
elif tok_type == tokenize.INDENT:
check_equal = False
self.check_indent_level(token, indents[-1]+1, line_num)
@@ -778,6 +790,22 @@ class FormatChecker(BaseTokenChecker):
if line_num > self.config.max_module_lines:
self.add_message('too-many-lines', args=line_num, line=1)
+ def _check_line_ending(self, line_ending, line_num):
+ # check if line endings are mixed
+ if self._last_line_ending is not None:
+ if line_ending != self._last_line_ending:
+ self.add_message('mixed-line-endings', line=line_num)
+
+ self._last_line_ending = line_ending
+
+ # check if line ending is as expected
+ expected = self.config.expected_line_ending_format
+ if expected:
+ line_ending = 'LF' if line_ending == '\n' else 'CRLF'
+ if line_ending != expected:
+ self.add_message('unexpected-line-ending-format', args=(line_ending, expected), line=line_num)
+
+
def _process_retained_warnings(self, tokens, current_pos):
single_line_block_stmt = not _last_token_on_line_is(tokens, current_pos, ':')
diff --git a/test/functional/line_endings.args b/test/functional/line_endings.args
new file mode 100644
index 0000000..95532ea
--- /dev/null
+++ b/test/functional/line_endings.args
@@ -0,0 +1,2 @@
+[Format]
+expected-line-ending-format=LF
diff --git a/test/functional/line_endings.py b/test/functional/line_endings.py
new file mode 100644
index 0000000..d6bc3fc
--- /dev/null
+++ b/test/functional/line_endings.py
@@ -0,0 +1,4 @@
+"mixing line endings are not welcome"
+# +1: [unexpected-line-ending-format, mixed-line-endings]
+CONST = 1
+
diff --git a/test/functional/line_endings.txt b/test/functional/line_endings.txt
new file mode 100644
index 0000000..a18a872
--- /dev/null
+++ b/test/functional/line_endings.txt
@@ -0,0 +1,2 @@
+mixed-line-endings:3::Mixed line endings LF and CRLF
+unexpected-line-ending-format:3::Unexpected line ending format. There is 'CRLF' while it should be 'LF'.