refactor(dart/transform): Remove `ngData`
Now that we have `Parser`, `ngData` is redundant & unnecessary.
This commit is contained in:
parent
8baedca972
commit
5d479fa0ae
|
@ -1,35 +0,0 @@
|
|||
library angular2.transform.common.ng_data;
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
const NG_DATA_VERSION = 1;
|
||||
|
||||
class NgData extends Object {
|
||||
int importOffset = 0;
|
||||
int registerOffset = 0;
|
||||
List<String> imports = [];
|
||||
|
||||
NgData();
|
||||
|
||||
factory NgData.fromJson(String json) {
|
||||
var data = JSON.decode(json);
|
||||
return new NgData()
|
||||
..importOffset = data['importOffset']
|
||||
..registerOffset = data['registerOffset']
|
||||
..imports = data['imports'];
|
||||
}
|
||||
|
||||
String toJson() {
|
||||
return JSON.encode({
|
||||
'version': NG_DATA_VERSION,
|
||||
'importOffset': importOffset,
|
||||
'registerOffset': registerOffset,
|
||||
'imports': imports
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '[NgData: ${toJson()}]';
|
||||
}
|
||||
}
|
|
@ -2,34 +2,44 @@ library angular2.transform.directive_linker.linker;
|
|||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:angular2/src/transform/common/asset_reader.dart';
|
||||
import 'package:angular2/src/transform/common/logging.dart';
|
||||
import 'package:angular2/src/transform/common/names.dart';
|
||||
import 'package:angular2/src/transform/common/ngdata.dart';
|
||||
import 'package:angular2/src/transform/common/parser.dart';
|
||||
import 'package:barback/barback.dart';
|
||||
import 'package:code_transformers/assets.dart';
|
||||
import 'package:dart_style/dart_style.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
Future<String> linkNgDeps(Transform transform, String code, String path) async {
|
||||
var commentIdx = code.lastIndexOf('//');
|
||||
if (commentIdx < 0) return code;
|
||||
Future<String> linkNgDeps(AssetReader reader, AssetId entryPoint) async {
|
||||
var parser = new Parser(reader);
|
||||
NgDeps ngDeps = await parser.parse(entryPoint);
|
||||
|
||||
var ngData = new NgData.fromJson(code.substring(commentIdx + 2));
|
||||
if (ngDeps == null) return null;
|
||||
if (ngDeps.imports.isEmpty) return ngDeps.code;
|
||||
|
||||
StringBuffer importBuf =
|
||||
new StringBuffer(code.substring(0, ngData.importOffset));
|
||||
StringBuffer declarationBuf = new StringBuffer(
|
||||
code.substring(ngData.importOffset, ngData.registerOffset));
|
||||
String tail = code.substring(ngData.registerOffset, commentIdx);
|
||||
var allDeps = ngDeps.imports.toList()..addAll(ngDeps.exports);
|
||||
var depList = await _processNgImports(
|
||||
reader, entryPoint, allDeps.map((node) => node.uri.stringValue));
|
||||
|
||||
var ngDeps = await _processNgImports(transform, ngData.imports);
|
||||
if (depList.isEmpty) return ngDeps.code;
|
||||
|
||||
for (var i = 0; i < ngDeps.length; ++i) {
|
||||
importBuf.write('import \'${ngDeps[i]}\' as i${i};');
|
||||
var importBuf = new StringBuffer();
|
||||
var declarationBuf = new StringBuffer();
|
||||
for (var i = 0; i < depList.length; ++i) {
|
||||
importBuf.write('''
|
||||
import '${depList[i]}' as i${i};
|
||||
''');
|
||||
declarationBuf.write('i${i}.${SETUP_METHOD_NAME}(${REFLECTOR_VAR_NAME});');
|
||||
}
|
||||
|
||||
return '${importBuf}${declarationBuf}${tail}';
|
||||
var code = ngDeps.code;
|
||||
var importSeamIdx = ngDeps.imports.last.end;
|
||||
var declarationSeamIdx = ngDeps.setupMethod.end - 1;
|
||||
return '${code.substring(0, importSeamIdx)}'
|
||||
'$importBuf'
|
||||
'${code.substring(importSeamIdx, declarationSeamIdx)}'
|
||||
'$declarationBuf'
|
||||
'${code.substring(declarationSeamIdx)}';
|
||||
}
|
||||
|
||||
String _toDepsUri(String importUri) =>
|
||||
|
@ -40,17 +50,16 @@ bool _isNotDartImport(String importUri) {
|
|||
}
|
||||
|
||||
Future<List<String>> _processNgImports(
|
||||
Transform transform, List<String> imports) async {
|
||||
AssetReader reader, AssetId entryPoint, Iterable<String> imports) {
|
||||
final nullFuture = new Future.value(null);
|
||||
var retVal = <String>[];
|
||||
|
||||
return Future
|
||||
.wait(imports.where(_isNotDartImport).map(_toDepsUri).map((ngDepsUri) {
|
||||
var importAsset = uriToAssetId(
|
||||
transform.primaryInput.id, ngDepsUri, logger, null /* span */);
|
||||
return transform.hasInput(importAsset).then((hasInput) {
|
||||
if (hasInput) {
|
||||
retVal.add(ngDepsUri);
|
||||
}
|
||||
var importAsset =
|
||||
uriToAssetId(entryPoint, ngDepsUri, logger, null /* span */);
|
||||
if (importAsset == entryPoint) return nullFuture;
|
||||
return reader.hasInput(importAsset).then((hasInput) {
|
||||
if (hasInput) retVal.add(ngDepsUri);
|
||||
});
|
||||
})).then((_) => retVal);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ library angular2.transform.directive_linker.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';
|
||||
|
@ -27,12 +28,11 @@ class DirectiveLinker extends Transformer {
|
|||
log.init(transform);
|
||||
|
||||
try {
|
||||
var assetCode = await transform.primaryInput.readAsString();
|
||||
var assetPath = transform.primaryInput.id.path;
|
||||
var transformedCode = await linkNgDeps(transform, assetCode, assetPath);
|
||||
var formattedCode = formatter.format(transformedCode, uri: assetPath);
|
||||
transform.addOutput(
|
||||
new Asset.fromString(transform.primaryInput.id, formattedCode));
|
||||
var assetId = transform.primaryInput.id;
|
||||
var transformedCode =
|
||||
await linkNgDeps(new AssetReader.fromTransform(transform), assetId);
|
||||
var formattedCode = formatter.format(transformedCode, uri: assetId.path);
|
||||
transform.addOutput(new Asset.fromString(assetId, formattedCode));
|
||||
} catch (ex, stackTrace) {
|
||||
log.logger.error('Linking ng directives failed.\n'
|
||||
'Exception: $ex\n'
|
||||
|
|
|
@ -4,7 +4,6 @@ import 'package:analyzer/analyzer.dart';
|
|||
import 'package:analyzer/src/generated/java_core.dart';
|
||||
import 'package:angular2/src/transform/common/logging.dart';
|
||||
import 'package:angular2/src/transform/common/names.dart';
|
||||
import 'package:angular2/src/transform/common/ngdata.dart';
|
||||
import 'package:angular2/src/transform/common/visitor_mixin.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
|
@ -42,7 +41,6 @@ class CreateNgDepsVisitor extends Object
|
|||
final FactoryTransformVisitor _factoryVisitor;
|
||||
final ParameterTransformVisitor _paramsVisitor;
|
||||
final AnnotationsTransformVisitor _metaVisitor;
|
||||
final NgData _ngData = new NgData();
|
||||
|
||||
/// The path to the file which we are parsing.
|
||||
final String importPath;
|
||||
|
@ -73,19 +71,16 @@ class CreateNgDepsVisitor extends Object
|
|||
_writeImport();
|
||||
wroteImport = true;
|
||||
}
|
||||
_ngData.imports.add(node.uri.stringValue);
|
||||
return node.accept(_copyVisitor);
|
||||
}
|
||||
|
||||
@override
|
||||
Object visitExportDirective(ExportDirective node) {
|
||||
_ngData.imports.add(node.uri.stringValue);
|
||||
return node.accept(_copyVisitor);
|
||||
}
|
||||
|
||||
void _openFunctionWrapper() {
|
||||
// TODO(kegluneq): Use a [PrintWriter] with a length getter.
|
||||
_ngData.importOffset = writer.toString().length;
|
||||
writer.print('bool _visited = false;'
|
||||
'void ${SETUP_METHOD_NAME}(${REFLECTOR_VAR_NAME}) {'
|
||||
'if (_visited) return; _visited = true;');
|
||||
|
@ -95,10 +90,7 @@ class CreateNgDepsVisitor extends Object
|
|||
if (foundNgDirectives) {
|
||||
writer.print(';');
|
||||
}
|
||||
// TODO(kegluneq): Use a [PrintWriter] with a length getter.
|
||||
_ngData.registerOffset = writer.toString().length;
|
||||
writer.print('}');
|
||||
writer.print('// ${_ngData.toJson()}');
|
||||
}
|
||||
|
||||
ConstructorDeclaration _getCtor(ClassDeclaration node) {
|
||||
|
|
|
@ -14,4 +14,4 @@ void setupReflection(reflector) {
|
|||
'parameters': const [const [Tasty, String], const [const Inject(Salt)]],
|
||||
'annotations': const [const Component(selector: '[soup]')]
|
||||
});
|
||||
} // {"version":1,"importOffset":104,"registerOffset":451,"imports":["package:angular2/src/core/annotations/annotations.dart"]}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue