简体   繁体   中英

How to disallow StyleSheet creation in ESLint?

For certain folders I would like to enable StyleSheet.create() whereas for other folder I would like to disallow them.

Now I figured out how to override ESLint rules per folder, but how do I disallow StyleSheet.create() ?

A way that I used is to restrict the import of StyleSheet . It is even possible to provide a message with the ESLint warning.

在此处输入图像描述

"no-restricted-imports": ["warn", {
  "paths": [
    {
      "name": "react-native",
      "importNames": ["StyleSheet"],
      "message": "Please import StyleSheet only in *.styles.ts files within the components folder."
    }
  ],
}],

Then for the override:

overrides: [
  {
    files: [
      'app/components/**/*.styles.ts',
    ],
    rules: {
      "no-restricted-imports": ["error", {}]
    }
  }
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM