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
Add experimental support for custom message arguments #6312
Conversation
🦋 Changeset detectedLatest commit: fb47cf7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
I am using
The minimum for the PoC would be JSON IMO.
What about
? eval('(pattern) => `Expected custom property name to be kebab-case, but actual "${pattern}"`') |
@Mouvedia Thanks for the feedback. JSON support is still essential. Perhaps, using |
i.e. the last element is always the body (exactly like for the reversal: Function('pattern', 'return `Expected custom property name to be kebab-case, but actual "${pattern}"`') i.e. you pass all the elements in the same order |
Using See also: |
Is stylelint being run in browser context? |
Not now, but it may be realized in the future (see #4796). |
Here is an example of a template syntax (in JSON), using {
"message": [
"pattern", // message arguments
"Expected custom property name to be kebab-case, but actual \"{pattern}\"" // message template
]
} EDIT: People can specify arbitrary argument names like this: {
"message": [
"foo", // message arguments
"Expected custom property name to be kebab-case, but actual \"{foo}\"" // message template
]
} |
Examples of interpolation characters:
|
Let's keep it simple:
|
Oh, I meant to indicate the candidates in #6312 (comment). Also, I think it's better to avoid {
message: [
'pattern',
'Expected custom property name to be kebab-case, but actual "${pattern}"',
/* ^^^^^^^^^^ not template literal */
]
} The idea of |
I find a more straightforward solution; to use the notation ( {
"message": "Expected custom property name to be kebab-case, but actual \"%s\""
}
|
For the PoC, yes. We should add the support of |
The eddbb4b commit adds JSON support using |
To-dos
|
related: #5170 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic work, thank you!
Update the usage document in this PR
Let's classify this as an experimental feature (like we did for `--fix) so that we can treat any issues that may emerge as non-breaking. It's possible this will happen when we add support to more rules or plugin authors start to make use of the feature.
As with exposing our reference data, we should only add support to a rule when a user requests it as we'll need to discuss and lockdown the message format.
I suggest we add support to color-no-hex
, rather than custom-property-pattern
, in this pull request as it was one of the rules originally requested in the issue.
Let's then add the following to the color-no-hex
rule README:
"The message
secondary option can accept the arguments of this rule."
It'll be the last item in the extended description, i.e. below the following if it's present:
"The fix
option can automatically fix all of the problems reported by this rule.".
We can also extend the description of the message
option to say something along the lines of:
"Experimental feature: some rules support message arguments. For example, when configuring the color-no-hex
rule the hex color can be used in message
string:
.stylelintrc.js
:
{
'color-no-hex': [true, {
message: (hex) => `Don't use hex colors like "${hex}"`,
}]
}
.stylelintrc.json
:
{
"color-no-hex": [true, {
"message": "Don't use hex colors like '${%s/'"
}]
}
@jeddy3 Thanks for the feedback. Your suggestion sounds good, so I'll implement it. By the way, I chose |
docs/user-guide/configure.md
Outdated
} | ||
``` | ||
|
||
With formats that don't support a function like JSON, you can use a `printf`-like format (e.g., `%s`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note] I think a description of %s
is necessary. Please let me know if you have a better expression.
See also the util.format
on the Node.js documentation.
|
||
`.stylelintrc.json`: | ||
|
||
<!-- prettier-ignore --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note] Unless the disabling comment, Prettier would format the snippet like this:
{
"color-no-hex": [
true,
{
"message": "Don't use hex colors like \"%s\""
}
]
}
Compared to the snippet above for .stylelintrc.js
, it would not be easier to read.
@jeddy3 I've done updating the doc. I'd appreciate your review when you have time. |
@@ -42,7 +42,8 @@ const rule = (primary) => { | |||
const endIndex = index + node.value.length; | |||
|
|||
report({ | |||
message: messages.rejected(node.value), | |||
message: messages.rejected, | |||
messageArgs: [node.value], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remark
If I understand correctly, the order will be arbitrary.
e.g. if you don't use --fix
but you want to use the fixed value in the message (this rule is not a good example because it's not fixable)
i.e.
- we would generate the value but not edit the file in place
- the user will need to know that the bad value is the first element and the good value is the second element
related
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mouvedia Thanks for the share. Other problems might be found than you pointed out, but this feature is still experimental. We would try considering it based on reactions or feedback later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
Closes #4117
This PR is still a PoC.I appreciate any comments or suggestions.Summary:
stylelint.utils.report()
can receive amessage
function and an array ofmessageArgs
, in addition to just amessage
stringOnly JS config file support (e.g..stylelintrc.js
)message
property can receive parameters like%s
for string interpolation. The Node.jsutil.format()
API is internally used.Considerations:
Should we support other formats like JSON, YAML, etc.? If doing so, another method than a message function might be necessary; for example, some template syntax likemessage: "a={a}, b={b}"
.It's necessary to changereport()
call in all the rules in this PR. This would be a tedious job. Is there a better API instead of addingmessageArgs
?messageArgs
to each rule implementation.Example:
.stylelintrc.js
:Or
.stylelintrc.json
:a.css
:Run: