Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeluo committed Jan 31, 2025
1 parent 725778f commit d9f5762
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 71 deletions.
82 changes: 24 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
[![Test Coverage][test-coverage-image]][test-coverage-url]
[![Follow Author on X][x-follow-image]][x-follow-url]

[![Donate][ko-fi-image]][ko-fi-url]

ESLint rules for consistent filename and folder. Allows you to enforce a consistent naming pattern for the filename and folder.
An ESLint plugin that enforces consistent naming conventions for files and folders in your project. It helps maintain a clean and organized codebase by allowing you to define and enforce specific patterns for filenames and directory structures.

## Installation

Expand All @@ -28,17 +26,19 @@ npm install eslint-plugin-check-file --save-dev

## Usage

### Flat Config
This plugin supports ESLint's [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files). Here's a complete example:

```javascript
import checkFile from 'eslint-plugin-check-file';

export default [
{
files: ['src/**/*'],
files: ['src/**/*.*'],
plugins: {
'check-file': checkFile,
},
// optional: add this processor if you want to lint non-js/ts files (images, styles, etc.)
processor: 'check-file/eslint-processor-check-file',
rules: {
'check-file/no-index': 'error',
'check-file/filename-blocklist': [
Expand All @@ -52,76 +52,28 @@ export default [
'error',
{
'*.test.{js,jsx,ts,tsx}': '**/__tests__/',
'*.styled.{jsx,tsx}': '**/pages/',
'*.styled.{jsx,tsx}': '**/components/',
},
],
'check-file/filename-naming-convention': [
'error',
{
'**/*.{jsx,tsx}': 'CAMEL_CASE',
'**/*.{js,ts}': 'KEBAB_CASE',
'**/*.{jsx,tsx}': 'PASCAL_CASE',
'**/*.{js,ts}': 'CAMEL_CASE',
},
],
'check-file/folder-naming-convention': [
'error',
{
'src/**/': 'CAMEL_CASE',
'mocks/*/': 'KEBAB_CASE',
'src/components/*/': 'PASCAL_CASE',
'src/!(components)/**/!(__tests__)/': 'CAMEL_CASE',
},
],
},
},
];
```

### `eslintrc`

Add `check-file` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:

```json
{
"plugins": ["check-file"]
}
```

Then configure the rules you want to use under the rules section.

```json
{
"rules": {
"check-file/no-index": "error",
"check-file/filename-blocklist": [
"error",
{
"**/*.model.ts": "*.models.ts",
"**/*.util.ts": "*.utils.ts"
}
],
"check-file/folder-match-with-fex": [
"error",
{
"*.test.{js,jsx,ts,tsx}": "**/__tests__/",
"*.styled.{jsx,tsx}": "**/pages/"
}
],
"check-file/filename-naming-convention": [
"error",
{
"**/*.{jsx,tsx}": "CAMEL_CASE",
"**/*.{js,ts}": "KEBAB_CASE"
}
],
"check-file/folder-naming-convention": [
"error",
{
"src/**/": "CAMEL_CASE",
"mocks/*/": "KEBAB_CASE"
}
]
}
}
```

## Supported Rules

- [check-file/no-index](docs/rules/no-index.md): A file cannot be named "index"
Expand All @@ -130,6 +82,18 @@ Then configure the rules you want to use under the rules section.
- [check-file/filename-naming-convention](docs/rules/filename-naming-convention.md): Enforce a consistent naming pattern for filenames for specified files
- [check-file/folder-naming-convention](docs/rules/folder-naming-convention.md): Enforce a consistent naming pattern for folder names for specified folders

## Version Compatibility

Version 3.x and above only support ESLint's flat configuration. For legacy configuration support, please use version 2.x.

## Support

If you find this plugin helpful, consider supporting the project:

[![GitHub Sponsors][github-sponsors-image]][github-sponsors-url]

[![Ko-fi][ko-fi-image]][ko-fi-url]

[npm-image]: https://img.shields.io/npm/v/eslint-plugin-check-file.svg
[downloads-image]: https://img.shields.io/npm/dm/eslint-plugin-check-file.svg
[license-image]: https://img.shields.io/npm/l/eslint-plugin-check-file
Expand All @@ -142,3 +106,5 @@ Then configure the rules you want to use under the rules section.
[test-coverage-url]: https://app.codecov.io/gh/dukeluo/eslint-plugin-check-file
[ko-fi-url]: https://ko-fi.com/huanluo
[x-follow-url]: https://x.com/ihuanluo
[github-sponsors-image]: https://img.shields.io/github/sponsors/dukeluo?label=Sponsor%20me%20on%20GitHub%20Sponsors
[github-sponsors-url]: https://github.com/sponsors/dukeluo
10 changes: 8 additions & 2 deletions docs/rules/filename-naming-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This rule aims to format the filename of the specified file. This rule uses the

There are six basic naming conventions built into this rule, including `CAMEL_CASE`, `PASCAL_CASE`, `SNAKE_CASE`, `KEBAB_CASE`, `SCREAMING_SNAKE_CASE` and `FLAT_CASE`.

