Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
refactor(compiler): Remove isExportedVar from EmitterVisitorContext.
Browse files Browse the repository at this point in the history
This method is not used, and we can clean up some code to generate this list.

PiperOrigin-RevId: 216244158
  • Loading branch information
alorenzen committed Oct 8, 2018
1 parent ae82fc1 commit 47c4dad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
9 changes: 4 additions & 5 deletions _tests/test/compiler/output/dart_emitter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ void main() {
emitter = DartEmitter();
someVar = o.variable('someVar');
});
String emitStmt(o.Statement stmt, [List<String> exportedVars]) {
exportedVars ??= [];
return emitter.emitStatements(someModuleUrl, [stmt], exportedVars, {});
String emitStmt(o.Statement stmt) {
return emitter.emitStatements(someModuleUrl, [stmt], {});
}

test('should declare variables', () {
Expand All @@ -50,8 +49,8 @@ void main() {
o.BuiltinType(o.BuiltinTypeName.Int, [o.TypeModifier.Const])))
.toDeclStmt(null, [o.StmtModifier.Final])),
'final int someVar = 1;');
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']),
'var someVar = 1;');
expect(
emitStmt(someVar.set(o.literal(1)).toDeclStmt()), 'var someVar = 1;');
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INT_TYPE)),
'int someVar = 1;');
});
Expand Down
35 changes: 12 additions & 23 deletions angular/lib/src/compiler/offline_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,31 @@ class OfflineCompiler {
throw StateError('No components nor injectorModules given');
}
var statements = <o.Statement>[];
var exportedVars = <String>[];
for (var componentWithDirs in artifacts.components) {
CompileDirectiveMetadata compMeta = componentWithDirs.component;
_assertComponent(compMeta);

// Compile Component View and Embedded templates.
var compViewFactoryVar = _compileComponent(
_compileComponent(
compMeta,
componentWithDirs.directives,
componentWithDirs.directiveTypes,
componentWithDirs.pipes,
statements,
_deferredModules);
exportedVars.add(compViewFactoryVar);

String hostViewFactoryVar = _compileComponentHost(compMeta, statements);

var compFactoryVar =
_registerComponentFactory(statements, compMeta, hostViewFactoryVar);
exportedVars.add(compFactoryVar);
_registerComponentFactory(statements, compMeta, hostViewFactoryVar);
}

for (CompileDirectiveMetadata directive in artifacts.directives) {
if (!directive.requiresDirectiveChangeDetector) continue;
var changeDetectorClassName = _compileDirective(directive, statements);
exportedVars.add(changeDetectorClassName);
_compileDirective(directive, statements);
}

String moduleUrl = _moduleUrlFor(artifacts);
return _createSourceModule(
moduleUrl, statements, exportedVars, _deferredModules);
return _createSourceModule(moduleUrl, statements, _deferredModules);
}

List<SourceModule> compileStylesheet(String stylesheetUrl, String cssText) {
Expand All @@ -129,9 +123,9 @@ class OfflineCompiler {
_styleCompiler.compileStylesheet(stylesheetUrl, cssText, true);
return [
_createSourceModule(stylesModuleUrl(stylesheetUrl, false),
plainStyles.statements, [plainStyles.stylesVar], _deferredModules),
plainStyles.statements, _deferredModules),
_createSourceModule(stylesModuleUrl(stylesheetUrl, true),
shimStyles.statements, [shimStyles.stylesVar], _deferredModules)
shimStyles.statements, _deferredModules)
];
}

Expand Down Expand Up @@ -179,7 +173,7 @@ class OfflineCompiler {
// ComponentFactory<Foo> FooNgFactory get _FooNgFactory;
//
// This is referenced in `initReflector/METADATA` and by user-code.
String _registerComponentFactory(List<o.Statement> statements,
void _registerComponentFactory(List<o.Statement> statements,
CompileDirectiveMetadata compMeta, String hostViewFactoryVar) {
var compFactoryVar = '${compMeta.type.name}NgFactory';
var factoryType = [o.importType(compMeta.type)];
Expand Down Expand Up @@ -212,25 +206,20 @@ class OfflineCompiler {
),
).toGetter('$compFactoryVar'),
);
return compFactoryVar;
}

