-
Notifications
You must be signed in to change notification settings - Fork 16
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
Enforce use of function
for declared functions
#324
Comments
Edge case for consideration: The const printCoordinates: (x: number) => (y: number) => `(${string}, ${string})`
= (x) => (y) => `(${x}, ${y})`;
console.log(printCoordinates(1)(-1));
// "(1, -1)" If we stick to the function printCoordinates(x: number): (y: number) => `(${string}, ${string})` {
return function printCoordinatesForGivenX(y: number): `(${string}, ${string})` {
return `(${x}, ${y})`
}
} But for most cases I agree with preferring the |
I'm assuming the rule won't be applied for things like props, or functions passed in objects? For example if a function has an argument that is an object, and one of those keys can be a function should we be enforcing declaration of the function outside the object? callSiteExample({
prop1: 'foo',
prop2: () => console.log('bar'),
}) versus function prop2() {
console.log('bar');
}
callSiteExample({ prop1: 'foo', prop2 }); There's definitely some cleanliness to the second example, just curious what your thoughts are here. |
@MajorLift Ah yes, thanks for mentioning that edge case. It would make sense that we'd use |
@brad-decker Good point! Yes, my thinking was that this rule would only apply to function declarations and not function expressions. I guess a function defined via |
There are two ways to declare functions:
I propose that we add a rule which enforces that declared functions use the first style rather the second. There are a couple of issues with using an arrow function that I think
function
solves:function
pops out more visually.=>
is a bit overloaded, as it's used to indicate the return value for a function type in TypeScript. When usingfunction
, however, you don't have to deal with the ambiguity of=>
nor do you have extra characters in the way.:
always separates the arguments from the return value, and the return value always goes before the{
.There are also some benefits for using
function
:function
functions are JSDoc'd, whereas we don't do that forconst
functions. We could certainly add another rule, but it would be nice if we didn't have to.function
functions are hoisted; when defined at the top level, this means they are automatically available anywhere in the file no matter where they're defined. This comes in handy when writing tests. If you want to define test helpers for tests, you can put them below thedescribe
to hide them, thereby makingdescribe
the first thing that readers see when reading the file.The text was updated successfully, but these errors were encountered: