-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule in order to enforce the declaration of a selector (#557)
* Add a new rule in order to enforce the declaration of a selector
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
import { sprintf } from 'sprintf-js'; | ||
import { NgWalker } from './angular/ngWalker'; | ||
import { ComponentMetadata } from './angular/metadata'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: 'enforce-component-selector', | ||
type: 'style', | ||
description: 'Component selector must be declared.', | ||
rationale: 'Omit the component selector makes debugging difficult.', | ||
options: null, | ||
optionsDescription: 'Not configurable.', | ||
typescriptOnly: true | ||
}; | ||
|
||
|
||
static SELECTOR_FAILURE: string = 'The selector of the component "%s" is mandatory.'; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new EnforceComponentSelectorValidatorWalker(sourceFile, this)); | ||
} | ||
} | ||
|
||
export class EnforceComponentSelectorValidatorWalker extends NgWalker { | ||
|
||
constructor(sourceFile: ts.SourceFile, private rule: Rule) { | ||
super(sourceFile, rule.getOptions()); | ||
} | ||
|
||
visitNgComponent(metadata: ComponentMetadata) { | ||
if (!metadata.selector) { | ||
const failureConfig: string[] = [metadata.controller.name.text]; | ||
failureConfig.unshift(Rule.SELECTOR_FAILURE); | ||
this.generateFailure(metadata.decorator.getStart(), metadata.decorator.getWidth(), failureConfig); | ||
} | ||
super.visitNgComponent(metadata); | ||
} | ||
|
||
private generateFailure(start: number, width: number, failureConfig: string[]) { | ||
this.addFailure( | ||
this.createFailure( | ||
start, | ||
width, | ||
sprintf.apply(this, failureConfig))); | ||
} | ||
} |
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,70 @@ | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe('enforceComoponentSelectorRule', () => { | ||
it('should fail when selector is not given in @Component', () => { | ||
let source = ` | ||
@Component() | ||
~~~~~~~~~~~~ | ||
class Test {}`; | ||
assertAnnotated({ | ||
ruleName: 'enforce-component-selector', | ||
message: 'The selector of the component "Test" is mandatory.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when selector is empty in @Component', () => { | ||
let source = ` | ||
@Component({ | ||
~~~~~~~~~~~~ | ||
selector: '' | ||
}) | ||
~~ | ||
class Test {}`; | ||
assertAnnotated({ | ||
ruleName: 'enforce-component-selector', | ||
message: 'The selector of the component "Test" is mandatory.', | ||
source | ||
}); | ||
}); | ||
|
||
|
||
it('should fail when selector equals 0 in @Component', () => { | ||
let source = ` | ||
@Component({ | ||
~~~~~~~~~~~~ | ||
selector: 0 | ||
}) | ||
~~ | ||
class Test {}`; | ||
assertAnnotated({ | ||
ruleName: 'enforce-component-selector', | ||
message: 'The selector of the component "Test" is mandatory.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when selector equals null in @Component', () => { | ||
let source = ` | ||
@Component({ | ||
~~~~~~~~~~~~ | ||
selector: null | ||
}) | ||
~~ | ||
class Test {}`; | ||
assertAnnotated({ | ||
ruleName: 'enforce-component-selector', | ||
message: 'The selector of the component "Test" is mandatory.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should succeed when selector is given in @Component', () => { | ||
let source = ` | ||
@Component({ | ||
selector: 'sg-bar-foo' | ||
}) | ||
class Test {}`; | ||
assertSuccess('enforce-component-selector', source); | ||
}); | ||
}); |