refactor(dart/transform): Delete unused bind_generator

All getters & setters generated by `bind_generator` are now generated in
the `directive_processor` or `template_compiler` stage.
This commit is contained in:
Tim Blasi 2015-10-13 15:42:00 -07:00
parent c91fc49d01
commit 563a76304a
13 changed files with 0 additions and 504 deletions

View File

@ -1,116 +0,0 @@
library angular2.transform.bind_generator.generator;
import 'dart:async';
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:angular2/src/transform/common/property_utils.dart' as prop;
import 'package:barback/barback.dart';
class _ExtractQueryFieldsFromAnnotation extends Object
with RecursiveAstVisitor<Object> {
final ConstantEvaluator _evaluator = new ConstantEvaluator();
final List<String> queryFields = [];
@override
Object visitNamedExpression(NamedExpression node) {
if ('${node.name.label}' == "queries") {
if (node.expression is! MapLiteral) {
throw new FormatException(
'Expected a map value for "queries", but got ${node.expression}',
node.toSource());
}
MapLiteral queries = node.expression;
queryFields.addAll(queries.entries.map((e) => e.key.accept(_evaluator)));
}
return super.visitNamedExpression(node);
}
Map asMap() {
return new Map.fromIterable(queryFields, value: (_) => 'Object');
}
}
class _ExtractQueryFieldsFromPropMetadata extends Object
with RecursiveAstVisitor<Object> {
final ConstantEvaluator _evaluator = new ConstantEvaluator();
final List<String> queryFields = [];
@override
Object visitMapLiteralEntry(MapLiteralEntry node) {
if (_hasQueryAnnotation(node.value)) {
queryFields.add(node.key.accept(_evaluator));
}
return super.visitMapLiteralEntry(node);
}
bool _hasQueryAnnotation(list) {
var res = false;
list.elements.forEach((item) {
if (item is! InstanceCreationExpression) return;
var n = item.constructorName.toString();
if (n == "ContentChild" ||
n == "ViewChild" ||
n == "ContentChildren" ||
n == "ViewChildren") {
res = true;
}
});
return res;
}
asMap() {
return new Map.fromIterable(queryFields, value: (_) => 'Object');
}
}
Future<String> createNgSettersAndGetters(
AssetReader reader, AssetId entryPoint) async {
NgDeps ngDeps = await NgDeps.parse(reader, entryPoint);
String code = ngDeps.code;
var setters = [];
ngDeps.registeredTypes.forEach((t) {
final fromAnnotation = new _ExtractQueryFieldsFromAnnotation();
t.annotations.accept(fromAnnotation);
final fromPropMetadata = new _ExtractQueryFieldsFromPropMetadata();
if (t.propMetadata != null) {
t.propMetadata.accept(fromPropMetadata);
}
setters.addAll(_generateSetters(fromAnnotation.asMap()));
setters.addAll(_generateSetters(fromPropMetadata.asMap()));
});
if (setters.isEmpty) return code;
var out = new StringBuffer();
var codeInjectIdx = ngDeps.registeredTypes.last.registerMethod.end;
out.write(code.substring(0, codeInjectIdx));
if (setters.isNotEmpty) {
out.write('..registerSetters({${setters.join(', ')}})');
}
out.write(code.substring(codeInjectIdx));
return '$out';
}
// TODO(kegluneq): De-dupe from template_compiler/generator.dart, #3589.
/// Consumes the map generated by {@link _createInputPropertiesMap} to codegen
/// setters.
List<String> _generateSetters(Map<String, String> bindMap) {
var setters = [];
// TODO(kegluneq): Include types for receivers. See #886.
bindMap.forEach((setterName, type) {
if (!prop.isValid(setterName)) {
// TODO(kegluenq): Eagerly throw here once #1295 is addressed.
setters.add(prop.lazyInvalidSetter(setterName));
} else {
setters.add(''' '${prop.sanitize(setterName)}': '''
''' (o, v) => o.$setterName = v ''');
}
});
return setters;
}

View File

@ -1,37 +0,0 @@
library angular2.transform.bind_generator.transformer;
import 'dart:async';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/formatter.dart';
import 'package:angular2/src/transform/common/logging.dart' as log;
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/options.dart';
import 'package:barback/barback.dart';
import 'generator.dart';
/// Transformer responsible for reading .ng_deps.dart files and generating
/// setters from the "annotations" information in the generated
/// `registerType` calls.
///
/// These setters are registered in the same `setupReflection` function with
/// the `registerType` calls.
class BindGenerator extends Transformer {
final TransformerOptions options;
BindGenerator(this.options);
@override
bool isPrimary(AssetId id) => id.path.endsWith(DEPS_EXTENSION);
@override
Future apply(Transform transform) async {
await log.initZoned(transform, () async {
var primaryId = transform.primaryInput.id;
var reader = new AssetReader.fromTransform(transform);
var transformedCode = await createNgSettersAndGetters(reader, primaryId);
transform.addOutput(new Asset.fromString(
primaryId, formatter.format(transformedCode, uri: primaryId.path)));
});
}
}

