diff --git a/packages/compiler-cli/integrationtest/src/basic.ts b/packages/compiler-cli/integrationtest/src/basic.ts index 02048676dd..49f9998b2b 100644 --- a/packages/compiler-cli/integrationtest/src/basic.ts +++ b/packages/compiler-cli/integrationtest/src/basic.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {NgFor, NgIf} from '@angular/common'; import {Component, Inject, LOCALE_ID, TRANSLATIONS_FORMAT} from '@angular/core'; diff --git a/packages/compiler-cli/integrationtest/src/bootstrap.ts b/packages/compiler-cli/integrationtest/src/bootstrap.ts index d8c8a50674..edbdbf6426 100644 --- a/packages/compiler-cli/integrationtest/src/bootstrap.ts +++ b/packages/compiler-cli/integrationtest/src/bootstrap.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {platformBrowser} from '@angular/platform-browser'; import {BasicComp} from './basic'; import {MainModuleNgFactory} from './module.ngfactory'; diff --git a/packages/compiler-cli/integrationtest/src/module_fixtures.ts b/packages/compiler-cli/integrationtest/src/module_fixtures.ts index febffef814..18fe662ac1 100644 --- a/packages/compiler-cli/integrationtest/src/module_fixtures.ts +++ b/packages/compiler-cli/integrationtest/src/module_fixtures.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {LowerCasePipe, NgIf} from '@angular/common'; -import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, ComponentFactoryResolver, Directive, Inject, Injectable, InjectionToken, Input, ModuleWithProviders, NgModule, Pipe} from '@angular/core'; +import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, Directive, Injectable, InjectionToken, Input, NgModule, Pipe} from '@angular/core'; @Injectable() export class SomeService { diff --git a/packages/compiler-cli/integrationtest/src/queries.ts b/packages/compiler-cli/integrationtest/src/queries.ts index e031e8e9b0..8b85bfd9fa 100644 --- a/packages/compiler-cli/integrationtest/src/queries.ts +++ b/packages/compiler-cli/integrationtest/src/queries.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {NgFor} from '@angular/common'; import {Component, Directive, QueryList, ViewChild, ViewChildren} from '@angular/core'; @Component({selector: 'comp-for-child-query', template: 'child'}) diff --git a/packages/compiler-cli/integrationtest/test/query_spec.ts b/packages/compiler-cli/integrationtest/test/query_spec.ts index 20b0c471ea..1d66f58c89 100644 --- a/packages/compiler-cli/integrationtest/test/query_spec.ts +++ b/packages/compiler-cli/integrationtest/test/query_spec.ts @@ -7,7 +7,7 @@ */ import './init'; -import {DebugElement, QueryList} from '@angular/core'; +import {QueryList} from '@angular/core'; import {By} from '@angular/platform-browser'; import {CompForChildQuery, CompWithChildQuery} from '../src/queries'; import {createComponent} from './util'; @@ -16,7 +16,7 @@ describe('child queries', () => { it('should support compiling child queries', () => { const childQueryCompFixture = createComponent(CompWithChildQuery); const debugElement = childQueryCompFixture.debugElement; - const compWithChildren = debugElement.query(By.directive(CompWithChildQuery)); + debugElement.query(By.directive(CompWithChildQuery)); expect(childQueryCompFixture.componentInstance.child).toBeDefined(); expect(childQueryCompFixture.componentInstance.child instanceof CompForChildQuery).toBe(true); @@ -25,7 +25,7 @@ describe('child queries', () => { it('should support compiling children queries', () => { const childQueryCompFixture = createComponent(CompWithChildQuery); const debugElement = childQueryCompFixture.debugElement; - const compWithChildren = debugElement.query(By.directive(CompWithChildQuery)); + debugElement.query(By.directive(CompWithChildQuery)); childQueryCompFixture.detectChanges(); diff --git a/packages/compiler-cli/integrationtest/test/test_ngtools_api.ts b/packages/compiler-cli/integrationtest/test/test_ngtools_api.ts index 5b81e6c075..c6db11a5f1 100644 --- a/packages/compiler-cli/integrationtest/test/test_ngtools_api.ts +++ b/packages/compiler-cli/integrationtest/test/test_ngtools_api.ts @@ -15,7 +15,7 @@ import * as path from 'path'; import * as ts from 'typescript'; import * as assert from 'assert'; import {tsc} from '@angular/tsc-wrapped/src/tsc'; -import {AngularCompilerOptions, CodeGenerator, CompilerHostContext, NodeCompilerHostContext, __NGTOOLS_PRIVATE_API_2} from '@angular/compiler-cli'; +import {NodeCompilerHostContext, __NGTOOLS_PRIVATE_API_2} from '@angular/compiler-cli'; const glob = require('glob'); diff --git a/packages/compiler-cli/integrationtest/test/util.ts b/packages/compiler-cli/integrationtest/test/util.ts index e08d3ea44b..15a3a6af21 100644 --- a/packages/compiler-cli/integrationtest/test/util.ts +++ b/packages/compiler-cli/integrationtest/test/util.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {NgModuleFactory, NgModuleRef} from '@angular/core'; +import {NgModuleRef} from '@angular/core'; import {ComponentFixture} from '@angular/core/testing'; import {platformServer} from '@angular/platform-server'; diff --git a/packages/compiler-cli/integrationtest/tsconfig-build.json b/packages/compiler-cli/integrationtest/tsconfig-build.json index 492b26a341..3c6446826f 100644 --- a/packages/compiler-cli/integrationtest/tsconfig-build.json +++ b/packages/compiler-cli/integrationtest/tsconfig-build.json @@ -16,7 +16,8 @@ "lib": ["es6", "dom"], "baseUrl": ".", // Prevent scanning up the directory tree for types - "typeRoots": ["node_modules/@types"] + "typeRoots": ["node_modules/@types"], + "noUnusedLocals": true }, "files": [ diff --git a/packages/compiler/src/jit/compiler.ts b/packages/compiler/src/jit/compiler.ts index 04e50f0be2..f8bce1ec83 100644 --- a/packages/compiler/src/jit/compiler.ts +++ b/packages/compiler/src/jit/compiler.ts @@ -258,15 +258,17 @@ export class JitCompiler implements Compiler { usedPipes); const statements = stylesCompileResult.componentStylesheet.statements.concat(compileResult.statements); + let viewClassAndRendererTypeVars = compMeta.isHost ? + [compileResult.viewClassVar] : + [compileResult.viewClassVar, compileResult.rendererTypeVar]; let viewClass: any; let rendererType: any; if (!this._compilerConfig.useJit) { - [viewClass, rendererType] = interpretStatements( - statements, [compileResult.viewClassVar, compileResult.rendererTypeVar]); + [viewClass, rendererType] = interpretStatements(statements, viewClassAndRendererTypeVars); } else { [viewClass, rendererType] = jitStatements( templateJitUrl(template.ngModule.type, template.compMeta), statements, - [compileResult.viewClassVar, compileResult.rendererTypeVar]); + viewClassAndRendererTypeVars); } template.compiled(viewClass, rendererType); } diff --git a/packages/compiler/src/output/output_ast.ts b/packages/compiler/src/output/output_ast.ts index 69929ff74f..5af791d49c 100644 --- a/packages/compiler/src/output/output_ast.ts +++ b/packages/compiler/src/output/output_ast.ts @@ -1001,6 +1001,28 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor } } +export function findReadVarNames(stmts: Statement[]): Set { + const visitor = new _ReadVarVisitor(); + visitor.visitAllStatements(stmts, null); + return visitor.varNames; +} + +class _ReadVarVisitor extends RecursiveAstVisitor { + varNames = new Set(); + visitDeclareFunctionStmt(stmt: DeclareFunctionStmt, context: any): any { + // Don't descend into nested functions + return stmt; + } + visitDeclareClassStmt(stmt: ClassStmt, context: any): any { + // Don't descend into nested classes + return stmt; + } + visitReadVarExpr(ast: ReadVarExpr, context: any): any { + this.varNames.add(ast.name); + return null; + } +} + export function applySourceSpanToStatementIfNeeded( stmt: Statement, sourceSpan: ParseSourceSpan): Statement { if (!sourceSpan) { diff --git a/packages/compiler/src/view_compiler/view_compiler.ts b/packages/compiler/src/view_compiler/view_compiler.ts index 9db0332da4..be445c5b48 100644 --- a/packages/compiler/src/view_compiler/view_compiler.ts +++ b/packages/compiler/src/view_compiler/view_compiler.ts @@ -44,24 +44,29 @@ export class ViewCompiler { const statements: o.Statement[] = []; - const customRenderData: o.LiteralMapEntry[] = []; - if (component.template.animations && component.template.animations.length) { - customRenderData.push(new o.LiteralMapEntry( - 'animation', convertValueToOutputAst(component.template.animations), true)); - } + let renderComponentVarName: string; + if (!component.isHost) { + const customRenderData: o.LiteralMapEntry[] = []; + if (component.template.animations && component.template.animations.length) { + customRenderData.push(new o.LiteralMapEntry( + 'animation', convertValueToOutputAst(component.template.animations), true)); + } - const renderComponentVar = o.variable(rendererTypeName(component.type.reference)); - statements.push( - renderComponentVar - .set(o.importExpr(createIdentifier(Identifiers.createRendererType2)).callFn([ - new o.LiteralMapExpr([ - new o.LiteralMapEntry('encapsulation', o.literal(component.template.encapsulation)), - new o.LiteralMapEntry('styles', styles), - new o.LiteralMapEntry('data', new o.LiteralMapExpr(customRenderData)) - ]) - ])) - .toDeclStmt( - o.importType(createIdentifier(Identifiers.RendererType2)), [o.StmtModifier.Final])); + const renderComponentVar = o.variable(rendererTypeName(component.type.reference)); + renderComponentVarName = renderComponentVar.name; + statements.push( + renderComponentVar + .set(o.importExpr(createIdentifier(Identifiers.createRendererType2)) + .callFn([new o.LiteralMapExpr([ + new o.LiteralMapEntry( + 'encapsulation', o.literal(component.template.encapsulation)), + new o.LiteralMapEntry('styles', styles), + new o.LiteralMapEntry('data', new o.LiteralMapExpr(customRenderData)) + ])])) + .toDeclStmt( + o.importType(createIdentifier(Identifiers.RendererType2)), + [o.StmtModifier.Final])); + } const viewBuilderFactory = (parent: ViewBuilder): ViewBuilder => { const embeddedViewIndex = embeddedViewCount++; @@ -74,7 +79,7 @@ export class ViewCompiler { statements.push(...visitor.build()); - return new ViewCompileResult(statements, visitor.viewName, renderComponentVar.name); + return new ViewCompileResult(statements, visitor.viewName, renderComponentVarName); } } @@ -204,7 +209,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { let updateFn: o.Expression; if (updateStmts.length > 0) { const preStmts: o.Statement[] = []; - if (!this.component.isHost) { + if (!this.component.isHost && o.findReadVarNames(updateStmts).has(COMP_VAR.name)) { preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType)); } updateFn = o.fn( @@ -835,7 +840,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { if (handleEventStmts.length > 0) { const preStmts: o.Statement[] = [ALLOW_DEFAULT_VAR.set(o.literal(true)).toDeclStmt(o.BOOL_TYPE)]; - if (!this.component.isHost) { + if (!this.component.isHost && o.findReadVarNames(handleEventStmts).has(COMP_VAR.name)) { preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType)); } handleEventFn = o.fn(