Skip to content

Instantly share code, notes, and snippets.

@Haroenv
Created September 27, 2018 13:29
Show Gist options
  • Save Haroenv/c896d424f5d842f641d2be53ba42ac86 to your computer and use it in GitHub Desktop.
Save Haroenv/c896d424f5d842f641d2be53ba42ac86 to your computer and use it in GitHub Desktop.
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