Skip to content

Commit

Permalink
Make all method/constructor parameters final (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitusortner authored Feb 16, 2019
1 parent 7c4d8d9 commit 0390991
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 50 deletions.
2 changes: 1 addition & 1 deletion floor_generator/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
5 changes: 4 additions & 1 deletion floor_generator/lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import 'package:source_gen/source_gen.dart';
/// Database generator
class FloorGenerator implements Generator {
@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) {
FutureOr<String> 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
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/misc/annotation_expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
36 changes: 18 additions & 18 deletions floor_generator/lib/misc/type_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion floor_generator/lib/model/change_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/model/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions floor_generator/lib/model/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,7 +52,7 @@ class Database {
.toList();
}

List<Entity> getEntities(LibraryReader library) {
List<Entity> getEntities(final LibraryReader library) {
return library.classes
.where((clazz) =>
!clazz.isAbstract && clazz.metadata.any(isEntityAnnotation))
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/model/delete_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion floor_generator/lib/model/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/model/insert_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions floor_generator/lib/model/query_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -74,7 +74,7 @@ class QueryMethod {
return entities.any((entity) => entity == flattenedReturnType.displayName);
}

List<ClassElement> _getEntities(LibraryReader library) {
List<ClassElement> _getEntities(final LibraryReader library) {
return library.classes
.where((clazz) =>
!clazz.isAbstract && clazz.metadata.any(isEntityAnnotation))
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/model/transaction_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/model/update_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 5 additions & 1 deletion floor_generator/lib/writer/change_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 12 additions & 12 deletions floor_generator/lib/writer/database_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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'
Expand All @@ -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);')
Expand All @@ -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'
Expand All @@ -115,46 +115,46 @@ class DatabaseWriter implements Writer {
'''));
}

List<Method> _generateInsertMethods(List<InsertMethod> insertMethods) {
List<Method> _generateInsertMethods(final List<InsertMethod> insertMethods) {
return insertMethods.map((method) {
final writer = InsertMethodBodyWriter(library, method);
return ChangeMethodWriter(library, method, writer).write();
}).toList();
}

List<Method> _generateUpdateMethods(List<UpdateMethod> updateMethods) {
List<Method> _generateUpdateMethods(final List<UpdateMethod> updateMethods) {
return updateMethods.map((method) {
final writer = UpdateMethodBodyWriter(library, method);
return ChangeMethodWriter(library, method, writer).write();
}).toList();
}

List<Method> _generateDeleteMethods(List<DeleteMethod> deleteMethods) {
List<Method> _generateDeleteMethods(final List<DeleteMethod> deleteMethods) {
return deleteMethods.map((method) {
final writer = DeleteMethodBodyWriter(library, method);
return ChangeMethodWriter(library, method, writer).write();
}).toList();
}

List<Method> _generateQueryMethods(List<QueryMethod> queryMethods) {
List<Method> _generateQueryMethods(final List<QueryMethod> queryMethods) {
return queryMethods
.map((method) => QueryMethodWriter(library, method).write())
.toList();
}

List<Method> _generateTransactionMethods(
List<TransactionMethod> transactionMethods,
final List<TransactionMethod> transactionMethods,
) {
return transactionMethods
.map((method) => TransactionMethodWriter(library, method).write())
.toList();
}

List<String> _generateCreateTableSqlStatements(List<Entity> entities) {
List<String> _generateCreateTableSqlStatements(final List<Entity> 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' : '';
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/writer/delete_method_body_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/writer/insert_method_body_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/writer/transaction_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion floor_generator/lib/writer/update_method_body_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 0390991

Please sign in to comment.