-
Notifications
You must be signed in to change notification settings - Fork 234
/
noUnusedCssRule.ts
267 lines (249 loc) · 9.12 KB
/
noUnusedCssRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {Ng2Walker} from './angular/ng2Walker';
import {getComponentDecorator, isSimpleTemplateString, getDecoratorPropertyInitializer} from './util/utils';
import {BasicCssAstVisitor} from './angular/styles/basicCssAstVisitor';
import {BasicTemplateAstVisitor} from './angular/templates/basicTemplateAstVisitor';
import {VERSION} from '@angular/core';
import {
TemplateAst,
ElementAst,
EmbeddedTemplateAst,
PropertyBindingType
} from '@angular/compiler';
import {parseTemplate} from './angular/templates/templateParser';
import { CssAst, CssSelectorRuleAst, CssSelectorAst, CssBlockAst } from './angular/styles/cssAst';
import {ComponentMetadata, StyleMetadata} from './angular/metadata';
import {ng2WalkerFactoryUtils} from './angular/ng2WalkerFactoryUtils';
import {logger} from './util/logger';
import {SemVerDSL} from './util/ngVersion';
const CssSelectorTokenizer = require('css-selector-tokenizer');
const getSymbolName = (t: any) => {
let expr = t;
if (t.expression) {
expr = t.expression;
}
if (t.expression && t.expression.name) {
expr = t.expression.name;
}
return expr.text;
};
const isEncapsulationEnabled = (encapsulation: any) => {
if (!encapsulation) {
return true;
} else {
// By default consider the encapsulation disabled
if (getSymbolName(encapsulation) !== 'ViewEncapsulation') {
return false;
} else {
const encapsulationType = encapsulation.name.text;
if (/^(Emulated|Native)$/.test(encapsulationType)) {
return true;
}
}
}
return false;
};
// Initialize the selector accessors
const lang = require('cssauron')({
tag(node: ElementAst) {
return (node.name || '').toLowerCase();
},
// We do not support it for now
contents(node: ElementAst) { return ''; },
id(node: ElementAst) {
return this.attr(node, 'id');
},
'class'(node: ElementAst) {
const classBindings = (node.inputs || [])
.filter(b => b.type === PropertyBindingType.Class)
.map(b => b.name).join(' ');
const classAttr = node.attrs.filter(a => a.name.toLowerCase() === 'class').pop();
let staticClasses = '';
if (classAttr) {
staticClasses = classAttr.value + ' ';
}
return staticClasses + classBindings;
},
parent(node: any) {
return node.parentNode;
},
children(node: ElementAst) {
return node.children;
},
attr(node: ElementAst, attr: string) {
const targetAttr = node.attrs.filter(a => a.name === attr).pop();
if (targetAttr) {
return targetAttr.value;
}
return undefined;
}
});
// Visitor which normalizes the elements and finds out if we have a match
class ElementVisitor extends BasicTemplateAstVisitor {
visitElement(ast: ElementAst, fn: any) {
fn(ast);
ast.children.forEach(c => {
if (c instanceof ElementAst) {
(<any>c).parentNode = ast;
}
this.visit(c, fn);
});
}
}
// Finds out if selector of given type has been used
const hasSelector = (s: any, type: string) => {
if (!s) {
return false;
}
if (s.type === 'selector' || s.type === 'selectors') {
return (s.nodes || []).some(n => hasSelector(n, type));
} else {
return s.type === type;
}
};
const dynamicFilters = {
id(ast: ElementAst, selector: any) {
return (ast.inputs || []).some(i => i.name === 'id');
},
attribute(ast: ElementAst, selector: any) {
return (ast.inputs || []).some(i => i.type === PropertyBindingType.Attribute);
},
'class'(ast: ElementAst, selector: any) {
return (ast.inputs || []).some(i => i.name === 'className' || i.name === 'ngClass');
}
};
// Filters elements following the strategies:
// - If has selector by id and any of the elements has a dynamically set id we just skip it.
// - If has selector by class and any of the elements has a dynamically set class we just skip it.
// - If has selector by attribute and any of the elements has a dynamically set attribute we just skip it.
class ElementFilterVisitor extends BasicTemplateAstVisitor {
shouldVisit(ast: ElementAst, strategies: any, selectorTypes: any): boolean {
return Object.keys(strategies).every(s => {
const strategy = strategies[s];
return !selectorTypes[s] || !strategy(ast);
}) && (ast.children || [])
.every(c => ast instanceof ElementAst && this.shouldVisit(<ElementAst>c, strategies, selectorTypes)
|| ast instanceof EmbeddedTemplateAst &&
(ast.children || []).every(c => this.shouldVisit(<ElementAst>c, strategies, selectorTypes)));
}
}
export class Rule extends Lint.Rules.AbstractRule {
static FAILURE: string = 'The %s "%s" that you\'re trying to access does not exist in the class declaration.';
public apply(sourceFile:ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new UnusedCssNg2Visitor(sourceFile,
this.getOptions(), {
cssVisitorCtrl: UnusedCssVisitor
}));
}
}
class UnusedCssVisitor extends BasicCssAstVisitor {
templateAst: TemplateAst;
constructor(sourceFile: ts.SourceFile,
originalOptions: Lint.IOptions,
context: ComponentMetadata,
protected style: StyleMetadata,
templateStart: number) {
super(sourceFile, originalOptions, context, style, templateStart);
}
visitCssSelectorRule(ast: CssSelectorRuleAst) {
try {
const match = ast.selectors.some(s => this.visitCssSelector(s));
if (!match) {
// We need this because of eventual source maps
const start = ast.start.offset;
const end = ast.end.offset;
const length = end - ast.start.offset + 1;
// length + 1 because we want to drop the '}'
const fix = this.createFix(this.createReplacement(start, length, ''));
this.addFailure(this.createFailure(start, length, 'Unused styles', fix));
}
} catch (e) {
logger.error(e);
}
return true;
}
visitCssSelector(ast: CssSelectorAst) {
const parts = [];
for (let i = 0; i < ast.selectorParts.length; i += 1) {
const c = ast.selectorParts[i];
c.strValue = c.strValue.split('::').shift();
// Stop on /deep/ and >>>
if (c.strValue.endsWith('/') ||
c.strValue.endsWith('>')) {
parts.push(c.strValue);
break;
} else if (!c.strValue.startsWith(':')) { // skip :host
parts.push(c.strValue);
}
}
if (!parts.length || !this.templateAst) {
return true;
}
const strippedSelector = parts.map(s => s.replace(/\/|>$/, '').trim()).join(' ');
const elementFilterVisitor = new ElementFilterVisitor(this.getSourceFile(), this._originalOptions, this.context, 0);
const tokenized = CssSelectorTokenizer.parse(strippedSelector);
const selectorTypesCache = Object.keys(dynamicFilters).reduce((a: any, key: string) => {
a[key] = hasSelector(tokenized, key);
return a;
}, {});
if (!elementFilterVisitor.shouldVisit(<ElementAst>this.templateAst, dynamicFilters, selectorTypesCache)) {
return true;
}
let matchFound = false;
const selector = (element: ElementAst) => {
if (lang(strippedSelector)(element)) {
matchFound = true;
return true;
}
return false;
};
const visitor = new ElementVisitor(this.getSourceFile(), this._originalOptions, this.context, 0);
visitor.visit(this.templateAst, selector);
return matchFound;
}
}
// Finds the template and wrapes the parsed content into a root element
export class UnusedCssNg2Visitor extends Ng2Walker {
private templateAst: TemplateAst;
visitClassDeclaration(declaration: ts.ClassDeclaration) {
const d = getComponentDecorator(declaration);
if (d) {
const meta: ComponentMetadata = <ComponentMetadata>this._metadataReader.read(declaration);
this.visitNg2Component(meta);
if (meta.template && meta.template.template) {
try {
const ElementAstCtr = ElementAst as any;
SemVerDSL
.gte('4.0.0-beta.8', () => {
this.templateAst =
new ElementAstCtr('*', [], [], [], [], [], [], false, [], parseTemplate(meta.template.template.code), 0, null, null);
})
.else(() => {
this.templateAst =
new ElementAstCtr('*', [], [], [], [], [], [], false, parseTemplate(meta.template.template.code), 0, null, null);
});
} catch (e) {
logger.error('Cannot parse the template', e);
}
}
}
super.visitClassDeclaration(declaration);
}
protected visitNg2StyleHelper(style: CssAst, context: ComponentMetadata, styleMetadata: StyleMetadata, baseStart: number) {
if (!style) {
return;
} else {
const file = this.getContextSourceFile(styleMetadata.url, styleMetadata.style.source);
const visitor = new UnusedCssVisitor(file, this._originalOptions, context, styleMetadata, baseStart);
visitor.templateAst = this.templateAst;
const d = getComponentDecorator(context.controller);
const encapsulation = getDecoratorPropertyInitializer(d, 'encapsulation');
if (isEncapsulationEnabled(encapsulation)) {
style.visit(visitor);
visitor.getFailures().forEach(f => this.addFailure(f));
}
}
}
}