-
Notifications
You must be signed in to change notification settings - Fork 237
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
include exception for no-input-rename rule #224
Comments
Thanks for pointing this out. I'll add it as a low priority enhancement. Before we implement this feature in codelyzer this rule can be selectively disabled. |
Hi its my first time and i would love to contribute, understood the issue, can you help me pointing where to start with this? |
Awesome! In general each TypeScript file is being parsed, by a couple of parsers, which in the end extract the metadata for the components (this includes the one attached with There's the so called Each "rule" has a "rule walker" which extends the export class InputMetadataWalker extends Ng2Walker {
metadata: DirectiveMetadata;
// The override, do not forget to invoke the method from the base class.
visitClassDeclaration(node: ts.ClassDeclaration) {
this.metadata = this._metadataReader.read(declaration);
super.visitClassDeclaration(node);
}
visitNg2Input(property:ts.PropertyDeclaration, input:ts.Decorator, args:string[]) {
let className = (<any>property).parent.name.text;
let memberName = (<any>property.name).text;
// check if the selector has the same name as the memberName
// the selector will be in the form '[selectorName]` so you may need to use
// compiler from '@angular/compiler', and it's CssSelector.parse export.
// https://github.com/mgechev/codelyzer/blob/master/src/selectorNameBase.ts#L176
// After that you'll get "selectorName" as value of any of the elements in the "attrs" property
// of the returned element.
// Should be modified.
if (args.length !== 0 && memberName !== args[0]) {
let failureConfig:string[] = [className, memberName, memberName];
failureConfig.unshift(Rule.FAILURE_STRING);
this.addFailure(
this.createFailure(
property.getStart(),
property.getWidth(),
sprintf.apply(this, failureConfig)));
}
}
} |
thanks
it('should succeed, when a directive input property is the same as the name of the directive selector name', () => {
let source = `
@Directive({
selector: ['label']
})
class ButtonComponent {
@Input('labelAttribute') label: string;
}`;
assertSuccess('no-input-rename', source);
});
|
Yes, in the above case it should not fail. You can run the tests with
This way on change the tests are going to run automatically. |
sorry for many questions but im learning a lot |
Here's the example from above: export class InputMetadataWalker extends Ng2Walker {
metadata: DirectiveMetadata;
// The override, do not forget to invoke the method from the base class.
visitClassDeclaration(node: ts.ClassDeclaration) {
this.metadata = this._metadataReader.read(declaration);
super.visitClassDeclaration(node);
}
visitNg2Input(property:ts.PropertyDeclaration, input:ts.Decorator, args:string[]) {
let className = (<any>property).parent.name.text;
let memberName = (<any>property.name).text;
// check if the selector has the same name as the memberName
// the selector will be in the form '[selectorName]` so you may need to use
// compiler from '@angular/compiler', and it's CssSelector.parse export.
// https://github.com/mgechev/codelyzer/blob/master/src/selectorNameBase.ts#L176
// After that you'll get "selectorName" as value of any of the elements in the "attrs" property
// of the returned element.
// Should be modified.
if (args.length !== 0 && memberName !== args[0]) {
let failureConfig:string[] = [className, memberName, memberName];
failureConfig.unshift(Rule.FAILURE_STRING);
this.addFailure(
this.createFailure(
property.getStart(),
property.getWidth(),
sprintf.apply(this, failureConfig)));
}
}
} The directive metadata is being extracted by the |
he thanks for all the help im not beeing able to get done this i write this
i dont understand how to get the directive selector i only get the input |
You can get the selector with: |
thank you for your time i managed to pass the test and the only thing its that i need to have the selector like 'label', its not in ['label'] (its not take in it) @directive({ |
Make sure you're parsing the selector with
What behavior are you planning to implement for this case? |
how to use with the CssSelector.parse |
@karptonite I'd suggest to keep the rule as it is for now. It can be disabled for a specific line in case an input/output rename is required. The snippet in the style guide is just an example which shows one appropriate case when renaming an input is not a bad idea but it's not exhaustive, and is not related to the name of the selector of the directive. |
See issue #138.
The style guide has now been amended to include the exception mentioned in that issue:
https://angular.io/docs/ts/latest/guide/style-guide.html#!#05-13
If possible, the linter should catch when the directive selector is the same as the name of the input being renamed, and not flag it in that case.
The text was updated successfully, but these errors were encountered: