Skip to content

Commit

Permalink
fix(dukeluo#25): rule filename-naming-convention should not lint sub …
Browse files Browse the repository at this point in the history
…folders other than the root one
  • Loading branch information
dukeluo authored and milos-sikic-nimbus-tech committed Jul 5, 2023
1 parent d5be3b1 commit b107552
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/utils/filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,29 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
const getAllFolders = (p) => p.split(path.posix.sep).filter(isNotEmpty);

/**
* @returns {string[]} all sub paths
* @example
* returns ['src/', 'src/DisplayLabel/', 'src/DisplayLabel/__tests__/', 'DisplayLabel/__tests__]
* getSubPaths('src/DisplayLabel/__tests__/');
* @returns {string[]} subpaths
* @param {string} p path of folder in posix style
*/
const getSubPaths = (p) => {
const folders = getAllFolders(p);
let subPaths = [];

const handler = (array) =>
const walk = (array) =>
array.reduce((acc, folder, index) => {
if (folder) {
acc.push(
index === 0
? path.posix.join(folder, path.posix.sep)
: path.posix.join(acc[acc.length - 1], folder, path.posix.sep)
);
}
return acc;
}, []);
const subpath = path.posix.join(acc, folder, path.posix.sep);

if (index >= 1) subPaths.push(subpath);

return subpath;
}, '');

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

return subPaths;
};
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/rules/folder-naming-convention.posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,59 @@ ruleTester.run('filename-naming-convention with option: []', rule, {
},
],
});

ruleTester.run(
'filename-naming-convention with option: [{ "*": "KEBAB_CASE"}]',
rule,
{
valid: [
{
code: "var foo = 'bar';",
filename: 'src/Login/Utils/validationUtils.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'src/login/utils/validation.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'src/Index.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'main.js',
options: [{ '*': 'KEBAB_CASE' }],
},
],

invalid: [
{
code: "var foo = 'bar';",
filename: 'SRC/login/utils/validation.js',
options: [{ '*': 'KEBAB_CASE' }],
errors: [
{
message: 'The folder "SRC" does not match the "KEBAB_CASE" pattern',
column: 1,
line: 1,
},
],
},
{
code: "var foo = 'bar';",
filename: 'Src/index.js',
options: [{ '*': 'KEBAB_CASE' }],
errors: [
{
message: 'The folder "Src" does not match the "KEBAB_CASE" pattern',
column: 1,
line: 1,
},
],
},
],
}
);
56 changes: 56 additions & 0 deletions tests/lib/rules/folder-naming-convention.windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,59 @@ ruleTester.run('filename-naming-convention with option on Windows: []', rule, {
},
],
});

ruleTester.run(
'filename-naming-convention with option on Windows: [{ "*": "KEBAB_CASE"}]',
rule,
{
valid: [
{
code: "var foo = 'bar';",
filename: 'src\\Login\\Utils\\validationUtils.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'src\\login\\utils\\validation.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'src\\Index.js',
options: [{ '*': 'KEBAB_CASE' }],
},
{
code: "var foo = 'bar';",
filename: 'main.js',
options: [{ '*': 'KEBAB_CASE' }],
},
],

invalid: [
{
code: "var foo = 'bar';",
filename: 'SRC\\login\\utils\\validation.js',
options: [{ '*': 'KEBAB_CASE' }],
errors: [
{
message: 'The folder "SRC" does not match the "KEBAB_CASE" pattern',
column: 1,
line: 1,
},
],
},
{
code: "var foo = 'bar';",
filename: 'Src\\index.js',
options: [{ '*': 'KEBAB_CASE' }],
errors: [
{
message: 'The folder "Src" does not match the "KEBAB_CASE" pattern',
column: 1,
line: 1,
},
],
},
],
}
);

0 comments on commit b107552

Please sign in to comment.