Skip to content
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

Make sure forbidden-imports rule checks files directly inside layers #64

Merged

Conversation

daniilsapa
Copy link
Collaborator

@daniilsapa daniilsapa commented Jul 21, 2024

Resolves #49

Had to move all layers into the /src folder for the mock filesystem used in the unit tests for the rule because there was an edge case where ts.resolveModuleName could not correctly resolve aliases for folders that are directly at the root of the filesystem (it did not resolve /app/index.ts from @/app and same problem for all layers). After moving everything in /src, they resolve just fine. I thought about creating an issue in TS repository, but it seems like an extremely rare edge case that nobody would care about. No one puts their real projects in the root of the file system.

Had to move all layers into the /src folder for the mock filesystem used in the unit tests for the rule because there was an edge case where ts.resolveModuleName could not correctly resolve aliases for folders that are directly at the root of the filesystem (it did not resolve /app/index.ts from @/app and same problem for all layers). After moving everything in /src, they resolve just fine. I thought about creating an issue in TS repository, but it seems like an extremely rare edge case that nobody would care about. No-one puts their real projects in the root of the file system.
@daniilsapa
Copy link
Collaborator Author

@illright
I fixed the problem that I introduced. But still have a question. Based on the logic of indexSourceFiles, currently, we index files based on the ideal folder structure according to FSD. But in the real world users sometimes can put files in unexpected (incorrect in terms of FSD) places. The question is, should we still index such files to find errors inside or report the placement of the file itself as an error?

packages/steiger-plugin-fsd/src/forbidden-imports/index.ts Outdated Show resolved Hide resolved
📄 index.ts
📂 pages
📂 editor
📂 src
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: the idea is to give the check function of rules the folder of the FSD root, so by putting the layers in src, we're not actually testing how it's going to be

suggestion: we can add a second optional argument to parseIntoFsdRoot — the root path of the folders, then we can avoid having src here in the structure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, now I see that in other tests, you access .children[0] to overcome this issue. That's also fine, although you forgot to do it in the positive case. I still think that it would be cleaner with an optional root path argument though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had to put all the files into /src because of the problem I wrote about in the PR description. Indeed I forgot to add .children[0] for the positive case, thanks for catching that.

I would be happy to pass that optional argument to parseIntoFsdRoot but I see that it accepts only 1 argument. Maybe you implemented the second parameter in some branch that has not yet been merged to the master?

Screenshot 2024-07-23 at 22 36 08

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to suggest for you to add that second parameter :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also took the liberty to unresolve this conversation. Usually, I prefer to mark threads as resolved when the comment was either fully addressed or it was decided not to address it, but this comment is still relevant partially. Let me know if you'd like to add that second parameter, or I can do it

Copy link
Collaborator Author

@daniilsapa daniilsapa Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, got it. No problem, I'll add that tomorrow. I think it will be useful in further test cases and also I don't like .children[0] too much.

Comment on lines 7 to 28
function findSubfolder(folder: Folder, path: string): Folder {
function checkFolder(folder: Folder): Folder {
if (folder.path === path) {
return folder
}

if (path.startsWith(folder.path)) {
for (const child of folder.children) {
if (child.type === 'folder') {
const result = checkFolder(child)
if (result) {
return result
}
}
}
}

throw new Error(`Path "${path}" not found in the provided file system mock!`)
}

return checkFolder(folder)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I meant a more simple solution — you pass the root path as the second argument, and whatever file tree you write with emojis is "mounted" to that root. For example:

parseIntoFsdRoot(`
  📂 test
    📄 file
`, "/home/illright");  // the "file" becomes `/home/illright/test/file`

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree, the simpler the better, the KISS principle should not be forgotten 😂

Copy link
Member

@illright illright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, we're ready to merge this. Thanks a lot for doing all the corrections!

@illright illright merged commit 4ed8d22 into feature-sliced:master Jul 25, 2024
7 checks passed
@daniilsapa
Copy link
Collaborator Author

No problem, thanks for your reviews and feedback!

@daniilsapa daniilsapa deleted the feature/fix-forbidden-imports-rule branch July 27, 2024 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: there is no error about forbidden vertical imports
2 participants