tl;dr
当我在单独的文件中有宏时出现错误,但如果宏在同一个文件中,它似乎可以工作。
test-macro-1.js:
macro genVar {
case {$name ($varName, $varVal, $name2)} => {
letstx $ident = [makeIdent(unwrapSyntax(#{$varName}), #{$name2})];
return #{var $ident = $varVal}
}
}
export genVar;
test-macro-2.js:
macro someVars {
case {$name ()} => {
return #{
genVar('foo', 'hello world', $name);
genVar('bar', 'goodbye cruel world', $name)
}
}
}
export someVars;
test.js:
someVars();
console.log('foo=%s', foo);
console.log('bar=%s', bar);
results:
$ sjs -r --module ./test-macro-1.js --module ./test-macro-2.js test.js
/usr/local/lib/node_modules/sweet.js/lib/sweet.js:90
throw new SyntaxError(syn.printSyntaxError(source$2, err))
^
SyntaxError: [macro] Macro `someVars` could not be matched with `...`
1: someVars();
^
at expand$2 (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:90:27)
at parse (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:123:29)
at Object.compile (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:129:19)
at Object.exports.run (/usr/local/lib/node_modules/sweet.js/lib/sjs.js:85:27)
at Object.<anonymous> (/usr/local/lib/node_modules/sweet.js/bin/sjs:7:23)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
但是...如果我将宏放在同一个文件中,如下所示:
test-macro-1.js:
macro genVar {
case {$name ($varName, $varVal, $name2)} => {
letstx $ident = [makeIdent(unwrapSyntax(#{$varName}), #{$name2})];
return #{var $ident = $varVal}
}
}
export genVar;
macro someVars {
case {$name ()} => {
return #{
genVar('foo', 'hello world', $name);
genVar('bar', 'goodbye cruel world', $name)
}
}
}
export someVars;
results:
$ sjs -r --module ./test-macro-1.js test.js
var foo = 'hello world';
var bar = 'goodbye cruel world';
console.log('foo=%s', foo);
console.log('bar=%s', bar);
那么有一些经验的人对将宏安排到文件中以实现模块化和功能的正确方法有任何指导吗?