Created
September 27, 2018 13:29
-
-
Save Haroenv/c896d424f5d842f641d2be53ba42ac86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function jsonToBabel(value, t) { | |
if (Array.isArray(value)) { | |
return t.arrayExpression(value.map(_value => jsonToBabel(_value, t))); | |
} | |
if (value === null) { | |
return t.identifier('null'); | |
} | |
switch (typeof value) { | |
case 'object': { | |
return t.objectExpression( | |
Object.entries(value).map(([key, _value]) => | |
t.objectProperty(t.identifier(key), jsonToBabel(_value, t)) | |
) | |
); | |
} | |
case 'undefined': { | |
return t.identifier('undefined'); | |
} | |
case 'string': { | |
return t.stringLiteral(value); | |
} | |
case 'number': { | |
return t.numberLiteral(value); | |
} | |
case 'boolean': { | |
return t.booleanLiteral(value); | |
} | |
default: { | |
throw new MacroError( | |
`Invalid type for ${JSON.stringify(value)} in package.json` | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment