+
Skip to content

Conversation

jhae-de
Copy link
Contributor

@jhae-de jhae-de commented Sep 22, 2025

This pull request improves the handling of custom error messages in a Stylelint configuration. It ensures that when a custom error message is specified in a Stylelint configuration, the rule name is automatically appended in parentheses at the end of the message if not already present.

Which issue, if any, is this issue related to?

Closes #8773

Is there anything in the PR that needs further explanation?

When a custom message is defined for a rule, such as for the custom-property-pattern rule:

{
  'custom-property-pattern': [
    '^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
    {
      message: (name) => `Expected custom property name "${name}" to be kebab-case`,
    },
  ],
}

the rule name is now automatically appended in parentheses at the end of the message if it is not already present. This makes it easier to identify the source rule for each reported problem and improves traceability. Existing messages that already include the rule name remain unchanged.

Before:

:root {
  --MyCustomProperty: '';
  // Stylelint error: Expected custom property name "--MyCustomProperty" to be kebab-case
  //                                                                                     ^
  //                                                                            Missing rule name
}

After:

:root {
  --MyCustomProperty: '';
  // Stylelint error: Expected custom property name "--MyCustomProperty" to be kebab-case (custom-property-pattern)
  //                                                                                      ^
  //                                                                            Automatically appended rule name
}

Copy link

changeset-bot bot commented Sep 22, 2025

🦋 Changeset detected

Latest commit: 0a199d6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jeddy3 jeddy3 changed the title Always append rule name to error messages Add rule name to custom messages Sep 23, 2025
@jeddy3
Copy link
Member

jeddy3 commented Sep 23, 2025

@jhae-de Thanks for the pull request.

I can't remember if this was an intentional design decision, as we added the message property a few years ago.

We can either:

  • automate adding the rule name via this PR
  • update our docs to say the rule name must be added manually if the users want it, and fix the custom messages rule in our shared configs so they end with the rule name

It seems the former leads to automated consistency, and the latter to choice.

Let's see if any other maintainers have an opinion.


Context: this is for the text property of the result object. Our string formatter removes the rule name from the message so that it can be placed in another column:

const ruleString = ` (${message.rule})`;
if (result.endsWith(ruleString)) {
result = result.slice(0, result.lastIndexOf(ruleString));
}

@jhae-de
Copy link
Contributor Author

jhae-de commented Sep 23, 2025

@jeddy3 Thanks for your feedback.

I had also thought about which of the two approaches would be better beforehand. The decisive factor for me in the end was that the messages defined in the built-in rules are also automatically enriched with the rule names. Applying this to custom messages as well was the logical consequence for me.

I am curious to hear what others think about this.

@ybiquitous
Copy link
Member

@jhae-de Thank you for discovering and fixing the issue. I implemented the custom message feature, but this problem was out of my mind, sorry.

First, let me show where the (<rule-name>) text is constructed in a lint problem message. It's a public utility stylelint.utils.ruleMessages():

Second, our default string formatter removes the (<rule-name>) text from a problem message here:

So, to achieve consistency for problem messages, we may need to choose either of the two options below:

  1. Add (<rule-name>) to custom messages, too (like this PR does)
  2. Remove (<rule-name>) from any messages (breaking)

If we choose option 1, there is a little possibility of breaking, but its impact should not be significant.

Instead, if we opt for option 2, it should be shipped in a major version.

Personally, I'm leaning towards option 2 because I think it's verbose to contain (<rule-name>) in a message. Let's see this example that the current Stylelint contains in the JS object:

{
  "rule": "block-no-empty",
  "text": "Unexpected empty block (block-no-empty)"
}

As shown above, our default string (also verbose) formatter removes this duplication before outputting it, like this:

✖  Unexpected empty block      block-no-empty

To make custom formatters easier to implement, should we separate (<rule-name>) from a message? Any thoughts?

@jhae-de
Copy link
Contributor Author

jhae-de commented Sep 27, 2025

@ybiquitous Thank you for your feedback and some context regarding the rule messages.

I hadn't actually thought about the option of removing the rule names from the messages altogether. At first, I didn't think the idea was so bad, as my main concern here was consistency.

However, I would like to point out the following:

  1. The compact and unix formatters currently only output the rule messages. With these outputs, we would also lose the rule names here.
  2. The rule names would also no longer be displayed in some IDEs. For example, PhpStorm only outputs the rule messages. To my knowledge, there is no way to configure Stylelint with custom options here, e.g., to customize the formatter. The output does not change even if I specify the formatter in the Stylelint configuration file itself.

Output in PhpStorm:

Stylelint rule message in PhpStorm

At least as far as point 2 is concerned, I always liked that the rule names were also displayed here. In this respect, I would personally find it regrettable if we were to remove the rule names from the messages in general.

@ybiquitous
Copy link
Member

@jhae-de Thanks for the feedback, too!

Regarding the compact and unix formatters, it would be easy to keep the message compatibility by explicitly adding (<rule-name>). 👌🏼

