Skip to content

Feature/rule no dom import #11

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

Merged
merged 4 commits into from
Sep 30, 2019
Merged

Feature/rule no dom import #11

merged 4 commits into from
Sep 30, 2019

Conversation

Belco90
Copy link
Member

@Belco90 Belco90 commented Sep 29, 2019

New rule to force there are no direct imports from @testing-library/dom or
@testing-library/dom when using some testing library framework
wrapper. All info within proper doc file.

Copy link
Collaborator

@thomaslombart thomaslombart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!
There's just the error message that I find unclear. I can't think of anything better than that though 😅

},
messages: {
noDomImport:
'import from DOM Testing Library is restricted, import from corresponding Testing library framework instead',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message sounds unclear to me. What do you think?

Copy link
Member Author

@Belco90 Belco90 Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not convinced at all about it but I can't suggest which module should be imported. What I can do is to check which DTL module is used (dom-testing-library or @testing-library/dom) and say something like import directly from '@testing-library/dom' is restricted, instead you must import from '@testing-library/<framework>' by literally leaving that <framework> placeholder

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would think of something like that too. I see two solutions possible to that:

  1. For each rule, we could configure it by adding the selected framework and then use it in the proper rule. For example:
{
  "rules": {
    "testing-library/no-dom-import": ["error", "react"]
  }
}

However, this would require the user to add the framework to each rule where we need to differentiate the frameworks.

  1. Generate a set of rules for every corresponding framework in this file. This could become:
module.exports = {
  rules,
  configs: {
    recommended: {
      plugins: ['testing-library'],
      rules: recommendedRules,
    },
    react: {
      plugins: ['testing-library'],
      rules: { ...generateRecommendedRules("react"), 'testing-library/no-debug': 'warn' },
    },
    // ...
  },
}

And in generateRecommendedRules:

function generateRecommendedRules(framework) {
  return allRules.map(rule => {
    const augmentedRule = rule(framework)
    return augmentedRule
  })
}

Where rule would be a higher-order function that returns the proper rule configuration based on the framework:

'use strict';

const { getDocsUrl } = require('../utils');

const DOM_TESTING_LIBRARY_MODULES = [
  'dom-testing-library',
  '@testing-library/dom',
];

module.exports = (framework) => {
  return {
    meta: {
      type: 'problem',
      docs: {
        description: 'Disallow importing from DOM Testing Library',
        // ...
     },
    messages: {
      noDomImport:
        `import from DOM Testing Library is restricted, import from "@testing-library/${framework}`,
    },
    fixable: null,
    schema: [],
  },
  // ...
}

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ of course, didn't think about context options! Really interesting approach, I'd go for something intermediate between those 2 options: we add the framework option to this rule through context.options (so you receive it within create function through context arg and pass it to report message, as this seems the standard instead of doing the whole workaround to pass it to the rule function itself) and then we setup automatically this option on each recommended framework config (so it's automatically setup for those frameworks but everyone can customize it when adding the rule manually).

So I'm gonna merge this one (as I wanted to improve the error message but obviously it's gonna be way better when the rule options are received so I'm gonna skip that for now) and create an issue for you to handle that separately, but only for receiving the context.options and improve the message with proper framework setup, nothing yet about fixable. Are you happy with that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

noDomImport:
'import from DOM Testing Library is restricted, import from corresponding Testing library framework instead',
},
fixable: null,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be awesome (and I think pretty easy though I'm not sure of that) to include a fix for this rule. I can look into it if you want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, and I was thinking about it but I prefer to leave the fixable for a future version. Every time I think about this one it seems more complex to implement as you don't know which TL framework the query must be imported (don't think about react only, this works for vue and angular). So for fixing the rule we could try to guess from where the user should import it by looking for other "@testing-library/" text and get that suffix to replace it by "@testing-library/dom" but then... what if it's the first import in the file referring to testing library? We can't guess from where we have to reimport. Did you have something else in mind for this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes that's true. This is the same problem than above, I have two potential solutions 👆

@Belco90 Belco90 merged commit 9329ed6 into master Sep 30, 2019
@Belco90 Belco90 deleted the feature/rule-no-dom-import branch September 30, 2019 13:01
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

Successfully merging this pull request may close these issues.

2 participants