Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoz committed Mar 9, 2024
1 parent 4a9de25 commit 7dbb9e6
Show file tree
Hide file tree
Showing 95 changed files with 27,657 additions and 27,354 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"prettier": {
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 240,
"printWidth": 120,
"quoteProps": "consistent",
"semi": false,
"singleQuote": true,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/convert/toCamelCase.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/** Returns the given value converted to camel-case. */
export const toCamelCase = (/** @type {string} */ value) => (!/[A-Z]/.test(value) ? value.replace(/-[^]/g, (capital) => capital[1].toUpperCase()) : value)
export const toCamelCase = (/** @type {string} */ value) =>
!/[A-Z]/.test(value) ? value.replace(/-[^]/g, (capital) => capital[1].toUpperCase()) : value
58 changes: 30 additions & 28 deletions packages/core/src/convert/toCssRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ const comma = /\s*,\s*(?![^()]*\))/
/** Default toString method of Objects. */
const toStringOfObject = Object.prototype.toString

export const toCssRules = (
style,
selectors,
conditions,
config,
onCssText
) => {
export const toCssRules = (style, selectors, conditions, config, onCssText) => {
/** @type {[string[], string[], string[]]} CSSOM-compatible rule being created. */
let currentRule = undefined

Expand Down Expand Up @@ -50,9 +44,13 @@ export const toCssRules = (

for (data of datas) {
const camelName = toCamelCase(name)

/** Whether the current data represents a nesting rule, which is a plain object whose key is not already a util. */
const isRuleLike = typeof data === 'object' && data && data.toString === toStringOfObject && (!config.utils[camelName] || !selectors.length)
const isRuleLike =
typeof data === 'object' &&
data &&
data.toString === toStringOfObject &&
(!config.utils[camelName] || !selectors.length)

// if the left-hand "name" matches a configured utility
// conditionally transform the current data using the configured utility
Expand Down Expand Up @@ -88,7 +86,9 @@ export const toCssRules = (
// if the left-hand "name" matches a configured at-rule
if (isAtRuleLike) {
// transform the current name with the configured media at-rule prelude
name = toResolvedMediaQueryRanges(name.slice(1) in config.media ? '@media ' + config.media[name.slice(1)] : name)
name = toResolvedMediaQueryRanges(
name.slice(1) in config.media ? '@media ' + config.media[name.slice(1)] : name,
)
}

if (isRuleLike) {
Expand All @@ -109,24 +109,27 @@ export const toCssRules = (
if (currentRule === undefined) currentRule = [[], selectors, conditions]

/** CSS left-hand side value, which may be a specially-formatted custom property. */
name = !isAtRuleLike && name.charCodeAt(0) === 36 ? `--${toTailDashed(config.prefix)}${name.slice(1).replace(/\$/g, '-')}` : name
name =
!isAtRuleLike && name.charCodeAt(0) === 36
? `--${toTailDashed(config.prefix)}${name.slice(1).replace(/\$/g, '-')}`
: name

/** CSS right-hand side value, which may be a specially-formatted custom property. */
data = (
data =
// preserve object-like data
isRuleLike ? data
// replace all non-unitless props that are not custom properties with pixel versions
: typeof data === 'number'
? data && !(camelName in unitlessProps) && !(name.charCodeAt(0) === 45)
? String(data) + 'px'
: String(data)
// replace tokens with stringified primitive values
: toTokenizedValue(
toSizingValue(camelName, data == null ? '' : data),
config.prefix,
config.themeMap[camelName]
)
)
isRuleLike
? data
: // replace all non-unitless props that are not custom properties with pixel versions
typeof data === 'number'
? data && !(camelName in unitlessProps) && !(name.charCodeAt(0) === 45)
? String(data) + 'px'
: String(data)
: // replace tokens with stringified primitive values
toTokenizedValue(
toSizingValue(camelName, data == null ? '' : data),
config.prefix,
config.themeMap[camelName],
)

currentRule[0].push(`${isAtRuleLike ? `${name} ` : `${toHyphenCase(name)}:`}${data}`)
}
Expand All @@ -145,9 +148,8 @@ export const toCssRules = (
walk(style, selectors, conditions)
}

const toCssString = (declarations, selectors, conditions) => (
const toCssString = (declarations, selectors, conditions) =>
`${conditions.map((condition) => `${condition}{`).join('')}${selectors.length ? `${selectors.join(',')}{` : ''}${declarations.join(';')}${selectors.length ? `}` : ''}${Array(conditions.length ? conditions.length + 1 : 0).join('}')}`
)

/** CSS Properties whose number values should be unitless. */
export const unitlessProps = {
Expand Down Expand Up @@ -197,5 +199,5 @@ export const unitlessProps = {
strokeDashoffset: 1,
strokeMiterlimit: 1,
strokeOpacity: 1,
strokeWidth: 1
strokeWidth: 1,
}
7 changes: 1 addition & 6 deletions packages/core/src/convert/toHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,4 @@ const toPhash = (/** @type {number} */ h, /** @type {string} */ x) => {
return h
}

export const toHash = (/** @type {object} */ value) => toAlphabeticName(
toPhash(
5381,
JSON.stringify(value)
) >>> 0
)
export const toHash = (/** @type {object} */ value) => toAlphabeticName(toPhash(5381, JSON.stringify(value)) >>> 0)
7 changes: 3 additions & 4 deletions packages/core/src/convert/toHyphenCase.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/** Returns the given value converted to kebab-case. */
export const toHyphenCase = (/** @type {string} */ value) => (
export const toHyphenCase = (/** @type {string} */ value) =>
// ignore kebab-like values
value.includes('-')
? value
// replace any upper-case letter with a dash and the lower-case variant
: value.replace(/[A-Z]/g, (capital) => '-' + capital.toLowerCase())
)
: // replace any upper-case letter with a dash and the lower-case variant
value.replace(/[A-Z]/g, (capital) => '-' + capital.toLowerCase())
9 changes: 8 additions & 1 deletion packages/core/src/convert/toPolyfilledValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ export const toPolyfilledValue = {
backgroundClip: (d) => ({ WebkitBackgroundClip: d, backgroundClip: d }),
boxDecorationBreak: (d) => ({ WebkitBoxDecorationBreak: d, boxDecorationBreak: d }),
clipPath: (d) => ({ WebkitClipPath: d, clipPath: d }),
content: (d) => ({ content: d.includes('"') || d.includes("'") || /^([A-Za-z]+\([^]*|[^]*-quote|inherit|initial|none|normal|revert|unset)$/.test(d) ? d : `"${d}"` }),
content: (d) => ({
content:
d.includes('"') ||
d.includes("'") ||
/^([A-Za-z]+\([^]*|[^]*-quote|inherit|initial|none|normal|revert|unset)$/.test(d)
? d
: `"${d}"`,
}),
hyphens: (d) => ({ WebkitHyphens: d, hyphens: d }),
maskImage: (d) => ({ WebkitMaskImage: d, maskImage: d }),
maskSize: (d) => ({ WebkitMaskSize: d, maskSize: d }),
Expand Down
80 changes: 41 additions & 39 deletions packages/core/src/convert/toResolvedMediaQueryRanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,47 @@ const mqunit = /([\d.]+)([^]*)/
/** Returns a media query with polyfilled ranges. */
export const toResolvedMediaQueryRanges = (
/** @type {string} */
media
) => media.replace(
/\(\s*([\w-]+)\s*(=|<|<=|>|>=)\s*([\w-]+)\s*(?:(<|<=|>|>=)\s*([\w-]+)\s*)?\)/g,
(
__,
/** @type {string} 1st param, either the name or value in the query. */
p1,
/** @type {string} 1st operator. */
o1,
/** @type {string} 2nd param, either the name or value in the query. */
p2,
/** @type {string} Optional 2nd operator. */
o2,
/** @type {string} Optional 3rd param, always a value in the query.*/
p3
) => {
/** Whether the first param is a value. */
const isP1Value = mqunit.test(p1)
media,
) =>
media.replace(
/\(\s*([\w-]+)\s*(=|<|<=|>|>=)\s*([\w-]+)\s*(?:(<|<=|>|>=)\s*([\w-]+)\s*)?\)/g,
(
__,
/** @type {string} 1st param, either the name or value in the query. */
p1,
/** @type {string} 1st operator. */
o1,
/** @type {string} 2nd param, either the name or value in the query. */
p2,
/** @type {string} Optional 2nd operator. */
o2,
/** @type {string} Optional 3rd param, always a value in the query.*/
p3,
) => {
/** Whether the first param is a value. */
const isP1Value = mqunit.test(p1)

/** Numeric shift applied to a value when an operator is `<` or `>`. */
const shift = 0.0625 * (isP1Value ? -1 : 1)
/** Numeric shift applied to a value when an operator is `<` or `>`. */
const shift = 0.0625 * (isP1Value ? -1 : 1)

const [name, value] = isP1Value ? [p2, p1] : [p1, p2]
const [name, value] = isP1Value ? [p2, p1] : [p1, p2]

return (
'(' +
(
o1[0] === '=' ? '' : (o1[0] === '>' === isP1Value ? 'max-' : 'min-')
) + name + ':' +
(o1[0] !== '=' && o1.length === 1 ? value.replace(mqunit, (_, v, u) => Number(v) + shift * (o1 === '>' ? 1 : -1) + u) : value) +
(
o2
? ') and (' + (
(o2[0] === '>' ? 'min-' : 'max-') + name + ':' +
(o2.length === 1 ? p3.replace(mqunit, (_, v, u) => Number(v) + shift * (o2 === '>' ? -1 : 1) + u) : p3)
)
: ''
) +
')'
)
}
)
return (
'(' +
(o1[0] === '=' ? '' : (o1[0] === '>') === isP1Value ? 'max-' : 'min-') +
name +
':' +
(o1[0] !== '=' && o1.length === 1
? value.replace(mqunit, (_, v, u) => Number(v) + shift * (o1 === '>' ? 1 : -1) + u)
: value) +
(o2
? ') and (' +
((o2[0] === '>' ? 'min-' : 'max-') +
name +
':' +
(o2.length === 1 ? p3.replace(mqunit, (_, v, u) => Number(v) + shift * (o2 === '>' ? -1 : 1) + u) : p3))
: '') +
')'
)
},
)
30 changes: 12 additions & 18 deletions packages/core/src/convert/toResolvedSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ export const toResolvedSelectors = (
parentSelectors,
/** @type {string[]} Nested selectors (e.g. `["&:hover", "&:focus"]`). */
nestedSelectors,
) => (
) =>
parentSelectors.length
? parentSelectors.reduce(
(resolvedSelectors, parentSelector) => {
? parentSelectors.reduce((resolvedSelectors, parentSelector) => {
resolvedSelectors.push(
...nestedSelectors.map(
(selector) => (
selector.includes('&') ? selector.replace(
/&/g,
/[ +>|~]/.test(parentSelector) && /&.*&/.test(selector)
? `:is(${parentSelector})`
: parentSelector
) : parentSelector + ' ' + selector
)
)
...nestedSelectors.map((selector) =>
selector.includes('&')
? selector.replace(
/&/g,
/[ +>|~]/.test(parentSelector) && /&.*&/.test(selector) ? `:is(${parentSelector})` : parentSelector,
)
: parentSelector + ' ' + selector,
),
)

return resolvedSelectors
},
[]
)
: nestedSelectors
)
}, [])
: nestedSelectors
20 changes: 9 additions & 11 deletions packages/core/src/convert/toSizingValue.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { toHyphenCase } from './toHyphenCase.js'

/** Returns a declaration sizing value with polyfilled sizing keywords. */
export const toSizingValue = (/** @type {string} */ declarationName, /** @type {string} */ declarationValue) => (
export const toSizingValue = (/** @type {string} */ declarationName, /** @type {string} */ declarationValue) =>
declarationName in sizeProps && typeof declarationValue === 'string'
? declarationValue.replace(
/^((?:[^]*[^\w-])?)(fit-content|stretch)((?:[^\w-][^]*)?)$/,
(data, lead, main, tail) => (
lead + (
main === 'stretch'
/^((?:[^]*[^\w-])?)(fit-content|stretch)((?:[^\w-][^]*)?)$/,
(data, lead, main, tail) =>
lead +
(main === 'stretch'
? `-moz-available${tail};${toHyphenCase(declarationName)}:${lead}-webkit-fill-available`
: `-moz-fit-content${tail};${toHyphenCase(declarationName)}:${lead}fit-content`
) + tail
),
)
: String(declarationValue)
)
: `-moz-fit-content${tail};${toHyphenCase(declarationName)}:${lead}fit-content`) +
tail,
)
: String(declarationValue)

/** CSS Properties whose value include a sizing keyword. */
const sizeProps = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/convert/toTailDashed.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/** Returns a filled value with a dash prefix. */
export const toTailDashed = (/** @type {string} */ value) => value ? value + '-' : ''
export const toTailDashed = (/** @type {string} */ value) => (value ? value + '-' : '')
44 changes: 14 additions & 30 deletions packages/core/src/convert/toTokenizedValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,17 @@ export const toTokenizedValue = (
prefix,
/** @type {string} */
scale,
) => value.replace(
/([+-])?((?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?)?(\$|--)([$\w-]+)/g,
($0, direction, multiplier, separator, token) => (
separator == "$" == !!multiplier
? $0
: (
direction || separator == '--'
? 'calc('
: ''
) + (
'var(--' + (
separator === '$'
? toTailDashed(prefix) + (
!token.includes('$')
? toTailDashed(scale)
: ''
) + token.replace(/\$/g, '-')
: token
) + ')' + (
direction || separator == '--'
? '*' + (
direction || ''
) + (
multiplier || '1'
) + ')'
: ''
)
)
),
)
) =>
value.replace(
/([+-])?((?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?)?(\$|--)([$\w-]+)/g,
($0, direction, multiplier, separator, token) =>
(separator == '$') == !!multiplier
? $0
: (direction || separator == '--' ? 'calc(' : '') +
('var(--' +
(separator === '$'
? toTailDashed(prefix) + (!token.includes('$') ? toTailDashed(scale) : '') + token.replace(/\$/g, '-')
: token) +
')' +
(direction || separator == '--' ? '*' + (direction || '') + (multiplier || '1') + ')' : '')),
)
14 changes: 6 additions & 8 deletions packages/core/src/createStitches.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const createStitches = (config) => {
const instance = createCssMap(config, (initConfig) => {
didRun = true

initConfig = typeof initConfig === 'object' && initConfig || {}
initConfig = (typeof initConfig === 'object' && initConfig) || {}

// internal configuration
const prefix = 'prefix' in initConfig ? String(initConfig.prefix) : ''
const media = typeof initConfig.media === 'object' && initConfig.media || {}
const media = (typeof initConfig.media === 'object' && initConfig.media) || {}
const root = typeof initConfig.root === 'object' ? initConfig.root || null : globalThis.document || null
const theme = typeof initConfig.theme === 'object' && initConfig.theme || {}
const themeMap = typeof initConfig.themeMap === 'object' && initConfig.themeMap || { ...defaultThemeMap }
const utils = typeof initConfig.utils === 'object' && initConfig.utils || {}
const theme = (typeof initConfig.theme === 'object' && initConfig.theme) || {}
const themeMap = (typeof initConfig.themeMap === 'object' && initConfig.themeMap) || { ...defaultThemeMap }
const utils = (typeof initConfig.utils === 'object' && initConfig.utils) || {}

/** External configuration. */
const config = {
Expand Down Expand Up @@ -57,9 +57,7 @@ export const createStitches = (config) => {
}

// initialize default theme
String(
returnValue.theme = returnValue.createTheme(theme)
)
String((returnValue.theme = returnValue.createTheme(theme)))

return returnValue
})
Expand Down
Loading

0 comments on commit 7dbb9e6

Please sign in to comment.