Skip to content

Commit

Permalink
fix(pipeNaming): do not fail on empty @pipe decorator
Browse files Browse the repository at this point in the history
Add `visitNg2Pipe` method in `NgwWalker`.
Refactor pipe related rules to use `Ng2Walker`.

Fix #111
  • Loading branch information
mgechev committed Oct 11, 2016
1 parent 3f90dad commit eb6ccc0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
15 changes: 11 additions & 4 deletions src/angular/ng2Walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ export class Ng2Walker extends Lint.RuleWalker {

protected visitClassDecorator(decorator: ts.Decorator) {
let name = getDecoratorName(decorator);
// Not invoked @Component or @Pipe, or @Directive
if (!(<ts.CallExpression>decorator.expression).arguments ||
!(<ts.CallExpression>decorator.expression).arguments.length ||
!(<ts.ObjectLiteralExpression>(<ts.CallExpression>decorator.expression).arguments[0]).properties) {
return;
}

if (name === 'Component') {
if (!(<ts.CallExpression>decorator.expression).arguments.length ||
!(<ts.ObjectLiteralExpression>(<ts.CallExpression>decorator.expression).arguments[0]).properties) {
return;
}
this.visitNg2Component(<ts.ClassDeclaration>decorator.parent, decorator);
const inlineTemplate = (<ts.ObjectLiteralExpression>
(<ts.CallExpression>decorator.expression).arguments[0])
Expand All @@ -165,6 +168,8 @@ export class Ng2Walker extends Lint.RuleWalker {
}
} else if (name === 'Directive') {
this.visitNg2Directive(<ts.ClassDeclaration>decorator.parent, decorator);
} else if (name === 'Pipe') {
this.visitNg2Pipe(<ts.ClassDeclaration>decorator.parent, decorator);
}
}

Expand Down Expand Up @@ -209,6 +214,8 @@ export class Ng2Walker extends Lint.RuleWalker {

protected visitNg2Directive(controller: ts.ClassDeclaration, decorator: ts.Decorator) {}

protected visitNg2Pipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {}

protected visitNg2Input(property: ts.PropertyDeclaration, input: ts.Decorator, args: string[]) {}

protected visitNg2Output(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) {}
Expand Down
13 changes: 4 additions & 9 deletions src/pipeImpureRule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Lint from 'tslint/lib/lint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import {Ng2Walker} from './angular/ng2Walker';
import SyntaxKind = require('./util/syntaxKind');

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -12,20 +13,14 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

export class ClassMetadataWalker extends Lint.RuleWalker {
export class ClassMetadataWalker extends Ng2Walker {

constructor(sourceFile:ts.SourceFile, private rule:Rule) {
super(sourceFile, rule.getOptions());
}

visitClassDeclaration(node:ts.ClassDeclaration) {
let className = node.name.text;
let decorators = <ts.Decorator[]>node.decorators || [];
decorators.filter(d => {
let baseExpr = <any>d.expression || {};
return baseExpr.expression.text === 'Pipe'
}).forEach(this.validateProperties.bind(this, className));
super.visitClassDeclaration(node);
visitNg2Pipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
this.validateProperties(controller.name.text, decorator);
}

private validateProperties(className:string, pipe:any) {
Expand Down
14 changes: 5 additions & 9 deletions src/pipeNamingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Lint from 'tslint/lib/lint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
import {Ng2Walker} from './angular/ng2Walker';
import {SelectorValidator} from './util/selectorValidator';

export class Rule extends Lint.Rules.AbstractRule {
Expand Down Expand Up @@ -43,20 +44,15 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

export class ClassMetadataWalker extends Lint.RuleWalker {
export class ClassMetadataWalker extends Ng2Walker {

constructor(sourceFile:ts.SourceFile, private rule:Rule) {
super(sourceFile, rule.getOptions());
}

visitClassDeclaration(node:ts.ClassDeclaration) {
let className = node.name.text;
let decorators = <ts.Decorator[]>node.decorators || [];
decorators.filter(d => {
let baseExpr = <any>d.expression || {};
return baseExpr.expression.text === 'Pipe'
}).forEach(this.validateProperties.bind(this, className));
super.visitClassDeclaration(node);
visitNg2Pipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
let className = controller.name.text;
this.validateProperties(className, decorator);
}

private validateProperties(className:string, pipe:any) {
Expand Down
9 changes: 9 additions & 0 deletions test/directiveClassSuffix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe('directive-class-suffix', () => {
});
});

describe('not called decorator', () => {
it('should not fail when @Directive is not called', () => {
let source = `
@Directive
class TestDirective {}`;
assertSuccess('directive-class-suffix', source);
});
});

describe('valid directive class', () => {
it('should succeed when is used @Component decorator', () => {
let source = `
Expand Down
9 changes: 9 additions & 0 deletions test/pipeImpureRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe('pipe-impure', () => {
});
});

describe('empty pipe', () => {
it('should not fail', () => {
let source = `
@Pipe
class Test {}`;
assertSuccess('pipe-impure', source);
});
});

describe('default pipe', () => {
it('should succeed when Pipe pure property is not set', () => {
let source = `
Expand Down
9 changes: 9 additions & 0 deletions test/pipeNamingRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ describe('pipe-naming', () => {
});
});

describe('empty pipe', () => {
it('should not fail when @Pipe not invoked', () => {
let source = `
@Pipe
class Test {}`;
assertSuccess('pipe-naming', source, ['camelCase', 'ng']);
});
});

describe('valid pipe name', () => {
it('should succeed when set valid name with prefix ng in @Pipe', () => {
let source = `
Expand Down

0 comments on commit eb6ccc0

Please sign in to comment.