From b107552209852e94cb59d1257b42d41dd6745c52 Mon Sep 17 00:00:00 2001 From: Huan Luo Date: Thu, 8 Jun 2023 00:18:29 +0800 Subject: [PATCH] fix(#25): rule filename-naming-convention should not lint sub folders other than the root one --- lib/utils/filename.js | 25 +++++---- .../rules/folder-naming-convention.posix.js | 56 +++++++++++++++++++ .../rules/folder-naming-convention.windows.js | 56 +++++++++++++++++++ 3 files changed, 125 insertions(+), 12 deletions(-) diff --git a/lib/utils/filename.js b/lib/utils/filename.js index a302192..3af9b4a 100644 --- a/lib/utils/filename.js +++ b/lib/utils/filename.js @@ -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; }; diff --git a/tests/lib/rules/folder-naming-convention.posix.js b/tests/lib/rules/folder-naming-convention.posix.js index 824ee46..1705f8c 100644 --- a/tests/lib/rules/folder-naming-convention.posix.js +++ b/tests/lib/rules/folder-naming-convention.posix.js @@ -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, + }, + ], + }, + ], + } +); diff --git a/tests/lib/rules/folder-naming-convention.windows.js b/tests/lib/rules/folder-naming-convention.windows.js index 0e5ea61..f749d0d 100644 --- a/tests/lib/rules/folder-naming-convention.windows.js +++ b/tests/lib/rules/folder-naming-convention.windows.js @@ -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, + }, + ], + }, + ], + } +);