Skip to content
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
24 changes: 8 additions & 16 deletions src/preferInlineDecoratorRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import { IOptions, IRuleMetadata, Replacement, RuleFailure } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { Decorator, isPropertyDeclaration, SourceFile } from 'typescript';
import { NgWalker } from './angular/ngWalker';
import { decoratorKeys, Decorators, DECORATORS, getDecoratorName, isSameLine } from './util/utils';
import { Decorators, getDecoratorName, isSameLine } from './util/utils';

export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: 'Ensures that decorators are on the same line as the property/method it decorates.',
descriptionDetails: 'See more at https://angular.io/guide/styleguide#style-05-12.',
hasFix: true,
optionExamples: [true, [true, Decorators.HostListener]],
optionExamples: [true, [true, Decorators.HostListener], [true, Decorators.Input, 'MyCustomDecorator']],
options: {
items: {
enum: decoratorKeys,
type: 'string'
},
maxLength: DECORATORS.size,
minLength: 0,
items: [
{
type: 'string'
}
],
type: 'array'
},
optionsDescription: 'A list of blacklisted decorators.',
Expand All @@ -33,14 +32,7 @@ export class Rule extends AbstractRule {
}

isEnabled(): boolean {
const {
metadata: {
options: { maxLength, minLength }
}
} = Rule;
const { length } = this.ruleArguments;

return super.isEnabled() && length >= minLength && length <= maxLength;
return super.isEnabled() && this.ruleArguments.every(ruleArgument => !!(typeof ruleArgument === 'string' && ruleArgument.trim()));
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/preferInlineDecoratorRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe(ruleName, () => {
class Test {
@Input('test1')
testVar1: string;
@Input('test2')
@MyCustomDecorator()
testVar2: string;
}
`;
Expand Down Expand Up @@ -99,7 +99,7 @@ describe(ruleName, () => {
const source = `
class Test {
@Input('test1') testVar1: string;
@Input('test2') testVar2: string;
@MyCustomDecorator() testVar2: string;
}
`;
assertSuccess(ruleName, source);
Expand Down Expand Up @@ -172,7 +172,7 @@ describe(ruleName, () => {
});

describe('replacements', () => {
it('should fail if a property is not on the same line as its decorator', () => {
it('should fail and apply proper replacements if a property is not on the same line as its decorator', () => {
const source = `
class Test {
@Output()
Expand Down