forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseComponentViewEncapsulationRule.ts
More file actions
44 lines (35 loc) · 1.55 KB
/
useComponentViewEncapsulationRule.ts
File metadata and controls
44 lines (35 loc) · 1.55 KB
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
import { IRuleMetadata, RuleFailure } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { isPropertyAccessExpression, SourceFile } from 'typescript/lib/typescript';
import { ComponentMetadata } from './angular/metadata';
import { NgWalker } from './angular/ngWalker';
import { getDecoratorPropertyInitializer } from './util/utils';
const NONE = 'None';
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: `Disallows using ViewEncapsulation.${NONE}.`,
options: null,
optionsDescription: 'Not configurable.',
ruleName: 'use-component-view-encapsulation',
type: 'maintainability',
typescriptOnly: true,
};
static readonly FAILURE_STRING = `Using ViewEncapsulation.${NONE} makes your styles global, which may have an unintended effect`;
apply(sourceFile: SourceFile): RuleFailure[] {
const walker = new Walker(sourceFile, this.getOptions());
return this.applyWithWalker(walker);
}
}
class Walker extends NgWalker {
protected visitNgComponent(metadata: ComponentMetadata): void {
this.validateComponent(metadata);
super.visitNgComponent(metadata);
}
private validateComponent(metadata: ComponentMetadata): void {
const encapsulationExpression = getDecoratorPropertyInitializer(metadata.decorator, 'encapsulation');
if (!encapsulationExpression || (isPropertyAccessExpression(encapsulationExpression) && encapsulationExpression.name.text !== NONE)) {
return;
}
this.addFailureAtNode(encapsulationExpression, Rule.FAILURE_STRING);
}
}