Skip to content
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

feat(eslint-plugin): [no-extran-class] add allowWithDecorator opt #886

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
c9fd764
feat(eslint-plugin): [no-extraneous-class] add allowWithDecorator option
a-tarasyuk Aug 20, 2019
7d4ffb8
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Aug 20, 2019
3c2ef6c
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Aug 27, 2019
abe72af
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Aug 29, 2019
7c499f8
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 3, 2019
7014ace
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 5, 2019
43a51fb
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 9, 2019
33ab32e
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 10, 2019
66f7624
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 12, 2019
4b87dbc
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Sep 30, 2019
6d1cd5b
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Oct 1, 2019
762189e
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Oct 10, 2019
4ddf9ac
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Nov 16, 2019
0eaa915
feat(eslint-plugin): add additional tests for allowWithDecorator option
a-tarasyuk Nov 19, 2019
411c8fe
Merge branch 'master' into feature/add-new-option-allow-with-decorato…
a-tarasyuk Nov 19, 2019
92f5865
docs: document option
bradzacher Nov 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
],
},
],
});