Skip to content

Commit

Permalink
Merge pull request #92 from mgechev/life-cycle-fix
Browse files Browse the repository at this point in the history
fix life cycle rule to support ng.HookName add change the way life cy…
  • Loading branch information
mgechev authored Sep 21, 2016
2 parents bd85b76 + 45b8faf commit a9104b2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/useLifeCycleInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');

const getInterfaceName = (t: any) => {
if (t.expression && t.expression.name) {
return t.expression.name.text;
}
return t.expression.text;
};

export class Rule extends Lint.Rules.AbstractRule {

static FAILURE:string = 'Implement lifecycle hook interface %s for method %s in class %s ($$09-01$$)';
Expand Down Expand Up @@ -43,14 +50,7 @@ export class ClassMetadataWalker extends Lint.RuleWalker {
if (node.heritageClauses) {
let interfacesClause = node.heritageClauses.filter(h=>h.token === syntaxKind.ImplementsKeyword);
if (interfacesClause.length !== 0) {
interfaces = interfacesClause[0].types.map(t=>{
let expr =(<any>t.expression);
if(expr.expression && expr.expression.text == Rule.HOOKS_PREFIX){
return expr.name.text;
} else {
return expr.text;
}
});
interfaces = interfacesClause[0].types.map(getInterfaceName);
}
}
return interfaces;
Expand Down Expand Up @@ -78,5 +78,5 @@ export class ClassMetadataWalker extends Lint.RuleWalker {
let isNotIn:boolean = interfaces.indexOf(hookName) === -1;
return isNg && isHook && isNotIn;
}

}
9 changes: 8 additions & 1 deletion src/usePipeTransformInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');

const getInterfaceName = (t: any) => {
if (t.expression && t.expression.name) {
return t.expression.name.text;
}
return t.expression.text;
};

export class Rule extends Lint.Rules.AbstractRule {
static FAILURE: string = 'The %s class has the Pipe decorator, so it should implement the PipeTransform interface';
static PIPE_INTERFACE_NAME = 'PipeTransform';
Expand Down Expand Up @@ -41,7 +48,7 @@ export class ClassMetadataWalker extends Lint.RuleWalker {
let interfacesClause = node.heritageClauses
.filter(h=>h.token === SyntaxKind.current().ImplementsKeyword);
if (interfacesClause.length !== 0) {
interfaces = interfacesClause[0].types.map(t=>(<any>t.expression).text);
interfaces = interfacesClause[0].types.map(getInterfaceName);
}
}
return interfaces.indexOf(Rule.PIPE_INTERFACE_NAME) !== -1;
Expand Down
20 changes: 20 additions & 0 deletions test/useLifeCycleInterfaceRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('use-life-cycle-interface', () => {
assertSuccess('use-life-cycle-interface', source);
});
});

describe('valid declaration of life hooks, using ng.hookName', () => {

it(`should succeed, when life cycle hook is used with it's interface`, () => {
Expand All @@ -130,6 +131,25 @@ describe('use-life-cycle-interface', () => {
}`;
assertSuccess('use-life-cycle-interface', source);
});

it('should succeed when life cycle hook is implemented by using any prefix', () => {
let source = `
class App implements bar.OnInit {
ngOnInit(){
}
}`;
assertSuccess('use-life-cycle-interface', source);
});

it('should succeed when life cycle hook is implemented by using nested interfaces', () => {
let source = `
class App implements bar.foo.baz.OnInit {
ngOnInit(){
}
}`;
assertSuccess('use-life-cycle-interface', source);
});

it(`should succeed, when life cycle hooks are used with their corresponding interfaces`, () => {
let source = `
class App extends Component implements ng.OnInit, ng.OnDestroy {
Expand Down
10 changes: 10 additions & 0 deletions test/usePipeTransformInterfaceRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ describe('use-pipe-transform-interface', () => {
}`;
assertSuccess('use-pipe-transform-interface', source);
});

it(`should succeed when Pipe is declared properly`, () => {
let source = `
@Pipe({name: 'fetch'})
export class NewPipe implements ng.PipeTransform {
transform(url:string):any {
}
}`;
assertSuccess('use-pipe-transform-interface', source);
});
});

describe('valid use of empty class', () => {
Expand Down

0 comments on commit a9104b2

Please sign in to comment.