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?
@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.
- 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 inhooks
folder.
@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 namedhelpers
- all the
js
file end withUtils
undersrc/
folder should put inutils
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?
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
@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.