-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from GregOnNet/view-encapsulation
Adding rule checking ViewEncapsulation
- Loading branch information
Showing
7 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"typescript.tsdk": "./node_modules/typescript/lib" | ||
"typescript.tsdk": "./node_modules/typescript/lib", | ||
"angulardoc.repoId": "51f64839-b313-47fa-8d89-9317081ebe22", | ||
"angulardoc.lastSync": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { getComponentDecorator, getDecoratorPropertyInitializer } from './util/utils'; | ||
import { ComponentMetadata } from './angular/metadata'; | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
|
||
import { NgWalker } from './angular/ngWalker'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
static metadata: Lint.IRuleMetadata = { | ||
ruleName: 'use-view-encapsulation', | ||
type: 'maintainability', | ||
description: 'Disallows using of ViewEncapsulation.None', | ||
rationale: '', | ||
options: null, | ||
optionsDescription: 'Not configurable', | ||
typescriptOnly: true | ||
}; | ||
|
||
static FAILURE = 'Using "ViewEncapsulation.None" will make your styles global which may have unintended effect'; | ||
|
||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const walker = new ViewEncapsulationWalker(sourceFile, this.getOptions()); | ||
return this.applyWithWalker(walker); | ||
} | ||
} | ||
|
||
class ViewEncapsulationWalker extends NgWalker { | ||
|
||
visitClassDeclaration(node: ts.ClassDeclaration) { | ||
const decorator = getComponentDecorator(node); | ||
const encapsulation = getDecoratorPropertyInitializer(decorator, 'encapsulation'); | ||
|
||
if(!encapsulation || | ||
encapsulation.name.text !== 'None') { | ||
return; | ||
} | ||
|
||
this.addFailure( | ||
this.createFailure( | ||
encapsulation.getStart(), | ||
encapsulation.getWidth(), | ||
Rule.FAILURE | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe('use-view-encapsulation', () => { | ||
describe('invalid view encapsulation', () => { | ||
it('should fail if ViewEncapsulation.None is set', () => { | ||
const source = ` | ||
@Component({ | ||
selector: 'sg-foo-bar', | ||
encapsulation: ViewEncapsulation.None | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
}) | ||
export class TestComponent { } | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName: 'use-view-encapsulation', | ||
message: 'Using "ViewEncapsulation.None" will make your styles global which may have unintended effect', | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('valid view encapsulation', () => { | ||
it('should succeed if ViewEncapsulation.Native is set', () => { | ||
const source = ` | ||
@Component({ | ||
selector: 'sg-foo-bar', | ||
encapsulation: ViewEncapsulation.Native | ||
}) | ||
export class TestComponent { } | ||
`; | ||
|
||
assertSuccess('use-view-encapsulation', source); | ||
}); | ||
|
||
it('should succeed if ViewEncapsulation.Emulated is set', () => { | ||
const source = ` | ||
@Component({ | ||
selector: 'sg-foo-bar', | ||
encapsulation: ViewEncapsulation.Emulated | ||
}) | ||
export class TestComponent { } | ||
`; | ||
|
||
assertSuccess('use-view-encapsulation', source); | ||
}); | ||
|
||
it('should succeed if no ViewEncapsulation is set explicitly', () => { | ||
const source = ` | ||
@Component({ | ||
selector: 'sg-foo-bar', | ||
}) | ||
export class TestComponent { } | ||
`; | ||
|
||
assertSuccess('use-view-encapsulation', source); | ||
}); | ||
}); | ||
}); |