3

I'm investigating the possibilities to set up a breakpoint instantly when debugging JavaScript

for (...) {
  for (...) {
    ...
  }
  // need a breakpoint here
}

The problem here is that a breakpoint cannot be toggled on comment line, it needs a statement.

And when debugger statement is added to line, another problem appears - it doesn't switch to Debugger tab automatically. It just looks like the application is pending, with no indication.

And I'm trying to avoid adding dummy statements because they can be neglected (as for debugger, there is at least an inspection rule to highlight it).

Are there any tricks to achieve this? Can debugger statement be made to behave like usual breakpoint at least?

6
  • 1
    Do you use the JetBrains IDE Support Chrome extension? chrome.google.com/webstore/detail/jetbrains-ide-support/… Commented Jun 28, 2017 at 9:11
  • 1
    @YannBertrand For client side development. The question is general, it applies to Node, too. Commented Jun 28, 2017 at 9:59
  • 1
    Could you add a GIF showing the problem with the debugger statement? Commented Jun 28, 2017 at 11:27
  • 1
    @YannBertrand I'm currently unable to do that, but the GIF would be static image - IDE built-in debugger switches to Debugger tab when it hits regular breakpoint, but it stays on Console tab when it hits debugger statement. So it just looks like nothing happens, unless a user switches to Debugger tab manually. Please, let me know if you cannot replicate this behaviour. Commented Jun 28, 2017 at 11:37
  • 1
    Well yeah it happens sometimes on my PhpStorm with PHP sources (I do my JS debuging inside the Chrome devtools (also possible for Node sources with --inspect). I'm not sure why this happens but this not really a big issue as I usually know that I've got a breakpoint somewhere. Commented Jun 28, 2017 at 11:56

1 Answer 1

1

Speaking to the general question, there is only one answer, and it is the one you do not want to hear:

You must have a statement for the breakpoint

There are no tricks that are widely applicable (although there may exist some IDE that allows for convenience breaking immediately after a loop... weird)

Unfortunately, what you want is typically done with a temporary/dummy statements (exactly the kind that you are trying to avoid):

for (...) {
  for (...) {
    ...
  }
  // TODO: editors like Eclipse flag TODO lines so they are not lost in the source forest
  setTimeout(Function.prototype, 10000);
}

This is due to the mechanics of how (most) debuggers work: basic file and line numbers are stored as debugger information (hints) in the compiled code, and then matched up with the available source during debug execution. For non-compiled languages like JS/PHP, similar techniques are employed with the string that is parsed from source, but lines with comments or brackets are not really executable.

This issue has occasionally reared its ugly head during my own developer journey. It is simply part of the nature of debugging. I hope you can find a coding solution that provides you comfort and some peace of mind.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have suggestions how to make dummy statement as concise as possible, yet be able to produce error in linter to not accidentally leave it there? debugger statement is able to produce an error in linter, yet there's another problem in it. Notice that the question is limited to JS.
I have edited the answer with a simple no-op statement targeted at JS.
A noop could be just 0; but I'm asking for a solution that will report the statement as an error in linter/inspector like it's done for debugger, so it would never be left there accidentally. This is what the question addresses, And I'm trying to avoid adding dummy statements because they can be neglected.
Yes, I understand that debugging code being left there accidentally is central. A good IDE will track TODO-type comments, but the linter/inspector will probably not. Have you tried something similar to console.error("need a breakpoint here"); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.