JamieMason / eslint-plugin-prefer-arrow-functions

Auto-fix plain Functions into Arrow Functions, in all cases where conversion would result in the same behaviour
https://www.npmjs.com/package/eslint-plugin-prefer-arrow-functions
MIT License
43 stars 11 forks source link

Allow functions with certain names #9

Open michaeljota opened 3 years ago

michaeljota commented 3 years ago

Description

In Angular, factories should be named functions, as you can read here angular/angular#13702 and here angular/angular#13614. Because of that, this rule interferes and throws when a factory is needed to be used.

@NgModule({
  providers: [
    {
      provide: APOLLO_OPTIONS,
      useFactory(httpLink: HttpLink): ApolloClientOptions<any> { // <-- Throws
        return {
          link: httpLink.create({ uri }),
          cache: new InMemoryCache(),
        };
      },
      deps: [HttpLink],
    },
  ],
})
export class GraphQLModule {}
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> { // <--- Also throws
  return {
    link: httpLink.create({ uri }),
    cache: new InMemoryCache(),
  };
}

Suggested Solution

Adding an option that allows us to add patterns to ignore would be helpful because then we would only need to come up with a standard name for factories in the project.

Help Needed

Samuel-Therrien-Beslogic commented 2 years ago

For now I disable this config in component files:

    {
      files: ["*.component.ts"],
      rules: {
        // Factories (including lifecycles) should be named functions
        "extra-rules/potential-point-free": "warn",
        "prefer-arrow/prefer-arrow-functions": [
          "error",
          {
            ...preferArrowFunctionsConfig,
            "singleReturnOnly": true,
          },
        ],
      },
    },
JamieMason commented 2 years ago

Hi @michaeljota @Samuel-Beslogic, This should be possible by modifying isSafeTransformation to return false if the name matches one of the ones you want to ignore:

https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/blob/4520ba6591a7bc489f5af967f5ffc508d70cb6c7/src/prefer-arrow-functions.ts#L203-L214

There is a `getFunctionName function

https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/blob/4520ba6591a7bc489f5af967f5ffc508d70cb6c7/src/prefer-arrow-functions.ts#L80-L81

Adding a new option of names to ignore gets read here

https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/blob/4520ba6591a7bc489f5af967f5ffc508d70cb6c7/src/prefer-arrow-functions.ts#L35-L43

And the new value will also want adding the ESLint's schema

https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/blob/4520ba6591a7bc489f5af967f5ffc508d70cb6c7/src/prefer-arrow-functions.ts#L17-L32

Thanks a lot.