Skip to content

Commit

Permalink
JamieMason#29 Don't flag constructors when this is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-merry committed May 23, 2024
1 parent a34f8f9 commit 3946a1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/prefer-arrow-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const alwaysValid = [
{
code: 'class obj {constructor(foo){this.foo = foo;}}; obj.prototype = {func: function() {}};',
},
{
code: 'class obj {constructor(private readonly foo: number){}};',
},
{
code: 'var foo = function() { return this.bar; };',
},
Expand Down Expand Up @@ -126,6 +129,12 @@ const alwaysValid = [
},
];

const validWhenClassPropertiesAllowed = [
{
code: 'class obj {constructor(private readonly foo: number){}};',
},
];

const validWhenSingleReturnOnly = [
{
code: 'var foo = (bar) => {return bar();}',
Expand Down Expand Up @@ -835,7 +844,10 @@ const withErrors = (errors) => (object) => ({
describe('when function is valid, or cannot be converted to an arrow function', () => {
describe('it considers the function valid', () => {
ruleTester.run('lib/rules/prefer-arrow-functions', rule, {
valid: alwaysValid,
valid: [
...alwaysValid,
...validWhenClassPropertiesAllowed.map(withOptions({ classPropertiesAllowed: true }))
],
invalid: [],
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/prefer-arrow-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default {
});
}
},
':matches(ClassProperty, MethodDefinition, Property)[key.name][value.type="FunctionExpression"][kind!=/^(get|set)$/]':
':matches(ClassProperty, MethodDefinition, Property)[key.name][value.type="FunctionExpression"][kind!=/^(get|set|constructor)$/]':
(node) => {
const propName = node.key.name;
const functionNode = node.value;
Expand Down

0 comments on commit 3946a1c

Please sign in to comment.