-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c0659f
commit debfab3
Showing
6 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import dlv from 'dlv' | ||
import { replaceWithLocation, astify } from './../macroHelpers' | ||
import { getTheme, assert } from './../utils' | ||
import { | ||
logGeneralError, | ||
themeErrorNotString, | ||
themeErrorNotFound, | ||
} from './../logging' | ||
|
||
const getFunctionValue = path => { | ||
if (path.parent.type !== 'CallExpression') return | ||
|
||
const parent = path.findParent(x => x.isCallExpression()) | ||
if (!parent) return | ||
|
||
const argument = parent.get('arguments')[0] || '' | ||
return { parent, input: argument.evaluate().value } | ||
} | ||
|
||
const getTaggedTemplateValue = path => { | ||
if (path.parent.type !== 'TaggedTemplateExpression') return | ||
|
||
const parent = path.findParent(x => x.isTaggedTemplateExpression()) | ||
if (!parent) return | ||
if (parent.node.tag.type !== 'Identifier') return | ||
|
||
return { parent, input: parent.get('quasi').evaluate().value } | ||
} | ||
|
||
const normalizeThemeValue = foundValue => | ||
Array.isArray(foundValue) | ||
? foundValue.join(', ') | ||
: typeof foundValue === 'string' | ||
? foundValue.trim() | ||
: foundValue | ||
|
||
const trimInput = themeValue => { | ||
const arrayValues = themeValue.split('.').filter(Boolean) | ||
if (arrayValues.length === 1) { | ||
return arrayValues[0] | ||
} | ||
|
||
return arrayValues.slice(0, -1).join('.') | ||
} | ||
|
||
const handleThemeFunction = ({ references, t, state }) => { | ||
if (!references.theme) return | ||
|
||
const theme = getTheme(state.config.theme) | ||
|
||
references.theme.forEach(path => { | ||
const { input, parent } = | ||
getTaggedTemplateValue(path) || getFunctionValue(path) | ||
|
||
if (input === '') { | ||
return replaceWithLocation(parent, astify('', t)) | ||
} | ||
|
||
assert(!parent || !input, () => | ||
logGeneralError( | ||
"The theme value doesn’t look right\n\nTry using it like this: theme`colors.black` or theme('colors.black')" | ||
) | ||
) | ||
|
||
const themeValue = dlv(theme(), input) | ||
assert(!themeValue, () => | ||
themeErrorNotFound({ | ||
theme: input.includes('.') ? dlv(theme(), trimInput(input)) : theme(), | ||
input, | ||
trimInput: trimInput(input), | ||
}) | ||
) | ||
|
||
const normalizedValue = normalizeThemeValue(themeValue) | ||
assert(typeof normalizedValue !== 'string', () => | ||
themeErrorNotString({ themeValue, input }) | ||
) | ||
|
||
return replaceWithLocation(parent, astify(normalizedValue, t)) | ||
}) | ||
} | ||
|
||
export { handleThemeFunction } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters