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

Add shorthand properties reference to public API #6168

Merged
merged 7 commits into from Jun 27, 2022

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Jun 23, 2022

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

Is there anything in the PR that needs further explanation?

This pull request is following the suggestion below by @jeddy3 in #6151 (comment):

  • align and expose data as it's requested (starting with the shorthand data)

The type definition is generated by the following script:

const shorthandData = require("./lib/reference/shorthandData")
console.log('export type ShorthandData = {')
for (const key of Object.keys(shorthandData).sort()) {
	console.log(`\t'${key}': Set<string>;`)
}
console.log('};')

The type definition is generated by the following script:

```js
const shorthandData = require("./lib/reference/shorthandData")
console.log('export type ShorthandData = {')
for (const key of Object.keys(shorthandData)) {
	console.log(`\t'${key}': Set<string>;`)
}
console.log('};')
```
@ybiquitous ybiquitous force-pushed the expose-shorthand-props-reference branch from 37fa87e to d12beec Compare June 23, 2022 00:23
@ybiquitous
Copy link
Member Author

[ask] The shorthandData name seems unclear to me. Is it acceptable, or is there a better name?

@jeddy3
Copy link
Member

jeddy3 commented Jun 23, 2022

@ybiquitous Thank you. It's looking good.

[ask] The shorthandData name seems unclear to me. Is it acceptable, or is there a better name?

Let's go with subPropertiesOfLonghandProperties as per #6151 (comment).

Future property sets could include acceptCustomIdentsProperties and these:

keywordSets.shorthandTimeProperties = new Set(['transition', 'animation']);
keywordSets.longhandTimeProperties = new Set([
'transition-duration',
'transition-delay',
'animation-duration',
'animation-delay',
]);
keywordSets.timeProperties = uniteSets(
keywordSets.shorthandTimeProperties,
keywordSets.longhandTimeProperties,
);

(Which are in the keywordSets.js at the moment...)

What file structure shall we use? Choices are:

  • 1 file per set, e.g. reference/subPropertiesOfLonghandProperties.js
  • 1 file per group, e.g. reference/properties.js (which would contain all the above)
  • a single file for all the sets, e.g. reference/reference.js

I feel like either of the last two will be easiest to work with and maintain. For the 1 file per group, offhand I can think of the following groups we have:

  • properties
  • functions
  • units
  • keywords
  • selectors (which would include all the HTML elements but renamed as type selectors)
  • media features
  • operators
  • combinators

@jeddy3 jeddy3 changed the title Expose shorthand properties reference as public API Add shorthand properties reference to public API Jun 23, 2022
@ybiquitous
Copy link
Member Author

@jeddy3 Thanks for your helpful feedback! The idea of "1 file per group" seems so nice. 👍🏼

I'll push commits into this pull request, following the ideas of subPropertiesOfLonghandProperties and "1 file per group".

@jeddy3
Copy link
Member

jeddy3 commented Jun 23, 2022

I'll push commits into this pull request, following the ideas of subPropertiesOfLonghandProperties and "1 file per group".

Oops, I got it back-to-front. From the spec:

Some properties are shorthand properties, meaning that they allow authors to specify the values of several properties with a single property. A shorthand property sets all of its longhand sub-properties, exactly as if expanded in place.

Let's go with longhandSubPropertiesOfShorthandProperties to be accurate with the wording in the spec, even if it's a bit verbose.

@ybiquitous
Copy link
Member Author

I have one more suggestion: what if using Map instead of Object?

This MDN doc describes the differences between Map and Object well. I think Map could be safer and more stable than Object, especially as a public API, even if Map doesn't support JSON serialization natively. E.g.

// Using Object
const longhandSubPropertiesOfShorthandProperties = {
  margin: new Set(['margin-top', 'margin-bottom', 'margin-left', 'margin-right']),
  // ...
};
longhandSubPropertiesOfShorthandProperties.margin; //=> Set(...)

// Using Map
const longhandSubPropertiesOfShorthandProperties = new Map([
  ['margin', new Set(['margin-top', 'margin-bottom', 'margin-left', 'margin-right'])],
  // ...
]);
longhandSubPropertiesOfShorthandProperties.get('margin'); //=> Set(...)

What do you think?

@ybiquitous ybiquitous marked this pull request as ready for review June 25, 2022 00:38
@ybiquitous ybiquitous marked this pull request as draft June 25, 2022 00:42
padding: new Set(['padding-top', 'padding-bottom', 'padding-left', 'padding-right']),
/** @type {import('stylelint').LonghandSubPropertiesOfShorthandProperties} */
const longhandSubPropertiesOfShorthandProperties = {
/* eslint sort-keys: 'error' */
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] I've sorted properties alphabetically for findability (the diff increases, though).

@@ -91,6 +47,18 @@ module.exports = {
'border-block-start-style',
'border-block-start-color',
]),
'border-bottom': new Set([
// prettier-ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] To align always multi-line for readability.

@ybiquitous ybiquitous marked this pull request as ready for review June 25, 2022 00:59
@jeddy3
Copy link
Member

jeddy3 commented Jun 25, 2022

I have one more suggestion: what if using Map instead of Object?

Sounds good to me.

All the other changes are looking great, by the way!

| 'text-decoration'
| 'text-emphasis'
| 'transition',
ReadonlySet<string>
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] ReadonlyMap and ReadonlySet are built-in types.


return;
}

for (const longhandProp of overrideables) {
if (!Object.prototype.hasOwnProperty.call(declarations, prefix + longhandProp)) {
const declaration = declarations.get(prefix + longhandProp);
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Refactor to use Map instead of Object; hasOwnProperty is no longer needed (safe access).

Copy link
Member

Choose a reason for hiding this comment

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

Much nicer.

shorthands.push(shorthand);
longhandToShorthands.set(longhand, shorthands);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Refactor to use Map and for...of instead of Object and reduce() for readability.

@ybiquitous ybiquitous requested a review from jeddy3 June 27, 2022 01:24
@ybiquitous
Copy link
Member Author

@jeddy3 I've switched from Object to Map and Set (ReadonlyMap and ReadonlySet in TypeScript), and also refactored declaration-block-* rules using Map.

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.

Fantastic, thank you!

I think we have a good direction here for exposing our reference data.

@jeddy3 jeddy3 merged commit 6ed812b into main Jun 27, 2022
@jeddy3 jeddy3 deleted the expose-shorthand-props-reference branch June 27, 2022 10:19
@jeddy3
Copy link
Member

jeddy3 commented Jun 27, 2022

  • Added: longhand sub-properties of shorthand properties reference data to public API (#6168).

@ybiquitous
Copy link
Member Author

@jeddy3 Thank you for all of your reviews. 😊 👍🏼

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.

Publicly expose reference data
2 participants