2019-08-28 00:42:18 +03:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
from mozlint import result
|
|
|
|
from mozlint.pathutils import expand_exclusions
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
|
|
|
|
def lint(paths, config, fix=None, **lintargs):
|
2020-07-09 17:09:31 +03:00
|
|
|
files = list(expand_exclusions(paths, config, lintargs["root"]))
|
2020-12-16 00:49:43 +03:00
|
|
|
log = lintargs["log"]
|
2021-02-08 22:00:10 +03:00
|
|
|
fixed = 0
|
2019-08-28 00:42:18 +03:00
|
|
|
|
|
|
|
for f in files:
|
2020-07-09 17:09:31 +03:00
|
|
|
with open(f, "rb") as open_file:
|
2019-08-28 00:42:18 +03:00
|
|
|
hasFix = False
|
|
|
|
content_to_write = []
|
2020-12-16 00:49:43 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
lines = open_file.readlines()
|
|
|
|
# Check for Empty spaces or newline character at end of file
|
|
|
|
if lines[:].__len__() != 0 and lines[-1:][0].strip().__len__() == 0:
|
|
|
|
# return file pointer to first
|
|
|
|
open_file.seek(0)
|
2020-12-16 00:50:06 +03:00
|
|
|
if fix:
|
2021-02-08 22:00:10 +03:00
|
|
|
fixed += 1
|
2020-12-16 00:50:06 +03:00
|
|
|
# fix Empty lines at end of file
|
|
|
|
for i, line in reversed(list(enumerate(open_file))):
|
|
|
|
# determine if line is empty
|
|
|
|
if line.strip() != b"":
|
|
|
|
with open(f, "wb") as write_file:
|
|
|
|
# determine if file's last line have \n, if not then add a \n
|
|
|
|
if not lines[i].endswith(b"\n"):
|
|
|
|
lines[i] = lines[i] + b"\n"
|
|
|
|
# write content to file
|
|
|
|
for e in lines[: i + 1]:
|
|
|
|
write_file.write(e)
|
|
|
|
# end the loop
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
res = {
|
|
|
|
"path": f,
|
|
|
|
"message": "Empty Lines at end of file",
|
|
|
|
"level": "error",
|
|
|
|
"lineno": open_file.readlines()[:].__len__(),
|
|
|
|
}
|
|
|
|
results.append(result.from_config(config, **res))
|
2020-12-16 00:49:43 +03:00
|
|
|
except Exception as ex:
|
|
|
|
log.debug("Error: " + str(ex) + ", in file: " + f)
|
|
|
|
|
|
|
|
# return file pointer to first
|
|
|
|
open_file.seek(0)
|
|
|
|
|
2021-01-07 11:53:16 +03:00
|
|
|
lines = open_file.readlines()
|
|
|
|
# Detect missing newline at the end of the file
|
|
|
|
if lines[:].__len__() != 0 and not lines[-1].endswith(b"\n"):
|
|
|
|
if fix:
|
2021-02-08 22:00:10 +03:00
|
|
|
fixed += 1
|
2021-01-07 11:53:16 +03:00
|
|
|
with open(f, "wb") as write_file:
|
|
|
|
# add a newline character at end of file
|
|
|
|
lines[-1] = lines[-1] + b"\n"
|
|
|
|
# write content to file
|
|
|
|
for e in lines:
|
|
|
|
write_file.write(e)
|
|
|
|
else:
|
|
|
|
res = {
|
|
|
|
"path": f,
|
|
|
|
"message": "File does not end with newline character",
|
|
|
|
"level": "error",
|
|
|
|
"lineno": lines.__len__(),
|
|
|
|
}
|
|
|
|
results.append(result.from_config(config, **res))
|
|
|
|
|
|
|
|
# return file pointer to first
|
|
|
|
open_file.seek(0)
|
|
|
|
|
2019-08-28 00:42:18 +03:00
|
|
|
for i, line in enumerate(open_file):
|
2019-09-19 00:11:02 +03:00
|
|
|
if line.endswith(b" \n"):
|
2019-08-28 00:42:18 +03:00
|
|
|
# We found a trailing whitespace
|
|
|
|
if fix:
|
|
|
|
# We want to fix it, strip the trailing spaces
|
2019-09-19 00:11:02 +03:00
|
|
|
content_to_write.append(line.rstrip() + b"\n")
|
2021-02-08 22:00:10 +03:00
|
|
|
fixed += 1
|
2019-08-28 00:42:18 +03:00
|
|
|
hasFix = True
|
|
|
|
else:
|
2020-07-09 17:09:31 +03:00
|
|
|
res = {
|
|
|
|
"path": f,
|
|
|
|
"message": "Trailing whitespace",
|
|
|
|
"level": "error",
|
|
|
|
"lineno": i + 1,
|
|
|
|
}
|
2019-08-28 00:42:18 +03:00
|
|
|
results.append(result.from_config(config, **res))
|
|
|
|
else:
|
|
|
|
if fix:
|
|
|
|
content_to_write.append(line)
|
|
|
|
if hasFix:
|
|
|
|
# Only update the file when we found a change to make
|
2020-07-09 17:09:31 +03:00
|
|
|
with open(f, "wb") as open_file_to_write:
|
2019-09-19 00:11:02 +03:00
|
|
|
open_file_to_write.write(b"".join(content_to_write))
|
2019-08-28 00:42:18 +03:00
|
|
|
|
|
|
|
# We are still using the same fp, let's return to the first
|
|
|
|
# line
|
|
|
|
open_file.seek(0)
|
|
|
|
# Open it as once as we just need to know if there is
|
|
|
|
# at least one \r\n
|
|
|
|
content = open_file.read()
|
|
|
|
|
2019-09-19 00:11:02 +03:00
|
|
|
if b"\r\n" in content:
|
2019-08-28 00:42:18 +03:00
|
|
|
if fix:
|
2021-02-08 22:00:10 +03:00
|
|
|
fixed += 1
|
2020-07-09 17:09:31 +03:00
|
|
|
content = content.replace(b"\r\n", b"\n")
|
|
|
|
with open(f, "wb") as open_file_to_write:
|
2019-08-28 00:42:18 +03:00
|
|
|
open_file_to_write.write(content)
|
|
|
|
else:
|
2020-07-09 17:09:31 +03:00
|
|
|
res = {
|
|
|
|
"path": f,
|
|
|
|
"message": "Windows line return",
|
|
|
|
"level": "error",
|
|
|
|
}
|
2019-08-28 00:42:18 +03:00
|
|
|
results.append(result.from_config(config, **res))
|
|
|
|
|
2021-02-08 22:00:10 +03:00
|
|
|
return {"results": results, "fixed": fixed}
|