From 0aff6b7773f8359bc93c78238e1a61aff11c9f56 Mon Sep 17 00:00:00 2001 From: Thomas Mair Date: Wed, 22 Feb 2017 20:45:25 +0100 Subject: [PATCH] fix: allow structural directives for `no-unused-css` rule fixes #249 --- src/noUnusedCssRule.ts | 5 +++- test/noUnusedCssRule.spec.ts | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/noUnusedCssRule.ts b/src/noUnusedCssRule.ts index afebf5f88..e05746398 100644 --- a/src/noUnusedCssRule.ts +++ b/src/noUnusedCssRule.ts @@ -8,6 +8,7 @@ import {VERSION} from '@angular/core'; import { TemplateAst, ElementAst, + EmbeddedTemplateAst, PropertyBindingType } from '@angular/compiler'; import {parseTemplate} from './angular/templates/templateParser'; @@ -131,7 +132,9 @@ class ElementFilterVisitor extends BasicTemplateAstVisitor { const strategy = strategies[s]; return !selectorTypes[s] || !strategy(ast); }) && (ast.children || []) - .every(c => ast instanceof ElementAst && this.shouldVisit(c, strategies, selectorTypes)); + .every(c => ast instanceof ElementAst && this.shouldVisit(c, strategies, selectorTypes) + || ast instanceof EmbeddedTemplateAst && + (ast.children || []).every(c => this.shouldVisit(c, strategies, selectorTypes))); } } diff --git a/test/noUnusedCssRule.spec.ts b/test/noUnusedCssRule.spec.ts index 4ec14b8d1..b91366be8 100644 --- a/test/noUnusedCssRule.spec.ts +++ b/test/noUnusedCssRule.spec.ts @@ -94,6 +94,30 @@ describe('no-unused-css', () => { assertSuccess('no-unused-css', source); }); + it('should succeed for structural directives when selector matches', () => { + let source = ` + @Component({ + selector: 'foobar', + template: \`
+
+

{{ foo }}

+
+
\`, + styles: [ + \` + div h1 { + color: red; + } + \` + ] + }) + class Test { + bar: number; + }`; + + assertSuccess('no-unused-css', source); + }); + describe('multiple styles', () => { it('should succeed when having valid complex selector', () => { @@ -309,6 +333,39 @@ describe('no-unused-css', () => { }); }); + it('should fail for structural directives when selector does not match', () => { + let source = ` + @Component({ + selector: 'foobar', + template: \`
+
+

{{ foo }}

+
+
\`, + styles: [ + \` + div h1#header { + color: red; + } + \` + ] + }) + class Test { + bar: number; + }`; + assertFailure('no-unused-css', source, { + message: 'Unused styles', + startPosition: { + line: 10, + character: 14 + }, + endPosition: { + line: 12, + character: 14 + } + }); + }); + describe('class setter', () => { it('should succeed when having valid complex selector', () => {