Skip to content

Commit

Permalink
3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorkowalski94 committed Oct 9, 2024
1 parent 48e6c03 commit 43ebb7c
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 57 deletions.
2 changes: 1 addition & 1 deletion 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": "3.4.0",
"version": "3.5.0",
"license": "MIT",
"description": "Powerful ESLint plugin with rules to help you achieve a scalable, consistent, and well-structured project. Create your own framework! Define your folder structure, file composition, advanced naming conventions, and create independent modules. Take your project to the next level and save time by automating the review of key principles of a healthy project! react folder structure react file structure react project structure react conventions architecture react next.js angular node solid vue svelte",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const handlePositionIndex = ({
name,
positionIndex,
positionIndexRules,
selectorType,
});

validatePositionIndex({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Selector } from "rules/fileComposition/fileComposition.types";

export interface PositionIndexRule {
positionIndex: number;
format: string[];
selector: Selector | Selector[];
expressionName?: string;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PositionIndexRule } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.types";
import { SelectorType } from "rules/fileComposition/fileComposition.types";
import { getPositionIndex } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndex";
import { getSelectorNamesFromBody } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getSelectorNamesFromBody";

Expand All @@ -12,47 +12,66 @@ jest.mock(
describe("getPositionIndex", () => {
test.each<{
name: string;
positionIndexRules: PositionIndexRule[];
selectorType: SelectorType;
expected: number;
}>([
{
name: "Return",
positionIndexRules: [
{ format: ["Props"], positionIndex: 0 },
{ format: ["Return"], positionIndex: 1 },
{ format: ["Name"], positionIndex: 2 },
],
name: "Props",
selectorType: "interface",
expected: 0,
},
{
name: "Return",
selectorType: "interface",
expected: 1,
},
{
name: "Name",
positionIndexRules: [
{ format: ["variable"], positionIndex: 0 },
{ format: ["Return"], positionIndex: 1 },
{ format: ["Name"], positionIndex: 2 },
],
selectorType: "arrowFunction",
expected: 2,
},
{
name: "Name",
positionIndexRules: [],
name: "Last2",
selectorType: "variable",
expected: 6,
},
{
name: "Last1",
selectorType: "variable",
expected: 7,
},
{
name: "Random",
selectorType: "variable",
expected: 1,
},
])(
"Should return correct value for = %o",
({ name, positionIndexRules, expected }) => {
({ name, selectorType, expected }) => {
(getSelectorNamesFromBody as jest.Mock).mockReturnValue([
"variable",
"Return",
"Name",
{ selector: "interface", name: "Return" },
{ selector: "variable", name: "Last2" },
{ selector: "variable", name: "variable3" },
{ selector: "arrowFunction", name: "Name" },
{ selector: "interface", name: "Props" },
{ selector: "variable", name: "variable1" },
{ selector: "variable", name: "Last1" },
{ selector: "variable", name: "variable2" },
]);

expect(
getPositionIndex({
bodyWithoutImports: [],
name,
positionIndex: 1,
positionIndexRules,
positionIndexRules: [
{ format: ["Props"], selector: "interface", positionIndex: 0 },
{ format: ["Return"], selector: "interface", positionIndex: 1 },
{ format: ["Name"], selector: "arrowFunction", positionIndex: 2 },
{ format: ["Last2"], selector: "variable", positionIndex: -2 },
{ format: ["Last1"], selector: "variable", positionIndex: -100 },
],
selectorType,
}),
).toEqual(expected);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";

import { SelectorType } from "rules/fileComposition/fileComposition.types";
import { isCorrectSelector } from "rules/fileComposition/helpers/validateFile/helpers/isCorrectSelector";
import { PositionIndexRule } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.types";
import { getSelectorNamesFromBody } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getSelectorNamesFromBody";

Expand All @@ -8,33 +10,71 @@ interface GetPositionIndexProps {
positionIndexRules: PositionIndexRule[];
bodyWithoutImports: TSESTree.ProgramStatement[];
name: string;
selectorType: SelectorType;
}

export const getPositionIndex = ({
positionIndexRules,
positionIndex,
bodyWithoutImports,
name,
selectorType,
}: GetPositionIndexProps): number => {
const selectorNamesFromBody = getSelectorNamesFromBody(bodyWithoutImports);

const positionIndexRulesBody = selectorNamesFromBody
.map((name) =>
positionIndexRules.find(({ format }) => format.includes(name)),
.map((body) =>
positionIndexRules.find(
({ format, selector }) =>
isCorrectSelector({
selector,
selectorType: body.selector,
expressionName: body.expressionName,
}) && format.includes(body.name),
),
)
.map((rule) => {
if (!rule) return;

const positionIndexNegativeDefault = selectorNamesFromBody.length - 1;
const positionIndexNegative =
selectorNamesFromBody.length + rule.positionIndex;
const currentPositionIndexNegative =
positionIndexNegative < 0
? positionIndexNegativeDefault
: positionIndexNegative;

return {
...rule,
positionIndex:
rule.positionIndex < 0
? currentPositionIndexNegative
: rule.positionIndex,
};
})
.filter((v): v is PositionIndexRule => v !== undefined)
.sort((a, b) => a.positionIndex - b.positionIndex);

let sortedIndex = 0;

const positionIndexRulesNewOrder = positionIndexRulesBody.map(
({ format }, index) => ({
({ format, selector, expressionName }) => ({
format,
positionIndex: index,
selector,
expressionName,
positionIndex: positionIndexRulesBody[sortedIndex++],
}),
);

const newPositionIndex = positionIndexRulesNewOrder.find(({ format }) =>
format.includes(name),
)?.positionIndex;
const newPositionIndex = positionIndexRulesNewOrder.find(
({ format, expressionName, selector }) =>
format.includes(name) &&
isCorrectSelector({
selector,
selectorType,
expressionName,
}),
)?.positionIndex.positionIndex;

return newPositionIndex ?? positionIndex;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("validateRules", () => {
{ selector: "variable" },
],
}),
).toEqual([{ format: ["Props"], positionIndex: 1 }]);
).toEqual([
{ format: ["Props"], selector: "arrowFunction", positionIndex: 1 },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getPositionIndexRules = ({
filenamePath,
}: GetPositionIndexRulesProps): PositionIndexRule[] =>
rules
.map(({ format, filenamePartsToRemove, positionIndex }) => {
.map(({ format, selector, filenamePartsToRemove, positionIndex }) => {
if (positionIndex === undefined) return;

const filenameWithoutParts = getFilenameWithoutParts({
Expand All @@ -27,6 +27,7 @@ export const getPositionIndexRules = ({

return {
positionIndex,
selector,
format: prepareFormat({
format,
filenameWithoutParts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,25 +382,108 @@ describe("getSelectorNamesFromBody", () => {
range: [397, 434],
exportKind: "value",
},
{
type: "VariableDeclaration",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "Identifier",
name: "VariableExpression",
range: [9, 27],
},
init: {
type: "CallExpression",
callee: {
type: "Identifier",
name: "fn",
range: [30, 32],
},
arguments: [],
optional: false,
range: [30, 34],
},
range: [9, 34],
},
],
kind: "const",
range: [3, 34],
},
] as unknown as TSESTree.ProgramStatement[]),
).toEqual([
"variable",
"arrowFunction",
"Function",
"Class",
"Type",
"Interface",
"Enum",
"variable",
"arrowFunction",
"Function",
"Class",
"Type",
"Interface",
"Enum",
"Function",
"Class",
"Interface",
{
name: "variable",
selector: "variable",
},
{
name: "arrowFunction",
selector: "variable",
},
{
name: "Function",
selector: "function",
},
{
name: "Class",
selector: "class",
},
{
name: "Type",
selector: "type",
},
{
name: "Interface",
selector: "interface",
},
{
name: "Enum",
selector: "enum",
},
{
name: "variable",
selector: "variable",
},
{
name: "arrowFunction",
selector: "variable",
},
{
name: "Function",
selector: "function",
},
{
name: "Class",
selector: "class",
},
{
name: "Type",
selector: "type",
},
{
name: "Interface",
selector: "interface",
},
{
name: "Enum",
selector: "enum",
},
{
name: "Function",
selector: "function",
},
{
name: "Class",
selector: "class",
},
{
name: "Interface",
selector: "interface",
},
{
expressionName: "fn",
name: "VariableExpression",
selector: "variableExpression",
},
]);
});
});
Loading

0 comments on commit 43ebb7c

Please sign in to comment.