From e60701c4e0acfc84a62d6b1867ed4771dea42e18 Mon Sep 17 00:00:00 2001 From: Igor Kowalski Date: Fri, 1 Sep 2023 00:26:06 +0200 Subject: [PATCH] 1.4.7 --- .husky/pre-commit | 6 +++--- jest.config.ts | 1 + package.json | 13 ++++++------- src/helpers/getIsFileFromNodeName.ts | 2 -- src/helpers/getIsFileFromPathName.test.ts | 15 --------------- src/helpers/getIsFileFromPathName.ts | 2 -- src/helpers/getNodeType.ts | 4 ++-- src/helpers/isFileFromNodeName.ts | 2 ++ src/helpers/isFileFromPathname.test.ts | 15 +++++++++++++++ src/helpers/isFileFromPathname.ts | 2 ++ .../validateChildren/helpers/filterRulesByType.ts | 4 ++-- .../validateName/helpers/getInvalidRegexError.ts | 4 ++++ src/validators/validateName/helpers/getIsRegex.ts | 1 - src/validators/validateName/helpers/isRegex.ts | 1 + .../validateName/helpers/isRegexInvalid.ts | 9 +++++++++ src/validators/validateName/validateName.test.ts | 11 +++++++++++ src/validators/validateName/validateName.ts | 7 +++++-- .../validatePath/helpers/getNodeName.ts | 4 ++-- 18 files changed, 65 insertions(+), 38 deletions(-) delete mode 100644 src/helpers/getIsFileFromNodeName.ts delete mode 100644 src/helpers/getIsFileFromPathName.test.ts delete mode 100644 src/helpers/getIsFileFromPathName.ts create mode 100644 src/helpers/isFileFromNodeName.ts create mode 100644 src/helpers/isFileFromPathname.test.ts create mode 100644 src/helpers/isFileFromPathname.ts create mode 100644 src/validators/validateName/helpers/getInvalidRegexError.ts delete mode 100644 src/validators/validateName/helpers/getIsRegex.ts create mode 100644 src/validators/validateName/helpers/isRegex.ts create mode 100644 src/validators/validateName/helpers/isRegexInvalid.ts diff --git a/.husky/pre-commit b/.husky/pre-commit index ac2789a..59067a4 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -3,9 +3,9 @@ echo 'šŸ•µļøā€ā™‚ļø Checking your crappy code šŸ•µļøā€ā™‚ļø' -echo 'šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø dead-code:check šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø' -yarn dead-code:check || (echo 'šŸ¤¢šŸ¤®šŸ¤¢ dead-code:check failed šŸ¤¢šŸ¤®šŸ¤¢'; false) -echo 'āœ…āœ…āœ… dead-code:check āœ…āœ…āœ…' +echo 'šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø deadCode:check šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø' +yarn deadCode:check || (echo 'šŸ¤¢šŸ¤®šŸ¤¢ deadCode:check failed šŸ¤¢šŸ¤®šŸ¤¢'; false) +echo 'āœ…āœ…āœ… deadCode:check āœ…āœ…āœ…' echo 'šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø lint:check šŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļøšŸ•µļøā€ā™‚ļø' yarn lint:check || (echo 'šŸ¤¢šŸ¤®šŸ¤¢ lint:check failed šŸ¤¢šŸ¤®šŸ¤¢'; false); diff --git a/jest.config.ts b/jest.config.ts index 974d48e..316a58d 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -1,6 +1,7 @@ import type { JestConfigWithTsJest } from "ts-jest"; const jestConfig: JestConfigWithTsJest = { + maxWorkers: "90%", testEnvironment: "node", transform: { "^.+\\.tsx?$": [ diff --git a/package.json b/package.json index 2ebb48e..e87d765 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "Igor Kowalski (Igorkowalski94)", "name": "eslint-plugin-project-structure", - "version": "1.4.6", + "version": "1.4.7", "license": "MIT", "description": "Eslint plugin that allows you to enforce rules on project structure. Folder and file naming, folder structure, file extensions all under your control to keep your repo consistent even in large teams.", "keywords": [ @@ -34,17 +34,16 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" }, "scripts": { + "clean": "del -rf dist", "build": "tsc -p tsconfig.build.json && node esbuild.config.js", - "dead-code:check": "ts-prune --error", - "types:check": "tsc", "format": "prettier --write --config .prettierrc.json .", "format:check": "prettier --check --config .prettierrc.json .", - "lint": "eslint src --max-warnings 0", + "deadCode:check": "ts-prune --error", + "types:check": "tsc", "lint:check": "eslint src --max-warnings 0", - "clean": "del -rf dist", - "test": "jest --coverage --watch", "test:check": "jest --coverage --watchAll=false --bail", - "test:current": "jest --coverage --changedSince=origin/main", + "test": "jest --coverage --watch", + "checkAll": "yarn deadCode:check && yarn format:check && yarn lint:check && yarn test:check", "husky:prepare": "husky install" }, "dependencies": { diff --git a/src/helpers/getIsFileFromNodeName.ts b/src/helpers/getIsFileFromNodeName.ts deleted file mode 100644 index aaa6cd1..0000000 --- a/src/helpers/getIsFileFromNodeName.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const getIsFileFromNodeName = (nodeName: string): boolean => - nodeName.includes("."); diff --git a/src/helpers/getIsFileFromPathName.test.ts b/src/helpers/getIsFileFromPathName.test.ts deleted file mode 100644 index 0f9f768..0000000 --- a/src/helpers/getIsFileFromPathName.test.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getIsFileFromPathname } from "./getIsFileFromPathName"; - -describe("getIsFileFromNodeName", () => { - it("should return false when pathname includes /", () => { - expect(getIsFileFromPathname("src/componentName")).toEqual(false); - }); - - it("should return false when pathname includes \\", () => { - expect(getIsFileFromPathname("src\\componentName")).toEqual(false); - }); - - it("should return true when pathname not includes / or \\", () => { - expect(getIsFileFromPathname("componentName")).toEqual(true); - }); -}); diff --git a/src/helpers/getIsFileFromPathName.ts b/src/helpers/getIsFileFromPathName.ts deleted file mode 100644 index 2f0337d..0000000 --- a/src/helpers/getIsFileFromPathName.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const getIsFileFromPathname = (pathname: string): boolean => - !(pathname.includes("/") || pathname.includes("\\")); diff --git a/src/helpers/getNodeType.ts b/src/helpers/getNodeType.ts index a5db8fb..9a63625 100644 --- a/src/helpers/getNodeType.ts +++ b/src/helpers/getNodeType.ts @@ -1,5 +1,5 @@ -import { getIsFileFromNodeName } from "./getIsFileFromNodeName"; +import { isFileFromNodeName } from "./isFileFromNodeName"; import { NodeType } from "../types"; export const getNodeType = (nodeName: string): NodeType => - getIsFileFromNodeName(nodeName) ? "File" : "Folder"; + isFileFromNodeName(nodeName) ? "File" : "Folder"; diff --git a/src/helpers/isFileFromNodeName.ts b/src/helpers/isFileFromNodeName.ts new file mode 100644 index 0000000..26b296c --- /dev/null +++ b/src/helpers/isFileFromNodeName.ts @@ -0,0 +1,2 @@ +export const isFileFromNodeName = (nodeName: string): boolean => + nodeName.includes("."); diff --git a/src/helpers/isFileFromPathname.test.ts b/src/helpers/isFileFromPathname.test.ts new file mode 100644 index 0000000..8bbc848 --- /dev/null +++ b/src/helpers/isFileFromPathname.test.ts @@ -0,0 +1,15 @@ +import { isFileFromPathname } from "./isFileFromPathname"; + +describe("isFileFromPathname", () => { + it("should return false when pathname includes /", () => { + expect(isFileFromPathname("src/componentName")).toEqual(false); + }); + + it("should return false when pathname includes \\", () => { + expect(isFileFromPathname("src\\componentName")).toEqual(false); + }); + + it("should return true when pathname not includes / or \\", () => { + expect(isFileFromPathname("componentName")).toEqual(true); + }); +}); diff --git a/src/helpers/isFileFromPathname.ts b/src/helpers/isFileFromPathname.ts new file mode 100644 index 0000000..f1f44a1 --- /dev/null +++ b/src/helpers/isFileFromPathname.ts @@ -0,0 +1,2 @@ +export const isFileFromPathname = (pathname: string): boolean => + !(pathname.includes("/") || pathname.includes("\\")); diff --git a/src/validators/validateChildren/helpers/filterRulesByType.ts b/src/validators/validateChildren/helpers/filterRulesByType.ts index 76db70c..52007ac 100644 --- a/src/validators/validateChildren/helpers/filterRulesByType.ts +++ b/src/validators/validateChildren/helpers/filterRulesByType.ts @@ -1,5 +1,5 @@ -import { getIsFileFromPathname } from "../../../helpers/getIsFileFromPathName"; import { getNodeRule } from "../../../helpers/getNodeRule/getNodeRule"; +import { isFileFromPathname } from "../../../helpers/isFileFromPathname"; import { ProjectStructureConfig, Rule } from "../../../types"; export interface FilterRulesByType { @@ -15,7 +15,7 @@ export const filterRulesByType = ({ }: FilterRulesByType): boolean => { const nodeRule = getNodeRule(rule, config); - const isFile = getIsFileFromPathname(pathname); + const isFile = isFileFromPathname(pathname); const isFolderNode = !!nodeRule.children; const isFileNode = !!nodeRule.extension; diff --git a/src/validators/validateName/helpers/getInvalidRegexError.ts b/src/validators/validateName/helpers/getInvalidRegexError.ts new file mode 100644 index 0000000..6976599 --- /dev/null +++ b/src/validators/validateName/helpers/getInvalidRegexError.ts @@ -0,0 +1,4 @@ +import { FinalError } from "../../../errors/FinalError/FinalError"; + +export const getInvalidRegexError = (regex: string): FinalError => + new FinalError(`\n\nšŸ”„ Regex: ${regex} is invalid. šŸ”„\n\n`); diff --git a/src/validators/validateName/helpers/getIsRegex.ts b/src/validators/validateName/helpers/getIsRegex.ts deleted file mode 100644 index 602e1d5..0000000 --- a/src/validators/validateName/helpers/getIsRegex.ts +++ /dev/null @@ -1 +0,0 @@ -export const getIsRegex = (regex: string): boolean => /^\/(.+)\/$/.test(regex); diff --git a/src/validators/validateName/helpers/isRegex.ts b/src/validators/validateName/helpers/isRegex.ts new file mode 100644 index 0000000..284b0c6 --- /dev/null +++ b/src/validators/validateName/helpers/isRegex.ts @@ -0,0 +1 @@ +export const isRegex = (regex: string): boolean => /^\/(.+)\/$/.test(regex); diff --git a/src/validators/validateName/helpers/isRegexInvalid.ts b/src/validators/validateName/helpers/isRegexInvalid.ts new file mode 100644 index 0000000..17a591c --- /dev/null +++ b/src/validators/validateName/helpers/isRegexInvalid.ts @@ -0,0 +1,9 @@ +export const isRegexInvalid = (regex: string): boolean => { + try { + new RegExp(regex); + } catch (e) { + return true; + } + + return false; +}; diff --git a/src/validators/validateName/validateName.test.ts b/src/validators/validateName/validateName.test.ts index 3c5ee95..75af3f8 100644 --- a/src/validators/validateName/validateName.test.ts +++ b/src/validators/validateName/validateName.test.ts @@ -1,4 +1,5 @@ import { getInvalidNameError } from "./helpers/getInvalidNameError"; +import { getInvalidRegexError } from "./helpers/getInvalidRegexError"; import { getNameError } from "./helpers/getNameError"; import { getNameRegexError } from "./helpers/validateRegexPattern/helpers/getNameRegexError"; import { validateName } from "./validateName"; @@ -17,6 +18,16 @@ describe("validateName", () => { }, ); + it("should throw error when regex is invalid", () => { + expect(() => + validateName({ + nodeName: "componentName.api", + ruleName: "/^?/", + parentName: "parentName", + }), + ).toThrow(getInvalidRegexError("/^?/")); + }); + it("should not throw error when nodeName match regex", () => { expect(() => validateName({ diff --git a/src/validators/validateName/validateName.ts b/src/validators/validateName/validateName.ts index e32efa7..13d1f49 100644 --- a/src/validators/validateName/validateName.ts +++ b/src/validators/validateName/validateName.ts @@ -1,6 +1,8 @@ import { getInvalidNameError } from "./helpers/getInvalidNameError"; -import { getIsRegex } from "./helpers/getIsRegex"; +import { getInvalidRegexError } from "./helpers/getInvalidRegexError"; import { getNameError } from "./helpers/getNameError"; +import { isRegex } from "./helpers/isRegex"; +import { isRegexInvalid } from "./helpers/isRegexInvalid"; import { validateRegexPattern } from "./helpers/validateRegexPattern/validateRegexPattern"; import { RegexParameters } from "../../types"; @@ -18,8 +20,9 @@ export const validateName = ({ regexParameters, }: ValidateName): void => { if (typeof ruleName !== "string") throw getInvalidNameError(ruleName); + if (isRegexInvalid(ruleName)) throw getInvalidRegexError(ruleName); - if (getIsRegex(ruleName)) + if (isRegex(ruleName)) return validateRegexPattern({ nodeName, parentName, diff --git a/src/validators/validatePath/helpers/getNodeName.ts b/src/validators/validatePath/helpers/getNodeName.ts index 2436529..7948473 100644 --- a/src/validators/validatePath/helpers/getNodeName.ts +++ b/src/validators/validatePath/helpers/getNodeName.ts @@ -1,7 +1,7 @@ import { sep } from "path"; import { getFileNameWithoutExtension } from "./getFileNameWithoutExtension"; -import { getIsFileFromPathname } from "../../../helpers/getIsFileFromPathName"; +import { isFileFromPathname } from "../../../helpers/isFileFromPathname"; interface GetNodeNameReturn { nodeName: string; @@ -9,7 +9,7 @@ interface GetNodeNameReturn { } export const getNodeName = (pathname: string): GetNodeNameReturn => { - const isFile = getIsFileFromPathname(pathname); + const isFile = isFileFromPathname(pathname); const currentNodeName = pathname.split(sep)[0];