Skip to content

Commit

Permalink
2.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorkowalski94 committed Sep 26, 2024
1 parent 9d8a09f commit 9229337
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 127 deletions.
6 changes: 3 additions & 3 deletions fileComposition.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const fileCompositionConfig = createFileComposition({
allowOnlySpecifiedSelectors: true,
rules: [
{
selector: "variable",
selector: ["variable", "variableExpression"],
format: "{SNAKE_CASE}",
},
],
Expand Down Expand Up @@ -41,7 +41,7 @@ export const fileCompositionConfig = createFileComposition({
allowOnlySpecifiedSelectors: true,
rules: [
{
selector: "variable",
selector: { type: "variableExpression", limitTo: "ESLintUtils" },
format: "{fileName}",
},
],
Expand Down Expand Up @@ -75,7 +75,7 @@ export const fileCompositionConfig = createFileComposition({
"arrowFunction",
"function",
"variable",
"variableCallExpression",
"variableExpression",
],
format: "{camelCase}",
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Igor Kowalski (Igorkowalski94)",
"name": "eslint-plugin-project-structure",
"version": "2.7.0",
"version": "2.7.1",
"license": "MIT",
"description": "ESLint plugin with rules to help you achieve a scalable, consistent, and well-structured project. Define your folder structure, advanced naming conventions, file composition, and create independent modules. react folder structure react file structure react project structure react conventions architecture react next.js angular node solid vue svelte",
"keywords": [
Expand Down Expand Up @@ -81,7 +81,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-project-structure": "2.6.3",
"eslint-plugin-project-structure": "2.7.0",
"husky": "^9.1.6",
"jest": "^29.7.0",
"prettier": "^3.3.3",
Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable project-structure/file-composition */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
const SNAKE_CASE_LOWER_RE = /((([a-z]|\d)+_)*([a-z]|\d)+)/;
const SNAKE_CASE_UPPER_RE = /((([A-Z]|\d)+_)*([A-Z]|\d)+)/;
Expand All @@ -18,6 +17,7 @@ export const STRICT_CAMEL_CASE = `${STRICT_CAMEL_CASE_RE}`.replace(/\//g, "");
export const STRICT_PASCAL_CASE = `${STRICT_PASCAL_CASE_RE}`.replace(/\//g, "");

export const RECURSION_LIMIT = 1000;
export const WILDCARD_REGEX = "(([^/]*)+)";

export const ESLINT_ERRORS = {
error: `{{error}}`,
Expand Down
8 changes: 4 additions & 4 deletions src/rules/fileComposition/fileComposition.consts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const ESLINT_ERRORS = {
invalidName: `🔥 Invalid {{selectorKey}} name, allowed formats = {{formatWithoutReferences}} 🔥`,
prohibitedSelector: `🔥 The use of '{{selectorKey}}' is prohibited in this file. 🔥{{error}}`,
prohibitedSelectorRoot: `🔥 The use of '{{selectorKey}}' is prohibited in the root of the file. 🔥{{error}}`,
prohibitedSelectorExport: `🔥 Exporting '{{selectorKey}}' is prohibited in this file. 🔥{{error}}`,
invalidName: `🔥 Invalid {{selectorType}} name, allowed formats = {{formatWithoutReferences}} 🔥`,
prohibitedSelector: `🔥 The use of '{{selectorType}}' is prohibited in this file. 🔥{{error}}`,
prohibitedSelectorRoot: `🔥 The use of '{{selectorType}}' is prohibited in the root of the file. 🔥{{error}}`,
prohibitedSelectorExport: `🔥 Exporting '{{selectorType}}' is prohibited in this file. 🔥{{error}}`,
rootSelectorsLimits:
"🔥 The limit for the given selectors in the root of the file has been exceeded. 🔥\n{{error}}\n\n",
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getInvalidRegexError } from "errors/getInvalidRegexError";

import {
SelectorType,
FileRule,
Expand All @@ -7,74 +9,96 @@ import { isCorrectSelector } from "rules/fileComposition/helpers/validateFile/he
describe("isCorrectNameType", () => {
test.each<{
selector: FileRule["selector"];
selectorKey: SelectorType;
selectorType: SelectorType;
expected: boolean;
expressionName?: string;
}>([
{
selector: "arrowFunction",
selectorKey: "arrowFunction",
selectorType: "arrowFunction",
expected: true,
},
{
selector: ["arrowFunction"],
selectorKey: "arrowFunction",
selectorType: "arrowFunction",
expected: true,
},
{
selector: "class",
selectorKey: "arrowFunction",
selectorType: "arrowFunction",
expected: false,
},
{
selector: ["class"],
selectorKey: "arrowFunction",
selectorType: "arrowFunction",
expected: false,
},
{
selector: { type: "variableExpression", limitTo: "styled" },
selectorKey: "variableExpression",
selectorType: "variableExpression",
expressionName: "styled",
expected: true,
},
{
selector: [{ type: "variableExpression", limitTo: "styled" }],
selectorKey: "variableExpression",
selectorType: "variableExpression",
expressionName: "styled",
expected: true,
},
{
selector: { type: "variableExpression", limitTo: "styled" },
selectorKey: "variableExpression",
selectorType: "variableExpression",
expressionName: "css",
expected: false,
},
{
selector: { type: "variableExpression", limitTo: "*" },
selectorType: "variableExpression",
expressionName: "css",
expected: true,
},
{
selector: { type: "variableExpression", limitTo: "(?!css)*" },
selectorType: "variableExpression",
expressionName: "css",
expected: false,
},
{
selector: [{ type: "variableExpression", limitTo: "styled" }],
selectorKey: "variableExpression",
selectorType: "variableExpression",
expressionName: "css",
expected: false,
},
{
selector: { type: "variableExpression", limitTo: "styled" },
selectorKey: "variable",
selectorType: "variable",
expected: false,
},
{
selector: [{ type: "variableExpression", limitTo: "styled" }],
selectorKey: "variable",
selectorType: "variable",
expected: false,
},
])(
"Should return correct values for %o",
({ selectorKey, selector, expressionName, expected }) => {
({ selectorType, selector, expressionName, expected }) => {
expect(
isCorrectSelector({
selectorKey,
selectorType,
selector,
expressionName,
}),
).toEqual(expected);
},
);

test("Should throw when regex is invalid", () => {
expect(() =>
isCorrectSelector({
selector: { type: "variableExpression", limitTo: "^?" },
selectorType: "variableExpression",
expressionName: "expressionName",
}),
).toThrow(getInvalidRegexError("^?"));
});
});
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
import { WILDCARD_REGEX } from "consts";

import { getInvalidRegexError } from "errors/getInvalidRegexError";

import { isRegexInvalid } from "helpers/isRegexInvalid";

import {
Selector,
SelectorType,
} from "rules/fileComposition/fileComposition.types";

interface IsCorrectSelectorProps {
selector: Selector | Selector[];
selectorKey: SelectorType;
selectorType: SelectorType;
expressionName?: string;
}

export const isCorrectSelector = ({
selector,
selectorKey,
selectorType,
expressionName,
}: IsCorrectSelectorProps): boolean => {
if (typeof selector === "string") return selector === selectorKey;
if (typeof selector === "string") return selector === selectorType;

if (!Array.isArray(selector)) {
if (!expressionName) return false;

return (
selector.type === selectorKey && selector.limitTo.includes(expressionName)
if (typeof selector.limitTo === "string") {
const regexImproved = selector.limitTo
.replaceAll("*", WILDCARD_REGEX)
.replaceAll(`*${WILDCARD_REGEX}`, "*");

if (isRegexInvalid(regexImproved))
throw getInvalidRegexError(regexImproved);

const finalRegex = new RegExp(`^${regexImproved}$`, "g");

return selector.type === selectorType && finalRegex.test(expressionName);
}

return selector.limitTo.some((limitTo) =>
isCorrectSelector({
selector: { type: "variableExpression", limitTo },
selectorType,
expressionName,
}),
);
}

return selector.some((sel) =>
isCorrectSelector({ selector: sel, selectorKey, expressionName }),
isCorrectSelector({ selector: sel, selectorType, expressionName }),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ describe("getRules", () => {
test("Should return error message when !!errors", () => {
expect(
getCustomError({
selectorKey: "arrowFunction",
selectorType: "arrowFunction",
errors: { arrowFunction: "arrowFunction error" },
}),
).toEqual("\n\narrowFunction error\n\n");
});

test("Should return undefined when !errors", () => {
expect(getCustomError({ selectorKey: "arrowFunction" })).toEqual("");
expect(getCustomError({ selectorType: "arrowFunction" })).toEqual("");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
} from "rules/fileComposition/fileComposition.types";

interface GetCustomErrorProps {
selectorKey: SelectorType;
selectorType: SelectorType;
errors?: CustomErrors;
}

export const getCustomError = ({
selectorKey,
selectorType,
errors,
}: GetCustomErrorProps): string => {
if (!errors?.[selectorKey]) return "";
if (!errors?.[selectorType]) return "";

return `\n\n${errors[selectorKey]}\n\n`;
return `\n\n${errors[selectorType]}\n\n`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
Node,
SelectorType,
} from "rules/fileComposition/fileComposition.types";
import { isCorrectSelector } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/isCorrectSelector";
import { getCustomError } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/isSelectorAllowed/helpers/getCustomError";

interface IsSelectorAllowedProps {
fileRule: FileRule[] | FileRuleObject;
report: Context["report"];
node: Node;
errorMessageId: keyof typeof ESLINT_ERRORS;
selectorKey: SelectorType;
selectorType: SelectorType;
expressionName?: string;
}

Expand All @@ -22,7 +23,7 @@ export const isSelectorAllowed = ({
report,
node,
errorMessageId,
selectorKey,
selectorType,
expressionName,
}: IsSelectorAllowedProps): boolean => {
if (
Expand All @@ -31,21 +32,16 @@ export const isSelectorAllowed = ({
!fileRule.rules
.map(({ selector }) => selector)
.flat()
.some(
(selector) =>
selector === selectorKey ||
(typeof selector !== "string" &&
selector.type === selectorKey &&
expressionName &&
selector.limitTo.includes(expressionName)),
.some((selector) =>
isCorrectSelector({ selector, selectorType, expressionName }),
)
) {
report({
messageId: errorMessageId,
data: {
selectorKey,
selectorType,
error: getCustomError({
selectorKey,
selectorType,
errors: fileRule.errors,
}),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WILDCARD_REGEX } from "consts";
import { RegexParameters } from "types";

import { getRegexWithoutReferences } from "helpers/getRegexWithoutReferences/getRegexWithoutReferences";
Expand Down Expand Up @@ -35,7 +36,9 @@ export const prepareFormat = ({

const formatWithoutReferences = currentFormat.map((regex) =>
getRegexWithoutReferences({
regex,
regex: regex
.replaceAll("*", WILDCARD_REGEX)
.replaceAll(`*${WILDCARD_REGEX}`, "*"),
regexParameters: defaultRegexParameters,
key: "format",
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("validateRules", () => {
},
messageId: "invalidName",
data: {
selectorKey: "variable",
selectorType: "variable",
formatWithoutReferences: "{camelCase}",
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export const validateRules = ({
regexParameters,
expressionName,
}: ValidateRulesProps): void => {
const selectorKey = SELECTORS[nodeType];
const selectorType = SELECTORS[nodeType];

if (
!isSelectorAllowed({
fileRule,
node,
selectorKey,
selectorType,
report,
errorMessageId,
expressionName,
Expand All @@ -58,7 +58,7 @@ export const validateRules = ({
getRules(fileRule).forEach((rule) => {
if (
!isCorrectSelector({
selectorKey,
selectorType,
selector: rule.selector,
expressionName,
})
Expand Down Expand Up @@ -93,7 +93,7 @@ export const validateRules = ({
node,
messageId: "invalidName",
data: {
selectorKey,
selectorType,
formatWithoutReferences: formatWithFilenameReferences.join(", "),
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const WILDCARD_REGEX = "(([^/]*)+)";
export const DOT_CHARACTER_REGEX = "\\.";
Loading

0 comments on commit 9229337

Please sign in to comment.