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.

Allow regexes in RuleTester

See original GitHub issue
  • ESLint Version: 3.5.0

It would be nice for the RuleTester to allow regular expressions to match messages, rather than just strings. At Facebook, some of our lint rules have very long messages - Here’s one such message from a lint rule that has several possible messages:

context.report({
  node: node,
  message:
    'This is a shared module but it is referencing a ' +
    '{{dependencyProduct}} module ({{requiredModule}}). Shared ' +
    'modules should not have any dependencies on product code. ' +
    'If this module is specific to {{dependencyProduct}}, it should ' +
    'be moved to {{productPath}}. Contact danlo@ if this is a ' +
    'false positive.',
  data: {
    dependencyProduct: dependencyProduct,
    productPath: getPathForProductName(dependencyProduct),
    requiredModule: requiredModule,
  },
});

And an example of one of the test cases:

{
  // Shared module requiring a Power Editor module
  code: 'require("AdsPEApplication")',
  filename: 'html/js/ads/utils/AdsBidUtils.js',
  errors: [
    'This is a shared module but it is referencing a Power Editor module ' +
    '(AdsPEApplication). Shared modules should not have any dependencies ' +
    'on product code. If this module is specific to Power Editor, it ' +
    'should be moved to html/js/ads/powereditor. Contact danlo@ if this ' +
    'is a false positive.',
  ],
},

All I really want to check is that the correct message is being returned, and the correct variables are merged into it. Right now I need to copy and paste the entire message for every test case. If the message changes at all, I need to remember to update every test case. Alternatively, I could export the message from the lint rule module and import it into the unit test, but that feels a bit messy and I’d still need to deal with the variables in the string.

It would be nicer to be able to use a regex, so I could do something like:

  errors: [
    /Power Editor module \(AdsPEApplication\).+should be moved to html\/js\/ads\/powereditor/,
  ],

Alternatively, it would also be useful to be able to provide a “message ID” to the context.report call:

context.report({
  node: node,
  message: 'Hello world',
  message_id: 2, // Any arbitrary integer
});

Such that the RuleTester could match the message by ID rather than by actual message text:

{
  code: '// Foo bar',
  errors: [
    {id: 2}, // "2" matches the integer used in context.report
  ],
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
Daniel15commented, Feb 7, 2017

Hi! Sorry for the delay, I got busy with other commitments. I’ll try to take a look this weekend 😃

Sent from my phone.

On Feb 6, 2017 7:00 PM, “Kevin Partington” notifications@github.com wrote:

@Daniel15 https://github.com/Daniel15 Friendly ping. Are you still interested in writing a PR for this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eslint/eslint/issues/7837#issuecomment-277887329, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFnHXXv0Y267gXajqz9MwfKuEARNeChks5rZ95ZgaJpZM4LY_iF .

0reactions
Daniel15commented, Feb 21, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I create an ESLint rule that will allow all globals matching ...
I am looking for an exception to the no-undef rule that will permit undeclared globals matching a naming rule. In this case, a...
Read more >
ESLint v3.17.0 released - ESLint中文
Error message assertions can now be regular expressions. ... 7ff42e8 New: Allow regexes in RuleTester (fixes #7837) (#8115) (Daniel Lo Nigro) ...
Read more >
Write eslint Rules That Meet Needs - Alibaba Cloud Community
Isn't it simple to place valid samples under valid and invalid samples under invalid? Let's look at the valid ones first: ruleTester.run("no- ...
Read more >
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET.
Read more >
simplest regex multi files renamer - AutoHotkey Community
another version allowing user test his own regex rule and make a rules ... Font,s14 gui, add,text, w500 +center , regex rule tester...
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