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 '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/logging.dart';
|
||||||
import 'package:angular2/src/transform/common/names.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:barback/barback.dart';
|
||||||
import 'package:code_transformers/assets.dart';
|
import 'package:code_transformers/assets.dart';
|
||||||
import 'package:dart_style/dart_style.dart';
|
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
|
|
||||||
Future<String> linkNgDeps(Transform transform, String code, String path) async {
|
Future<String> linkNgDeps(AssetReader reader, AssetId entryPoint) async {
|
||||||
var commentIdx = code.lastIndexOf('//');
|
var parser = new Parser(reader);
|
||||||
if (commentIdx < 0) return code;
|
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 =
|
var allDeps = ngDeps.imports.toList()..addAll(ngDeps.exports);
|
||||||
new StringBuffer(code.substring(0, ngData.importOffset));
|
var depList = await _processNgImports(
|
||||||
StringBuffer declarationBuf = new StringBuffer(
|
reader, entryPoint, allDeps.map((node) => node.uri.stringValue));
|
||||||
code.substring(ngData.importOffset, ngData.registerOffset));
|
|
||||||
String tail = code.substring(ngData.registerOffset, commentIdx);
|
|
||||||
|
|
||||||
var ngDeps = await _processNgImports(transform, ngData.imports);
|
if (depList.isEmpty) return ngDeps.code;
|
||||||
|
|
||||||
for (var i = 0; i < ngDeps.length; ++i) {
|
var importBuf = new StringBuffer();
|
||||||
importBuf.write('import \'${ngDeps[i]}\' as i${i};');
|
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});');
|
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) =>
|
String _toDepsUri(String importUri) =>
|
||||||
|
@ -40,17 +50,16 @@ bool _isNotDartImport(String importUri) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<String>> _processNgImports(
|
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>[];
|
var retVal = <String>[];
|
||||||
|
|
||||||
return Future
|
return Future
|
||||||
.wait(imports.where(_isNotDartImport).map(_toDepsUri).map((ngDepsUri) {
|
.wait(imports.where(_isNotDartImport).map(_toDepsUri).map((ngDepsUri) {
|
||||||
var importAsset = uriToAssetId(
|
var importAsset =
|
||||||
transform.primaryInput.id, ngDepsUri, logger, null /* span */);
|
uriToAssetId(entryPoint, ngDepsUri, logger, null /* span */);
|
||||||
return transform.hasInput(importAsset).then((hasInput) {
|
if (importAsset == entryPoint) return nullFuture;
|
||||||
if (hasInput) {
|
return reader.hasInput(importAsset).then((hasInput) {
|
||||||
retVal.add(ngDepsUri);
|
if (hasInput) retVal.add(ngDepsUri);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
})).then((_) => retVal);
|
})).then((_) => retVal);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ library angular2.transform.directive_linker.transformer;
|
||||||
|
|
||||||
import 'dart:async';
|
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/formatter.dart';
|
||||||
import 'package:angular2/src/transform/common/logging.dart' as log;
|
import 'package:angular2/src/transform/common/logging.dart' as log;
|
||||||
import 'package:angular2/src/transform/common/names.dart';
|
import 'package:angular2/src/transform/common/names.dart';
|
||||||
|
@ -27,12 +28,11 @@ class DirectiveLinker extends Transformer {
|
||||||
log.init(transform);
|
log.init(transform);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var assetCode = await transform.primaryInput.readAsString();
|
var assetId = transform.primaryInput.id;
|
||||||
var assetPath = transform.primaryInput.id.path;
|
var transformedCode =
|
||||||
var transformedCode = await linkNgDeps(transform, assetCode, assetPath);
|
await linkNgDeps(new AssetReader.fromTransform(transform), assetId);
|
||||||
var formattedCode = formatter.format(transformedCode, uri: assetPath);
|
var formattedCode = formatter.format(transformedCode, uri: assetId.path);
|
||||||
transform.addOutput(
|
transform.addOutput(new Asset.fromString(assetId, formattedCode));
|
||||||
new Asset.fromString(transform.primaryInput.id, formattedCode));
|
|
||||||
} catch (ex, stackTrace) {
|
} catch (ex, stackTrace) {
|
||||||
log.logger.error('Linking ng directives failed.\n'
|
log.logger.error('Linking ng directives failed.\n'
|
||||||
'Exception: $ex\n'
|
'Exception: $ex\n'
|
||||||
|
|
|
@ -4,7 +4,6 @@ import 'package:analyzer/analyzer.dart';
|
||||||
import 'package:analyzer/src/generated/java_core.dart';
|
import 'package:analyzer/src/generated/java_core.dart';
|
||||||
import 'package:angular2/src/transform/common/logging.dart';
|
import 'package:angular2/src/transform/common/logging.dart';
|
||||||
import 'package:angular2/src/transform/common/names.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:angular2/src/transform/common/visitor_mixin.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
|
|
||||||
|
@ -42,7 +41,6 @@ class CreateNgDepsVisitor extends Object
|
||||||
final FactoryTransformVisitor _factoryVisitor;
|
final FactoryTransformVisitor _factoryVisitor;
|
||||||
final ParameterTransformVisitor _paramsVisitor;
|
final ParameterTransformVisitor _paramsVisitor;
|
||||||
final AnnotationsTransformVisitor _metaVisitor;
|
final AnnotationsTransformVisitor _metaVisitor;
|
||||||
final NgData _ngData = new NgData();
|
|
||||||
|
|
||||||
/// The path to the file which we are parsing.
|
/// The path to the file which we are parsing.
|
||||||
final String importPath;
|
final String importPath;
|
||||||
|
@ -73,19 +71,16 @@ class CreateNgDepsVisitor extends Object
|
||||||
_writeImport();
|
_writeImport();
|
||||||
wroteImport = true;
|
wroteImport = true;
|
||||||
}
|
}
|
||||||
_ngData.imports.add(node.uri.stringValue);
|
|
||||||
return node.accept(_copyVisitor);
|
return node.accept(_copyVisitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Object visitExportDirective(ExportDirective node) {
|
Object visitExportDirective(ExportDirective node) {
|
||||||
_ngData.imports.add(node.uri.stringValue);
|
|
||||||
return node.accept(_copyVisitor);
|
return node.accept(_copyVisitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _openFunctionWrapper() {
|
void _openFunctionWrapper() {
|
||||||
// TODO(kegluneq): Use a [PrintWriter] with a length getter.
|
// TODO(kegluneq): Use a [PrintWriter] with a length getter.
|
||||||
_ngData.importOffset = writer.toString().length;
|
|
||||||
writer.print('bool _visited = false;'
|
writer.print('bool _visited = false;'
|
||||||
'void ${SETUP_METHOD_NAME}(${REFLECTOR_VAR_NAME}) {'
|
'void ${SETUP_METHOD_NAME}(${REFLECTOR_VAR_NAME}) {'
|
||||||
'if (_visited) return; _visited = true;');
|
'if (_visited) return; _visited = true;');
|
||||||
|
@ -95,10 +90,7 @@ class CreateNgDepsVisitor extends Object
|
||||||
if (foundNgDirectives) {
|
if (foundNgDirectives) {
|
||||||
writer.print(';');
|
writer.print(';');
|
||||||
}
|
}
|
||||||
// TODO(kegluneq): Use a [PrintWriter] with a length getter.
|
|
||||||
_ngData.registerOffset = writer.toString().length;
|
|
||||||
writer.print('}');
|
writer.print('}');
|
||||||
writer.print('// ${_ngData.toJson()}');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstructorDeclaration _getCtor(ClassDeclaration node) {
|
ConstructorDeclaration _getCtor(ClassDeclaration node) {
|
||||||
|
|
|
@ -14,4 +14,4 @@ void setupReflection(reflector) {
|
||||||
'parameters': const [const [Tasty, String], const [const Inject(Salt)]],
|
'parameters': const [const [Tasty, String], const [const Inject(Salt)]],
|
||||||
'annotations': const [const Component(selector: '[soup]')]
|
'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