Skip to content

Commit

Permalink
fix(rule): 'prefer-inline-decorator' limiting the number of options (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 authored and mgechev committed Mar 8, 2019
1 parent c023c75 commit 31b2b6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
24 changes: 8 additions & 16 deletions src/preferInlineDecoratorRule.ts
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
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

0 comments on commit 31b2b6a

Please sign in to comment.