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

Fix prefer-class-properties to ignore constructor overload declarations #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/healthy-llamas-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/eslint-plugin': patch
---

Fix prefer-class-properties to ignore empty constructor overload declarations
3 changes: 2 additions & 1 deletion packages/eslint-plugin/lib/rules/prefer-class-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ module.exports = {
return classNode.body.body.find((propertyNode) => {
return (
propertyNode.type === 'MethodDefinition' &&
propertyNode.key.name === 'constructor'
propertyNode.key.name === 'constructor' &&
propertyNode.value.body
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {RuleTester} = require('eslint');
const rule = require('../../../lib/rules/prefer-class-properties');

const ruleTester = new RuleTester({
parser: require.resolve('@babel/eslint-parser'),
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 6,
},
Expand Down Expand Up @@ -127,6 +127,15 @@ ruleTester.run('prefer-class-properties', rule, {
}`,
options: ['always'],
},
{
code: `class Foo {
constructor();
constructor() {
this.foo = {[foo]: 123};
}
}`,
options: ['always'],
},
],
invalid: [
{
Expand Down Expand Up @@ -243,5 +252,15 @@ ruleTester.run('prefer-class-properties', rule, {
errors: assignErrors,
options: ['always'],
},
{
code: `class Foo {
constructor();
constructor(a: number) {
this['foo'] = 123;
}
}`,
errors: assignErrors,
options: ['always'],
},
],
});