And there is also a special naming convention for Next.js page router project, which is `NEXT_JS_PAGE_ROUTER_FILENAME_CASE`, you can use it to ensure the filename of the page router is consistent with the naming convention. You can read more about it under [`NEXT_JS_PAGE_ROUTER_FILENAME_CASE`](#NEXT_JS_PAGE_ROUTER_FILENAME_CASE).

| Formatting | Name |
| ----------- | ---------------------- |
| helloWorld | `CAMEL_CASE` |
Expand All @@ -17,8 +19,6 @@ There are six basic naming conventions built into this rule, including `CAMEL_CA
| HELLO_WORLD | `SCREAMING_SNAKE_CASE` |
| helloworld | `FLAT_CASE` |

And there is also a special naming convention for Next.js page router project, which is `NEXT_JS_PAGE_ROUTER_FILENAME_CASE`, you can use it to ensure the filename of the page router is consistent with the naming convention.

If the rule had been set as follows:

```js
Expand Down Expand Up @@ -53,6 +53,12 @@ In addition to the built-in naming conventions, you can also set custom naming p

**Tip:** To selecte all your `js` files, your can use the glob expression `**/*.js`.

### `NEXT_JS_PAGE_ROUTER_FILENAME_CASE`

The `NEXT_JS_PAGE_ROUTER_FILENAME_CASE` aims to support a wide range of filename naming convention in Next.js Page Router projects. If you would like to enforce a camelCase naming convention for your filename, but also support Next.js' Dynamic segments, Catch-all segments and Optional Catch-all Segments, this pattern is for you.

While `NEXT_JS_PAGE_ROUTER_FILENAME_CASE` covers many naming cases, it's possible that some cases may be missing. If you come across any missing cases, I encourage you to open an issue and provide the necessary details. Your feedback will help me improve and enhance the naming convention.

### Prefined Match Syntax

Prefined match syntax allow you to capture specific part of the target file pattern and use it in your naming convention pattern. This syntax is particularly useful when you want to make a file to be named the same as its parent folder.
Expand Down
12 changes: 2 additions & 10 deletions docs/rules/folder-naming-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This rule aims to format the name of the specified folder. This rule uses the gl

There are six basic naming conventions built into this rule, including `CAMEL_CASE`, `PASCAL_CASE`, `SNAKE_CASE`, `KEBAB_CASE`, `SCREAMING_SNAKE_CASE` and `FLAT_CASE`.

Additionally, there is a naming convention called `NEXT_JS_APP_ROUTER_CASE` used to format folder names in Next.js projects that use the App Router. You can read more about it under [Built-in custom patterns](#built-in-custom-patterns).
Additionally, there is a naming convention called `NEXT_JS_APP_ROUTER_CASE` used to format folder names in Next.js projects that use the App Router. You can read more about it under [`NEXT_JS_APP_ROUTER_CASE`](#NEXT_JS_APP_ROUTER_CASE).

| Formatting | Name |
| ----------- | ---------------------- |
Expand Down Expand Up @@ -41,8 +41,6 @@ Examples of **correct** folder name for this rule:
src/components/displayLabel/displayLabel.js
```

## Custom patterns

In addition to the built-in naming conventions, you can also set custom naming patterns using glob match syntax. The following code shows an example of how to ensure that all the folders under the `components` folder are named begin with `__`:

```js
Expand All @@ -53,11 +51,7 @@ In addition to the built-in naming conventions, you can also set custom naming p

**Tip:** To exclude `__tests__` folder in `src`, use the glob expression `src/**/!(__tests__)/` to get the target folders.

## Built-in custom patterns

Some patterns are complex enough that they warrant their own definition within the lib.

### Next.js custom pattern
### `NEXT_JS_APP_ROUTER_CASE`

The `NEXT_JS_APP_ROUTER_CASE` aims to support a wide range of named constructs in Next.js App Router projects.

Expand All @@ -83,8 +77,6 @@ export const Page({ params: { pageId } }) { ... }

Besides this, the custom pattern should support all other Next.js naming conventions.

You can read more about them [here](https://github.com/dukeluo/eslint-plugin-check-file/pull/27#issuecomment-1582551071).

While `NEXT_JS_APP_ROUTER_CASE` covers many naming cases, it's possible that some cases may be missing. If you come across any missing cases, I encourage you to open an issue and provide the necessary details. Your feedback will help me improve and enhance the naming convention.

### Options
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import FolderMatchWithFex from './rules/folder-match-with-fex.js';
import FolderNamingConvention from './rules/folder-naming-convention.js';
import NoIndex from './rules/no-index.js';

export const rules = {
const rules = {
'filename-blocklist': FilenameBlocklist,
'filename-naming-convention': FilenameNamingConvention,
'folder-match-with-fex': FolderMatchWithFex,
Expand Down

0 comments on commit d9f5762

Please sign in to comment.