Skip to content

Commit aac25d6

Browse files
committed
Fix bug where config utils was not passed through to nested closures
1 parent 9389661 commit aac25d6

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

__tests__/resolveConfig.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,75 @@ test('theme values in the extend section are lazily evaluated', () => {
899899
})
900900
})
901901

902+
test('lazily evaluated values have access to the config utils', () => {
903+
const userConfig = {
904+
theme: {
905+
shift: (theme, { negative }) => ({
906+
...theme('spacing'),
907+
...negative(theme('spacing')),
908+
}),
909+
extend: {
910+
nudge: (theme, { negative }) => ({
911+
...theme('spacing'),
912+
...negative(theme('spacing')),
913+
}),
914+
},
915+
},
916+
}
917+
918+
const defaultConfig = {
919+
prefix: '-',
920+
important: false,
921+
separator: ':',
922+
theme: {
923+
spacing: {
924+
'1': '1px',
925+
'2': '2px',
926+
'3': '3px',
927+
'4': '4px',
928+
},
929+
},
930+
variants: {},
931+
}
932+
933+
const result = resolveConfig([userConfig, defaultConfig])
934+
935+
expect(result).toEqual({
936+
prefix: '-',
937+
important: false,
938+
separator: ':',
939+
theme: {
940+
spacing: {
941+
'1': '1px',
942+
'2': '2px',
943+
'3': '3px',
944+
'4': '4px',
945+
},
946+
shift: {
947+
'-1': '-1px',
948+
'-2': '-2px',
949+
'-3': '-3px',
950+
'-4': '-4px',
951+
'1': '1px',
952+
'2': '2px',
953+
'3': '3px',
954+
'4': '4px',
955+
},
956+
nudge: {
957+
'-1': '-1px',
958+
'-2': '-2px',
959+
'-3': '-3px',
960+
'-4': '-4px',
961+
'1': '1px',
962+
'2': '2px',
963+
'3': '3px',
964+
'4': '4px',
965+
},
966+
},
967+
variants: {},
968+
})
969+
})
970+
902971
test('the original theme is not mutated', () => {
903972
const userConfig = {
904973
theme: {

src/util/resolveConfig.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import defaults from 'lodash/defaults'
44
import map from 'lodash/map'
55
import get from 'lodash/get'
66

7-
const utils = {
7+
const configUtils = {
88
negative(scale) {
99
return Object.keys(scale)
1010
.filter(key => scale[key] !== '0')
@@ -31,23 +31,23 @@ function mergeExtensions({ extend, ...theme }) {
3131
}
3232
}
3333

34-
return resolveThemePath => ({
35-
...value(themeValue, resolveThemePath),
36-
...value(extensions, resolveThemePath),
34+
return (resolveThemePath, utils) => ({
35+
...value(themeValue, resolveThemePath, utils),
36+
...value(extensions, resolveThemePath, utils),
3737
})
3838
})
3939
}
4040

4141
function resolveFunctionKeys(object) {
42-
const resolveObjectPath = (key, defaultValue) => {
42+
const resolveThemePath = (key, defaultValue) => {
4343
const val = get(object, key, defaultValue)
44-
return isFunction(val) ? val(resolveObjectPath) : val
44+
return isFunction(val) ? val(resolveThemePath) : val
4545
}
4646

4747
return Object.keys(object).reduce((resolved, key) => {
4848
return {
4949
...resolved,
50-
[key]: isFunction(object[key]) ? object[key](resolveObjectPath, utils) : object[key],
50+
[key]: isFunction(object[key]) ? object[key](resolveThemePath, configUtils) : object[key],
5151
}
5252
}, {})
5353
}

0 commit comments

Comments
 (0)