-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't prefix output properties check rule (#440)
* don't prefix output properties check rule * I18nRule export back * Update index.ts
- Loading branch information
Showing
6 changed files
with
78 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -23,7 +23,8 @@ | |
"contributors": [ | ||
"Minko Gechev <[email protected]>", | ||
"Preslav Semov <[email protected]>", | ||
"William Koza <[email protected]>" | ||
"William Koza <[email protected]>", | ||
"Eugenio Romano <[email protected]>" | ||
], | ||
"repository": { | ||
"type": "git", | ||
|
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,44 @@ | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
import { sprintf } from 'sprintf-js'; | ||
import { NgWalker } from './angular/ngWalker'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: 'no-on-prefix-output-name', | ||
type: 'maintainability', | ||
description: `Name events without the prefix on`, | ||
descriptionDetails: `See more at https://angular.io/guide/styleguide#dont-prefix-output-properties`, | ||
rationale: `Angular allows for an alternative syntax on-*. If the event itself was prefixed with on | ||
this would result in an on-onEvent binding expression`, | ||
options: null, | ||
optionsDescription: `Not configurable.`, | ||
typescriptOnly: true, | ||
}; | ||
|
||
static FAILURE_STRING: string = 'In the class "%s", the output ' + | ||
'property "%s" should not be prefixed with on'; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new OutputWalker(sourceFile, | ||
this.getOptions())); | ||
} | ||
} | ||
|
||
export class OutputWalker extends NgWalker { | ||
visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) { | ||
let className = (<any>property).parent.name.text; | ||
let memberName = (<any>property.name).text; | ||
|
||
if (memberName && memberName.startsWith('on')) { | ||
let failureConfig: string[] = [className, memberName]; | ||
failureConfig.unshift(Rule.FAILURE_STRING); | ||
this.addFailure( | ||
this.createFailure( | ||
property.getStart(), | ||
property.getWidth(), | ||
sprintf.apply(this, failureConfig))); | ||
} | ||
} | ||
} |
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,27 @@ | ||
import { assertSuccess, assertAnnotated } from './testHelper'; | ||
|
||
describe('no-on-prefix-output-name', () => { | ||
describe('invalid directive output property', () => { | ||
it(`should fail, when a directive output property is named with on prefix`, () => { | ||
let source = ` | ||
class ButtonComponent { | ||
@Output() onChange = new EventEmitter<any>(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
}`; | ||
assertAnnotated({ | ||
ruleName: 'no-on-prefix-output-name', | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('valid directive output property', () => { | ||
it('should succeed, when a directive output property is properly named', () => { | ||
let source = ` | ||
class ButtonComponent { | ||
@Output() change = new EventEmitter<any>(); | ||
}`; | ||
assertSuccess('no-on-prefix-output-name', source); | ||
}); | ||
}); | ||
}); |