1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from mozlint
import result
6 from mozlint
.pathutils
import expand_exclusions
11 def lint(paths
, config
, fix
=None, **lintargs
):
12 files
= list(expand_exclusions(paths
, config
, lintargs
["root"]))
17 with
open(f
, "rb") as open_file
:
22 lines
= open_file
.readlines()
23 # Check for Empty spaces or newline character at end of file
24 if lines
[:].__len
__() != 0 and lines
[-1:][0].strip().__len
__() == 0:
25 # return file pointer to first
29 # fix Empty lines at end of file
30 for i
, line
in reversed(list(enumerate(open_file
))):
31 # determine if line is empty
32 if line
.strip() != b
"":
33 with
open(f
, "wb") as write_file
:
34 # determine if file's last line have \n, if not then add a \n
35 if not lines
[i
].endswith(b
"\n"):
36 lines
[i
] = lines
[i
] + b
"\n"
37 # write content to file
38 for e
in lines
[: i
+ 1]:
45 "message": "Empty Lines at end of file",
47 "lineno": open_file
.readlines()[:].__len
__(),
49 results
.append(result
.from_config(config
, **res
))
50 except Exception as ex
:
51 log
.debug("Error: " + str(ex
) + ", in file: " + f
)
53 # return file pointer to first
56 lines
= open_file
.readlines()
57 # Detect missing newline at the end of the file
58 if lines
[:].__len
__() != 0 and not lines
[-1].endswith(b
"\n"):
61 with
open(f
, "wb") as write_file
:
62 # add a newline character at end of file
63 lines
[-1] = lines
[-1] + b
"\n"
64 # write content to file
70 "message": "File does not end with newline character",
72 "lineno": lines
.__len
__(),
74 results
.append(result
.from_config(config
, **res
))
76 # return file pointer to first
79 for i
, line
in enumerate(open_file
):
80 if line
.endswith(b
" \n"):
81 # We found a trailing whitespace
83 # We want to fix it, strip the trailing spaces
84 content_to_write
.append(line
.rstrip() + b
"\n")
90 "message": "Trailing whitespace",
94 results
.append(result
.from_config(config
, **res
))
97 content_to_write
.append(line
)
99 # Only update the file when we found a change to make
100 with
open(f
, "wb") as open_file_to_write
:
101 open_file_to_write
.write(b
"".join(content_to_write
))
103 # We are still using the same fp, let's return to the first
106 # Open it as once as we just need to know if there is
108 content
= open_file
.read()
110 if b
"\r\n" in content
:
113 content
= content
.replace(b
"\r\n", b
"\n")
114 with
open(f
, "wb") as open_file_to_write
:
115 open_file_to_write
.write(content
)
119 "message": "Windows line return",
122 results
.append(result
.from_config(config
, **res
))
124 return {"results": results
, "fixed": fixed
}