View File

@ -1,76 +0,0 @@
library angular2.transform.common.parser;
import 'dart:async';
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/names.dart';
import 'package:barback/barback.dart';
import 'registered_type.dart';
export 'registered_type.dart';
/// The contents of a `.ng_deps.dart` file.
class NgDeps {
final String code;
final List<ImportDirective> imports = [];
final List<ExportDirective> exports = [];
final List<RegisteredType> registeredTypes = [];
LibraryDirective lib = null;
FunctionDeclaration setupMethod = null;
static _ParseNgDepsVisitor _visitor = new _ParseNgDepsVisitor();
/// Parses only the `.ng_deps.dart` file represented by `id`.
static Future<NgDeps> parse(AssetReader reader, AssetId id) async {
if (!(await reader.hasInput(id))) return null;
var ngDeps = new NgDeps(await reader.readAsString(id));
_visitor.ngDeps = ngDeps;
parseCompilationUnit(ngDeps.code, name: id.path).accept(_visitor);
return ngDeps;
}
NgDeps(this.code);
}
class _ParseNgDepsVisitor extends Object with RecursiveAstVisitor<Object> {
NgDeps ngDeps = null;
@override
Object visitLibraryDirective(LibraryDirective node) {
ngDeps.lib = node;
return null;
}
@override
Object visitImportDirective(ImportDirective node) {
ngDeps.imports.add(node);
return super.visitImportDirective(node);
}
@override
Object visitExportDirective(ExportDirective node) {
ngDeps.exports.add(node);
return super.visitExportDirective(node);
}
@override
Object visitFunctionDeclaration(FunctionDeclaration node) {
if ('${node.name}' == SETUP_METHOD_NAME) {
ngDeps.setupMethod = node;
}
return super.visitFunctionDeclaration(node);
}
@override
Object visitMethodInvocation(MethodInvocation node) {
var isRegisterType = '${node.methodName}' == REGISTER_TYPE_METHOD_NAME;
if (isRegisterType) {
ngDeps.registeredTypes.add(new RegisteredType.fromMethodInvocation(node));
}
return super.visitMethodInvocation(node);
}
}

View File

@ -1,91 +0,0 @@
library angular2.transform.common.registered_type;
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/names.dart';
/// A call to `Reflector#registerType` generated by `DirectiveProcessor`.
class RegisteredType {
/// The type registered by this call.
final Identifier typeName;
/// The actual call to `Reflector#registerType`.
final MethodInvocation registerMethod;
/// The `ReflectionInfo` [InstanceCreationExpression]
final InstanceCreationExpression reflectionInfoCreate;
/// The factory method registered.
final Expression factoryFn;
/// The parameters registered.
final Expression parameters;
/// The annotations registered.
final Expression annotations;
/// The property metadata registered.
final Expression propMetadata;
RegisteredType._(
this.typeName,
this.registerMethod,
this.reflectionInfoCreate,
this.factoryFn,
this.parameters,
this.annotations,
this.propMetadata);
/// Creates a {@link RegisteredType} given a {@link MethodInvocation} node representing
/// a call to `registerType`.
factory RegisteredType.fromMethodInvocation(MethodInvocation registerMethod) {
var visitor = new _ParseRegisterTypeVisitor();
registerMethod.accept(visitor);
return new RegisteredType._(
visitor.typeName,
registerMethod,
visitor.info,
visitor.factoryFn,
visitor.parameters,
visitor.annotations,
visitor.propMetadata);
}
}
class _ParseRegisterTypeVisitor extends Object
with RecursiveAstVisitor<Object> {
Identifier typeName;
InstanceCreationExpression info;
Expression factoryFn;
Expression parameters;
Expression annotations;
Expression propMetadata;
@override
Object visitMethodInvocation(MethodInvocation node) {
assert('${node.methodName}' == REGISTER_TYPE_METHOD_NAME);
// The first argument to a `registerType` call is the type.
typeName = node.argumentList.arguments[0] is Identifier
? node.argumentList.arguments[0]
: null;
// The second argument to a `registerType` call is the `ReflectionInfo`
// object creation.
info = node.argumentList.arguments[1] as InstanceCreationExpression;
var args = info.argumentList.arguments;
for (int i = 0; i < args.length; i++) {
var arg = args[i];
if (i == 0) {
annotations = arg;
} else if (i == 1) {
parameters = arg;
} else if (i == 2) {
factoryFn = arg;
} else if (i == 4) {
propMetadata = arg;
}
}
return null;
}
}

View File

