Navigation Menu

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

Refactor getting and setting declaration values into utils #5183

Merged
merged 2 commits into from Mar 5, 2021

Conversation

Dru89
Copy link
Contributor

@Dru89 Dru89 commented Mar 5, 2021

This change adds two utility functions, getDeclarationValue and setDeclarationValue, that replace a pattern found across many rule files. Extracting the getValue and setValue functions across the rule files was mentioned by @jeddy3 in #5106. (But if we want a specific issue to track this as well, I can still file one.)

I was about to tackle replacing another style-search based rule, but remembered that I'd probably want this utility function. And this change felt large enough to make on its own without also updating an additional rule.

This change adds two utility functions, `getDeclarationValue` and
`setDeclarationValue`, that replace a pattern found across many rule
files.
Comment on lines +13 to +14
if (_.has(decl, 'raws.value')) {
_.set(decl, 'raws.value.raw', value);
Copy link
Contributor Author

@Dru89 Dru89 Mar 5, 2021

Choose a reason for hiding this comment

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

This change uses lodash because Declaration doesn't strictly adhere to the version of postcss.Declaration that we use. stylelint uses PostCSS 7 and, as best as I can tell, these utilities were written so that support for migration to PostCSS 8 would be easier? Or perhaps the typescript definitions in PostCSS 7 are simply wrong.

The Declaration interface in PostCSS 7 inherits from NodeBase, where raws is defined as NodeRaws and does not support a value property.

In PostCSS 8, there is a declaration.d.ts file that has a DeclarationRaws interface that does supply a type signature that meets the first definition, however.

I thought about creating some complicated @typedefs, but it seemed like we were already using lodash in a few places to get around this type signature problem. Let me know if you have any suggestions on how to define this better, though.

(It's a similar story for using _.get in the getDeclarationValue utility.)

Copy link
Member

Choose a reason for hiding this comment

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

Great! 👏
I think it would be nice if you could leave the reason as a code comment as below:

// The reason to use Lodash here: ...
// See also: https://github.com/stylelint/stylelint/pull/5183/files#r588047494
if (_.has(decl, 'raws.value')) {

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.

I think this is a great improvement! 👏

* @param {string} value
* @returns {Declaration} The declaration that was passed in.
*/
module.exports = function getDeclarationValue(decl, value) {
Copy link
Member

Choose a reason for hiding this comment

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

Oh, this is a typo. 😅

Suggested change
module.exports = function getDeclarationValue(decl, value) {
module.exports = function setDeclarationValue(decl, value) {

Comment on lines +13 to +14
if (_.has(decl, 'raws.value')) {
_.set(decl, 'raws.value.raw', value);
Copy link
Member

Choose a reason for hiding this comment

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

Great! 👏
I think it would be nice if you could leave the reason as a code comment as below:

// The reason to use Lodash here: ...
// See also: https://github.com/stylelint/stylelint/pull/5183/files#r588047494
if (_.has(decl, 'raws.value')) {

@ybiquitous
Copy link
Member

I believe the upload error to Coverall should be resolved by retry:

https://github.com/stylelint/stylelint/pull/5183/checks?check_run_id=2037304546#step:7:9

@jeddy3 jeddy3 changed the title Create utils for getting and setting declaration values Refactor getting and setting declaration values into utils Mar 5, 2021
@jeddy3
Copy link
Member

jeddy3 commented Mar 5, 2021

@Dru89 A great refactor, thanks!

LGTM, once @ybiquitous suggestions are in.

I've restarted the tests.

@Dru89
Copy link
Contributor Author

Dru89 commented Mar 5, 2021

Thanks, y'all! I've updated the setDeclarationValue method to have the right name and add a comment about the use of lodash.

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, thanks!

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.

@Dru89 Thank you! 😄

@jeddy3 jeddy3 merged commit 7b894ba into stylelint:master Mar 5, 2021
@Dru89 Dru89 deleted the get-set-declaration-value branch March 6, 2021 00:00
This was referenced Mar 7, 2021
This was referenced Mar 13, 2021
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.

None yet

3 participants