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

[Bug?] Inconsistent matching patterns for files & folders

wenfangdu opened this issue · comments

First of all, thank you for creating this.

Here are my current rules:

{
  "check-file/folder-naming-convention": [
    "error",
    {
      "*": "KEBAB_CASE"
    }
  ],
  "check-file/filename-naming-convention": [
    "error",
    {
      "*": "KEBAB_CASE"
    }
  ]
}

As you can see, the file name foB didn't throw the error:
image
But if I change the rules to:

{
  "check-file/folder-naming-convention": [
    "error",
    {
      "*": "KEBAB_CASE"
    }
  ],
  "check-file/filename-naming-convention": [
    "error",
    {
      "**": "KEBAB_CASE"
    }
  ]
}

Now it works as expected:
image
Notice the file pattern uses ** whereas the folder pattern uses *, is this inconsistency intentional or a bug?

commented

@wenfangdu Thanks for your issue, I think it isn't a bug, it is an interesting scenario.

When check-file plugin linting your file types/foo/sdoiOJI/foB.js:

  • * matches within a single directory level, while ** matches across multiple directory levels, including subdirectories.
  • the rule filename-naming-convention will use the target glob to match its path, so ** can select this file and get a lint error, while * cant select this file.
  • the rule folder-naming-convention will create it's all subdirectories firstly. In this case, this value will be ['types/', 'types/foo/', 'types/foo/sdoiOJI/', 'foo/', 'foo/sdoiOJI/', 'sdoiOJI/']. And then use the target glob to match the subdirectory item, so * can select the sdoiOJI/ item in your case, and get a lint error.

Hope my explanation helps you understand this scenario.

@dukeluo Thanks for the detailed explanation, that's a bit of mental overhead. If we unify this behavior/implementation i.e. use * to lint all files (same behavior as eslint overrides), could it be better?

commented

@dukeluo Thanks for the detailed explanation, that's a bit of mental overhead. If we unify this behavior/implementation i.e. use * to lint all files (same behavior as eslint overrides), could it be better?

Which behavior/implementation are you referring to? Do you mean "* matches within a single directory level, while ** matches across multiple directory levels"?

@dukeluo * matches across directory levels for both files and folders.

@dukeluo Or ** matches across directory levels for both files and folders, the point here is being consistent.

commented

@wenfangdu The behavior of the globstar** and the wildcard * come from the community. You can use this link to get more info. I think eslint overrides can use the wildcard * to select all the files across directories because its business logic ignores its folder, only care about its filename.

But this plugin cant do like this. For example, in your case (types/foo/sdoiOJI/foB.js), types/*/ will only select foo folder while types/**/ will select all its directories. Users can set the target scope according to their needs.

@dukeluo I understand, but the folder matching mechanism is not the same as the file one, e.g. if I set * for "check-file/folder-naming-convention", all folders & their subfolders will be matched, not just the root level folder names, therefore, inconsistent.

commented

@dukeluo I understand, but the folder matching mechanism is not the same as the file one, e.g. if I set * for "check-file/folder-naming-convention", all folders & their subfolders will be matched, not just the root level folder names, therefore, inconsistent.

I got your point. I will consider how to deal with this case.

commented

@wenfangdu The new version 2.4.0 is released, you can try it now.

@dukeluo Sweet, thanks.