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.

Could we put lint messages in rule metadata?

See original GitHub issue

Inspired by #6738, where it was noticed that some of our lint messages are inconsistent about ending in periods.

I think it would be really awesome to have lint messages as a part of rule metadata. This does not have immediate benefit for linting, but it would allow us to do interesting things with auto-generated documentation, and even allow searching by error message on the site.

Proposed API

We would support a new messages key on rule meta objects. The value of this key is an object with arbitrary keys and with string values equal to different messages being used.

Examples

// lib/rules/array-bracket-spacing.js
module.exports = {
    meta: {
        docs: {
            description: "enforce consistent spacing inside array brackets",
            category: "Stylistic Issues",
            recommended: false
        },
        fixable: "whitespace",
        schema: [
            {
                enum: ["always", "never"]
            },
            {
                type: "object",
                properties: {
                    singleValue: {
                        type: "boolean"
                    },
                    objectsInArrays: {
                        type: "boolean"
                    },
                    arraysInArrays: {
                        type: "boolean"
                    }
                },
                additionalProperties: false
            }
        ],
        messages: {
            noBeginningSpace: "There should be no space after '{{ token }}'",
            noEndingSpace: "There should be no space before '{{ token }}'",
            requiredBeginningSpace: "A space is required after '{{ token }}'",
            requiredEndingSpace: "A space is required before '{{ token }}'"
        }
    }
    // create: function (context) ...
};
// lib/rules/no-continue.js
module.exports = {
    meta: {
        docs: {
            description: "disallow `continue` statements",
            category: "Stylistic Issues",
            recommended: false
        },

        schema: [],

        messages: {
            default: "Unexpected use of continue statement"
        }
    },
    // create: function (context) ...
};

Example usage:

context.report({
    node: node,
    messageId: "noSpacesBefore",
    data: {
        token: token
    }
});

Challenges

The main challenge is figuring out how to access the messages from within the rule visitor functions, where we typically don’t use object-oriented patterns. Very worst case (I’m hoping to avoid this), maybe we could augment RuleContext to support a getMessages() function which can return the available messages.

A smaller challenge is to convert messages that use string concatenation (see array-bracket-spacing source for an example) to use our token replacement engine instead. I assume this is not a significant roadblock.

Benefits

Here are the benefits I can see with doing this (none of which have to do with actual linting):

  1. Make it easier for information architect types like @pedrottimark or @scriptdaemon or @aubergine10 to find lint messages and ensure consistency across all core rules
  2. Rule documentation auto-generation could include a block showing the possible linting messages for a rule, so that a user knows how violations will actually look (notwithstanding placeholder tokens which depend on the violation context).
  3. Rule linting messages could be indexed in the site search. That way a user could search for the error message they are getting, and we can pull up the rule generating that message. (Anyone remember jslinterrors.com?)

Etc.

What do folks think? Am I missing any significant challenges or roadblocks? Have I overstated the potential benefits? Or could this be worth pursuing at some point?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:18 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
platinumazurecommented, Jul 26, 2016

I think I count 3 approvals (@mysticatea, @nzakas, @ilyavolodin) and I’m willing to champion and implement.

Regarding i18n, I propose that we agree to try to implement in an i18n friendly manner, but that we do not attempt to include i18n in this specific proposal or to choose a particular library for it in this discussion.

Regarding potential implementation, I would propose something like this:

  • Semver-minor PR to support messageId and meta.messages and implement in one or two rules
  • Semver-patch PR to update documentation for context.report
  • One or more semver-patch PRs to convert our rules to use this new style of message if all goes well
  • One semver-minor (?) PR to create a new internal rule enforcing new style messages (pending feedback on usability for rule developers)

Does this seem like a sensible approach? Am I missing anything?

1reaction
j-f1commented, Aug 25, 2017

An addition to this could be the ability for RuleTester to accept error objects in the invalid section with messageId and data, reducing code duplication.

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 write custom ESLint rules
Let's look into how to write custom ESLint rules. Creating these isn't hard once you have the framework in place.
Read more >
How to Create Custom ESLint Rules (It's Not as Hard ...
Learn how to create custom ESLint rules to better lint your Angular applications.
Read more >
unified-lint-rule
You can use this package when you want to make custom lint rules. Install. This package is ESM only. In Node.js (version 12.20+,...
Read more >
Android Lint API Guide - Google Samples
13.0.16 Can I make my lint check “not suppressible?” ... All error messages and issue metadata strings in lint are interpreted using simple...
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