TristonJ / eslint-plugin-prefer-arrow

ESLint plugin to prefer arrow functions
MIT License
55 stars 10 forks source link

Allow standalone function declarations #16

Closed astorije closed 4 years ago

astorije commented 4 years ago

This plugin is an excellent replacement for TSLint's only-arrow-functions, as even recommended by the typescript-eslint project.

However, only-arrow-functions has the allow-declarations option which allows for standalone declarations. In our codebase, we use non-arrow on every top-level function so using this plugin triggers hundreds of violations.

I would love to be able to use this rule to disallow the format above, without having to reformat our codebase. What do you think of an additional allowDeclarations option?

In fact, we were/are using the recommended preset of tslint-microsoft-contrib, which uses this option, so it would make transitioning from TSLint to ESLint even easier :)

TristonJ commented 4 years ago

@astorije Thank you for the detailed issue! I've just created a new commit (https://github.com/TristonJ/eslint-plugin-prefer-arrow/commit/2d631d97dff1bce5d671b505f1f11f761985e703) and published a new minor version to npm adding a allowStandaloneDeclarations option.

The option allows function declarations at the top level, but still warns about declarations inside other "inner" scope. For example, with allowStandaloneDeclarations set to true, this would be valid:

function hello() {
  return "world";
}

but the following would throw a warning

function hello() {
  // Would warn about the following function
  return function() {
    return "world";
  }
}

Please let me know if this addresses your issue, thanks!

TristonJ commented 4 years ago

I am going to go ahead and close this issue. If there are bugs or additional requests / thoughts, please open a new issue.