Skip to content

Commit

Permalink
feat: try to resolve array & object expression
Browse files Browse the repository at this point in the history
closes #22
  • Loading branch information
sxzz committed Mar 4, 2024
1 parent ebd99cf commit bddf800
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ export async function transformMacros({
if (!imports.has(id[0]) || scope.contains(id[0])) return
const args = node.arguments.map((arg) => {
if (isLiteralType(arg)) return resolveLiteral(arg)
throw new Error('Macro arguments must be literals.')
try {
if (isTypeOf(arg, ['ObjectExpression', 'ArrayExpression']))
return new Function(
`return (${source.slice(arg.start!, arg.end!)})`,
)()
} catch {}
throw new Error('Macro arguments cannot be resolved.')
})

macros.push({
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exports[`fixture > tests/fixtures/args.js 1`] = `
false === false;
null === null;
undefined === undefined;
console.log({"a":"b"});
console.log([1,false,"hello"]);
"
`;

Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ arg('1') === '1'
arg(false) === false
arg(null) === null
arg() === undefined
console.log(arg({ a: 'b' }))
console.log(arg([1, false, 'hello']))

0 comments on commit bddf800

Please sign in to comment.