Set up Kotlin linting step in ci with ktlint (#143478)

Adds a kotlin linting step to the linux analyze check, using a version of [ktlint](https://github.com/pinterest/ktlint) hosted on [CIPD](https://chrome-infra-packages.appspot.com/p/flutter/ktlint/linux-amd64/+/version_1_1_1).

Configured to disallow trailing commas, because of (and now acting as a test for) https://github.com/flutter/flutter/pull/145718. Because of this configuration the PR also removes some trailing commas in some kotlin files.

Also checks in a baseline file.
This commit is contained in:
Gray Mackall
2024-04-03 14:21:00 -07:00
committed by GitHub
parent 64af93b707
commit 29ed214e7f
8 changed files with 69 additions and 9 deletions

View File

@@ -153,6 +153,9 @@ Future<void> run(List<String> arguments) async {
printProgress('Taboo words...');
await verifyTabooDocumentation(flutterRoot);
printProgress('Lint Kotlin files...');
await lintKotlinFiles(flutterRoot);
// Ensure that all package dependencies are in sync.
printProgress('Package dependencies...');
await runCommand(flutter, <String>['update-packages', '--verify-only'],
@@ -1973,6 +1976,22 @@ Future<void> verifyTabooDocumentation(String workingDirectory, { int minimumMatc
}
}
Future<void> lintKotlinFiles(String workingDirectory) async {
const String baselineRelativePath = 'dev/bots/test/analyze-test-input/ktlint-baseline.xml';
const String editorConfigRelativePath = 'dev/bots/test/analyze-test-input/.editorconfig';
final EvalResult lintResult = await _evalCommand('ktlint',
<String>['--baseline=$flutterRoot/$baselineRelativePath', '--editorconfig=$flutterRoot/$editorConfigRelativePath'],
workingDirectory: workingDirectory);
if (lintResult.exitCode != 0) {
final String errorMessage = 'Found lint violations in Kotlin files:\n ${lintResult.stdout}\n\n'
'To reproduce this lint locally:\n'
'1. Identify the CIPD version tag used to resolve this particular version of ktlint (check the dependencies section of this shard in the ci.yaml). \n'
'2. Download that version from https://chrome-infra-packages.appspot.com/p/flutter/ktlint/linux-amd64/+/<version_tag>\n'
'3. From the repository root, run `<path_to_ktlint>/ktlint --editorconfig=$editorConfigRelativePath --baseline=$baselineRelativePath`';
foundError(<String>[errorMessage]);
}
}
const List<String> _kIgnoreList = <String>[
'Runner.rc.tmpl',
'flutter_window.cpp',

View File

@@ -0,0 +1,4 @@
[*.{kt,kts}]
# Disable trailing commas to allow compatibility with Kotlin versions less than 1.4.
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="dev/a11y_assessments/android/app/src/main/kotlin/com/example/a11y_assessments/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt">
<error line="5" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/abstract_method_smoke_test/android/app/src/main/kotlin/com/example/abstract_method_smoke_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/android_embedding_v2_smoke_test/android/app/src/main/kotlin/com/example/android_embedding_v2_smoke_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/deferred_components_test/android/app/src/main/kotlin/io/flutter/integration/deferred_components_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/non_nullable/android/app/src/main/kotlin/com/example/non_nullable/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/spell_check/android/app/src/main/kotlin/com/example/sc_int_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/manual_tests/android/app/src/main/kotlin/dev/flutter/manual_tests/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/tracing_tests/android/app/src/main/kotlin/com/example/tracing_tests/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="examples/api/android/app/src/main/kotlin/dev/flutter/flutter_api_samples/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
</baseline>