Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add eslint rule no-useless-constructor#167
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JamesHenry merged 9 commits intotypescript-eslint:masterfromarmano2:no-useless-constructorFeb 1, 2019
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
5e7c3ff
feat(eslint-plugin): add eslint rule no-useless-constructor
armano2eeb1dfe
docs: correct issue id
armano2f362ecc
fix(eslint-plugin): ignore private and protected constructors
armano2643dfd6
fix(eslint-plugin): add additonal test cases and fix formatting
armano2841171c
test(eslint-plugin): fix issue in test case
armano2bafe080
docs(eslint-plugin): add example of valid ts code
armano2a06d950
fix(eslint-plugin): improve detection of useless constructors
armano229d44f4
Merge branch 'master' into no-useless-constructor
armano2fbb9586
Merge branch 'master' into no-useless-constructor
JamesHenryFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/README.md
This file contains hidden or 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
78 changes: 78 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-useless-constructor.md
This file contains hidden or 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,78 @@ | ||
# Disallow unnecessary constructors (no-useless-constructor) | ||
ES2015 provides a default class constructor if one is not specified. As such, it is unnecessary to provide an empty constructor or one that simply delegates into its parent class, as in the following examples: | ||
```js | ||
class A { | ||
constructor() {} | ||
} | ||
class A extends B { | ||
constructor(value) { | ||
super(value); | ||
} | ||
} | ||
``` | ||
## Rule Details | ||
This rule flags class constructors that can be safely removed without changing how the class works. | ||
## Examples | ||
Examples of **incorrect** code for this rule: | ||
```js | ||
/*eslint no-useless-constructor: "error"*/ | ||
class A { | ||
constructor() {} | ||
} | ||
class A extends B { | ||
constructor(...args) { | ||
super(...args); | ||
} | ||
} | ||
``` | ||
Examples of **correct** code for this rule: | ||
armano2 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
```js | ||
/*eslint no-useless-constructor: "error"*/ | ||
class A {} | ||
class A { | ||
constructor() { | ||
doSomething(); | ||
} | ||
} | ||
class A extends B { | ||
constructor() { | ||
super('foo'); | ||
} | ||
} | ||
class A extends B { | ||
constructor() { | ||
super(); | ||
doSomething(); | ||
} | ||
} | ||
class A extends B { | ||
constructor(protected name: string) {} | ||
} | ||
class A extends B { | ||
protected constructor() {} | ||
} | ||
``` | ||
## When Not To Use It | ||
If you don't want to be notified about unnecessary constructors, you can safely disable this rule. | ||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-useless-constructor.md)</sup> |
80 changes: 80 additions & 0 deletionspackages/eslint-plugin/lib/rules/no-useless-constructor.js
This file contains hidden or 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,80 @@ | ||
/** | ||
* @fileoverview Disallow unnecessary constructors | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
'use strict'; | ||
const baseRule = require('eslint/lib/rules/no-useless-constructor'); | ||
const util = require('../util'); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
/** | ||
* Check if method with accessibility is not useless | ||
* @param {MethodDefinition} node | ||
* @returns {boolean} | ||
*/ | ||
function checkAccessibility(node) { | ||
switch (node.accessibility) { | ||
case 'protected': | ||
case 'private': | ||
return false; | ||
case 'public': | ||
if ( | ||
node.parent.type === 'ClassBody' && | ||
node.parent.parent && | ||
node.parent.parent.superClass | ||
) { | ||
return false; | ||
} | ||
break; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Check if method is not unless due to typescript parameter properties | ||
* @param {MethodDefinition} node | ||
* @returns {boolean} | ||
*/ | ||
function checkParams(node) { | ||
return ( | ||
!node.value.params || | ||
!node.value.params.some(param => param.type === 'TSParameterProperty') | ||
); | ||
} | ||
module.exports = Object.assign({}, baseRule, { | ||
meta: { | ||
armano2 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow unnecessary constructors', | ||
category: 'ECMAScript 6', | ||
url: util.metaDocsUrl('no-useless-constructor') | ||
}, | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages | ||
}, | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
MethodDefinition(node) { | ||
if ( | ||
node.value && | ||
node.value.type === 'FunctionExpression' && | ||
checkAccessibility(node) && | ||
checkParams(node) | ||
) { | ||
rules.MethodDefinition(node); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
100 changes: 100 additions & 0 deletionspackages/eslint-plugin/tests/lib/rules/no-useless-constructor.js
This file contains hidden or 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,100 @@ | ||
'use strict'; | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const rule = require('../../../lib/rules/no-useless-constructor'), | ||
RuleTester = require('eslint').RuleTester; | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
}, | ||
parser: '@typescript-eslint/parser' | ||
}); | ||
const error = { message: 'Useless constructor.', type: 'MethodDefinition' }; | ||
ruleTester.run('no-useless-constructor', rule, { | ||
valid: [ | ||
'class A { }', | ||
'class A { constructor(){ doSomething(); } }', | ||
'class A extends B { constructor(){} }', | ||
"class A extends B { constructor(){ super('foo'); } }", | ||
'class A extends B { constructor(foo, bar){ super(foo, bar, 1); } }', | ||
'class A extends B { constructor(){ super(); doSomething(); } }', | ||
'class A extends B { constructor(...args){ super(...args); doSomething(); } }', | ||
'class A { dummyMethod(){ doSomething(); } }', | ||
'class A extends B.C { constructor() { super(foo); } }', | ||
'class A extends B.C { constructor([a, b, c]) { super(...arguments); } }', | ||
'class A extends B.C { constructor(a = f()) { super(...arguments); } }', | ||
'class A extends B { constructor(a, b, c) { super(a, b); } }', | ||
'class A extends B { constructor(foo, bar){ super(foo); } }', | ||
'class A extends B { constructor(test) { super(); } }', | ||
'class A extends B { constructor() { foo; } }', | ||
'class A extends B { constructor(foo, bar) { super(bar); } }', | ||
// https://github.com/typescript-eslint/typescript-eslint/issues/15 | ||
'declare class A { constructor(); }', | ||
'class A { constructor(); }', | ||
'abstract class A { constructor(); }', | ||
'abstract class A { abstract constructor(); }', | ||
// https://github.com/typescript-eslint/typescript-eslint/issues/48 | ||
'class A { constructor(private name: string) {} }', | ||
'class A { constructor(public name: string) {} }', | ||
'class A { constructor(protected name: string) {} }', | ||
// https://github.com/typescript-eslint/typescript-eslint/pull/167#discussion_r252638401 | ||
'class A { private constructor() {} }', | ||
'class A { protected constructor() {} }', | ||
'class A extends B { public constructor() {} }', | ||
'class A extends B { protected constructor(foo, bar) { super(bar); } }', | ||
'class A extends B { private constructor(foo, bar) { super(bar); } }', | ||
'class A extends B { public constructor(foo){ super(foo); } }', | ||
'class A extends B { public constructor(foo){} }' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'class A { constructor(){} }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: "class A { 'constructor'(){} }", | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A extends B { constructor() { super(); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A extends B { constructor(foo){ super(foo); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A extends B { constructor(foo, bar){ super(foo, bar); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A extends B { constructor(...args){ super(...args); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A extends B.C { constructor() { super(...arguments); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: | ||
'class A extends B { constructor(a, b, ...c) { super(...arguments); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: | ||
'class A extends B { constructor(a, b, ...c) { super(a, b, ...c); } }', | ||
errors: [error] | ||
}, | ||
{ | ||
code: 'class A { public constructor() {} }', | ||
errors: [error] | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.