Skip to content

Commit e871e3c

Browse files
committed
feat: rule filename-naming-convention can ignore middle extensions when match pattern
1 parent 2e087cc commit e871e3c

File tree

3 files changed

+232
-4
lines changed

3 files changed

+232
-4
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ module.exports = {
3232
type: 'string',
3333
},
3434
},
35+
{
36+
type: 'object',
37+
properties: {
38+
ignoreMiddleExtensions: { type: 'boolean' },
39+
},
40+
},
3541
],
3642
},
3743

3844
create(context) {
3945
return {
4046
Program: (node) => {
4147
const rules = context.options[0];
48+
const { ignoreMiddleExtensions } = context.options[1] || {};
49+
4250
const invalidPattern = checkSettings(
4351
rules,
4452
globPatternValidator,
@@ -65,7 +73,7 @@ module.exports = {
6573
matchRule(
6674
filenameWithPath,
6775
fexPattern,
68-
getBasename(filename),
76+
getBasename(filename, ignoreMiddleExtensions),
6977
namingPattern
7078
) ||
7179
// legacy support for version below v1.2.0
@@ -74,7 +82,7 @@ module.exports = {
7482
matchRule(
7583
filename,
7684
fexPattern,
77-
getBasename(filename),
85+
getBasename(filename, ignoreMiddleExtensions),
7886
namingPattern
7987
);
8088

lib/utils/filename.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const getFolderPath = (path) => path.substring(0, path.lastIndexOf('/') + 1);
1919
/**
2020
* @type {string} base name
2121
* @param {string} filename filename without path
22+
* @param {boolean} [ignoreMiddleExtensions=false] flag to ignore middle extensions
2223
*/
23-
const getBasename = (filename) =>
24-
filename.substring(0, filename.lastIndexOf('.'));
24+
const getBasename = (filename, ignoreMiddleExtensions = false) =>
25+
filename.substring(
26+
0,
27+
ignoreMiddleExtensions ? filename.indexOf('.') : filename.lastIndexOf('.')
28+
);
2529

2630
/**
2731
* @type {string[]} all folders

tests/lib/rules/filename-naming-convention.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,3 +1003,219 @@ ruleTester.run(
10031003
],
10041004
}
10051005
);
1006+
1007+
ruleTester.run(
1008+
"filename-naming-convention with option: [{ '**/*.js': 'CAMEL_CASE' }, { ignoreMiddleExtensions: true }]",
1009+
rule,
1010+
{
1011+
valid: [
1012+
{
1013+
code: "var foo = 'bar';",
1014+
filename: 'src/utils/date.js',
1015+
options: [
1016+
{ '**/*.js': 'CAMEL_CASE' },
1017+
{ ignoreMiddleExtensions: true },
1018+
],
1019+
},
1020+
{
1021+
code: "var foo = 'bar';",
1022+
filename: 'src/utils/date.test.js',
1023+
options: [
1024+
{ '**/*.js': 'CAMEL_CASE' },
1025+
{ ignoreMiddleExtensions: true },
1026+
],
1027+
},
1028+
{
1029+
code: "var foo = 'bar';",
1030+
filename: 'src/utils/date.spec.js',
1031+
options: [
1032+
{ '**/*.js': 'CAMEL_CASE' },
1033+
{ ignoreMiddleExtensions: true },
1034+
],
1035+
},
1036+
{
1037+
code: "var foo = 'bar';",
1038+
filename: 'src/utils/date.spec.test.js',
1039+
options: [
1040+
{ '**/*.js': 'CAMEL_CASE' },
1041+
{ ignoreMiddleExtensions: true },
1042+
],
1043+
},
1044+
],
1045+
1046+
invalid: [
1047+
{
1048+
code: "var foo = 'bar';",
1049+
filename: 'src/utils/Date.js',
1050+
options: [
1051+
{ '**/*.js': 'CAMEL_CASE' },
1052+
{ ignoreMiddleExtensions: true },
1053+
],
1054+
errors: [
1055+
{
1056+
message:
1057+
'The filename "src/utils/Date.js" does not match the "CAMEL_CASE" style',
1058+
column: 1,
1059+
line: 1,
1060+
},
1061+
],
1062+
},
1063+
{
1064+
code: "var foo = 'bar';",
1065+
filename: 'src/utils/Date.test.js',
1066+
options: [
1067+
{ '**/*.js': 'CAMEL_CASE' },
1068+
{ ignoreMiddleExtensions: true },
1069+
],
1070+
errors: [
1071+
{
1072+
message:
1073+
'The filename "src/utils/Date.test.js" does not match the "CAMEL_CASE" style',
1074+
column: 1,
1075+
line: 1,
1076+
},
1077+
],
1078+
},
1079+
{
1080+
code: "var foo = 'bar';",
1081+
filename: 'src/utils/Date.spec.js',
1082+
options: [
1083+
{ '**/*.js': 'CAMEL_CASE' },
1084+
{ ignoreMiddleExtensions: true },
1085+
],
1086+
errors: [
1087+
{
1088+
message:
1089+
'The filename "src/utils/Date.spec.js" does not match the "CAMEL_CASE" style',
1090+
column: 1,
1091+
line: 1,
1092+
},
1093+
],
1094+
},
1095+
{
1096+
code: "var foo = 'bar';",
1097+
filename: 'src/utils/date_util.spec.js',
1098+
options: [
1099+
{ '**/*.js': 'CAMEL_CASE' },
1100+
{ ignoreMiddleExtensions: true },
1101+
],
1102+
errors: [
1103+
{
1104+
message:
1105+
'The filename "src/utils/date_util.spec.js" does not match the "CAMEL_CASE" style',
1106+
column: 1,
1107+
line: 1,
1108+
},
1109+
],
1110+
},
1111+
{
1112+
code: "var foo = 'bar';",
1113+
filename: 'src/utils/date_util.spec.test.js',
1114+
options: [
1115+
{ '**/*.js': 'CAMEL_CASE' },
1116+
{ ignoreMiddleExtensions: true },
1117+
],
1118+
errors: [
1119+
{
1120+
message:
1121+
'The filename "src/utils/date_util.spec.test.js" does not match the "CAMEL_CASE" style',
1122+
column: 1,
1123+
line: 1,
1124+
},
1125+
],
1126+
},
1127+
],
1128+
}
1129+
);
1130+
1131+
ruleTester.run(
1132+
"filename-naming-convention with option: [{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' }, { ignoreMiddleExtensions: false }]",
1133+
rule,
1134+
{
1135+
valid: [
1136+
{
1137+
code: "var foo = 'bar';",
1138+
filename: 'src/components/login.jsx',
1139+
options: [
1140+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1141+
{ ignoreMiddleExtensions: false },
1142+
],
1143+
},
1144+
{
1145+
code: "var foo = 'bar';",
1146+
filename: 'src/utils/calculatePrice.js',
1147+
options: [
1148+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1149+
{ ignoreMiddleExtensions: false },
1150+
],
1151+
},
1152+
],
1153+
1154+
invalid: [
1155+
{
1156+
code: "var foo = 'bar';",
1157+
filename: 'src/utils/CalculatePrice.js',
1158+
options: [
1159+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1160+
{ ignoreMiddleExtensions: false },
1161+
],
1162+
errors: [
1163+
{
1164+
message:
1165+
'The filename "CalculatePrice.js" does not match the "CAMEL_CASE" style',
1166+
column: 1,
1167+
line: 1,
1168+
},
1169+
],
1170+
},
1171+
{
1172+
code: "var foo = 'bar';",
1173+
filename: 'src/utils/calculate_price.js',
1174+
options: [
1175+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1176+
{ ignoreMiddleExtensions: false },
1177+
],
1178+
errors: [
1179+
{
1180+
message:
1181+
'The filename "calculate_price.js" does not match the "CAMEL_CASE" style',
1182+
column: 1,
1183+
line: 1,
1184+
},
1185+
],
1186+
},
1187+
{
1188+
code: "var foo = 'bar';",
1189+
filename: 'src/utils/calculate-price.js',
1190+
options: [
1191+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1192+
{ ignoreMiddleExtensions: false },
1193+
],
1194+
errors: [
1195+
{
1196+
message:
1197+
'The filename "calculate-price.js" does not match the "CAMEL_CASE" style',
1198+
column: 1,
1199+
line: 1,
1200+
},
1201+
],
1202+
},
1203+
{
1204+
code: "var foo = 'bar';",
1205+
filename: 'src/utils/CALCULATE_PRICE.js',
1206+
options: [
1207+
{ '*.js': 'CAMEL_CASE', '*.jsx': 'CAMEL_CASE' },
1208+
{ ignoreMiddleExtensions: false },
1209+
],
1210+
errors: [
1211+
{
1212+
message:
1213+
'The filename "CALCULATE_PRICE.js" does not match the "CAMEL_CASE" style',
1214+
column: 1,
1215+
line: 1,
1216+
},
1217+
],
1218+
},
1219+
],
1220+
}
1221+
);

0 commit comments

Comments
 (0)