javascript - Custom eslint no-restricted-imports for lodash vs. lodashfp - Stack Overflow

I'm trying to define a list of certain lodash methods that should be imported from fp.In addition

I'm trying to define a list of certain lodash methods that should be imported from fp. In addition, I'm trying to disallow importing from lodash's top-level. Should disallow destructured imports from lodash.

Here's the goal:

import _ from 'lodash'; // should show message "Import [module] from lodash/[module] instead"
import { isEqual } from 'lodash'; // should show message "Import [module] from lodash/[module] instead"
import isEmpty from 'lodash/isEqual'; // should pass
import set from 'lodash/set'; // should show message "Import [module] from lodash/fp/[module] instead"

eslintrc.json

"no-restricted-imports": [
   "error",
   {
      "patterns": [
         {
            "group": ["lodash/set"],
            "message": "Import [module] from lodash/fp/[module] instead"
         },
         {
            "group": ["lodash", "!lodash/*"],
            "message": "Import [module] from lodash/[module] instead"
         }
      ]
   }
],

These are the flags/patterns I've been working with. I've tried toggling the negative matchers on and off, and switched the order of the group array strings, and the groups themselves. Unable to get the desired eslint errors.

I'm trying to define a list of certain lodash methods that should be imported from fp. In addition, I'm trying to disallow importing from lodash's top-level. Should disallow destructured imports from lodash.

Here's the goal:

import _ from 'lodash'; // should show message "Import [module] from lodash/[module] instead"
import { isEqual } from 'lodash'; // should show message "Import [module] from lodash/[module] instead"
import isEmpty from 'lodash/isEqual'; // should pass
import set from 'lodash/set'; // should show message "Import [module] from lodash/fp/[module] instead"

eslintrc.json

"no-restricted-imports": [
   "error",
   {
      "patterns": [
         {
            "group": ["lodash/set"],
            "message": "Import [module] from lodash/fp/[module] instead"
         },
         {
            "group": ["lodash", "!lodash/*"],
            "message": "Import [module] from lodash/[module] instead"
         }
      ]
   }
],

These are the flags/patterns I've been working with. I've tried toggling the negative matchers on and off, and switched the order of the group array strings, and the groups themselves. Unable to get the desired eslint errors.

Share Improve this question edited Oct 19, 2021 at 13:06 curtybear asked Oct 18, 2021 at 19:25 curtybearcurtybear 1,5484 gold badges22 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

How about this?

"no-restricted-imports": [
   "error",
   {
      "paths": [
         {
            "name": "lodash",
            "message": "Import [module] from lodash/[module] instead"
         }
      ],
      "patterns": [
         {
            "group": ["lodash/set"],
            "message": "Import [module] from lodash/fp/[module] instead"
         }
      ]
   }
],

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745372554a4624858.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信