-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add use-injectable-provided-in (#814)
- Loading branch information
1 parent
3b82574
commit 656816f
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |