forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
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 no-queries-parameter rule (mgechev#571)
- Loading branch information
1 parent
0faa463
commit e9f4d23
Showing
3 changed files
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as Lint from 'tslint'; | ||
import { UsePropertyDecorator } from './propertyDecoratorBase'; | ||
|
||
export class Rule extends UsePropertyDecorator { | ||
static metadata: Lint.IRuleMetadata = { | ||
description: | ||
'Use @ContentChild, @ContentChildren, @ViewChild or @ViewChildren instead of the `queries` property of ' + | ||
'`@Component` or `@Directive` metadata.', | ||
options: null, | ||
optionsDescription: 'Not configurable.', | ||
rationale: | ||
'The property associated with `@ContentChild`, `@ContentChildren`, `@ViewChild` or `@ViewChildren` ' + | ||
`can be modified only in a single place: in the directive's class. If you use the \`queries\` metadata ` + | ||
'property, you must modify both the property declaration inside the controller, and the metadata ' + | ||
'associated with the directive.', | ||
ruleName: 'no-queries-parameter', | ||
type: 'style', | ||
typescriptOnly: true | ||
}; | ||
|
||
static FAILURE_STRING = 'Use @ContentChild, @ContentChildren, @ViewChild or @ViewChildren instead of the queries property'; | ||
|
||
constructor(options: Lint.IOptions) { | ||
super( | ||
{ | ||
decoratorName: ['ContentChild', 'ContentChildren', 'ViewChild', 'ViewChildren'], | ||
propertyName: 'queries', | ||
errorMessage: Rule.FAILURE_STRING | ||
}, | ||
options | ||
); | ||
} | ||
} |
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,69 @@ | ||
import { Rule } from '../src/noQueriesParameterRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
type MetadataType = 'Component' | 'Directive'; | ||
|
||
const { FAILURE_STRING, metadata: { ruleName } } = Rule; | ||
const metadataTypes = new Set<MetadataType>(['Component', 'Directive']); | ||
|
||
describe(ruleName, () => { | ||
metadataTypes.forEach(metadataType => { | ||
describe(metadataType, () => { | ||
describe('failure', () => { | ||
it('should fail when "queries" is used', () => { | ||
const source = ` | ||
@${metadataType}({ | ||
queries: { | ||
~~~~~~~~ | ||
contentChildren: new ContentChildren(ChildDirective), | ||
viewChildren: new ViewChildren(ChildDirective) | ||
} | ||
~ | ||
}) | ||
class Test {} | ||
`; | ||
assertAnnotated({ | ||
message: FAILURE_STRING, | ||
ruleName, | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when "queries" is used', () => { | ||
const source = ` | ||
@${metadataType}({ | ||
queries: { | ||
~~~~~~~~ | ||
contentChild: new ContentChild(ChildDirective), | ||
viewChild: new ViewChild(ChildDirective) | ||
} | ||
~ | ||
}) | ||
class Test {} | ||
`; | ||
assertAnnotated({ message: FAILURE_STRING, ruleName, source }); | ||
}); | ||
}); | ||
|
||
describe('success', () => { | ||
it('should succeed when "queries" is not used', () => { | ||
const source = ` | ||
@${metadataType}({}) | ||
class Test {} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should succeed when content decorators are used', () => { | ||
const source = ` | ||
@${metadataType}({}) | ||
class Test { | ||
@ContentChild(ChildDirective) contentChild: ChildDirective; | ||
} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |