Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilise functional support for message option #6966

Open
Tracked by #6930
ybiquitous opened this issue Jun 24, 2023 · 6 comments
Open
Tracked by #6930

Stabilise functional support for message option #6966

ybiquitous opened this issue Jun 24, 2023 · 6 comments
Labels
status: ask to implement ask before implementing as may no longer be relevant type: enhancement a new feature that isn't related to rules

Comments

@ybiquitous
Copy link
Member

Currently, many built-in rules support the message option's function feature:

Experimental feature: some rules support message arguments. For example, when configuring the `color-no-hex` rule, the hex color can be used in the message string:
`.stylelintrc.js`:
```js
{
'color-no-hex': [true, {
message: (hex) => `Don't use hex colors like "${hex}"`,
}]
}
```
`.stylelintrc.json`:
<!-- prettier-ignore -->
```json
{
"color-no-hex": [true, {
"message": "Don't use hex colors like \"%s\""
}]
}
```
With formats that don't support a function like JSON, you can use a `printf`-like format (e.g., `%s`). On the other hand, with JS format, you can use both a `printf`-like format and a function.

Rough calculation:

$ git grep -l 'messageArgs: \[' lib/rules/*/index.js | wc -l
      92

And people have never complained about the feature spec, as I remember.

So, I suggest dropping experimental from this feature and officially publishing it.

Here're tasks:

  • Add the function support to all rules except for deprecated ones.
  • Fix the test:
  • Remove the sentence from all the rule README:
  • Remove "Experimental feature" wording from the doc:
@ybiquitous ybiquitous added the status: needs discussion triage needs further discussion label Jun 24, 2023
@ybiquitous ybiquitous mentioned this issue Jun 24, 2023
10 tasks
@jeddy3
Copy link
Member

jeddy3 commented Jun 24, 2023

SGTM.

@jeddy3 jeddy3 added status: ready to implement is ready to be worked on by someone type: enhancement a new feature that isn't related to rules and removed status: needs discussion triage needs further discussion labels Jun 24, 2023
@jeddy3 jeddy3 changed the title Drop experimental from message option's function support Stabilise functional support for message option Jun 24, 2023
@mattxwang
Copy link
Member

I'm doing this work now! I have a handful of cases that I'm not exactly sure how to proceed:

  • for rules like block-no-empty, there is no set of arguments to pass to the message (it doesn't reference any elements). Should I:
    • make the error message a parameterless (constant) function, and then pass an empty [] into messageArgs? The pro here is that we consistently use the same method to create custom messages, but it feels a bit strange/potentially inefficient.
    • not add messageArgs?
    • something else?
  • for rules like color-named, the expected and rejected functions have different numbers of arguments. Should I:
    • just have different input lengths in messageArgs
    • create a falsy parameter so that messageArgs always has the same number of elements (or standardize the functions to throw away an argument, etc.)
    • something else?

In #7194, I have included demos of:

  • making no-argument message functions constant (see: block-no-empty)
  • proceeding to have variable-length functions when the expected/rejected disagree with each other (see: color-named)

I may also be missing some context (I've read through #4117, #6312, #6589 but may have missed something). Let me know what we think!

@mattxwang
Copy link
Member

Separately, I may have missed: is there a specific way we're documenting the order and meaning of arguments for each message function?

@ybiquitous
Copy link
Member Author

make the error message a parameterless (constant) function, and then pass an empty [] into messageArgs? The pro here is that we consistently use the same method to create custom messages, but it feels a bit strange/potentially inefficient.

I have no strong opinion about that, but I feel passing messageArgs: [] is a bit verbose, even if doing so is consistent.

the expected and rejected functions have different numbers of arguments

I think it's OK to pass different messageArgs for the each function.

is there a specific way we're documenting the order and meaning of arguments for each message function?

For now, I don't find a good way to document them. I believe we can avoid documenting if no idea, although it's the best ideally.

@mattxwang
Copy link
Member

Thanks for the quick response! For now, I can:

  • pass different messageArgs
  • hold on if others have opinions about messageArgs: []
  • not document them for now

I could also do a documentation pass in another PR. One thought is perhaps we can autogenerate the documentation from examples and/or the source code?

Copy link
Contributor

This issue is older than one month. Please ask before opening a pull request, as it may no longer be relevant.

@github-actions github-actions bot added status: ask to implement ask before implementing as may no longer be relevant and removed status: ready to implement is ready to be worked on by someone labels Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ask to implement ask before implementing as may no longer be relevant type: enhancement a new feature that isn't related to rules
Development

No branches or pull requests

4 participants