question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuleTester - Add "debug" flag for valid/invalid code

See original GitHub issue

The problem you want to solve. While writing a custom rule, I found myself wanting to debug my rule but only for a specific code block. I was able to do this by commenting out all of the others - but then I was unable to see the status of those tests as I tweaked my custom rule for the code that I was interested in debugging. Once I had the rule working as expected for the code I was debugging, I uncommented the others and realized that some were broken.

It would be nice if there was a flag that I could pass into a specific code config that would enable a conditional breakpoint so that I could step into my custom rule for that code.

Your take on the correct solution to problem. I was thinking update the config for code to accept a “debug” flag, that would wrap the rule in an anonymous function with a debugger at the top.

// config accepts a "debug" flag that would enable a conditional debugger during testing of the code block
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
    valid: [],
    invalid: [{
        code: "var foo = bar || baz",
        debug: true
    }],
});

// and then somewhere inside of RuleTester/linter the rule would be wrapped like so
function debuggedRule() {
    debugger;
    rule.apply(this, arguments);
}

// somewhere else 😅
var ruleToRun = config.debug ? debuggedRule : rule;
callRuleWithAstParsedSourceCode(ruleToRun, astParsedSourceCode)

Also as a best practice, I was thinking to automatically trigger a test failure if any of the rules have a “debug: true” flag - this way a debugger would hopefully not sneak into “live” code.

Are you willing to submit a pull request to implement this change? Yes - I can attempt to make a minimal set of changes. I have not really considered or understand how we might want to differentiate between wanting to debug the custom rule code vs the auto-fixing of the rule.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mdjermanoviccommented, Aug 28, 2020

Can we tweak this to make it something like adding the debugger by the user itself instead of conditionally calling the function that is having the debugger?

Just an idea, maybe something like before function that would be called before running the rule on the test code:

ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
    valid: [],
    invalid: [{
        code: "var foo = bar || baz",
        before() { debugger; }
    }],
});
2reactions
anikethsahacommented, Aug 28, 2020

Thanks for the issue.

I have faced the similar issue multiple times. Rule tester is a public API and IMO, shipping debugger in public API maybe not be a good move. Can we tweak this to make it something like adding the debugger by the user itself instead of conditionally calling the function that is having the debugger?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Rules - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How to get WebStorm to run ESLint ruletester efficiently
run('myruletest',rule,{invalid,valid});. Normally, when I install a test runner I get a run/configuration for it and handy play ⏯ and debug ...
Read more >
How to use the eslint.RuleTester.setDefaultConfig function ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
How to debug for absolute beginners - Visual Studio
Step through your code in debugging mode to find where the problem occurred. When you normally run an app, you see errors and...
Read more >
3.10 Options for Debugging Your Program
Some debug formats can co-exist (like DWARF with CTF) when each of them is enabled explicitly by adding the respective command line option...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found