Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / tools / lint / file-whitespace / __init__.py
blobab7a6944b71062df1cf1212ce2496d22b095dd52
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
8 results = []
11 def lint(paths, config, fix=None, **lintargs):
12 files = list(expand_exclusions(paths, config, lintargs["root"]))
13 log = lintargs["log"]
14 fixed = 0
16 for f in files:
17 with open(f, "rb") as open_file:
18 hasFix = False
19 content_to_write = []
21 try:
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
26 open_file.seek(0)
27 if fix:
28 fixed += 1
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]:
39 write_file.write(e)
40 # end the loop
41 break
42 else:
43 res = {
44 "path": f,
45 "message": "Empty Lines at end of file",
46 "level": "error",
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
54 open_file.seek(0)
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"):
59 if fix:
60 fixed += 1
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
65 for e in lines:
66 write_file.write(e)
67 else:
68 res = {
69 "path": f,
70 "message": "File does not end with newline character",
71 "level": "error",
72 "lineno": lines.__len__(),
74 results.append(result.from_config(config, **res))
76 # return file pointer to first
77 open_file.seek(0)
79 for i, line in enumerate(open_file):
80 if line.endswith(b" \n"):
81 # We found a trailing whitespace
82 if fix:
83 # We want to fix it, strip the trailing spaces
84 content_to_write.append(line.rstrip() + b"\n")
85 fixed += 1
86 hasFix = True
87 else:
88 res = {
89 "path": f,
90 "message": "Trailing whitespace",
91 "level": "error",
92 "lineno": i + 1,
94 results.append(result.from_config(config, **res))
95 else:
96 if fix:
97 content_to_write.append(line)
98 if hasFix:
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
104 # line
105 open_file.seek(0)
106 # Open it as once as we just need to know if there is
107 # at least one \r\n
108 content = open_file.read()
110 if b"\r\n" in content:
111 if fix:
112 fixed += 1
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)
116 else:
117 res = {
118 "path": f,
119 "message": "Windows line return",
120 "level": "error",
122 results.append(result.from_config(config, **res))
124 return {"results": results, "fixed": fixed}