About some IDEs, I didn't have any ideas before your comment, such as PhpStorm. Thank you for the info. I think it'd be more flexible to construct messages if (<rule-name>) were removed, but some clients might expect that messages would still contain (<rule-name>) by default, as you mentioned.

I don't recall any requests for such flexibility having been submitted. Maybe, no requests.

So now, it seems reasonable to me to add (<rule-name>) to custom messages, as well. Probably, I guess that it won't be a breaking change. 👍🏼

@jeddy3 Any thoughts?

@jeddy3
Copy link
Member

jeddy3 commented Sep 27, 2025

So now, it seems reasonable to me to add (<rule-name>) to custom messages, as well.

SGTM. In addition to IDEs like PhpStorm, the rulename is useful when Stylelint is used as a PostCSS plugin and postcss-reporter reports the warnings:

Screenshot 2025-09-27 at 19 52 22

We now recommend using our built-in formatters via the CLI or Node.js API, but some people may still be using the PostCSS plugin.

Copy link
Contributor

github-actions bot commented Sep 28, 2025

This PR is packaged and the instant preview is available (0a199d6). View the demo website.

Install it locally:

npm i -D https://pkg.pr.new/stylelint@0a199d6

@jhae-de
Copy link
Contributor Author

jhae-de commented Sep 28, 2025

It looks to me as if this PR will be accepted. Is there anything else I need to do, such as generating the changeset?

@ybiquitous
Copy link
Member

@jhae-de Yes, please add a changelog, respecting the PR title, e.g.:

---
"stylelint": minor
---
Added: rule name to custom messages

@jhae-de jhae-de force-pushed the feature/append-rule-name-to-custom-messages branch 2 times, most recently from f9fde25 to 2d11f2b Compare September 28, 2025 21:53
@jhae-de jhae-de requested a review from ybiquitous September 28, 2025 21:53
Ensures that when a custom message is specified in a Stylelint configuration, the rule name is automatically appended in parentheses at the end of the message if not already present, improving traceability of reported problems.
@jhae-de jhae-de force-pushed the feature/append-rule-name-to-custom-messages branch from 2d11f2b to 0a199d6 Compare September 29, 2025 13:16
Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks 👍🏼

Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you

@jeddy3 jeddy3 merged commit 242f757 into stylelint:main Sep 30, 2025
17 checks passed
@romainmenke romainmenke mentioned this pull request Oct 1, 2025
5 tasks
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 3, 2025
| datasource | package   | from    | to      |
| ---------- | --------- | ------- | ------- |
| npm        | stylelint | 16.24.0 | 16.25.0 |


## [v16.25.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16250---2025-10-03)

It adds 3 new features, including experimental support for bulk suppressions. It's also our first [immutable release](https://github.blog/changelog/2025-08-26-releases-now-support-immutability-in-public-preview/), with the package published to npm using [trusted publishing](https://github.blog/changelog/2025-07-31-npm-trusted-publishing-with-oidc-is-generally-available/) and our dependencies updated on a [cool down](https://github.blog/changelog/2025-07-01-dependabot-supports-configuration-of-a-minimum-package-age/) for improved supply chain security.

- Added: support for bulk suppressions ([#8564](stylelint/stylelint#8564)) ([@ryo-manba](https://github.com/ryo-manba)).
- Added: `ignoreAtRules: []` to `no-invalid-position-declaration` ([#8781](stylelint/stylelint#8781)) ([@jrmlt](https://github.com/jrmlt)).
- Added: rule name to custom messages ([#8774](stylelint/stylelint#8774)) ([@jhae-de](https://github.com/jhae-de)).
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 3, 2025
| datasource | package   | from    | to      |
| ---------- | --------- | ------- | ------- |
| npm        | stylelint | 16.24.0 | 16.25.0 |


## [v16.25.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16250---2025-10-03)

It adds 3 new features, including experimental support for bulk suppressions. It's also our first [immutable release](https://github.blog/changelog/2025-08-26-releases-now-support-immutability-in-public-preview/), with the package published to npm using [trusted publishing](https://github.blog/changelog/2025-07-31-npm-trusted-publishing-with-oidc-is-generally-available/) and our dependencies updated on a [cool down](https://github.blog/changelog/2025-07-01-dependabot-supports-configuration-of-a-minimum-package-age/) for improved supply chain security.

- Added: support for bulk suppressions ([#8564](stylelint/stylelint#8564)) ([@ryo-manba](https://github.com/ryo-manba)).
- Added: `ignoreAtRules: []` to `no-invalid-position-declaration` ([#8781](stylelint/stylelint#8781)) ([@jrmlt](https://github.com/jrmlt)).
- Added: rule name to custom messages ([#8774](stylelint/stylelint#8774)) ([@jhae-de](https://github.com/jhae-de)).
@jhae-de jhae-de deleted the feature/append-rule-name-to-custom-messages branch October 4, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add rule name to custom messages
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载