Bug 1740799 - Format Kotlin code using ktlint. r=geckoview-reviewers,owlish,linter-reviewers,sylvestre

Android-component and Fenix use ktlint, so mozilla-central should use same
tools via spotless.

This is from https://phabricator.services.mozilla.com/D131018

Differential Revision: https://phabricator.services.mozilla.com/D161557
This commit is contained in:
Makoto Kato 2022-11-18 00:55:03 +00:00
parent 3f99c2ac51
commit 1756e8266e
7 changed files with 49 additions and 6 deletions

View File

@ -362,5 +362,12 @@ subprojects {
}
googleJavaFormat('1.15.0')
}
kotlin {
target project.fileTree(project.projectDir) {
include '**/*.kt'
exclude '**/thirdparty/**'
}
ktlint('0.46.1')
}
}
}

View File

@ -132,7 +132,7 @@ In this document, we try to list these all tools.
- :ref:`clippy`
- https://github.com/rust-lang/rust-clippy
.. list-table:: Java
.. list-table:: Java/Kotlin
:widths: 20 20 20 20 20
:header-rows: 1

View File

@ -6,7 +6,10 @@ for Gradle and Android.
In our current configuration, Spotless includes the
`Google Java Format plug-in <https://github.com/google/google-java-format>`__
which formats all our Java code using the Google Java coding style guidelines.
which formats all our Java code using the Google Java coding style guidelines,
and `ktlint <https://ktlint.github.io/>`__ which formats all
our Kotlin code using the official Kotlin coding convention and Android Kotlin
Style Guide.
Run Locally

View File

@ -123,6 +123,10 @@ native code. `Bug
1509539 <https://bugzilla.mozilla.org/show_bug.cgi?id=1509539>`_ tracks
making Android Studio and Gradle do this automatically.
If you want set up code formatting for Kotlin, please reference
`IntelliJ IDEA configuration
<https://pinterest.github.io/ktlint/rules/configuration-intellij-idea/>`_.
Custom mozconfig with Android Studio
------------------------------------

View File

@ -284,12 +284,14 @@ def gradle_android_api_lint_tasks(build_config):
set_config("GRADLE_ANDROID_API_LINT_TASKS", gradle_android_api_lint_tasks)
set_config("GRADLE_ANDROID_FORMAT_LINT_FIX_TASKS", ["spotlessJavaApply"])
set_config(
"GRADLE_ANDROID_FORMAT_LINT_FIX_TASKS", ["spotlessJavaApply", "spotlessKotlinApply"]
)
@dependable
def gradle_android_format_lint_check_tasks():
return ["spotlessJavaCheck"]
return ["spotlessJavaCheck", "spotlessKotlinCheck"]
set_config(

View File

@ -3,7 +3,7 @@ android-format:
description: Android formatting lint
include: ['mobile/android']
exclude: []
extensions: ['java']
extensions: ['java', 'kt']
support-files:
- 'mobile/android/**/Makefile.in'
- 'mobile/android/config/**'

View File

@ -83,6 +83,8 @@ def gradle(log, topsrcdir=None, topobjdir=None, tasks=[], extra_args=[], verbose
proc.kill()
raise
return proc.returncode
def format(config, fix=None, **lintargs):
topsrcdir = lintargs["root"]
@ -93,7 +95,7 @@ def format(config, fix=None, **lintargs):
else:
tasks = lintargs["substs"]["GRADLE_ANDROID_FORMAT_LINT_CHECK_TASKS"]
gradle(
ret = gradle(
lintargs["log"],
topsrcdir=topsrcdir,
topobjdir=topobjdir,
@ -116,6 +118,31 @@ def format(config, fix=None, **lintargs):
"level": "error",
}
results.append(result.from_config(config, **err))
folder = os.path.join(
topobjdir, "gradle", "build", path, "spotless", "spotlessKotlin"
)
for filename in glob.iglob(folder + "/**/*.kt", recursive=True):
err = {
"rule": "spotless-kt",
"path": os.path.join(path, mozpath.relpath(filename, folder)),
"lineno": 0,
"column": 0,
"message": "Formatting error, please run ./mach lint -l android-format --fix",
"level": "error",
}
results.append(result.from_config(config, **err))
if len(results) == 0 and ret != 0:
# spotless seems to hit unfixed error.
err = {
"rule": "spotless",
"path": "",
"lineno": 0,
"column": 0,
"message": "Unexpected error",
"level": "error",
}
results.append(result.from_config(config, **err))
# If --fix was passed, we just report the number of files that were changed
if fix: