Skip to content

Commit cc5decf

Browse files
committed
fix(#25): rule filename-naming-convention should not lint sub folders other than the root one
1 parent 1a904b3 commit cc5decf

File tree

3 files changed

+125
-12
lines changed

3 files changed

+125
-12
lines changed

lib/utils/filename.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,29 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
3939
const getAllFolders = (p) => p.split(path.posix.sep).filter(isNotEmpty);
4040

4141
/**
42-
* @returns {string[]} all sub paths
42+
* @example
43+
* returns ['src/', 'src/DisplayLabel/', 'src/DisplayLabel/__tests__/', 'DisplayLabel/__tests__]
44+
* getSubPaths('src/DisplayLabel/__tests__/');
45+
* @returns {string[]} subpaths
4346
* @param {string} p path of folder in posix style
4447
*/
4548
const getSubPaths = (p) => {
4649
const folders = getAllFolders(p);
4750
let subPaths = [];
4851

49-
const handler = (array) =>
52+
const walk = (array) =>
5053
array.reduce((acc, folder, index) => {
51-
if (folder) {
52-
acc.push(
53-
index === 0
54-
? path.posix.join(folder, path.posix.sep)
55-
: path.posix.join(acc[acc.length - 1], folder, path.posix.sep)
56-
);
57-
}
58-
return acc;
59-
}, []);
54+
const subpath = path.posix.join(acc, folder, path.posix.sep);
55+
56+
if (index >= 1) subPaths.push(subpath);
57+
58+
return subpath;
59+
}, '');
6060

6161
for (let i = 0; i < folders.length; i++) {
62-
subPaths = subPaths.concat(handler(folders.slice(i)));
62+
walk(folders.slice(i));
6363
}
64+
subPaths.unshift(path.posix.join(folders[0], path.posix.sep));
6465

6566
return subPaths;
6667
};

tests/lib/rules/folder-naming-convention.posix.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,59 @@ ruleTester.run('filename-naming-convention with option: []', rule, {
308308
},
309309
],
310310
});
311+
312+
ruleTester.run(
313+
'filename-naming-convention with option: [{ "*": "KEBAB_CASE"}]',
314+
rule,
315+
{
316+
valid: [
317+
{
318+
code: "var foo = 'bar';",
319+
filename: 'src/Login/Utils/validationUtils.js',
320+
options: [{ '*': 'KEBAB_CASE' }],
321+
},
322+
{
323+
code: "var foo = 'bar';",
324+
filename: 'src/login/utils/validation.js',
325+
options: [{ '*': 'KEBAB_CASE' }],
326+
},
327+
{
328+
code: "var foo = 'bar';",
329+
filename: 'src/Index.js',
330+
options: [{ '*': 'KEBAB_CASE' }],
331+
},
332+
{
333+
code: "var foo = 'bar';",
334+
filename: 'main.js',
335+
options: [{ '*': 'KEBAB_CASE' }],
336+
},
337+
],
338+
339+
invalid: [
340+
{
341+
code: "var foo = 'bar';",
342+
filename: 'SRC/login/utils/validation.js',
343+
options: [{ '*': 'KEBAB_CASE' }],
344+
errors: [
345+
{
346+
message: 'The folder "SRC" does not match the "KEBAB_CASE" pattern',
347+
column: 1,
348+
line: 1,
349+
},
350+
],
351+
},
352+
{
353+
code: "var foo = 'bar';",
354+
filename: 'Src/index.js',
355+
options: [{ '*': 'KEBAB_CASE' }],
356+
errors: [
357+
{
358+
message: 'The folder "Src" does not match the "KEBAB_CASE" pattern',
359+
column: 1,
360+
line: 1,
361+
},
362+
],
363+
},
364+
],
365+
}
366+
);

tests/lib/rules/folder-naming-convention.windows.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,59 @@ ruleTester.run('filename-naming-convention with option on Windows: []', rule, {
311311
},
312312
],
313313
});
314+
315+
ruleTester.run(
316+
'filename-naming-convention with option on Windows: [{ "*": "KEBAB_CASE"}]',
317+
rule,
318+
{
319+
valid: [
320+
{
321+
code: "var foo = 'bar';",
322+
filename: 'src\\Login\\Utils\\validationUtils.js',
323+
options: [{ '*': 'KEBAB_CASE' }],
324+
},
325+
{
326+
code: "var foo = 'bar';",
327+
filename: 'src\\login\\utils\\validation.js',
328+
options: [{ '*': 'KEBAB_CASE' }],
329+
},
330+
{
331+
code: "var foo = 'bar';",
332+
filename: 'src\\Index.js',
333+
options: [{ '*': 'KEBAB_CASE' }],
334+
},
335+
{
336+
code: "var foo = 'bar';",
337+
filename: 'main.js',
338+
options: [{ '*': 'KEBAB_CASE' }],
339+
},
340+
],
341+
342+
invalid: [
343+
{
344+
code: "var foo = 'bar';",
345+
filename: 'SRC\\login\\utils\\validation.js',
346+
options: [{ '*': 'KEBAB_CASE' }],
347+
errors: [
348+
{
349+
message: 'The folder "SRC" does not match the "KEBAB_CASE" pattern',
350+
column: 1,
351+
line: 1,
352+
},
353+
],
354+
},
355+
{
356+
code: "var foo = 'bar';",
357+
filename: 'Src\\index.js',
358+
options: [{ '*': 'KEBAB_CASE' }],
359+
errors: [
360+
{
361+
message: 'The folder "Src" does not match the "KEBAB_CASE" pattern',
362+
column: 1,
363+
line: 1,
364+
},
365+
],
366+
},
367+
],
368+
}
369+
);

0 commit comments

Comments
 (0)