-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
859a100
commit dd4ce91
Showing
26 changed files
with
834 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...dateFile/helpers/validateRules/helpers/getFilenameWithoutParts/getFilenameWithoutParts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Rule } from "rules/fileComposition/fileComposition.types"; | ||
import { getFileNameWithoutExtension } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/getFilenameWithoutParts/helpers/getFileNameWithoutExtension"; | ||
import { removeFilenameParts } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/getFilenameWithoutParts/helpers/removeFilenameParts"; | ||
|
||
interface GetFilenameWithoutPartsProps { | ||
filenamePartsToRemove: Rule["filenamePartsToRemove"]; | ||
filenamePath: string; | ||
} | ||
|
||
export const getFilenameWithoutParts = ({ | ||
filenamePartsToRemove, | ||
filenamePath, | ||
}: GetFilenameWithoutPartsProps): string => { | ||
const filenameWithoutExtension = getFileNameWithoutExtension(filenamePath); | ||
return removeFilenameParts({ | ||
filenameWithoutExtension, | ||
filenamePartsToRemove, | ||
}); | ||
}; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...Rules/helpers/removeFilenameParts.test.ts → ...Parts/helpers/removeFilenameParts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
55 changes: 55 additions & 0 deletions
55
...ers/validateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { RegexParameters } from "types"; | ||
|
||
import { | ||
Context, | ||
Node, | ||
Rule, | ||
SelectorType, | ||
} from "rules/fileComposition/fileComposition.types"; | ||
import { getBodyWithoutImports } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getBodyWithoutImports"; | ||
import { getPositionIndex } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndex"; | ||
import { getPositionIndexRules } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndexRules"; | ||
import { validatePositionIndex } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/validatePositionIndex/validatePositionIndex"; | ||
|
||
interface HandlePositionIndexProps { | ||
positionIndex: number; | ||
node: Node; | ||
filenamePath: string; | ||
rules: Rule[]; | ||
regexParameters?: RegexParameters; | ||
context: Context; | ||
name: string; | ||
selectorType: SelectorType; | ||
} | ||
|
||
export const handlePositionIndex = ({ | ||
filenamePath, | ||
positionIndex, | ||
node, | ||
rules, | ||
regexParameters, | ||
context, | ||
name, | ||
selectorType, | ||
}: HandlePositionIndexProps): void => { | ||
const positionIndexRules = getPositionIndexRules({ | ||
filenamePath, | ||
rules, | ||
regexParameters, | ||
}); | ||
const bodyWithoutImports = getBodyWithoutImports(node); | ||
const newPositionIndex = getPositionIndex({ | ||
bodyWithoutImports, | ||
name, | ||
positionIndex, | ||
positionIndexRules, | ||
}); | ||
|
||
validatePositionIndex({ | ||
node, | ||
positionIndex: newPositionIndex, | ||
selectorType, | ||
context, | ||
bodyWithoutImports, | ||
}); | ||
}; |
4 changes: 4 additions & 0 deletions
4
...lidateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface PositionIndexRule { | ||
positionIndex: number; | ||
format: string[]; | ||
} |
14 changes: 14 additions & 0 deletions
14
...teFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getBodyWithoutImports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { TSESTree } from "@typescript-eslint/utils"; | ||
|
||
import { Node } from "rules/fileComposition/fileComposition.types"; | ||
import { getProgramFromNode } from "rules/fileComposition/helpers/validateFile/helpers/getProgramFromNode"; | ||
|
||
export const getBodyWithoutImports = ( | ||
node: Node, | ||
): TSESTree.ProgramStatement[] => { | ||
const program = getProgramFromNode(node); | ||
|
||
return program.body.filter( | ||
({ type }) => type !== TSESTree.AST_NODE_TYPES.ImportDeclaration, | ||
); | ||
}; |
60 changes: 60 additions & 0 deletions
60
...teFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndex.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { PositionIndexRule } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.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"; | ||
|
||
jest.mock( | ||
"rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getSelectorNamesFromBody", | ||
() => ({ | ||
getSelectorNamesFromBody: jest.fn(), | ||
}), | ||
); | ||
|
||
describe("getPositionIndex", () => { | ||
test.each<{ | ||
name: string; | ||
positionIndexRules: PositionIndexRule[]; | ||
expected: number; | ||
}>([ | ||
{ | ||
name: "Return", | ||
positionIndexRules: [ | ||
{ format: ["Props"], positionIndex: 0 }, | ||
{ format: ["Return"], positionIndex: 1 }, | ||
{ format: ["Name"], positionIndex: 2 }, | ||
], | ||
expected: 0, | ||
}, | ||
{ | ||
name: "Name", | ||
positionIndexRules: [ | ||
{ format: ["variable"], positionIndex: 0 }, | ||
{ format: ["Return"], positionIndex: 1 }, | ||
{ format: ["Name"], positionIndex: 2 }, | ||
], | ||
expected: 2, | ||
}, | ||
{ | ||
name: "Name", | ||
positionIndexRules: [], | ||
expected: 1, | ||
}, | ||
])( | ||
"Should return correct value for = %o", | ||
({ name, positionIndexRules, expected }) => { | ||
(getSelectorNamesFromBody as jest.Mock).mockReturnValue([ | ||
"variable", | ||
"Return", | ||
"Name", | ||
]); | ||
|
||
expect( | ||
getPositionIndex({ | ||
bodyWithoutImports: [], | ||
name, | ||
positionIndex: 1, | ||
positionIndexRules, | ||
}), | ||
).toEqual(expected); | ||
}, | ||
); | ||
}); |
40 changes: 40 additions & 0 deletions
40
...alidateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { TSESTree } from "@typescript-eslint/utils"; | ||
|
||
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"; | ||
|
||
interface GetPositionIndexProps { | ||
positionIndex: number; | ||
positionIndexRules: PositionIndexRule[]; | ||
bodyWithoutImports: TSESTree.ProgramStatement[]; | ||
name: string; | ||
} | ||
|
||
export const getPositionIndex = ({ | ||
positionIndexRules, | ||
positionIndex, | ||
bodyWithoutImports, | ||
name, | ||
}: GetPositionIndexProps): number => { | ||
const selectorNamesFromBody = getSelectorNamesFromBody(bodyWithoutImports); | ||
|
||
const positionIndexRulesBody = selectorNamesFromBody | ||
.map((name) => | ||
positionIndexRules.find(({ format }) => format.includes(name)), | ||
) | ||
.filter((v): v is PositionIndexRule => v !== undefined) | ||
.sort((a, b) => a.positionIndex - b.positionIndex); | ||
|
||
const positionIndexRulesNewOrder = positionIndexRulesBody.map( | ||
({ format }, index) => ({ | ||
format, | ||
positionIndex: index, | ||
}), | ||
); | ||
|
||
const newPositionIndex = positionIndexRulesNewOrder.find(({ format }) => | ||
format.includes(name), | ||
)?.positionIndex; | ||
|
||
return newPositionIndex ?? positionIndex; | ||
}; |
15 changes: 15 additions & 0 deletions
15
...e/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndexRules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getPositionIndexRules } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndexRules"; | ||
|
||
describe("validateRules", () => { | ||
test("Should return correct values", () => { | ||
expect( | ||
getPositionIndexRules({ | ||
filenamePath: "", | ||
rules: [ | ||
{ selector: "arrowFunction", positionIndex: 1, format: "Props" }, | ||
{ selector: "variable" }, | ||
], | ||
}), | ||
).toEqual([{ format: ["Props"], positionIndex: 1 }]); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
...teFile/helpers/validateRules/helpers/handlePositionIndex/helpers/getPositionIndexRules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RegexParameters } from "types"; | ||
|
||
import { Rule } from "rules/fileComposition/fileComposition.types"; | ||
import { getFilenameWithoutParts } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/getFilenameWithoutParts/getFilenameWithoutParts"; | ||
import { PositionIndexRule } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/handlePositionIndex/handlePositionIndex.types"; | ||
import { prepareFormat } from "rules/fileComposition/helpers/validateFile/helpers/validateRules/helpers/prepareFormat/prepareFormat"; | ||
|
||
interface GetPositionIndexRulesProps { | ||
rules: Rule[]; | ||
regexParameters?: RegexParameters; | ||
filenamePath: string; | ||
} | ||
|
||
export const getPositionIndexRules = ({ | ||
rules, | ||
regexParameters, | ||
filenamePath, | ||
}: GetPositionIndexRulesProps): PositionIndexRule[] => | ||
rules | ||
.map(({ format, filenamePartsToRemove, positionIndex }) => { | ||
if (positionIndex === undefined) return; | ||
|
||
const filenameWithoutParts = getFilenameWithoutParts({ | ||
filenamePartsToRemove, | ||
filenamePath, | ||
}); | ||
|
||
return { | ||
positionIndex, | ||
format: prepareFormat({ | ||
format, | ||
filenameWithoutParts, | ||
regexParameters, | ||
}).formatWithoutReferences, | ||
}; | ||
}) | ||
.filter((v): v is PositionIndexRule => v !== undefined); |
Oops, something went wrong.