-
Notifications
You must be signed in to change notification settings - Fork 234
refactor(rule): rename/clean up 'no-attribute-parameter-decorator' #781
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
Merged
mgechev
merged 1 commit into
mgechev:master
from
rafaelss95:refactor/no-attribute-decorator
Mar 6, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,61 @@ | ||
| import { IRuleMetadata, RuleFailure, WalkContext } from 'tslint'; | ||
| import { AbstractRule } from 'tslint/lib/rules'; | ||
| import { | ||
| ConstructorDeclaration, | ||
| Decorator, | ||
| forEachChild, | ||
| isConstructorDeclaration, | ||
| Node, | ||
| ParameterDeclaration, | ||
| SourceFile | ||
| } from 'typescript'; | ||
| import { getDecoratorName } from './util/utils'; | ||
|
|
||
| export class Rule extends AbstractRule { | ||
| static readonly metadata: IRuleMetadata = { | ||
| description: 'Disallows usage of @Attribute decorator.', | ||
| options: null, | ||
| optionsDescription: 'Not configurable.', | ||
| rationale: '@Attribute is considered bad practice. Use @Input instead.', | ||
| ruleName: 'no-attribute-decorator', | ||
| type: 'functionality', | ||
| typescriptOnly: true | ||
| }; | ||
|
|
||
| static readonly FAILURE_STRING = '@Attribute is considered bad practice. Use @Input instead.'; | ||
|
|
||
| apply(sourceFile: SourceFile): RuleFailure[] { | ||
| return this.applyWithFunction(sourceFile, walk); | ||
| } | ||
| } | ||
|
|
||
| const isAttributeDecorator = (decorator: Decorator): boolean => getDecoratorName(decorator) === 'Attribute'; | ||
|
|
||
| const validateConstructor = (context: WalkContext<void>, node: ConstructorDeclaration): void => | ||
| node.parameters.forEach(parameter => validateParameter(context, parameter)); | ||
|
|
||
| const validateDecorator = (context: WalkContext<void>, decorator: Decorator): void => { | ||
| if (!isAttributeDecorator(decorator)) return; | ||
|
|
||
| context.addFailureAtNode(decorator, Rule.FAILURE_STRING); | ||
| }; | ||
|
|
||
| const validateParameter = (context: WalkContext<void>, parameter: ParameterDeclaration): void => { | ||
| const { decorators } = parameter; | ||
|
|
||
| if (!decorators) return; | ||
|
|
||
| decorators.forEach(decorator => validateDecorator(context, decorator)); | ||
| }; | ||
|
|
||
| const walk = (context: WalkContext<void>): void => { | ||
| const { sourceFile } = context; | ||
|
|
||
| const callback = (node: Node): void => { | ||
| if (isConstructorDeclaration(node)) validateConstructor(context, node); | ||
|
|
||
| forEachChild(node, callback); | ||
| }; | ||
|
|
||
| forEachChild(sourceFile, callback); | ||
| }; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,112 @@ | ||
| import { Rule } from '../src/noAttributeDecoratorRule'; | ||
| import { assertAnnotated, assertMultipleAnnotated, assertSuccess } from './testHelper'; | ||
|
|
||
| const { | ||
| FAILURE_STRING, | ||
| metadata: { ruleName } | ||
| } = Rule; | ||
|
|
||
| describe(ruleName, () => { | ||
| describe('failure', () => { | ||
| it('should fail if @Attribute decorator is used', () => { | ||
| const source = ` | ||
| class Test { | ||
| label: string; | ||
|
|
||
| constructor(@Attribute('label') label) { | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| this.label = label; | ||
| } | ||
| } | ||
| `; | ||
| assertAnnotated({ | ||
| message: FAILURE_STRING, | ||
| ruleName, | ||
| source | ||
| }); | ||
| }); | ||
|
|
||
| it('should fail if @Attribute decorator is used in a class expression', () => { | ||
| const source = ` | ||
| class Test { | ||
| private count = 0; | ||
|
|
||
| InnerTestClass = new class { | ||
| constructor(@Attribute('label') label: string) {} | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| }(this); | ||
| } | ||
| `; | ||
| assertAnnotated({ | ||
| message: FAILURE_STRING, | ||
| ruleName, | ||
| source | ||
| }); | ||
| }); | ||
|
|
||
| it('should fail if @Attribute decorator is used in a property class declaration', () => { | ||
| const source = ` | ||
| class Test { | ||
| static InnerTestClass = class extends TestCase { | ||
| constructor(@Attribute('label') label: string) {} | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| }; | ||
| } | ||
| `; | ||
| assertAnnotated({ | ||
| message: FAILURE_STRING, | ||
| ruleName, | ||
| source | ||
| }); | ||
| }); | ||
|
|
||
| it('should fail if multiple @Attribute decorators are used', () => { | ||
| const source = ` | ||
| class Test { | ||
| constructor(@Attribute('label') label: string) { | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| } | ||
|
|
||
| InnerTest1 = new class { | ||
| constructor(@Attribute('label') label: string) {} | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| }(this); | ||
|
|
||
| static InnerTestClass2 = class extends TestCase { | ||
| constructor(@Attribute('label') label: string) {} | ||
| ################### | ||
| }; | ||
| } | ||
| `; | ||
| assertMultipleAnnotated({ | ||
| failures: [ | ||
| { | ||
| char: '~', | ||
| msg: FAILURE_STRING | ||
| }, | ||
| { | ||
| char: '^', | ||
| msg: FAILURE_STRING | ||
| }, | ||
| { | ||
| char: '#', | ||
| msg: FAILURE_STRING | ||
| } | ||
| ], | ||
| ruleName, | ||
| source | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('success', () => { | ||
| it('should succeed if no @Attribute decorator is used', () => { | ||
| const source = ` | ||
| class Test { | ||
| constructor(@Property('label') label: string) {} | ||
| } | ||
| `; | ||
| assertSuccess(ruleName, source); | ||
| }); | ||
| }); | ||
| }); |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this message...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's any place in the Angular Style Guide that says it's a bad practice to use
@Attributewe should link it here, otherwise better rephrase this (and also therationale).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM