-
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.
feat(rule): add prefer-output-readonly rule (#564)
- Loading branch information
1 parent
b9c899b
commit 3d652d1
Showing
3 changed files
with
67 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,34 @@ | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
import { NgWalker } from './angular/ngWalker'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned.', | ||
options: null, | ||
optionsDescription: 'Not configurable.', | ||
rationale: '', | ||
ruleName: 'prefer-output-readonly', | ||
type: 'maintainability', | ||
typescriptOnly: true, | ||
}; | ||
|
||
static FAILURE_STRING = 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned'; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new OutputMetadataWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
export class OutputMetadataWalker extends NgWalker { | ||
visitNgOutput(property: ts.PropertyDeclaration, output: ts.Decorator, args: string[]) { | ||
if (property.modifiers && property.modifiers.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword)) { | ||
return; | ||
} | ||
|
||
const className = (property.parent as ts.PropertyAccessExpression).name.getText(); | ||
const memberName = property.name.getText(); | ||
this.addFailureAtNode(property.name, Rule.FAILURE_STRING); | ||
super.visitNgOutput(property, output, args); | ||
} | ||
} |
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,32 @@ | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
const ruleName = 'prefer-output-readonly'; | ||
|
||
describe(ruleName, () => { | ||
describe('failure', () => { | ||
it('should fail when an @Output is not readonly', () => { | ||
const source = ` | ||
class Test { | ||
@Output() testEmitter = new EventEmitter<string>(); | ||
~~~~~~~~~~~ | ||
} | ||
`; | ||
assertAnnotated({ | ||
ruleName, | ||
message: 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned', | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('success', () => { | ||
it('should pass when an @Output is readonly', () => { | ||
const source = ` | ||
class Test { | ||
@Output() readonly testEmitter = new EventEmitter<string>(); | ||
} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
}); |