From 7f17800069a8b85bd1aa54581846bfa34116c131 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 20 Jan 2024 00:44:14 +1300 Subject: [PATCH] fix: fix #75 --- .github/workflows/format.yml | 7 +- analysis_options.yaml | 2 +- bin/main.dart | 26 ++--- example/example_app/analysis_options.yaml | 4 + example/example_app/lib/main.dart | 122 +--------------------- example/example_app/pubspec.yaml | 9 +- lib/sort.dart | 10 +- pubspec.yaml | 5 +- test/sort_test.dart | 2 - 9 files changed, 37 insertions(+), 150 deletions(-) create mode 100644 example/example_app/analysis_options.yaml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 9c7985e..1ea4f00 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -10,10 +10,13 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v4 - - name: Setup Dart SDK - uses: dart-lang/setup-dart@v1 + - name: Setup Flutter SDK + uses: subosito/flutter-action@v2 with: sdk: stable + - name: Run pub get + run: dart pub get + working-directory: ./example/example_app - name: Run format run: dart format --set-exit-if-changed . diff --git a/analysis_options.yaml b/analysis_options.yaml index cccf1f0..38f100c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,4 +1,4 @@ -include: package:flutter_lints/flutter.yaml +include: package:lints/recommended.yaml analyzer: diff --git a/bin/main.dart b/bin/main.dart index 2752cfc..ce34ccc 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -30,7 +30,7 @@ void main(List args) { Package name is one factor used to identify project imports Dependencies/dev_dependencies names are used to identify package imports */ - final pubspecYamlFile = File('${currentPath}/pubspec.yaml'); + final pubspecYamlFile = File('$currentPath/pubspec.yaml'); final pubspecYaml = loadYaml(pubspecYamlFile.readAsStringSync()); // Getting all dependencies and project package name @@ -40,13 +40,13 @@ void main(List args) { final stopwatch = Stopwatch(); stopwatch.start(); - final pubspecLockFile = File('${currentPath}/pubspec.lock'); + final pubspecLockFile = File('$currentPath/pubspec.lock'); final pubspecLock = loadYaml(pubspecLockFile.readAsStringSync()); dependencies.addAll(pubspecLock['packages'].keys); var emojis = false; var noComments = false; - final ignored_files = []; + final ignoredFiles = []; // Reading from config in pubspec.yaml safely if (!argResults.contains('--ignore-config')) { @@ -55,7 +55,7 @@ void main(List args) { if (config.containsKey('emojis')) emojis = config['emojis']; if (config.containsKey('comments')) noComments = !config['comments']; if (config.containsKey('ignored_files')) { - ignored_files.addAll(config['ignored_files']); + ignoredFiles.addAll(config['ignored_files']); } } } @@ -69,16 +69,16 @@ void main(List args) { final dartFiles = files.dartFiles(currentPath, args); final containsFlutter = dependencies.contains('flutter'); final containsRegistrant = dartFiles - .containsKey('${currentPath}/lib/generated_plugin_registrant.dart'); + .containsKey('$currentPath/lib/generated_plugin_registrant.dart'); - stdout.writeln('contains flutter: ${containsFlutter}'); - stdout.writeln('contains registrant: ${containsRegistrant}'); + stdout.writeln('contains flutter: $containsFlutter'); + stdout.writeln('contains registrant: $containsRegistrant'); if (containsFlutter && containsRegistrant) { - dartFiles.remove('${currentPath}/lib/generated_plugin_registrant.dart'); + dartFiles.remove('$currentPath/lib/generated_plugin_registrant.dart'); } - for (final pattern in ignored_files) { + for (final pattern in ignoredFiles) { dartFiles.removeWhere((key, _) => RegExp(pattern).hasMatch(key.replaceFirst(currentPath, ''))); } @@ -113,14 +113,14 @@ void main(List args) { for (int i = 0; i < sortedFiles.length; i++) { final file = dartFiles[sortedFiles[i]]; stdout.write( - '${sortedFiles.length == 1 ? '\n' : ''}┃ ${i == sortedFiles.length - 1 ? '┗' : '┣'}━━ ${success} Sorted imports for ${file?.path.replaceFirst(currentPath, '')}/'); + '${sortedFiles.length == 1 ? '\n' : ''}┃ ${i == sortedFiles.length - 1 ? '┗' : '┣'}━━ $success Sorted imports for ${file?.path.replaceFirst(currentPath, '')}/'); String filename = file!.path.split(Platform.pathSeparator).last; - stdout.write(filename + '\n'); + stdout.write('$filename\n'); } - if (sortedFiles.length == 0) { + if (sortedFiles.isEmpty) { stdout.write('\n'); } stdout.write( - '┗━━ ${success} Sorted ${sortedFiles.length} files in ${stopwatch.elapsed.inSeconds}.${stopwatch.elapsedMilliseconds} seconds\n'); + '┗━━ $success Sorted ${sortedFiles.length} files in ${stopwatch.elapsed.inSeconds}.${stopwatch.elapsedMilliseconds} seconds\n'); } diff --git a/example/example_app/analysis_options.yaml b/example/example_app/analysis_options.yaml new file mode 100644 index 0000000..95fa91a --- /dev/null +++ b/example/example_app/analysis_options.yaml @@ -0,0 +1,4 @@ +analyzer: + errors: + unused_import: ignore + unnecessary_import: ignore \ No newline at end of file diff --git a/example/example_app/lib/main.dart b/example/example_app/lib/main.dart index a387907..bab5376 100644 --- a/example/example_app/lib/main.dart +++ b/example/example_app/lib/main.dart @@ -1,4 +1,7 @@ // 🎯 Dart imports: +import 'dart:async'; +import 'dart:io'; +import 'dart:js'; // 🐦 Flutter imports: import 'package:flutter/cupertino.dart'; @@ -7,126 +10,9 @@ import 'package:flutter/painting.dart'; import 'package:flutter/physics.dart'; // 📦 Package imports: -import 'package:flutter_gen/gen_l10n/translations.dart'; import 'package:intl/intl.dart'; -import 'package:mdi/mdi.dart'; import 'package:provider/provider.dart'; // 🌎 Project imports: import 'package:example_app/anotherFile2.dart'; - -void main() { - runApp(MyApp()); -} - -class MyApp extends StatelessWidget { - // This widget is the root of your application. - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. - primarySwatch: Colors.blue, - // This makes the visual density adapt to the platform that you run - // the app on. For desktop platforms, the controls will be smaller and - // closer together (more dense) than on mobile platforms. - visualDensity: VisualDensity.adaptivePlatformDensity, - ), - home: MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - - @override - _MyHomePageState createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} +import 'anotherFile.dart'; diff --git a/example/example_app/pubspec.yaml b/example/example_app/pubspec.yaml index 5c076c9..929f3e2 100644 --- a/example/example_app/pubspec.yaml +++ b/example/example_app/pubspec.yaml @@ -18,18 +18,17 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: '>=2.7.0 <3.0.0' + sdk: ">=2.12.0 <4.0.0" dependencies: flutter: sdk: flutter - provider: ^4.0.5 - mdi: ^2.0.0 - intl: ^0.16.1 + provider: ^6.1.1 + intl: ^0.19.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.3 + cupertino_icons: ^1.0.6 dev_dependencies: flutter_test: diff --git a/lib/sort.dart b/lib/sort.dart index f23721f..ba44e4e 100644 --- a/lib/sort.dart +++ b/lib/sort.dart @@ -7,7 +7,7 @@ import 'dart:io'; /// at index 1 ImportSortData sortImports( List lines, - String package_name, + String packageName, bool emojis, bool exitIfChanged, bool noComments, { @@ -55,7 +55,7 @@ ImportSortData sortImports( dartImports.add(lines[i]); } else if (lines[i].contains('package:flutter/')) { flutterImports.add(lines[i]); - } else if (lines[i].contains('package:$package_name/')) { + } else if (lines[i].contains('package:$packageName/')) { projectImports.add(lines[i]); } else if (lines[i].contains('package:')) { packageImports.add(lines[i]); @@ -152,11 +152,11 @@ ImportSortData sortImports( sortedLines.add(''); final sortedFile = sortedLines.join('\n'); - final original = lines.join('\n') + '\n'; + final original = '${lines.join('\n')}\n'; if (exitIfChanged && original != sortedFile) { if (filePath != null) { - stdout.writeln( - '\n┗━━🚨 File ${filePath} does not have its imports sorted.'); + stdout + .writeln('\n┗━━🚨 File $filePath does not have its imports sorted.'); } exit(1); } diff --git a/pubspec.yaml b/pubspec.yaml index da7c317..2c50b83 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,9 +4,6 @@ version: 4.6.0 repository: "https://github.com/fluttercommunity/import_sorter" homepage: "https://mattglei.ch" maintainer: Matthew Gleich (@gleich) -authors: - - Matthew Gleich - - Flutter Community environment: sdk: ">=2.12.0 <4.0.0" @@ -17,8 +14,8 @@ dependencies: yaml: ^3.1.2 dev_dependencies: - test: ^1.16.7 lints: ^3.0.0 + test: ^1.16.7 import_sorter: diff --git a/test/sort_test.dart b/test/sort_test.dart index 6817409..f0a299c 100644 --- a/test/sort_test.dart +++ b/test/sort_test.dart @@ -20,9 +20,7 @@ import 'package:flutter/painting.dart'; import 'package:flutter/physics.dart'; '''; const packageImports = ''' -import 'package:flutter_gen/gen_l10n/translations.dart'; import 'package:intl/intl.dart'; -import 'package:mdi/mdi.dart'; import 'package:provider/provider.dart'; '''; const projectImports = '''