Skip to content

Commit 05a4405

Browse files
committed
feat: add rule no-index
1 parent cd9fb60 commit 05a4405

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

docs/rules/no-index.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# A file cannot be named 'index' (no-index)
2+
3+
Please describe the origin of the rule here.
4+
5+
## Rule Details
6+
7+
This rule aims to...
8+
9+
Examples of **incorrect** code for this rule:
10+
11+
```js
12+
13+
// fill me in
14+
15+
```
16+
17+
Examples of **correct** code for this rule:
18+
19+
```js
20+
21+
// fill me in
22+
23+
```
24+
25+
### Options
26+
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.
32+
33+
## Further Reading
34+
35+
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.

lib/rules/no-index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @fileoverview A file cannot be named "index"
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
const { getDocUrl } = require('../utils/doc');
8+
const { getFilename, getBasename } = require('../utils/filename');
9+
10+
/**
11+
* @type {import('eslint').Rule.RuleModule}
12+
*/
13+
module.exports = {
14+
meta: {
15+
type: 'layout',
16+
docs: {
17+
description: 'A file cannot be named "index"',
18+
category: 'Layout & Formatting',
19+
recommended: false,
20+
url: getDocUrl('no-index'),
21+
},
22+
fixable: null,
23+
schema: [],
24+
},
25+
26+
create(context) {
27+
return {
28+
Program: (node) => {
29+
const filenameWithPath = context.getFilename();
30+
const filename = getFilename(filenameWithPath);
31+
const basename = getBasename(filename);
32+
33+
if (basename === 'index') {
34+
context.report({
35+
node,
36+
message:
37+
'The filename "index" is not allowed, please use another one',
38+
});
39+
return;
40+
}
41+
},
42+
};
43+
},
44+
};

tests/lib/rules/no-index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @fileoverview A file cannot be named "index"
3+
* @author Duke Luo
4+
*/
5+
'use strict';
6+
7+
const rule = require('../../../lib/rules/no-index');
8+
const RuleTester = require('eslint').RuleTester;
9+
10+
const ruleTester = new RuleTester();
11+
12+
ruleTester.run('no-index', rule, {
13+
valid: [
14+
{
15+
code: "var foo = 'bar';",
16+
filename: 'src/components/login.jsx',
17+
},
18+
{
19+
code: "var foo = 'bar';",
20+
filename: 'src/utils/calculatePrice.js',
21+
},
22+
],
23+
24+
invalid: [
25+
{
26+
code: "var foo = 'bar';",
27+
filename: 'src/utils/index.js',
28+
errors: [
29+
{
30+
message:
31+
'The filename "index" is not allowed, please use another one',
32+
column: 1,
33+
line: 1,
34+
},
35+
],
36+
},
37+
{
38+
code: "var foo = 'bar';",
39+
filename: 'src/utils/index.ts',
40+
errors: [
41+
{
42+
message:
43+
'The filename "index" is not allowed, please use another one',
44+
column: 1,
45+
line: 1,
46+
},
47+
],
48+
},
49+
],
50+
});

0 commit comments

Comments
 (0)