Skip to content

Commit

Permalink
feat(rules): allow custom class suffix
Browse files Browse the repository at this point in the history
Fix #132
  • Loading branch information
mgechev committed Dec 10, 2016
1 parent d5f117e commit a019e3f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/componentClassSuffixRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {Ng2Walker} from './angular/ng2Walker';
import {ComponentMetadata} from './angular/metadata';

export class Rule extends Lint.Rules.AbstractRule {
static FAILURE: string = 'The name of the class %s should end with the suffix Component ($$02-03$$)';
static FAILURE: string = 'The name of the class %s should end with the suffix %s ($$02-03$$)';

static validate(className:string):boolean {
return /.*Component$/.test(className);
static validate(className: string, suffix: string):boolean {
return className.endsWith(suffix);
}

public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
Expand All @@ -22,12 +22,13 @@ export class ClassMetadataWalker extends Ng2Walker {
visitNg2Component(meta: ComponentMetadata) {
let name = meta.controller.name;
let className: string = name.text;
if (!Rule.validate(className)) {
const suffix = this.getOptions()[0] || 'Component';
if (!Rule.validate(className, suffix)) {
this.addFailure(
this.createFailure(
name.getStart(),
name.getWidth(),
sprintf.apply(this, [Rule.FAILURE, className])));
sprintf.apply(this, [Rule.FAILURE, className, suffix])));
}
super.visitNg2Component(meta);
}
Expand Down
11 changes: 6 additions & 5 deletions src/directiveClassSuffixRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {Ng2Walker} from './angular/ng2Walker';
import {DirectiveMetadata} from './angular/metadata';

export class Rule extends Lint.Rules.AbstractRule {
static FAILURE:string = 'The name of the class %s should end with the suffix Directive ($$02-03$$)';
static FAILURE:string = 'The name of the class %s should end with the suffix %s ($$02-03$$)';

static validate(className: string):boolean {
return /.*Directive/.test(className);
static validate(className: string, suffix: string):boolean {
return className.endsWith(suffix);
}

public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
Expand All @@ -23,12 +23,13 @@ export class ClassMetadataWalker extends Ng2Walker {
visitNg2Directive(meta: DirectiveMetadata) {
let name = meta.controller.name;
let className:string = name.text;
if (!Rule.validate(className)) {
const suffix = this.getOptions()[0] || 'Directive';
if (!Rule.validate(className, suffix)) {
this.addFailure(
this.createFailure(
name.getStart(),
name.getWidth(),
sprintf.apply(this, [Rule.FAILURE, className])));
sprintf.apply(this, [Rule.FAILURE, className, suffix])));
}
}
}
49 changes: 49 additions & 0 deletions test/componentClassSuffixRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,53 @@ describe('component-class-suffix', () => {
assertSuccess('component-class-suffix', source);
});
});

describe('changed suffix', () => {
it('should suceed when different sufix is set', () => {
let source = `
@Component({
selector: 'sgBarFoo'
})
class TestPage {}`;
assertSuccess('component-class-suffix', source, ['Page']);
});

it('should fail when different sufix is set and doesnt match', () => {
let source = `
@Component({
selector: 'sgBarFoo'
})
class TestPage {}`;
assertFailure('component-class-suffix', source, {
message: 'The name of the class TestPage should end with the suffix Component ($$02-03$$)',
startPosition: {
line: 4,
character: 18
},
endPosition: {
line: 4,
character: 26
}
}, ['Component']);
});

it('should fail when different sufix is set and doesnt match', () => {
let source = `
@Component({
selector: 'sgBarFoo'
})
class TestDirective {}`;
assertFailure('component-class-suffix', source, {
message: 'The name of the class TestDirective should end with the suffix Page ($$02-03$$)',
startPosition: {
line: 4,
character: 18
},
endPosition: {
line: 4,
character: 31
}
}, ['Page']);
});
});
});
49 changes: 49 additions & 0 deletions test/directiveClassSuffix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,53 @@ describe('directive-class-suffix', () => {
assertSuccess('directive-class-suffix', source);
});
});

describe('changed suffix', () => {
it('should suceed when different sufix is set', () => {
let source = `
@Directive({
selector: 'sgBarFoo'
})
class TestPage {}`;
assertSuccess('directive-class-suffix', source, ['Page']);
});

it('should fail when different sufix is set and doesnt match', () => {
let source = `
@Directive({
selector: 'sgBarFoo'
})
class TestPage {}`;
assertFailure('directive-class-suffix', source, {
message: 'The name of the class TestPage should end with the suffix Directive ($$02-03$$)',
startPosition: {
line: 4,
character: 18
},
endPosition: {
line: 4,
character: 26
}
}, ['Directive']);
});

it('should fail when different sufix is set and doesnt match', () => {
let source = `
@Directive({
selector: 'sgBarFoo'
})
class TestDirective {}`;
assertFailure('directive-class-suffix', source, {
message: 'The name of the class TestDirective should end with the suffix Page ($$02-03$$)',
startPosition: {
line: 4,
character: 18
},
endPosition: {
line: 4,
character: 31
}
}, ['Page']);
});
});
});

0 comments on commit a019e3f

Please sign in to comment.