Skip to content
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

Allow functions with certain names #9

Open
michaeljota opened this issue Feb 28, 2021 · 2 comments
Open

Allow functions with certain names #9

michaeljota opened this issue Feb 28, 2021 · 2 comments

Comments

@michaeljota
Copy link

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

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
Copy link
Owner

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:

const isSafeTransformation = (node) => {
return (
!isGeneratorFunction(node) &&
!containsThis(node) &&
!containsSuper(node) &&
!containsArguments(node) &&
!containsNewDotTarget(node) &&
(!isPrototypeAssignment(node) || disallowPrototype) &&
(!singleReturnOnly ||
(returnsImmediately(node) && !isNamedDefaultExport(node)))
);
};

There is a `getFunctionName function

const getFunctionName = (node) =>
node && node.id && node.id.name ? node.id.name : '';

Adding a new option of names to ignore gets read here

const options = context.options[0] || {};
const getOption = (name) =>
typeof options[name] !== 'undefined'
? options[name]
: DEFAULT_OPTIONS[name];
const singleReturnOnly = getOption('singleReturnOnly');
const classPropertiesAllowed = getOption('classPropertiesAllowed');
const disallowPrototype = getOption('disallowPrototype');
const returnStyle = getOption('returnStyle');

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

schema: [
{
additionalProperties: false,
properties: {
classPropertiesAllowed: { type: 'boolean' },
disallowPrototype: { type: 'boolean' },
returnStyle: {
default: DEFAULT_OPTIONS.returnStyle,
pattern: '^(explicit|implicit|unchanged)$',
type: 'string',
},
singleReturnOnly: { type: 'boolean' },
},
type: 'object',
},
],

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants