Skip to content
This repository has been archived by the owner on Dec 6, 2017. It is now read-only.

fix(injector_generator): allow library in /web #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/transformer/injector_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,20 @@ class _Processor {
var unit = lib.definingCompilationUnit.node;
var transaction = resolver.createTextEditTransaction(lib);

FunctionDeclaration main = unit.declarations.firstWhere(
(d) => d is FunctionDeclaration && d.name.toString() == 'main',
orElse: () => null);

if (main == null) {
return;
}

var imports = unit.directives.where((d) => d is ImportDirective);
transaction.edit(imports.last.end, imports.last.end, '\nimport '
"'${path.url.basenameWithoutExtension(id.path)}"
"_generated_type_factory_maps.dart' show setStaticReflectorAsDefault;");

FunctionExpression main = unit.declarations.where((d) =>
d is FunctionDeclaration && d.name.toString() == 'main')
.first.functionExpression;
var body = main.body;
var body = main.functionExpression.body;
if (body is BlockFunctionBody) {
var location = body.beginToken.end;
transaction.edit(location, location, '\n setStaticReflectorAsDefault();');
Expand Down
47 changes: 47 additions & 0 deletions test/transformer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,53 @@ main() {
});
});

it('transforms main with library', () {
return tests.applyTransformers(phases,
inputs: {
'a|web/main.dart': '''
library main;
import 'package:di/di.dart';

main() {
print('abc');
}''',
'a|web/lib.dart': '''
library lib;
part "a.dart";
part "b.dart";''',
'a|web/a.dart': '''
part of lib;
Class A {
}''',
'a|web/b.dart': '''
part of lib;
Class B{
}'''
},
results: {
'a|web/main.dart': '''
library main;
import 'package:di/di.dart';
import 'main_generated_type_factory_maps.dart' show setStaticReflectorAsDefault;

main() {
setStaticReflectorAsDefault();
print('abc');
}''',
'a|web/lib.dart': '''
library lib;
part "a.dart";
part "b.dart";''',
'a|web/a.dart': '''
part of lib;
Class A {
}''',
'a|web/b.dart': '''
part of lib;
Class B{
}''' });
});

it('supports using a child of an injectable annotations as an injection marker', () {
injectableAnnotations.add('di.annotations.Injectable');
return generates(phases,
Expand Down