Skip to content

Commit

Permalink
#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 35166cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"release-it": "17.0.1",
"rimraf": "3.0.2",
"ts-jest": "27.0.7",
"typescript": "4.4.4"
"typescript": "5.1.6"
},
"files": [
"dist"
Expand Down
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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5573,10 +5573,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

typescript@4.4.4:
version "4.4.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c"
integrity sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==
typescript@5.1.6:
version "5.1.6"
resolved "https://packages.atlassian.com/api/npm/npm-remote/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==

uglify-js@^3.1.4:
version "3.17.4"
Expand Down

0 comments on commit 35166cd

Please sign in to comment.