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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const createStylelint = require('./createStylelint');
const formatters = require('./formatters');
const postcssPlugin = require('./postcssPlugin');
const report = require('./utils/report');
const resolveConfig = require('./resolveConfig');
const ruleMessages = require('./utils/ruleMessages');
const rules = require('./rules');
const { longhandSubPropertiesOfShorthandProperties } = require('./reference/properties');
const standalone = require('./standalone');
const validateOptions = require('./utils/validateOptions');
const resolveConfig = require('./resolveConfig');

/** @type {import('stylelint').PublicApi} */
const stylelint = Object.assign(postcssPlugin, {
Expand All @@ -26,6 +27,9 @@ const stylelint = Object.assign(postcssPlugin, {
validateOptions,
checkAgainstRule,
},
reference: {
longhandSubPropertiesOfShorthandProperties,
},
});

module.exports = stylelint;
237 changes: 165 additions & 72 deletions lib/reference/shorthandData.js → lib/reference/properties.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
'use strict';

module.exports = {
margin: new Set(['margin-top', 'margin-bottom', 'margin-left', 'margin-right']),
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).

animation: new Set([
'animation-name',
'animation-duration',
'animation-timing-function',
'animation-delay',
'animation-iteration-count',
'animation-direction',
'animation-fill-mode',
'animation-play-state',
]),
background: new Set([
'background-image',
'background-size',
Expand All @@ -13,15 +23,6 @@ module.exports = {
'background-attachment',
'background-color',
]),
font: new Set([
'font-style',
'font-variant',
'font-weight',
'font-stretch',
'font-size',
'font-family',
'line-height',
]),
border: new Set([
'border-top-width',
'border-bottom-width',
Expand All @@ -36,51 +37,6 @@ module.exports = {
'border-left-color',
'border-right-color',
]),
'border-top': new Set(['border-top-width', 'border-top-style', 'border-top-color']),
'border-bottom': new Set(['border-bottom-width', 'border-bottom-style', 'border-bottom-color']),
'border-left': new Set(['border-left-width', 'border-left-style', 'border-left-color']),
'border-right': new Set(['border-right-width', 'border-right-style', 'border-right-color']),
'border-width': new Set([
'border-top-width',
'border-bottom-width',
'border-left-width',
'border-right-width',
]),
'border-style': new Set([
'border-top-style',
'border-bottom-style',
'border-left-style',
'border-right-style',
]),
'border-color': new Set([
'border-top-color',
'border-bottom-color',
'border-left-color',
'border-right-color',
]),
'list-style': new Set(['list-style-type', 'list-style-position', 'list-style-image']),
'border-radius': new Set([
'border-top-right-radius',
'border-top-left-radius',
'border-bottom-right-radius',
'border-bottom-left-radius',
]),
transition: new Set([
'transition-delay',
'transition-duration',
'transition-property',
'transition-timing-function',
]),
animation: new Set([
'animation-name',
'animation-duration',
'animation-timing-function',
'animation-delay',
'animation-iteration-count',
'animation-direction',
'animation-fill-mode',
'animation-play-state',
]),
'border-block-end': new Set([
'border-block-end-width',
'border-block-end-style',
Expand All @@ -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.

'border-bottom-width',
'border-bottom-style',
'border-bottom-color',
]),
'border-color': new Set([
'border-top-color',
'border-bottom-color',
'border-left-color',
'border-right-color',
]),
'border-image': new Set([
'border-image-source',
'border-image-slice',
Expand All @@ -108,10 +76,73 @@ module.exports = {
'border-inline-start-style',
'border-inline-start-color',
]),
'column-rule': new Set(['column-rule-width', 'column-rule-style', 'column-rule-color']),
columns: new Set(['column-width', 'column-count']),
flex: new Set(['flex-grow', 'flex-shrink', 'flex-basis']),
'flex-flow': new Set(['flex-direction', 'flex-wrap']),
'border-left': new Set([
// prettier-ignore
'border-left-width',
'border-left-style',
'border-left-color',
]),
'border-radius': new Set([
'border-top-right-radius',
'border-top-left-radius',
'border-bottom-right-radius',
'border-bottom-left-radius',
]),
'border-right': new Set([
// prettier-ignore
'border-right-width',
'border-right-style',
'border-right-color',
]),
'border-style': new Set([
'border-top-style',
'border-bottom-style',
'border-left-style',
'border-right-style',
]),
'border-top': new Set([
// prettier-ignore
'border-top-width',
'border-top-style',
'border-top-color',
]),
'border-width': new Set([
'border-top-width',
'border-bottom-width',
'border-left-width',
'border-right-width',
]),
'column-rule': new Set([
// prettier-ignore
'column-rule-width',
'column-rule-style',
'column-rule-color',
]),
columns: new Set([
// prettier-ignore
'column-width',
'column-count',
]),
flex: new Set([
// prettier-ignore
'flex-grow',
'flex-shrink',
'flex-basis',
]),
'flex-flow': new Set([
// prettier-ignore
'flex-direction',
'flex-wrap',
]),
font: new Set([
'font-style',
'font-variant',
'font-weight',
'font-stretch',
'font-size',
'font-family',
'line-height',
]),
grid: new Set([
'grid-template-rows',
'grid-template-columns',
Expand All @@ -122,18 +153,47 @@ module.exports = {
'grid-column-gap',
'grid-row-gap',
]),
'grid-area': new Set(['grid-row-start', 'grid-column-start', 'grid-row-end', 'grid-column-end']),
'grid-column': new Set(['grid-column-start', 'grid-column-end']),
'grid-gap': new Set(['grid-row-gap', 'grid-column-gap']),
'grid-row': new Set(['grid-row-start', 'grid-row-end']),
'grid-template': new Set(['grid-template-columns', 'grid-template-rows', 'grid-template-areas']),
outline: new Set(['outline-color', 'outline-style', 'outline-width']),
'text-decoration': new Set([
'text-decoration-color',
'text-decoration-style',
'text-decoration-line',
'grid-area': new Set([
// prettier-ignore
'grid-row-start',
'grid-column-start',
'grid-row-end',
'grid-column-end',
]),
'grid-column': new Set([
// prettier-ignore
'grid-column-start',
'grid-column-end',
]),
'grid-gap': new Set([
// prettier-ignore
'grid-row-gap',
'grid-column-gap',
]),
'grid-row': new Set([
// prettier-ignore
'grid-row-start',
'grid-row-end',
]),
'grid-template': new Set([
// prettier-ignore
'grid-template-columns',
'grid-template-rows',
'grid-template-areas',
]),
'list-style': new Set([
// prettier-ignore
'list-style-type',
'list-style-position',
'list-style-image',
]),
margin: new Set([
// prettier-ignore
'margin-top',
'margin-bottom',
'margin-left',
'margin-right',
]),
'text-emphasis': new Set(['text-emphasis-style', 'text-emphasis-color']),
mask: new Set([
'mask-image',
'mask-mode',
Expand All @@ -144,4 +204,37 @@ module.exports = {
'mask-clip',
'mask-composite',
]),
outline: new Set([
// prettier-ignore
'outline-color',
'outline-style',
'outline-width',
]),
padding: new Set([
// prettier-ignore
'padding-top',
'padding-bottom',
'padding-left',
'padding-right',
]),
'text-decoration': new Set([
'text-decoration-color',
'text-decoration-style',
'text-decoration-line',
]),
'text-emphasis': new Set([
// prettier-ignore
'text-emphasis-style',
'text-emphasis-color',
]),
transition: new Set([
'transition-delay',
'transition-duration',
'transition-property',
'transition-timing-function',
]),
};

module.exports = {
longhandSubPropertiesOfShorthandProperties,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const shorthandData = require('../../reference/shorthandData');
const { longhandSubPropertiesOfShorthandProperties } = require('../../reference/properties');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const { isRegExp, isString } = require('../../utils/validateTypes');
Expand Down Expand Up @@ -40,7 +40,7 @@ const rule = (primary, secondaryOptions) => {
return;
}

const longhandProperties = Object.entries(shorthandData).reduce(
const longhandProperties = Object.entries(longhandSubPropertiesOfShorthandProperties).reduce(
(/** @type {Record<string, string[]>} */ longhandProps, [key, values]) => {
if (optionsMatches(secondaryOptions, 'ignoreShorthands', key)) {
return longhandProps;
Expand Down Expand Up @@ -80,9 +80,9 @@ const rule = (primary, secondaryOptions) => {

longhandDeclaration.push(prop);

const shorthandProps = /** @type {Record<string, Set<string>>} */ (shorthandData)[
shorthandProperty
];
const shorthandProps = /** @type {Record<string, Set<string>>} */ (
longhandSubPropertiesOfShorthandProperties
)[shorthandProperty];
const prefixedShorthandData = Array.from(shorthandProps || []).map(
(item) => prefix + item,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const shorthandData = require('../../reference/shorthandData');
const { longhandSubPropertiesOfShorthandProperties } = require('../../reference/properties');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');

Expand Down Expand Up @@ -35,9 +35,9 @@ const rule = (primary) => {
const unprefixedProp = vendor.unprefixed(prop).toLowerCase();
const prefix = vendor.prefix(prop).toLowerCase();

const overrideables = /** @type {Record<string, Set<string>>} */ (shorthandData)[
unprefixedProp
];
const overrideables = /** @type {Record<string, Set<string>>} */ (
longhandSubPropertiesOfShorthandProperties
)[unprefixedProp];

if (!overrideables) {
declarations[prop.toLowerCase()] = prop;
Expand Down