Skip to content

Commit f47188d

Browse files
committed
docs: add docs for rule filename-naming-convention
1 parent ee0a03e commit f47188d

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ Then configure the rules you want to use under the rules section.
4949
## Supported Rules
5050

5151
- [check-file/folder-match-with-fex](docs/rules/folder-match-with-fex.md): Enforce a consistent naming pattern for the folder of the specified file extension
52-
53-
52+
- [check-file/filename-naming-convention](docs/rules/filename-naming-convention.md): Enforce a consistent naming pattern for the filename of the specified file extension
Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,66 @@
11
# The filename should follow the filename naming convention (filename-naming-convention)
22

3-
Please describe the origin of the rule here.
3+
Allows you to enforce a consistent naming pattern for the filename of the specified file.
44

55
## Rule Details
66

7-
This rule aims to...
7+
This rule aims to format the filename of the specified file. This rule uses the glob match syntax to match a file and declare the naming pattern for the filename.
88

9-
Examples of **incorrect** code for this rule:
9+
There are six naming conventions built into this rule, including `CAMEL_CASE`, `PASCAL_CASE`, `SNAKE_CASE`, `KEBAB_CASE`, `SCREAMING_SNAKE_CASE` and `FLAT_CASE`.
1010

11-
```js
11+
| Formatting | Name |
12+
|---|---|
13+
| helloWorld | `CAMEL_CASE` |
14+
| HelloWorld | `PASCAL_CASE` |
15+
| hello_world | `SNAKE_CASE` |
16+
| hello-world | `KEBAB_CASE` |
17+
| HELLO_WORLD | `SCREAMING_SNAKE_CASE` |
18+
| helloworld | `FLAT_CASE` |
1219

13-
// fill me in
20+
If the rule had been set as follows:
21+
```js
22+
...
23+
'check-file/filename-naming-convention': ['error', {'*.js': 'CAMEL_CASE'}],
24+
...
25+
```
1426

27+
Examples of **incorrect** filename for this rule:
28+
```sh
29+
calculate-price.js
30+
CalculatePrice.js
31+
calculate_price.js
32+
calculateprice.js
1533
```
1634

17-
Examples of **correct** code for this rule:
35+
Examples of **correct** filename for this rule:
36+
```sh
37+
calculatePrice.js
38+
```
1839

40+
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 your `js` files are named begin with `__`:
1941
```js
20-
21-
// fill me in
22-
42+
...
43+
'check-file/filename-naming-convention': ['error', {'*.js': '__+([a-z])'}],
44+
...
2345
```
2446

2547
### Options
48+
You need to specify a different naming pattern for different file extensions. The plugin will only check files with extensions you explicitly provided:
2649

27-
If there are any options, describe them here. Otherwise, delete this section.
28-
29-
## When Not To Use It
30-
31-
Give a short description of when it would be appropriate to turn off this rule.
50+
```js
51+
module.exports = {
52+
plugins: [
53+
'check-file',
54+
],
55+
rules: {
56+
'check-file/filename-naming-convention': ['error', {
57+
'*.{js,jsx,ts,tsx}': 'CAMEL_CASE',
58+
'*.json': 'KEBAB_CASE',
59+
}],
60+
},
61+
};
62+
```
3263

3364
## Further Reading
34-
35-
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
65+
- [micromatch](https://github.com/micromatch/micromatch)
66+
- [glob](https://en.wikipedia.org/wiki/Glob_(programming))

lib/rules/filename-naming-convention.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const micromatch = require('micromatch');
88
const isGlob = require('is-glob');
99
const { getFilename, getBasename } = require('../utils/filename');
10+
const { getDocUrl } = require('../utils/doc');
1011
const NAMING_CONVENTION = require('../constants/naming-convention');
1112

1213
/**
@@ -38,7 +39,7 @@ module.exports = {
3839
description: 'The filename should follow the filename naming convention',
3940
category: 'Layout & Formatting',
4041
recommended: false,
41-
url: null, // TODO: URL to the documentation page for this rule
42+
url: getDocUrl('filename-naming-convention'),
4243
},
4344
fixable: null,
4445
schema: [

lib/rules/folder-match-with-fex.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const micromatch = require('micromatch');
88
const { getFolder, getFilename } = require('../utils/filename');
9+
const { getDocUrl } = require('../utils/doc');
910

1011
/**
1112
* @type {import('eslint').Rule.RuleModule}
@@ -18,7 +19,7 @@ module.exports = {
1819
'The folder should match the naming pattern specified by the file extension',
1920
category: 'Layout & Formatting',
2021
recommended: false,
21-
url: 'https://github.com/DukeLuo/eslint-plugin-check-file/blob/main/docs/rules/folder-match-with-fex.md',
22+
url: getDocUrl('folder-match-with-fex'),
2223
},
2324
fixable: null,
2425
schema: [

lib/utils/doc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @fileoverview Utils about document
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
/**
8+
* @type {string} rule document url
9+
* @param {string} rule rule name
10+
*/
11+
const getDocUrl = (rule) =>
12+
`https://github.com/DukeLuo/eslint-plugin-check-file/blob/main/docs/rules/${rule}.md`;
13+
14+
module.exports = {
15+
getDocUrl,
16+
};

0 commit comments

Comments
 (0)