Skip to content

Commit

Permalink
Refactor to use Set for shorthandData and mathFunctions types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jun 22, 2022
1 parent a6318d4 commit bd8c7fd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/reference/mathFunctions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';

module.exports = ['calc', 'clamp', 'max', 'min'];
module.exports = new Set(['calc', 'clamp', 'max', 'min']);
107 changes: 55 additions & 52 deletions lib/reference/shorthandData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

/** @type {Record<string, string[]>} */
module.exports = {
margin: ['margin-top', 'margin-bottom', 'margin-left', 'margin-right'],
padding: ['padding-top', 'padding-bottom', 'padding-left', 'padding-right'],
background: [
margin: new Set(['margin-top', 'margin-bottom', 'margin-left', 'margin-right']),
padding: new Set(['padding-top', 'padding-bottom', 'padding-left', 'padding-right']),
background: new Set([
'background-image',
'background-size',
'background-position',
Expand All @@ -13,17 +12,17 @@ module.exports = {
'background-clip',
'background-attachment',
'background-color',
],
font: [
]),
font: new Set([
'font-style',
'font-variant',
'font-weight',
'font-stretch',
'font-size',
'font-family',
'line-height',
],
border: [
]),
border: new Set([
'border-top-width',
'border-bottom-width',
'border-left-width',
Expand All @@ -36,43 +35,43 @@ module.exports = {
'border-bottom-color',
'border-left-color',
'border-right-color',
],
'border-top': ['border-top-width', 'border-top-style', 'border-top-color'],
'border-bottom': ['border-bottom-width', 'border-bottom-style', 'border-bottom-color'],
'border-left': ['border-left-width', 'border-left-style', 'border-left-color'],
'border-right': ['border-right-width', 'border-right-style', 'border-right-color'],
'border-width': [
]),
'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': [
]),
'border-style': new Set([
'border-top-style',
'border-bottom-style',
'border-left-style',
'border-right-style',
],
'border-color': [
]),
'border-color': new Set([
'border-top-color',
'border-bottom-color',
'border-left-color',
'border-right-color',
],
'list-style': ['list-style-type', 'list-style-position', 'list-style-image'],
'border-radius': [
]),
'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: [
]),
transition: new Set([
'transition-delay',
'transition-duration',
'transition-property',
'transition-timing-function',
],
animation: [
]),
animation: new Set([
'animation-name',
'animation-duration',
'animation-timing-function',
Expand All @@ -81,39 +80,39 @@ module.exports = {
'animation-direction',
'animation-fill-mode',
'animation-play-state',
],
'border-block-end': [
]),
'border-block-end': new Set([
'border-block-end-width',
'border-block-end-style',
'border-block-end-color',
],
'border-block-start': [
]),
'border-block-start': new Set([
'border-block-start-width',
'border-block-start-style',
'border-block-start-color',
],
'border-image': [
]),
'border-image': new Set([
'border-image-source',
'border-image-slice',
'border-image-width',
'border-image-outset',
'border-image-repeat',
],
'border-inline-end': [
]),
'border-inline-end': new Set([
'border-inline-end-width',
'border-inline-end-style',
'border-inline-end-color',
],
'border-inline-start': [
]),
'border-inline-start': new Set([
'border-inline-start-width',
'border-inline-start-style',
'border-inline-start-color',
],
'column-rule': ['column-rule-width', 'column-rule-style', 'column-rule-color'],
columns: ['column-width', 'column-count'],
flex: ['flex-grow', 'flex-shrink', 'flex-basis'],
'flex-flow': ['flex-direction', 'flex-wrap'],
grid: [
]),
'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']),
grid: new Set([
'grid-template-rows',
'grid-template-columns',
'grid-template-areas',
Expand All @@ -122,16 +121,20 @@ module.exports = {
'grid-auto-flow',
'grid-column-gap',
'grid-row-gap',
],
'grid-area': ['grid-row-start', 'grid-column-start', 'grid-row-end', 'grid-column-end'],
'grid-column': ['grid-column-start', 'grid-column-end'],
'grid-gap': ['grid-row-gap', 'grid-column-gap'],
'grid-row': ['grid-row-start', 'grid-row-end'],
'grid-template': ['grid-template-columns', 'grid-template-rows', 'grid-template-areas'],
outline: ['outline-color', 'outline-style', 'outline-width'],
'text-decoration': ['text-decoration-color', 'text-decoration-style', 'text-decoration-line'],
'text-emphasis': ['text-emphasis-style', 'text-emphasis-color'],
mask: [
]),
'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',
]),
'text-emphasis': new Set(['text-emphasis-style', 'text-emphasis-color']),
mask: new Set([
'mask-image',
'mask-mode',
'mask-position',
Expand All @@ -140,5 +143,5 @@ module.exports = {
'mask-origin',
'mask-clip',
'mask-composite',
],
]),
};
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ const rule = (primary, secondaryOptions) => {

longhandDeclaration.push(prop);

const prefixedShorthandData = (shorthandData[shorthandProperty] || []).map(
const shorthandProps = /** @type {Record<string, Set<string>>} */ (shorthandData)[
shorthandProperty
];
const prefixedShorthandData = Array.from(shorthandProps || []).map(
(item) => prefix + item,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const rule = (primary) => {

eachDecl((decl) => {
const prop = decl.prop;
const unprefixedProp = vendor.unprefixed(prop);
const unprefixedProp = vendor.unprefixed(prop).toLowerCase();
const prefix = vendor.prefix(prop).toLowerCase();

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

if (!overrideables) {
declarations[prop.toLowerCase()] = prop;
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/isMathFunction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const MATH_FUNCTIONS = require('../reference/mathFunctions');
const mathFunctions = require('../reference/mathFunctions');

/**
* Check whether a node is math function
Expand All @@ -9,5 +9,5 @@ const MATH_FUNCTIONS = require('../reference/mathFunctions');
* @return {boolean} If `true`, the node is math function
*/
module.exports = function isMathFunction(node) {
return node.type === 'function' && MATH_FUNCTIONS.includes(node.value.toLowerCase());
return node.type === 'function' && mathFunctions.has(node.value.toLowerCase());
};

0 comments on commit bd8c7fd

Please sign in to comment.