Skip to content

Commit

Permalink
fix(rules): Fix contextual-life-cycle when we have more than one clas…
Browse files Browse the repository at this point in the history
…s in a file (#548)
  • Loading branch information
wKoza authored and mgechev committed Mar 25, 2018
1 parent 8e7e875 commit dcb4b3e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/contextualLifeCycleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,33 @@ export class ClassMetadataWalker extends NgWalker {
visitNgInjectable(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
this.className = controller.name.text;
this.isInjectable = true;
this.isComponent = false;
this.isDirective = false;
this.isPipe = false;
}

visitNgComponent(metadata: ComponentMetadata) {
this.className = metadata.controller.name.text;
this.isComponent = true;
this.isInjectable = false;
this.isDirective = false;
this.isPipe = false;
}

visitNgDirective(metadata: DirectiveMetadata) {
this.className = metadata.controller.name.text;
this.isDirective = true;
this.isInjectable = false;
this.isComponent = false;
this.isPipe = false;
}

visitNgPipe(controller: ts.ClassDeclaration, decorator: ts.Decorator) {
this.className = controller.name.text;
this.isPipe = true;
this.isInjectable = false;
this.isComponent = false;
this.isDirective = false;
}

visitMethodDeclaration(method: ts.MethodDeclaration) {
Expand Down
30 changes: 30 additions & 0 deletions test/contextualLifeCycleRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@ describe('contextual-life-cycle', () => {

});

describe('valid component class', () => {

it('should succeed when an @Injectable is defined before him', () => {
let source = `
@Injectable()
class Sample {
public constructor() { }
}
@Component({
selector: 'sg-hero-cmp',
template: \`
<h1>Hello <span>{{ hero.name }}</span></h1>
\`,
providers: [Sample]
})
class HeroComponent implements OnInit {
public hero: Hero;
public constructor(private sample: Sample) { }
ngOnInit() {
console.log('Initialized');
}
}`;
assertSuccess('contextual-life-cycle', source);
});

});

});

describe('failure', () => {
Expand Down

0 comments on commit dcb4b3e

Please sign in to comment.