String _compileDirective(
void _compileDirective(
CompileDirectiveMetadata directive, List<o.Statement> statements) {
DirectiveCompiler comp =
DirectiveCompiler(directive, _templateParser.schemaRegistry);
DirectiveCompileResult res = comp.compile();
statements.addAll(res.statements);
return comp.changeDetectorClassName;
}

SourceModule _createSourceModule(
String moduleUrl,
List<o.Statement> statements,
List<String> exportedVars,
Map<String, String> deferredModules) {
String sourceCode = _outputEmitter.emitStatements(
moduleUrl, statements, exportedVars, deferredModules);
SourceModule _createSourceModule(String moduleUrl,
List<o.Statement> statements, Map<String, String> deferredModules) {
String sourceCode =
_outputEmitter.emitStatements(moduleUrl, statements, deferredModules);
return SourceModule(moduleUrl, sourceCode, deferredModules);
}

Expand Down
14 changes: 4 additions & 10 deletions angular/lib/src/compiler/output/abstract_emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final catchStackVar = o.variable('stack');

abstract class OutputEmitter {
String emitStatements(String moduleUrl, List<o.Statement> stmts,
List<String> exportedVars, Map<String, String> deferredModules);
Map<String, String> deferredModules);
}

class _EmittedLine {
Expand All @@ -17,7 +17,6 @@ class _EmittedLine {

class EmitterVisitorContext {
final Map<String, String> deferredModules;
final List<String> _exportedVars;
int _indent;
int _outputPos = 0;
// Current method being emitted. Allows expressions access to method
Expand All @@ -28,12 +27,11 @@ class EmitterVisitorContext {
final List<_EmittedLine> _lines;
final List<o.ClassStmt> _classes = [];

static EmitterVisitorContext createRoot(
List<String> exportedVars, Map<String, String> deferredModules) {
return EmitterVisitorContext(exportedVars, 0, deferredModules);
static EmitterVisitorContext createRoot(Map<String, String> deferredModules) {
return EmitterVisitorContext(0, deferredModules);
}

EmitterVisitorContext(this._exportedVars, this._indent, this.deferredModules)
EmitterVisitorContext(this._indent, this.deferredModules)
: this._lines = [_EmittedLine(_indent)];

_EmittedLine get _currentLine {
Expand All @@ -42,10 +40,6 @@ class EmitterVisitorContext {

int get currentLineLength => _currentLine.indent + _outputPos;

bool isExportedVar(String varName) {
return !identical(_exportedVars.indexOf(varName), -1);
}

void println([String lastPart = '']) {
print(lastPart, true);
_outputPos += lastPart.length;
Expand Down
6 changes: 3 additions & 3 deletions angular/lib/src/compiler/output/dart_emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _METADATA_MAP_VAR = '_METADATA';
String debugOutputAstAsDart(
dynamic /* o . Statement | o . Expression | o . Type | List < dynamic > */ ast) {
var converter = _DartEmitterVisitor(_debugModuleUrl);
var ctx = EmitterVisitorContext.createRoot([], {});
var ctx = EmitterVisitorContext.createRoot({});
List<dynamic> asts;
if (ast is! List) {
asts = [ast];
Expand All @@ -36,12 +36,12 @@ String debugOutputAstAsDart(
class DartEmitter implements OutputEmitter {
@override
String emitStatements(String moduleUrl, List<o.Statement> stmts,
List<String> exportedVars, Map<String, String> deferredModules) {
Map<String, String> deferredModules) {
final srcParts = <String>[];
// Note: We are not creating a library here as Dart does not need it.
// Dart analyzer might complain about it though.
final converter = _DartEmitterVisitor(moduleUrl);
final ctx = EmitterVisitorContext.createRoot(exportedVars, deferredModules);
final ctx = EmitterVisitorContext.createRoot(deferredModules);
converter.visitAllStatements(stmts, ctx);
converter.importsWithPrefixes.forEach((importedModuleUrl, prefix) {
String importPath = getImportModulePath(moduleUrl, importedModuleUrl);
Expand Down

0 comments on commit 47c4dad

Please sign in to comment.