dukeluo / eslint-plugin-check-file

ESLint rules for consistent filename and folder. Allows you to enforce a consistent naming pattern for the filename and folder.

Home Page:https://www.npmjs.com/package/eslint-plugin-check-file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: How to check that there are no folders named "helpers" in the repo

acherkashin opened this issue Β· comments

We in our team have a rule, that we put all *Utils files under utils folder. But sometimes new team members create helpers folders.

How can I check with this plugin that there is no helpers folders in the repo?

commented

@acherkashin You can use a glob like !(helpers) to exclude the helpers folders in your repo.

{
  "plugins": [
    "check-file"
  ],
  "rules": {
    "check-file/folder-naming-convention": [
      "error",
      {
        "src/**/": "!(helpers)"
      }
    ]
  }
}

@dukeluo Thank you, it works πŸ‘

Could you help me understand how to read this linter object? I mean, we specify the following object in config

{
        "src/**/": "!(helpers)"
}

What should we specify as property name ("src/**/") and what as a value "!(helpers)"?

I mean, after reading the documentation, I don't really understand how to use folder-match-with-fex rule.

commented

@acherkashin

  • For the rule check-file/folder-naming-convention, both the key and value in the naming pattern object are a glob expression. The key is used to select target folders and the value is used to set a naming convention for the selected folder.
  • For the rule check-file/folder-match-with-fex, both the key and value in the naming pattern object are a glob expression. The key is used to select target files and the value is used to set a naming convention for the selected file's folder.
  • Just like the examples in the documentation of rule check-file/folder-match-with-fex, it will be useful when you want to group the specified files into a folder. e.g. *.test.js should be in __tests__ folder, use*.js should be in hooks folder.
commented

@acherkashin Maybe in your case, you can try something like this:

{
  "plugins": [
    "check-file"
  ],
  "rules": {
    "check-file/folder-naming-convention": [
      "error",
      {
        "src/**/": "!(helpers)"
      }
    ],
    "check-file/folder-match-with-fex": [
      "error",
      {
        "src/**/*Utils.js": "**/utils/"
      }
    ]
  }
}

In this way, your repo will have two limitations:

  • no folder under src/ folder allow named helpers
  • all the js file end with Utils under src/ folder should put in utils folder

The key is used to select target files and the value is used to set a naming convention for the selected file's folder.

"check-file/folder-naming-convention": [
      "error",
      {
        "src/**/": "!(helpers)"
      }
    ]

It means that with the key we can specify names of both: files and folders, right?

The example above I can read as "for all files ("src/**/"), the folder where they are should not be named helpers ( "!(helpers)"), right?

commented

The example above I can read as "for all files ("src/**/"), the folder where they are should not be named helpers ( "!(helpers)"), right?

A misunderstanding here: src/**/ select all subfolders rather than all files, src/** select all files and all subfolders.

var micromatch = require("micromatch")

console.log(micromatch.isMatch('src/a/b/foo.js', 'src/**/')); // false
console.log(micromatch.isMatch('src/a/b/', 'src/**/')); // true
console.log(micromatch.isMatch('src/a/b/foo.js', 'src/**')); // true
console.log(micromatch.isMatch('src/a/b/', 'src/**')); // true

code

@dukeluo Thank you very much for your help! Everything works fine for me! πŸ‘ πŸŽ‰

Maybe it is worth to add it as an example to documentation. I could send a PR if you want.

commented

@dukeluo Thank you very much for your help! Everything works fine for me! πŸ‘ πŸŽ‰

Maybe it is worth to add it as an example to documentation. I could send a PR if you want.

Cool. Thanks for your PR!