-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Add a way to match a rule based on the relative path from "from" to "to" #330
Comments
Would be useful to have this. Maybe a way to ease the implementation would be to add an extra property such as |
Hi @forty thanks for this suggestion. Could capture groups/ group matching fit your use cases? With this you have the from's path or any part of it you need to at your disposal. Below is an example I'm reasonably confident the end result you had in mind is achievable with capture groups as well - but let me know if you need help with it! So how does it work? You can but brackets around parts of the regular expression in the from part of the rule. The substring matches are available for later use in the {
"allowed": [
{
"from": {
"path": "(.+/)[^/]+$" // will match e.g. src/cool/stuff/index.ts
},
"to": {
// e.g. for the module src/cool/stuff/index.ts $1 contains src/cool/stuff/ .
// For that module the paths src/other/stuff or src/cool won't match.
// Anything in and below src/cool/stuff will, though
"path": "$1"
}
}
]
} Or, written as a {
"forbidden": [
{
"name": "no-parents",
"from": { "path": "(.+/)[^/]+$" },
"to": {
"pathNot": "$1"
}
}
]
}
|
Hi @sverweij , thanks a lot for your answer. What I need is a bit more complex than preventing parents from being imported. I what to allow importing siblings, and all ancestors siblings (but not their descendants) So |
@forty you'd like to achieve something like this? Let me see if I can construct some rules for this. If I infer correctly two things are not allowed:
1. no grand childrenLets first try and write an RE that catches the (generic case of)
Everything like $1/xxx.js and $1/somefolder/yyy.js is okidoki, $1/deeper/nested/zzz.js isn't, though. In a regex that deeper nested stuff will look like {
forbidden: [
{
"name": "no-grand-child-folders",
"from": {
"path": "(.+)/[^/]+$"
},
"to": {
"path": "$1/[^/]+/[^/]+/.+$"
}
}
]
} 2. not your aunts and their childrenLet's for easy reasoning say c.js resides in We still need to be able to catch
Now we want to select any aunt & their children, which is simply put /grand-parent/not-mom/yaddayadda. We can do this by saying
{
{
"name": "not-your-aunts-and-children",
"from": {
"path": "(.+)/([^/]+)/[^/]+$"
},
"to": {
"path": "$1/[^/]+/.+$", // it _is_ a child of your grand-mom ...
"pathNot": "$1/$2/" // but it's not your mother
}
}
]
}
Applying these rules to a part of dependency-cruiser's code base yields this (I've set the severities of not-your-aunts-and-children to The report for whole dependency-cruiser looks like what you see below. If this indeed fits your use case (=> let me know!) I'm going to stick this as an example somewhere in the documentation. |
Wow that is a great answer, thanks a lot. But this is not exactly what I'm trying to do, sorry it's not clear. I'll try another way of explaining, using relative path (as that's what I'm asking): I want the relative path off all import to match Example okay:
Example not okay:
My idea being that if It kind of force to have one index.js in all folders. In the case of dependency cruiser for example, it would force Does that make sense? |
I would like to be able to write rules such as
and
The rule would match if
path.relative(path.dirname(from), to)
matches the regex (or doesn't match the regex for the relativePathNot)Context
I'm trying to do something similar to the "no-inside" rule above. Basically want to consider all folder as module boundaries. So for example:
A.ts
can require./B.ts
(or./B/index.ts
),../B.ts
,../../../B.ts
but NOT./b/B.ts
or../b/B.ts
I tried to do my best with regexp, but I'm not really sure it's possible to simulate that with regex alone :)
Expected Behavior
Be able to match based on relative paths
Current Behavior
Only absolute path can be user
Possible Solution
Add this new matcher property as suggested above
Considered alternatives
many complex regexp with all the possible depth of folder....
The text was updated successfully, but these errors were encountered: