Skip to content

Commit a76ebbc

Browse files
rafaelss95mgechev
authored andcommitted
refactor(rule): rework prefer-inline-decorator to accept options (#794)
* feat: create objectKeys helper * fix: adjust getDecoratorName function * refactor(rule): minor refactor for no-attribute-decorator * refactor(rule): rework prefer-inline-decorator to accept options
1 parent 1e8cbbf commit a76ebbc

5 files changed

+3202
-181
lines changed

src/noAttributeDecoratorRule.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IRuleMetadata, RuleFailure, WalkContext } from 'tslint';
22
import { AbstractRule } from 'tslint/lib/rules';
33
import {
44
ConstructorDeclaration,
5+
createNodeArray,
56
Decorator,
67
forEachChild,
78
isConstructorDeclaration,
@@ -11,48 +12,50 @@ import {
1112
} from 'typescript';
1213
import { getDecoratorName } from './util/utils';
1314

15+
const ATTRIBUTE = 'Attribute';
16+
1417
export class Rule extends AbstractRule {
1518
static readonly metadata: IRuleMetadata = {
16-
description: 'Disallows usage of @Attribute decorator.',
19+
description: `Disallows usage of @${ATTRIBUTE} decorator.`,
1720
options: null,
1821
optionsDescription: 'Not configurable.',
19-
rationale: '@Attribute is considered bad practice. Use @Input instead.',
2022
ruleName: 'no-attribute-decorator',
2123
type: 'functionality',
2224
typescriptOnly: true
2325
};
2426

25-
static readonly FAILURE_STRING = '@Attribute is considered bad practice. Use @Input instead.';
27+
static readonly FAILURE_STRING = `@${ATTRIBUTE} is considered bad practice. Use @Input instead.`;
2628

2729
apply(sourceFile: SourceFile): RuleFailure[] {
2830
return this.applyWithFunction(sourceFile, walk);
2931
}
3032
}
3133

32-
const isAttributeDecorator = (decorator: Decorator): boolean => getDecoratorName(decorator) === 'Attribute';
33-
34-
const validateConstructor = (context: WalkContext<void>, node: ConstructorDeclaration): void =>
35-
node.parameters.forEach(parameter => validateParameter(context, parameter));
34+
const callbackHandler = (walkContext: WalkContext<void>, node: Node): void => {
35+
if (isConstructorDeclaration(node)) validateConstructor(walkContext, node);
36+
};
3637

37-
const validateDecorator = (context: WalkContext<void>, decorator: Decorator): void => {
38-
if (!isAttributeDecorator(decorator)) return;
38+
const isAttributeDecorator = (decorator: Decorator): boolean => getDecoratorName(decorator) === ATTRIBUTE;
3939

40-
context.addFailureAtNode(decorator, Rule.FAILURE_STRING);
40+
const validateConstructor = (walkContext: WalkContext<void>, node: ConstructorDeclaration): void => {
41+
node.parameters.forEach(parameter => validateParameter(walkContext, parameter));
4142
};
4243

43-
const validateParameter = (context: WalkContext<void>, parameter: ParameterDeclaration): void => {
44-
const { decorators } = parameter;
44+
const validateDecorator = (walkContext: WalkContext<void>, decorator: Decorator): void => {
45+
if (!isAttributeDecorator(decorator)) return;
4546

46-
if (!decorators) return;
47+
walkContext.addFailureAtNode(decorator, Rule.FAILURE_STRING);
48+
};
4749

48-
decorators.forEach(decorator => validateDecorator(context, decorator));
50+
const validateParameter = (walkContext: WalkContext<void>, node: ParameterDeclaration): void => {
51+
createNodeArray(node.decorators).forEach(decorator => validateDecorator(walkContext, decorator));
4952
};
5053

51-
const walk = (context: WalkContext<void>): void => {
52-
const { sourceFile } = context;
54+
const walk = (walkContext: WalkContext<void>): void => {
55+
const { sourceFile } = walkContext;
5356

5457
const callback = (node: Node): void => {
55-
if (isConstructorDeclaration(node)) validateConstructor(context, node);
58+
callbackHandler(walkContext, node);
5659

5760
forEachChild(node, callback);
5861
};

0 commit comments

Comments
 (0)