From 0390991e69406ac76514be41488f76488613a168 Mon Sep 17 00:00:00 2001 From: Vitus Date: Sat, 16 Feb 2019 18:11:45 +0100 Subject: [PATCH] Make all method/constructor parameters final (#55) --- floor_generator/lib/builder.dart | 2 +- floor_generator/lib/generator.dart | 5 ++- .../lib/misc/annotation_expression.dart | 2 +- floor_generator/lib/misc/type_utils.dart | 36 +++++++++---------- floor_generator/lib/model/change_method.dart | 2 +- floor_generator/lib/model/column.dart | 2 +- floor_generator/lib/model/database.dart | 4 +-- floor_generator/lib/model/delete_method.dart | 2 +- floor_generator/lib/model/entity.dart | 2 +- floor_generator/lib/model/insert_method.dart | 2 +- floor_generator/lib/model/query_method.dart | 4 +-- .../lib/model/transaction_method.dart | 2 +- floor_generator/lib/model/update_method.dart | 2 +- .../lib/writer/change_method_writer.dart | 6 +++- .../lib/writer/database_writer.dart | 24 ++++++------- .../lib/writer/delete_method_body_writer.dart | 2 +- .../lib/writer/insert_method_body_writer.dart | 2 +- .../lib/writer/query_method_writer.dart | 2 +- .../lib/writer/transaction_method_writer.dart | 2 +- .../lib/writer/update_method_body_writer.dart | 2 +- 20 files changed, 57 insertions(+), 50 deletions(-) diff --git a/floor_generator/lib/builder.dart b/floor_generator/lib/builder.dart index a2a4eaff..08ec55a1 100644 --- a/floor_generator/lib/builder.dart +++ b/floor_generator/lib/builder.dart @@ -8,5 +8,5 @@ import 'package:source_gen/source_gen.dart'; /// /// Use 'flutter packages pub run build_runner watch' to trigger /// code generation on changes. -Builder floorBuilder(BuilderOptions _) => +Builder floorBuilder(final BuilderOptions _) => SharedPartBuilder([FloorGenerator()], 'floor'); diff --git a/floor_generator/lib/generator.dart b/floor_generator/lib/generator.dart index b0f5a80c..1e7bb505 100644 --- a/floor_generator/lib/generator.dart +++ b/floor_generator/lib/generator.dart @@ -8,7 +8,10 @@ import 'package:source_gen/source_gen.dart'; /// Database generator class FloorGenerator implements Generator { @override - FutureOr generate(LibraryReader library, BuildStep buildStep) { + FutureOr generate( + final LibraryReader library, + final BuildStep buildStep, + ) { final database = DatabaseWriter(library).write(); // TODO generator runs for every file of the project, so this fails without diff --git a/floor_generator/lib/misc/annotation_expression.dart b/floor_generator/lib/misc/annotation_expression.dart index f9e983da..5a2d09eb 100644 --- a/floor_generator/lib/misc/annotation_expression.dart +++ b/floor_generator/lib/misc/annotation_expression.dart @@ -4,7 +4,7 @@ import 'package:code_builder/code_builder.dart'; class AnnotationExpression extends CodeExpression { final String annotation; - AnnotationExpression(this.annotation) : super(Code(annotation)); + AnnotationExpression(final this.annotation) : super(Code(annotation)); } final overrideAnnotationExpression = AnnotationExpression('override'); diff --git a/floor_generator/lib/misc/type_utils.dart b/floor_generator/lib/misc/type_utils.dart index 9865dfb9..3b35d450 100644 --- a/floor_generator/lib/misc/type_utils.dart +++ b/floor_generator/lib/misc/type_utils.dart @@ -2,27 +2,27 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:floor_generator/misc/constants.dart'; -bool isString(DartType type) { +bool isString(final DartType type) { return type.displayName == SupportedType.STRING && _isDartCore(type); } -bool isBool(DartType type) { +bool isBool(final DartType type) { return type.displayName == SupportedType.BOOL && _isDartCore(type); } -bool isInt(DartType type) { +bool isInt(final DartType type) { return type.isDartCoreInt; } -bool isDouble(DartType type) { +bool isDouble(final DartType type) { return type.displayName == SupportedType.DOUBLE && _isDartCore(type); } -bool isList(DartType type) { +bool isList(final DartType type) { return type.name == 'List' && _isDartCore(type); } -bool isSupportedType(DartType type) { +bool isSupportedType(final DartType type) { return [ SupportedType.STRING, SupportedType.BOOL, @@ -32,50 +32,50 @@ bool isSupportedType(DartType type) { _isDartCore(type); } -bool isEntityAnnotation(ElementAnnotation annotation) { +bool isEntityAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.ENTITY; } -bool isDatabaseAnnotation(ElementAnnotation annotation) { +bool isDatabaseAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.DATABASE; } -bool isColumnInfoAnnotation(ElementAnnotation annotation) { +bool isColumnInfoAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.COLUMN_INFO; } -bool isPrimaryKeyAnnotation(ElementAnnotation annotation) { +bool isPrimaryKeyAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.PRIMARY_KEY; } -bool isQueryAnnotation(ElementAnnotation annotation) { +bool isQueryAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.QUERY; } -bool isInsertAnnotation(ElementAnnotation annotation) { +bool isInsertAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.INSERT; } -bool isUpdateAnnotation(ElementAnnotation annotation) { +bool isUpdateAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.UPDATE; } -bool isDeleteAnnotation(ElementAnnotation annotation) { +bool isDeleteAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.DELETE; } -bool isTransactionAnnotation(ElementAnnotation annotation) { +bool isTransactionAnnotation(final ElementAnnotation annotation) { return _getAnnotationName(annotation) == Annotation.TRANSACTION; } -DartType flattenList(DartType type) { +DartType flattenList(final DartType type) { return (type as ParameterizedType).typeArguments.first; } -bool _isDartCore(DartType type) { +bool _isDartCore(final DartType type) { return type.element.library.isDartCore; } -String _getAnnotationName(ElementAnnotation annotation) { +String _getAnnotationName(final ElementAnnotation annotation) { return annotation.computeConstantValue().type.displayName; } diff --git a/floor_generator/lib/model/change_method.dart b/floor_generator/lib/model/change_method.dart index a4be90b3..f952f8a5 100644 --- a/floor_generator/lib/model/change_method.dart +++ b/floor_generator/lib/model/change_method.dart @@ -8,7 +8,7 @@ import 'package:source_gen/source_gen.dart'; class ChangeMethod { final MethodElement method; - ChangeMethod(this.method); + ChangeMethod(final this.method); DartType get returnType => method.returnType; diff --git a/floor_generator/lib/model/column.dart b/floor_generator/lib/model/column.dart index 842fb293..80e56115 100644 --- a/floor_generator/lib/model/column.dart +++ b/floor_generator/lib/model/column.dart @@ -7,7 +7,7 @@ import 'package:source_gen/source_gen.dart'; class Column { final FieldElement field; - Column(this.field); + Column(final this.field); String get name { if (!_hasColumnInfoAnnotation) { diff --git a/floor_generator/lib/model/database.dart b/floor_generator/lib/model/database.dart index 1c3728e0..547a1899 100644 --- a/floor_generator/lib/model/database.dart +++ b/floor_generator/lib/model/database.dart @@ -11,7 +11,7 @@ import 'package:source_gen/source_gen.dart'; class Database { final ClassElement clazz; - Database(this.clazz); + Database(final this.clazz); String get name => clazz.displayName; @@ -52,7 +52,7 @@ class Database { .toList(); } - List getEntities(LibraryReader library) { + List getEntities(final LibraryReader library) { return library.classes .where((clazz) => !clazz.isAbstract && clazz.metadata.any(isEntityAnnotation)) diff --git a/floor_generator/lib/model/delete_method.dart b/floor_generator/lib/model/delete_method.dart index 6ab09c65..55bae9f4 100644 --- a/floor_generator/lib/model/delete_method.dart +++ b/floor_generator/lib/model/delete_method.dart @@ -2,5 +2,5 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:floor_generator/model/change_method.dart'; class DeleteMethod extends ChangeMethod { - DeleteMethod(MethodElement method) : super(method); + DeleteMethod(final MethodElement method) : super(method); } diff --git a/floor_generator/lib/model/entity.dart b/floor_generator/lib/model/entity.dart index 0da4fe8f..090729b9 100644 --- a/floor_generator/lib/model/entity.dart +++ b/floor_generator/lib/model/entity.dart @@ -6,7 +6,7 @@ import 'package:floor_generator/model/column.dart'; class Entity { final ClassElement clazz; - Entity(this.clazz); + Entity(final this.clazz); String get name { return clazz.metadata diff --git a/floor_generator/lib/model/insert_method.dart b/floor_generator/lib/model/insert_method.dart index 4b837957..5b668674 100644 --- a/floor_generator/lib/model/insert_method.dart +++ b/floor_generator/lib/model/insert_method.dart @@ -2,5 +2,5 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:floor_generator/model/change_method.dart'; class InsertMethod extends ChangeMethod { - InsertMethod(MethodElement method) : super(method); + InsertMethod(final MethodElement method) : super(method); } diff --git a/floor_generator/lib/model/query_method.dart b/floor_generator/lib/model/query_method.dart index 48e941ad..1d3367e7 100644 --- a/floor_generator/lib/model/query_method.dart +++ b/floor_generator/lib/model/query_method.dart @@ -10,7 +10,7 @@ import 'package:source_gen/source_gen.dart'; class QueryMethod { final MethodElement method; - QueryMethod(this.method); + QueryMethod(final this.method); /// Query as defined in by user in Dart code. String get rawQuery { @@ -74,7 +74,7 @@ class QueryMethod { return entities.any((entity) => entity == flattenedReturnType.displayName); } - List _getEntities(LibraryReader library) { + List _getEntities(final LibraryReader library) { return library.classes .where((clazz) => !clazz.isAbstract && clazz.metadata.any(isEntityAnnotation)) diff --git a/floor_generator/lib/model/transaction_method.dart b/floor_generator/lib/model/transaction_method.dart index 69707d53..2417e992 100644 --- a/floor_generator/lib/model/transaction_method.dart +++ b/floor_generator/lib/model/transaction_method.dart @@ -5,7 +5,7 @@ class TransactionMethod { final MethodElement method; final String databaseName; - TransactionMethod(this.method, this.databaseName); + TransactionMethod(final this.method, final this.databaseName); DartType get returnType => method.returnType; diff --git a/floor_generator/lib/model/update_method.dart b/floor_generator/lib/model/update_method.dart index f7867c25..763695c9 100644 --- a/floor_generator/lib/model/update_method.dart +++ b/floor_generator/lib/model/update_method.dart @@ -2,5 +2,5 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:floor_generator/model/change_method.dart'; class UpdateMethod extends ChangeMethod { - UpdateMethod(MethodElement method) : super(method); + UpdateMethod(final MethodElement method) : super(method); } diff --git a/floor_generator/lib/writer/change_method_writer.dart b/floor_generator/lib/writer/change_method_writer.dart index b13864de..121d467c 100644 --- a/floor_generator/lib/writer/change_method_writer.dart +++ b/floor_generator/lib/writer/change_method_writer.dart @@ -9,7 +9,11 @@ class ChangeMethodWriter implements Writer { final ChangeMethod method; final Writer methodBodyWriter; - ChangeMethodWriter(this.library, this.method, this.methodBodyWriter); + ChangeMethodWriter( + final this.library, + final this.method, + final this.methodBodyWriter, + ); @override Method write() { diff --git a/floor_generator/lib/writer/database_writer.dart b/floor_generator/lib/writer/database_writer.dart index cf1a28bc..440eba7d 100644 --- a/floor_generator/lib/writer/database_writer.dart +++ b/floor_generator/lib/writer/database_writer.dart @@ -21,7 +21,7 @@ import 'package:code_builder/code_builder.dart'; class DatabaseWriter implements Writer { final LibraryReader library; - DatabaseWriter(this.library); + DatabaseWriter(final this.library); @override Spec write() { @@ -56,7 +56,7 @@ class DatabaseWriter implements Writer { } } - Method _generateOpenDatabaseFunction(String databaseName) { + Method _generateOpenDatabaseFunction(final String databaseName) { return Method((builder) => builder ..returns = refer('Future<$databaseName>') ..name = '_\$open' @@ -68,7 +68,7 @@ class DatabaseWriter implements Writer { ''')); } - Class _generateDatabaseImplementation(Database database) { + Class _generateDatabaseImplementation(final Database database) { final createTableStatements = _generateCreateTableSqlStatements(database.getEntities(library)) .map((statement) => 'await database.execute($statement);') @@ -94,8 +94,8 @@ class DatabaseWriter implements Writer { } Method _generateOpenMethod( - String databaseName, - String createTableStatements, + final String databaseName, + final String createTableStatements, ) { return Method((builder) => builder ..name = 'open' @@ -115,46 +115,46 @@ class DatabaseWriter implements Writer { ''')); } - List _generateInsertMethods(List insertMethods) { + List _generateInsertMethods(final List insertMethods) { return insertMethods.map((method) { final writer = InsertMethodBodyWriter(library, method); return ChangeMethodWriter(library, method, writer).write(); }).toList(); } - List _generateUpdateMethods(List updateMethods) { + List _generateUpdateMethods(final List updateMethods) { return updateMethods.map((method) { final writer = UpdateMethodBodyWriter(library, method); return ChangeMethodWriter(library, method, writer).write(); }).toList(); } - List _generateDeleteMethods(List deleteMethods) { + List _generateDeleteMethods(final List deleteMethods) { return deleteMethods.map((method) { final writer = DeleteMethodBodyWriter(library, method); return ChangeMethodWriter(library, method, writer).write(); }).toList(); } - List _generateQueryMethods(List queryMethods) { + List _generateQueryMethods(final List queryMethods) { return queryMethods .map((method) => QueryMethodWriter(library, method).write()) .toList(); } List _generateTransactionMethods( - List transactionMethods, + final List transactionMethods, ) { return transactionMethods .map((method) => TransactionMethodWriter(library, method).write()) .toList(); } - List _generateCreateTableSqlStatements(List entities) { + List _generateCreateTableSqlStatements(final List entities) { return entities.map(_generateSql).toList(); } - String _generateSql(Entity entity) { + String _generateSql(final Entity entity) { final columns = entity.columns.map((column) { final primaryKey = column.isPrimaryKey ? ' PRIMARY KEY' : ''; final autoIncrement = column.autoGenerate ? ' AUTOINCREMENT' : ''; diff --git a/floor_generator/lib/writer/delete_method_body_writer.dart b/floor_generator/lib/writer/delete_method_body_writer.dart index b29bed00..9b6140c6 100644 --- a/floor_generator/lib/writer/delete_method_body_writer.dart +++ b/floor_generator/lib/writer/delete_method_body_writer.dart @@ -8,7 +8,7 @@ class DeleteMethodBodyWriter implements Writer { final LibraryReader library; final DeleteMethod method; - DeleteMethodBodyWriter(this.library, this.method); + DeleteMethodBodyWriter(final this.library, final this.method); @override Code write() { diff --git a/floor_generator/lib/writer/insert_method_body_writer.dart b/floor_generator/lib/writer/insert_method_body_writer.dart index 44b855ae..319be02c 100644 --- a/floor_generator/lib/writer/insert_method_body_writer.dart +++ b/floor_generator/lib/writer/insert_method_body_writer.dart @@ -9,7 +9,7 @@ class InsertMethodBodyWriter implements Writer { final LibraryReader library; final InsertMethod method; - InsertMethodBodyWriter(this.library, this.method); + InsertMethodBodyWriter(final this.library, final this.method); @override Code write() { diff --git a/floor_generator/lib/writer/query_method_writer.dart b/floor_generator/lib/writer/query_method_writer.dart index b9a57703..7dffa00b 100644 --- a/floor_generator/lib/writer/query_method_writer.dart +++ b/floor_generator/lib/writer/query_method_writer.dart @@ -11,7 +11,7 @@ class QueryMethodWriter implements Writer { final LibraryReader library; final QueryMethod queryMethod; - const QueryMethodWriter(this.library, this.queryMethod); + const QueryMethodWriter(final this.library, final this.queryMethod); @override Method write() { diff --git a/floor_generator/lib/writer/transaction_method_writer.dart b/floor_generator/lib/writer/transaction_method_writer.dart index 0d659ec7..2c4daeb4 100644 --- a/floor_generator/lib/writer/transaction_method_writer.dart +++ b/floor_generator/lib/writer/transaction_method_writer.dart @@ -8,7 +8,7 @@ class TransactionMethodWriter implements Writer { final LibraryReader library; final TransactionMethod method; - TransactionMethodWriter(this.library, this.method); + TransactionMethodWriter(final this.library, final this.method); @override Method write() { diff --git a/floor_generator/lib/writer/update_method_body_writer.dart b/floor_generator/lib/writer/update_method_body_writer.dart index 750e595e..ab637884 100644 --- a/floor_generator/lib/writer/update_method_body_writer.dart +++ b/floor_generator/lib/writer/update_method_body_writer.dart @@ -10,7 +10,7 @@ class UpdateMethodBodyWriter implements Writer { final LibraryReader library; final UpdateMethod method; - UpdateMethodBodyWriter(this.library, this.method); + UpdateMethodBodyWriter(final this.library, final this.method); @override Code write() {