Skip to content

Commit e6d9ba6

Browse files
committed
feat(#26): rule no-index can ignore middle extensions
1 parent acb926d commit e6d9ba6

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

docs/rules/no-index.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@ Examples of **correct** filename for this rule:
1919
calculatePrice.js
2020
login.tsx
2121
```
22+
23+
### Options
24+
25+
#### rule configuration object
26+
27+
##### `ignoreMiddleExtensions`
28+
29+
If `true`, the rule will ignore the middle extensions of the filename.
30+
31+
In some cases, you may want to ignore the middle extensions of the filename. For example, you want to lint the base name of the config files, e.g. `index.config.js`, you can do so by setting the `ignoreMiddleExtensions` option to `true`, and the rule will only validate its base name, in this case the base name will be `index`.
32+
33+
```js
34+
module.exports = {
35+
plugins: ['check-file'],
36+
rules: {
37+
'check-file/no-index': [
38+
'error',
39+
{
40+
ignoreMiddleExtensions: true,
41+
},
42+
],
43+
},
44+
};
45+
```

lib/rules/no-index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ module.exports = {
2121
url: getDocUrl('no-index'),
2222
},
2323
fixable: null,
24-
schema: [],
24+
schema: [
25+
{
26+
type: 'object',
27+
properties: {
28+
ignoreMiddleExtensions: { type: 'boolean' },
29+
},
30+
},
31+
],
2532
},
2633

2734
create(context) {
2835
return {
2936
Program: (node) => {
37+
const { ignoreMiddleExtensions } = context.options[0] || {};
3038
const filenameWithPath = getFilePath(context);
3139
const filename = getFilename(filenameWithPath);
32-
const basename = getBasename(filename);
40+
const basename = getBasename(filename, ignoreMiddleExtensions);
3341

3442
if (basename === 'index') {
3543
context.report({

tests/lib/rules/no-index.posix.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ ruleTester.run('no-index', rule, {
2323
code: "var foo = 'bar';",
2424
filename: 'src/utils/calculatePrice.js',
2525
},
26+
{
27+
code: "var foo = 'bar';",
28+
filename: 'src/utils/index.config.js',
29+
},
30+
{
31+
code: "var foo = 'bar';",
32+
filename: 'index.config.ts',
33+
},
2634
],
2735

2836
invalid: [
@@ -50,3 +58,73 @@ ruleTester.run('no-index', rule, {
5058
},
5159
],
5260
});
61+
62+
ruleTester.run(
63+
'no-index with option: [{ ignoreMiddleExtensions: true }]',
64+
rule,
65+
{
66+
valid: [
67+
{
68+
code: "var foo = 'bar';",
69+
filename: 'src/components/login.jsx',
70+
options: [{ ignoreMiddleExtensions: true }],
71+
},
72+
{
73+
code: "var foo = 'bar';",
74+
filename: 'calculatePrice.js',
75+
options: [{ ignoreMiddleExtensions: true }],
76+
},
77+
],
78+
79+
invalid: [
80+
{
81+
code: "var foo = 'bar';",
82+
filename: 'src/utils/index.js',
83+
options: [{ ignoreMiddleExtensions: true }],
84+
errors: [
85+
{
86+
message: 'The filename "index" is not allowed',
87+
column: 1,
88+
line: 1,
89+
},
90+
],
91+
},
92+
{
93+
code: "var foo = 'bar';",
94+
filename: 'index.ts',
95+
options: [{ ignoreMiddleExtensions: true }],
96+
errors: [
97+
{
98+
message: 'The filename "index" is not allowed',
99+
column: 1,
100+
line: 1,
101+
},
102+
],
103+
},
104+
{
105+
code: "var foo = 'bar';",
106+
filename: 'src/utils/index.config.js',
107+
options: [{ ignoreMiddleExtensions: true }],
108+
errors: [
109+
{
110+
message: 'The filename "index" is not allowed',
111+
column: 1,
112+
line: 1,
113+
},
114+
],
115+
},
116+
{
117+
code: "var foo = 'bar';",
118+
filename: 'index.config.ts',
119+
options: [{ ignoreMiddleExtensions: true }],
120+
errors: [
121+
{
122+
message: 'The filename "index" is not allowed',
123+
column: 1,
124+
line: 1,
125+
},
126+
],
127+
},
128+
],
129+
}
130+
);

tests/lib/rules/no-index.windows.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ ruleTester.run('no-index on Windows', rule, {
2323
code: "var foo = 'bar';",
2424
filename: 'src\\main.ts',
2525
},
26+
{
27+
code: "var foo = 'bar';",
28+
filename: 'src\\utils\\index.config.js',
29+
},
30+
{
31+
code: "var foo = 'bar';",
32+
filename: 'index.config.ts',
33+
},
2634
],
2735

2836
invalid: [
@@ -50,3 +58,73 @@ ruleTester.run('no-index on Windows', rule, {
5058
},
5159
],
5260
});
61+
62+
ruleTester.run(
63+
'no-index with option on Windows: [{ ignoreMiddleExtensions: true }]',
64+
rule,
65+
{
66+
valid: [
67+
{
68+
code: "var foo = 'bar';",
69+
filename: 'src\\components\\login.jsx',
70+
options: [{ ignoreMiddleExtensions: true }],
71+
},
72+
{
73+
code: "var foo = 'bar';",
74+
filename: 'calculatePrice.js',
75+
options: [{ ignoreMiddleExtensions: true }],
76+
},
77+
],
78+
79+
invalid: [
80+
{
81+
code: "var foo = 'bar';",
82+
filename: 'src\\utils\\index.js',
83+
options: [{ ignoreMiddleExtensions: true }],
84+
errors: [
85+
{
86+
message: 'The filename "index" is not allowed',
87+
column: 1,
88+
line: 1,
89+
},
90+
],
91+
},
92+
{
93+
code: "var foo = 'bar';",
94+
filename: 'index.ts',
95+
options: [{ ignoreMiddleExtensions: true }],
96+
errors: [
97+
{
98+
message: 'The filename "index" is not allowed',
99+
column: 1,
100+
line: 1,
101+
},
102+
],
103+
},
104+
{
105+
code: "var foo = 'bar';",
106+
filename: 'src\\utils\\index.config.js',
107+
options: [{ ignoreMiddleExtensions: true }],
108+
errors: [
109+
{
110+
message: 'The filename "index" is not allowed',
111+
column: 1,
112+
line: 1,
113+
},
114+
],
115+
},
116+
{
117+
code: "var foo = 'bar';",
118+
filename: 'index.config.ts',
119+
options: [{ ignoreMiddleExtensions: true }],
120+
errors: [
121+
{
122+
message: 'The filename "index" is not allowed',
123+
column: 1,
124+
line: 1,
125+
},
126+
],
127+
},
128+
],
129+
}
130+
);

0 commit comments

Comments
 (0)