Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rule): add use-injectable-provided-in #814

Merged
merged 3 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/angular/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ export class ModuleMetadata {
}

export class InjectableMetadata {
constructor(readonly controller: ts.ClassDeclaration, readonly decorator: ts.Decorator) {}
constructor(readonly controller: ts.ClassDeclaration, readonly decorator: ts.Decorator, readonly providedIn?: string | ts.Expression) {}
}
4 changes: 3 additions & 1 deletion src/angular/metadataReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export class MetadataReader {
}

protected readInjectableMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): DirectiveMetadata {
return new InjectableMetadata(d, dec);
const providedInExpression = getDecoratorPropertyInitializer(dec, 'providedIn');

return new InjectableMetadata(d, dec, providedInExpression);
}

protected readComponentMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): ComponentMetadata {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export { Rule as TemplateNoNegatedAsyncRule } from './templateNoNegatedAsyncRule
export { Rule as TemplateUseTrackByFunctionRule } from './templateUseTrackByFunctionRule';
export { Rule as UseComponentSelectorRule } from './useComponentSelectorRule';
export { Rule as UseComponentViewEncapsulationRule } from './useComponentViewEncapsulationRule';
export { Rule as UseInjectableProvidedInRule } from './useInjectableProvidedInRule';
export { Rule as UseLifecycleInterfaceRule } from './useLifecycleInterfaceRule';
export { Rule as UsePipeDecoratorRule } from './usePipeDecoratorRule';
export { Rule as UsePipeTransformInterfaceRule } from './usePipeTransformInterfaceRule';
Expand Down
38 changes: 38 additions & 0 deletions src/useInjectableProvidedInRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IRuleMetadata, RuleFailure } from 'tslint';
import { AbstractRule } from 'tslint/lib/rules';
import { SourceFile } from 'typescript';
import { InjectableMetadata } from './angular';
import { NgWalker } from './angular/ngWalker';

export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: "Enforces classes decorated with @Injectable to use the 'providedIn' property.",
options: null,
optionsDescription: 'Not configurable.',
rationale: "Using the 'providedIn' property makes classes decorated with @Injectable tree shakeable.",
ruleName: 'use-injectable-provided-in',
type: 'functionality',
typescriptOnly: true
};

static readonly FAILURE_STRING = "Classes decorated with @Injectable should use the 'providedIn' property";

apply(sourceFile: SourceFile): RuleFailure[] {
const walker = new Walker(sourceFile, this.getOptions());

return this.applyWithWalker(walker);
}
}

class Walker extends NgWalker {
protected visitNgInjectable(metadata: InjectableMetadata): void {
this.validateInjectable(metadata);
super.visitNgInjectable(metadata);
}

private validateInjectable(metadata: InjectableMetadata): void {
if (metadata.providedIn) return;

this.addFailureAtNode(metadata.decorator, Rule.FAILURE_STRING);
}
}
46 changes: 46 additions & 0 deletions test/useInjectableProvidedInRule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Rule } from '../src/useInjectableProvidedInRule';
import { assertAnnotated, assertSuccess } from './testHelper';

const {
metadata: { ruleName },
FAILURE_STRING
} = Rule;

describe(ruleName, () => {
describe('failures', () => {
it('should fail if providedIn property is not set', () => {
const source = `
@Injectable()
~~~~~~~~~~~~~
class Test {}
`;
assertAnnotated({
message: FAILURE_STRING,
ruleName,
source
});
});
});

describe('success', () => {
it('should succeed if providedIn property is set to a literal string', () => {
const source = `
@Injectable({
providedIn: 'root'
})
class Test {}
`;
assertSuccess(ruleName, source);
});

it('should succeed if providedIn property is set to a module', () => {
const source = `
@Injectable({
providedIn: SomeModule
})
class Test {}
`;
assertSuccess(ruleName, source);
});
});
});