Skip to content

Commit

Permalink
refactor(eslint-plugin): [prefer-output-readonly] use computed select…
Browse files Browse the repository at this point in the history
…or (#569)
  • Loading branch information
rafaelss95 authored Jul 10, 2021
1 parent c296827 commit 8dcb33b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/eslint-plugin/src/rules/prefer-output-readonly.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/experimental-utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { OUTPUT_DECORATOR } from '../utils/selectors';

type Options = [];
export type MessageIds = 'preferOutputReadonly' | 'suggestAddReadonlyModifier';
Expand All @@ -11,22 +12,22 @@ export default createESLintRule<Options, MessageIds>({
type: 'suggestion',
docs: {
description:
'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned',
'Prefer to declare `@Output` as `readonly` since they are not supposed to be reassigned',
category: 'Best Practices',
recommended: false,
suggestion: true,
},
schema: [],
messages: {
preferOutputReadonly:
'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned',
suggestAddReadonlyModifier: 'Add readonly modifier',
'Prefer to declare `@Output` as `readonly` since they are not supposed to be reassigned',
suggestAddReadonlyModifier: 'Add `readonly` modifier',
},
},
defaultOptions: [],
create(context) {
return {
'ClassProperty[readonly=undefined] > Decorator[expression.callee.name="Output"]'({
[`ClassProperty:not([readonly]) > ${OUTPUT_DECORATOR}`]({
parent: { key },
}: TSESTree.Decorator & { parent: TSESTree.ClassProperty }) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const suggestAddReadonlyModifier: MessageIds = 'suggestAddReadonlyModifier';

ruleTester.run(RULE_NAME, rule, {
valid: [
`
class Test {
testEmitter = new EventEmitter<string>();
}
`,
`
class Test {
@Output() readonly testEmitter = new EventEmitter<string>();
Expand Down

0 comments on commit 8dcb33b

Please # to comment.