Skip to content

Commit

Permalink
chore: add more lint checks and fix them (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grodien authored Oct 30, 2024
1 parent e4ff051 commit 4f37ee3
Show file tree
Hide file tree
Showing 83 changed files with 884 additions and 305 deletions.
1 change: 0 additions & 1 deletion .github/workflows/flutter_android_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ jobs:
echo "ANDROID_KEY_PASSWORD=$ANDROID_KEY_PASSWORD" >> "$GITHUB_ENV"
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
- run: flutter test
- run: flutter build appbundle --flavor dev -t lib/main_dev.dart
- run: flutter build appbundle --flavor inte -t lib/main_inte.dart
- run: flutter build appbundle --flavor prod -t lib/main_prod.dart
46 changes: 46 additions & 0 deletions .github/workflows/flutter_build_test_analyze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Flutter Build Test Analyze

defaults:
run:
working-directory: das_client

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

on:
push:
paths:
- 'das_client/**'
pull_request:
paths:
- 'das_client/**'
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- uses: subosito/flutter-action@v2
with:
flutter-version: '3.24.3'
- name: Prepare Flutter Build
run: |
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
- run: flutter analyze
- run: flutter pub run custom_lint
- run: flutter test
1 change: 0 additions & 1 deletion .github/workflows/flutter_ios_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
run: |
flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
- run: flutter test
- name: Build iOS App DEV
run: flutter build ipa --flavor dev -t lib/main_dev.dart --release --no-codesign
- name: Build iOS App INTE
Expand Down
10 changes: 3 additions & 7 deletions das_client/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ linter:
# producing the lint.
rules:
always_use_package_imports: true
constant_identifier_names: false
library_prefixes: false
unnecessary_library_name: false
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
prefer_single_quotes: true

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
plugins:
- custom_lint
exclude:
- lib/**.g.dart
- test/**.mocks.dart
errors:
invalid_annotation_target: ignore
16 changes: 16 additions & 0 deletions das_client/das_custom_lints/lib/das_custom_lints.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:das_custom_lints/dont_use_src_folder_imports.dart';

// Entrypoint of plugin
PluginBase createPlugin() => _DasCustomLints();

// The class listing all the [LintRule]s and [Assist]s defined by our plugin
class _DasCustomLints extends PluginBase {
// Lint rules
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [DontUseSrcFolderImports()];

// Assists
@override
List<Assist> getAssists() => [];
}
56 changes: 56 additions & 0 deletions das_client/das_custom_lints/lib/dont_use_src_folder_imports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:analyzer/error/error.dart';

// Lint rule to use src folder imports
class DontUseSrcFolderImports extends DartLintRule {
const DontUseSrcFolderImports() : super(code: _code);

// Lint rule metadata
static const _code = LintCode(
name: 'dont_use_src_folder_imports',
problemMessage: 'Don\'t use source folder imports',
errorSeverity: ErrorSeverity.ERROR,
);

// `run` is where you analyze a file and report lint errors
// Invoked on a file automatically on every file edit
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {

// A call back fn that runs on all import declarations in a file
context.registry.addImportDirective((importDirective) {
var importUri = importDirective.uri.stringValue;

if (importUri == null || !importUri.contains('/src/')) return;

var filePath = importDirective.element!.source.fullName;
// ignore imports from outside the lib folder (mainly tests)
if (!filePath.contains('/lib/')) return;

importUri = importUri.substring(importUri.indexOf('/') + 1);
filePath = filePath.substring(filePath.indexOf('/lib/') + 5);

final importParts = importUri.split('/src/');
final fileParts = filePath.split('/src/');

// allow it if its part of the same package
if (fileParts[0].startsWith(importParts[0])) return;

// report a lint error with the `code` and the respective import directive
reporter.atNode(importDirective, LintCode(
name: _code.name,
problemMessage: 'Don\'t use source folder imports for importing ${importDirective.uri.stringValue}',
errorSeverity: ErrorSeverity.ERROR,
));
});
}

// Possible fixes for the lint error go here
@override
List<Fix> getFixes() => [];
}
Loading

0 comments on commit 4f37ee3

Please sign in to comment.