Skip to content

Commit

Permalink
feat(eslint-plugin): [no-extran-class] add allowWithDecorator opt (#886)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
a-tarasyuk and bradzacher committed Nov 19, 2019
1 parent 56c00b3 commit f1ab9a2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
22 changes: 19 additions & 3 deletions packages/eslint-plugin/docs/rules/no-extraneous-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,25 @@ const StaticOnly = {

This rule accepts a single object option.

- `allowConstructorOnly: true` will silence warnings about classes containing only a constructor.
- `allowEmpty: true` will silence warnings about empty classes.
- `allowStaticOnly: true` will silence warnings about classes containing only static members.
```ts
type Options = {
// allow extraneous classes if they only contain a constructor
allowConstructorOnly?: boolean;
// allow extraneous classes if they have no body (i.e. are empty)
allowEmpty?: boolean;
// allow extraneous classes if they only contain static members
allowStaticOnly?: boolean;
// allow extraneous classes if they are have a decorator
allowWithDecorator?: boolean;
};

const defaultOptions: Options = {
allowConstructorOnly: false,
allowEmpty: false,
allowStaticOnly: false,
allowWithDecorator: false,
};
```

## When Not To Use It

Expand Down
24 changes: 21 additions & 3 deletions packages/eslint-plugin/src/rules/no-extraneous-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Options = [
allowConstructorOnly?: boolean;
allowEmpty?: boolean;
allowStaticOnly?: boolean;
allowWithDecorator?: boolean;
},
];
type MessageIds = 'empty' | 'onlyStatic' | 'onlyConstructor';
Expand Down Expand Up @@ -36,6 +37,9 @@ export default util.createRule<Options, MessageIds>({
allowStaticOnly: {
type: 'boolean',
},
allowWithDecorator: {
type: 'boolean',
},
},
},
],
Expand All @@ -50,9 +54,24 @@ export default util.createRule<Options, MessageIds>({
allowConstructorOnly: false,
allowEmpty: false,
allowStaticOnly: false,
allowWithDecorator: false,
},
],
create(context, [{ allowConstructorOnly, allowEmpty, allowStaticOnly }]) {
create(
context,
[{ allowConstructorOnly, allowEmpty, allowStaticOnly, allowWithDecorator }],
) {
const isAllowWithDecorator = (
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression | undefined,
): boolean => {
return !!(
allowWithDecorator &&
node &&
node.decorators &&
node.decorators.length
);
};

return {
ClassBody(node): void {
const parent = node.parent as
Expand All @@ -65,9 +84,8 @@ export default util.createRule<Options, MessageIds>({
}

const reportNode = 'id' in parent && parent.id ? parent.id : parent;

if (node.body.length === 0) {
if (allowEmpty) {
if (allowEmpty || isAllowWithDecorator(parent)) {
return;
}

Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin/tests/rules/no-extraneous-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export class Bar {
},
// https://github.com/typescript-eslint/typescript-eslint/issues/170
'export default class { hello() { return "I am foo!"; } }',
{
code: `
@FooDecorator
class Foo {}
`,
options: [{ allowWithDecorator: true }],
},
],

invalid: [
Expand Down Expand Up @@ -125,5 +132,17 @@ export class AClass {
},
],
},
{
code: `
@FooDecorator
class Foo {}
`,
options: [{ allowWithDecorator: false }],
errors: [
{
messageId: 'empty',
},
],
},
],
});

0 comments on commit f1ab9a2

Please sign in to comment.