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

diagnostics throws error if a notfound route is within a Private tag #1325

Merged
merged 7 commits into from
Oct 13, 2020

Conversation

M0nica
Copy link
Contributor

@M0nica M0nica commented Oct 10, 2020

This should resolve (Add error diagnostic: notfound page cannot be private) #920.

I tested it locally by running a test app alongside the redwood instance and checking that an error was only thrown when running the diagnostics command when a Private tag contained a Route with the notfound attribute.

Below is a screenshot of how the error renders for me when running yarn rw check
Screen Shot 2020-10-10 at 6 32 36 PM

@M0nica M0nica changed the title add check to throw error if a notfound route is within a Private tag diagnostics throws error if a notfound route is within a Private tag Oct 10, 2020
yield err(
this.jsxNode!,
"The 'Not Found' page cannot be within a <Private> tag"
)
if (this.isAuthenticated && this.isNotFound)
Copy link
Contributor Author

@M0nica M0nica Oct 10, 2020

Choose a reason for hiding this comment

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

references to isAuthenticated should be removed in #917 there's a comment by @aldonline indicating that references to authenticated are no longer being used in favor of private however, it can't be removed safely without modifying the outline file which is still referencing isAuthenticated.

You'll notice that there is some old code/stub that references a property called route.isAuthenticated. The "isAuthenticated" name is from before the "Private" tag name was chosen.

Copy link
Contributor

@aldonline aldonline Oct 11, 2020

Choose a reason for hiding this comment

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

Yeah. We need to clean that up :)

if (typeof this.jsxNode.getAttribute('notfound') !== 'undefined') {
return this.jsxNode.compilerNode.parent
.getFullText()
.startsWith('<Private')
Copy link
Contributor Author

@M0nica M0nica Oct 10, 2020

Choose a reason for hiding this comment

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

This was the best way I could figure out how to access the tagName for parent as the methods available to jsxNode aren't available for the parent node but I am open to suggestions re: different approaches to determining if the parent node is a Private tag.

Copy link
Contributor

@aldonline aldonline Oct 11, 2020

Choose a reason for hiding this comment

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

Hi Monica,

(edit)

  1. I'm not 100% sure, but I think this heuristic might break if there is trivia before the <Private> tag (like a comment). The getFullText() method might return the trivia as well. Please be sure to check this.
  2. Once you've found a good strategy to test for this, please extract it to a separate getter @lazy() get isPrivate(): boolean {...}. It is important to keep all these heuristics consistent. We don't want some algorithms to use your strategy, and then someone else reinventing the wheel.

FYI: the ts-morph way of doing this would be something like this:

    const tagText = this.jsxNode
      .getParentIfKind(tsm.SyntaxKind.JsxElement)
      ?.getOpeningElement()
      ?.getTagNameNode()
      ?.getText()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I refactored the code based on your feedback and it worked both with and without a comment infront of the tag from my manual testing.

Copy link
Contributor

@aldonline aldonline left a comment

Choose a reason for hiding this comment

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

Thanks for your PR!
I added a few comments suggesting changes ;)

@@ -38,6 +38,16 @@ export class RWRoute extends BaseNode {
return LocationLike_toLocation(this.jsxNode)
}

@lazy() get hasInvalidPrivateRoute() {
// Return `true` when a notfound route is nested within a Private Tag
if (typeof this.jsxNode.getAttribute('notfound') !== 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (typeof this.jsxNode.getAttribute('notfound') !== 'undefined') {
if (this.isNotFound) {

if (typeof this.jsxNode.getAttribute('notfound') !== 'undefined') {
return this.jsxNode.compilerNode.parent
.getFullText()
.startsWith('<Private')
Copy link
Contributor

@aldonline aldonline Oct 11, 2020

Choose a reason for hiding this comment

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

Hi Monica,

(edit)

  1. I'm not 100% sure, but I think this heuristic might break if there is trivia before the <Private> tag (like a comment). The getFullText() method might return the trivia as well. Please be sure to check this.
  2. Once you've found a good strategy to test for this, please extract it to a separate getter @lazy() get isPrivate(): boolean {...}. It is important to keep all these heuristics consistent. We don't want some algorithms to use your strategy, and then someone else reinventing the wheel.

FYI: the ts-morph way of doing this would be something like this:

    const tagText = this.jsxNode
      .getParentIfKind(tsm.SyntaxKind.JsxElement)
      ?.getOpeningElement()
      ?.getTagNameNode()
      ?.getText()

yield err(
this.jsxNode!,
"The 'Not Found' page cannot be within a <Private> tag"
)
if (this.isAuthenticated && this.isNotFound)
Copy link
Contributor

@aldonline aldonline Oct 11, 2020

Choose a reason for hiding this comment

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

Yeah. We need to clean that up :)

@thedavidprice
Copy link
Contributor

Hi @M0nica excited to see others helping out with the structure package! You're in good hands with @aldonline who's been teaching us all the ins and outs about how to do this.

Copy link
Contributor

@aldonline aldonline left a comment

Choose a reason for hiding this comment

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

Thanks @M0nica!
I think this is ok to merge now (pending rebasing to "main", but let's wait for @thedavidprice or @peterp to suggest if that's necessary)
It also works in the IDE ;)
Screen Shot 2020-10-13 at 12 47 40 PM

Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

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

Love it, thanks!

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.

None yet

4 participants