@ -3,7 +3,6 @@ library angular2.src.transform.transformer;
import 'package:barback/barback.dart';
import 'package:dart_style/dart_style.dart';
import 'bind_generator/transformer.dart';
import 'common/formatter.dart' as formatter;
import 'common/options.dart';
import 'common/options_reader.dart';
@ -36,7 +35,6 @@ class AngularTransformerGroup extends TransformerGroup {
[new ReflectionRemover(options)],
[new DirectiveProcessor(options)],
[new DirectiveMetadataLinker()],
[new BindGenerator(options)],
[
new TemplateCompiler(options),
new StylesheetCompiler(),

View File

@ -1,58 +0,0 @@
library angular2.test.transform.bind_generator.all_tests;
import 'package:barback/barback.dart';
import 'package:angular2/src/transform/bind_generator/generator.dart';
import 'package:dart_style/dart_style.dart';
import 'package:guinness/guinness.dart';
import '../common/read_file.dart';
var formatter = new DartFormatter();
main() => allTests();
void allTests() {
var reader = new TestAssetReader();
it('should generate setters for queries defined in the class annotation.',
() async {
var inputPath = 'queries_class_annotation_files/bar.ng_deps.dart';
var expected =
_readFile('queries_class_annotation_files/expected/bar.ng_deps.dart');
var output = formatter
.format(await createNgSettersAndGetters(reader, _assetId(inputPath)));
expect(output).toEqual(expected);
});
it('should generate setters for queries defined via prop annotations.',
() async {
var inputPath = 'queries_prop_annotations_files/bar.ng_deps.dart';
var expected =
_readFile('queries_prop_annotations_files/expected/bar.ng_deps.dart');
var output = formatter
.format(await createNgSettersAndGetters(reader, _assetId(inputPath)));
expect(output).toEqual(expected);
});
it('should gracefully handle const objects as prop annotations.', () async {
var inputPath = 'queries_override_annotation_files/bar.ng_deps.dart';
var expected = formatter.format(_readFile(
'queries_override_annotation_files/expected/bar.ng_deps.dart'));
var output = formatter
.format(await createNgSettersAndGetters(reader, _assetId(inputPath)));
expect(output).toEqual(expected);
});
}
AssetId _assetId(String path) => new AssetId('a', 'bind_generator/$path');
String _readFile(String path) {
var code = readFile('bind_generator/$path');
if (path.endsWith('.dart')) {
code = formatter.format(code);
}
return code;
}

View File

@ -1,18 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(const [
const Directive(
selector: '[tool-tip]',
queries: const {'queryField': const ContentChild('child')})
], const [], () => new ToolTip()));
}

View File

@ -1,19 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(const [
const Directive(
selector: '[tool-tip]',
queries: const {'queryField': const ContentChild('child')})
], const [], () => new ToolTip()))
..registerSetters({'queryField': (o, v) => o.queryField = v});
}

View File

@ -1,21 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(
const [const Directive(selector: '[tool-tip]')],
const [],
() => new ToolTip(),
null,
const {
'queryField': const [override]
}));
}

View File

@ -1,21 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(
const [const Directive(selector: '[tool-tip]')],
const [],
() => new ToolTip(),
null,
const {
'queryField': const [override]
}));
}

View File

@ -1,21 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(
const [const Directive(selector: '[tool-tip]')],
const [],
() => new ToolTip(),
null,
const {
'queryField': const [const ContentChild('child')]
}));
}

View File

@ -1,22 +0,0 @@
library bar.ng_deps.dart;
import 'bar.dart';
import 'package:angular2/src/core/metadata.dart';
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(
ToolTip,
new ReflectionInfo(
const [const Directive(selector: '[tool-tip]')],
const [],
() => new ToolTip(),
null,
const {
'queryField': const [const ContentChild('child')]
}))
..registerSetters({'queryField': (o, v) => o.queryField = v});
}

View File

@ -8,7 +8,6 @@ import 'common/async_string_writer_tests.dart' as asyncStringWriter;
import 'common/code/ng_deps_code_tests.dart' as ngDepsCode;
import 'common/ng_meta_test.dart' as ngMetaTest;
import 'common/url_resolver_tests.dart' as urlResolver;
import 'bind_generator/all_tests.dart' as bindGenerator;
import 'deferred_rewriter/all_tests.dart' as deferredRewriter;
import 'directive_metadata_linker/all_tests.dart' as directiveMeta;
import 'directive_processor/all_tests.dart' as directiveProcessor;
@ -23,7 +22,6 @@ main() {
describe('AsyncStringWriter', asyncStringWriter.allTests);
describe('NgDepsCode', ngDepsCode.allTests);
describe('NgMeta', ngMetaTest.allTests);
describe('Bind Generator', bindGenerator.allTests);
describe('Directive Metadata Linker', directiveMeta.allTests);
describe('Directive Processor', directiveProcessor.allTests);
describe('Inliner For Test', inliner.allTests);