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

TypeError: Cannot read properties of undefined (reading 'startLine') #685

Closed
jameshearttech opened this issue Oct 2, 2024 · 4 comments · Fixed by #688
Closed

TypeError: Cannot read properties of undefined (reading 'startLine') #685

jameshearttech opened this issue Oct 2, 2024 · 4 comments · Fixed by #688

Comments

@jameshearttech
Copy link

I'm getting a type error formatting 1 particular file. Any ideas?

@jtkiesel
Copy link
Contributor

jtkiesel commented Oct 3, 2024

Can you provide the Java file that you experience this error with?

@jameshearttech
Copy link
Author

jameshearttech commented Oct 3, 2024

I probably cannot share it. It's from a private repository for work. I spent some time trying to figure out what is causing the error on my own, but this file is 4.5k lines so going through it by hand to look at every comment takes some time.

Can you tell anything from the prettier output? If I cannot share the file, can you give me guidance on how to determine what is causing the problem?

@jameshearttech
Copy link
Author

I confirmed I cannot share the file. How can I tell where in the file the exception occurred?

@jameshearttech
Copy link
Author

jameshearttech commented Oct 4, 2024

@jtkiesel I created a launch in VS Code for pnpm exec prettier --check ... and set a breakpoint at prettier-plugin-java/dist/printers/comments/handle-comments.js line 84. I debugged prettier and found that binaryOperator is undefined when the exception occurs.

I found the line in the file that is being formatted from the breakpoint, too. The line has a comment at the end. I moved the comment to its own line and the problem went away.

I made a quick example. When I remove //comment1 then prettier is able to format the file.

public class Example {
    public static void main(String[] args) {
        Foo bar
        = !baz //comment1
        || bat //comment2
        ? null //comment3
        : bam //comment4
        ;
    }
}

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 a pull request may close this issue.

2 participants