Bug 1575250 - Add a lint to check for trailing white spaces and windows line return r=ahal

Differential Revision: https://phabricator.services.mozilla.com/D42675

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sylvestre Ledru 2019-08-27 21:42:18 +00:00
Родитель e41cf7ee42
Коммит 71f8303be7
2 изменённых файлов: 156 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,95 @@
---
file-whitespace:
description: File content sanity check
include:
- .
exclude:
- build/pymake/pymake/command.py
- build/pymake/pymake/data.py
- build/pymake/pymake/functions.py
- build/pymake/pymake/implicit.py
- build/pymake/pymake/parser.py
- build/pymake/pymake/process.py
- build/pymake/pymake/util.py
- build/pymake/tests/runtests.py
- dom/bindings/Codegen.py
- dom/bindings/Configuration.py
- dom/bindings/parser/WebIDL.py
- dom/bindings/parser/tests/test_attributes_on_types.py
- dom/bindings/parser/tests/test_extended_attributes.py
- dom/bindings/parser/tests/test_implements.py
- dom/bindings/parser/tests/test_interface.py
- dom/bindings/parser/tests/test_record.py
- dom/bindings/parser/tests/test_securecontext_extended_attribute.py
- dom/bindings/parser/tests/test_special_methods.py
- dom/bindings/parser/tests/test_toJSON.py
- dom/bindings/parser/tests/test_typedef.py
- dom/encoding/encodings2arrays.py
- dom/media/gtest/AudioGenerator.cpp
- dom/media/gtest/AudioGenerator.h
- dom/security/test/csp/file_websocket_self_wsh.py
- dom/webauthn/winwebauthn/webauthn.h
- dom/websocket/tests/file_websocket_wsh.py
- gfx/vr/nsFxrCommandLineHandler.cpp
- gfx/vr/vrhost/vrhostapi.cpp
- js/src/frontend/BytecodeEmitter.cpp
- js/src/frontend/SharedContext.h
- layout/reftests/fonts/gsubtest/makegsubfonts.py
- layout/reftests/fonts/mark-generate.py
- media/mtransport/nricectx.cpp
- netwerk/dns/prepare_tlds.py
- python/devtools/migrate-l10n/migrate/main.py
- python/l10n/convert_xul_to_fluent/convert.py
- python/l10n/convert_xul_to_fluent/lib/__init__.py
- python/l10n/convert_xul_to_fluent/lib/dtd.py
- python/l10n/convert_xul_to_fluent/lib/fluent.py
- python/l10n/convert_xul_to_fluent/lib/migration.py
- python/l10n/convert_xul_to_fluent/lib/utils.py
- python/l10n/convert_xul_to_fluent/lib/xul.py
- testing/mochitest/bisection.py
- testing/mozharness/configs/raptor/linux64_config_taskcluster.py
- testing/mozharness/configs/talos/linux64_config_taskcluster.py
- testing/mozharness/configs/web_platform_tests/test_config_windows.py
- testing/mozharness/external_tools/virtualenv/virtualenv_embedded/distutils-init.py
- testing/talos/talos/cmanager_base.py
- testing/talos/talos/profiler/profiling.py
- testing/talos/talos/unittests/conftest.py
- testing/talos/talos/unittests/test_ffsetup.py
- testing/talos/talos/unittests/test_test.py
- testing/talos/talos/unittests/test_xtalos/test_etlparser.py
- testing/web-platform/tests/content-security-policy/embedded-enforcement/support/echo-allow-csp-from.py
- testing/web-platform/tests/content-security-policy/embedded-enforcement/support/echo-policy-multiple.py
- testing/web-platform/tests/css/tools/apiclient/apiclient/__init__.py
- testing/web-platform/tests/css/tools/apiclient/apiclient/apiclient.py
- testing/web-platform/tests/css/tools/apiclient/apiclient/uritemplate.py
- testing/web-platform/tests/css/tools/apiclient/setup.py
- testing/web-platform/tests/css/tools/apiclient/test.py
- testing/web-platform/tests/css/tools/w3ctestlib/Groups.py
- testing/web-platform/tests/css/tools/w3ctestlib/HTMLSerializer.py
- testing/web-platform/tests/css/tools/w3ctestlib/Indexer.py
- testing/web-platform/tests/css/tools/w3ctestlib/OutputFormats.py
- testing/web-platform/tests/css/tools/w3ctestlib/Suite.py
- testing/web-platform/tests/css/tools/w3ctestlib/Utils.py
- testing/web-platform/tests/css/tools/w3ctestlib/__init__.py
- testing/web-platform/tests/tools/webdriver/webdriver/transport.py
- testing/web-platform/tests/tools/wptrunner/wptrunner/browsers/edgechromium.py
- testing/web-platform/tests/tools/wptrunner/wptrunner/executors/executoredgechromium.py
- testing/web-platform/tests/tools/wptrunner/wptrunner/manifestupdate.py
- testing/web-platform/tests/tools/wptrunner/wptrunner/metadata.py
- testing/web-platform/tests/tools/wptrunner/wptrunner/tests/test_update.py
- toolkit/components/telemetry/build_scripts/setup.py
- toolkit/components/telemetry/tests/marionette/mach_commands.py
- toolkit/mozapps/installer/windows/nsis/preprocess-locale.py
- widget/nsFilePickerProxy.cpp
- widget/windows/tests/TestUrisToValidate.h
extensions:
- .c
- .cc
- .cpp
- .h
- .py
- .rs
support-files:
- 'tools/lint/file-whitespace/**'
type: external
payload: file-whitespace:lint

Просмотреть файл

@ -0,0 +1,61 @@
# 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 __future__ import absolute_import
from mozlint import result
from mozlint.pathutils import expand_exclusions
results = []
def lint(paths, config, fix=None, **lintargs):
files = list(expand_exclusions(paths, config, lintargs['root']))
for f in files:
with open(f, 'rb') as open_file:
hasFix = False
content_to_write = []
for i, line in enumerate(open_file):
if line.endswith(" \n"):
# We found a trailing whitespace
if fix:
# We want to fix it, strip the trailing spaces
content_to_write.append(line.rstrip() + "\n")
hasFix = True
else:
res = {'path': f,
'message': "Trailing whitespace",
'level': 'error'
}
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
with open(f, 'wb') as open_file_to_write:
open_file_to_write.write("".join(content_to_write))
# 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()
if "\r\n" in content:
if fix:
# replace \r\n by \n
content = content.replace(b'\r\n', b'\n')
with open(f, 'wb') as open_file_to_write:
open_file_to_write.write(content)
else:
res = {'path': f,
'message': "Windows line return",
'level': 'error'
}
results.append(result.from_config(config, **res))
return results