-
I have an entities folder and I structure it like this:
if i want to access auth entity from user service, i need to go @x folder only, and if there is a board.ts for example, i also want to throw an error, because it should access only user file there. if i import it from user/index.ts, i can still access everything in auth entity, i want to fix it, here is my current configuration:
dirname_2 indicates that i can import user types from user service, for example. I'm just wondering what rules should I apply to consider all possibilities |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @chertik77, There are two possible configurations, if I understood your requirements correctly: Configuration1 dynamic rules: // @ts-check
import { createIndependentModules } from "eslint-plugin-project-structure";
export const independentModulesConfig = createIndependentModules({
modules: [
{
name: "Entity folder",
pattern: "src/entities/**",
allowImportsFrom: [
// {family} === "src/entities"
// {family_3} === "src/entities/user"
"{family_3}/**",
"src/shared/**",
// All files from the @x folder will be accessible, there’s no other way if you want the rule to be dynamic.
"src/entities/auth/@x/*",
],
},
],
}); Configuration2 fixed rules: /**
* @param {string} folderName
*/
const createEntityRule = (folderName) => ({
name: `${folderName} folder`,
pattern: `src/entities/${folderName}/**`,
allowImportsFrom: [
"{family_3}/**",
"src/shared/**",
`src/entities/auth/@x/${folderName}.ts`,
],
});
export const independentModulesConfig = createIndependentModules({
modules: [
createEntityRule("user"),
createEntityRule("someOtherEntity"),
{
name: "Unknown entity",
pattern: "src/entities/**",
allowImportsFrom: [],
allowExternalImports: false,
errorMessage:
"🔥 This entity is not specified as an independent module in `independentModules.mjs`. Use createEntityRule function. 🔥",
},
],
}); |
Beta Was this translation helpful? Give feedback.
-
If you want fully dynamic rules I would consider keeping the auth files of a given entity in its folder rather than in the auth folder, e.g., This way, you can also restrict // @ts-check
import { createIndependentModules } from "eslint-plugin-project-structure";
export const independentModulesConfig = createIndependentModules({
modules: [
{
name: "Auth entity folder",
pattern: "src/entities/auth/**",
allowImportsFrom: [
// {family} === "src/entities"
// {family_3} === "src/entities/auth"
"{family_3}/**",
// Auth entity can import only auth.ts file from other entities.
"src/entities/*/auth.ts",
"src/shared/**",
],
},
{
name: "Entity folder",
pattern: "src/entities/**",
allowImportsFrom: [
// {family} === "src/entities"
// {family_3} === "src/entities/user"
"{family_3}/**",
"src/shared/**",
],
},
],
});
|
Beta Was this translation helpful? Give feedback.
I bumped to eslint 9 to be able to createEntities and dynamic rules, thank you so much for your help!
I've created something like this:
it work great, but requires to add another entity everytime