将任意字符串插入 sweet.js 输出
Inserting arbitrary strings into sweet.js output
如何在 sweet.js 生成的输出中插入任意字符串?
这对于以编程方式执行字符串根据各种条件不同的操作非常有用。
例如,在下面代码的第 25 行,我想插入一个字符串作为结果。
sweet.js代码:
import { produceNormalParams } from './abc/produceNormalParams'
import { produceParamChecks } from './abc/produceParamChecks'
import { produceInnerChecks } from './abc/produceInnerChecks'
syntax function = function(ctx) {
let funcName = ctx.next().value;
let funcParams = ctx.next().value;
let funcBody = ctx.next().value;
//produce the normal params array
var normalParams = produceNormalParams(funcParams)
//produce the checks
var paramChecks = produceParamChecks(funcParams)
//produce the original funcBody code
var inner = produceInnerChecks(funcParams)
var someArbitraryString = "console.log('hey')"
//put them together as the final result
var result = #`function ${funcName} ${normalParams} {
${someArbitraryString}
${paramChecks}
${inner}
}`
return result
}
示例输入:
module.exports = multiply
function multiply(a:array,b,c:array) {
return a * c
}
示例输出:
// Example Output
module.exports = multiply;
function multiply(a_31, b_32, c_33) {
console.log('hey')
if (Object.prototype.toString.call(a_31) !== "[object Array]") throw new Error("Must be array:" + a_31);
if (Object.prototype.toString.call(c_33) !== "[object Array]") throw new Error("Must be array:" + c_33);
return a_31 * c_33;
}
虽然不能将任意字符串插入语法模板,但可以插入其他语法模板。
import { produceNormalParams } from './abc/produceNormalParams' for syntax;
import { produceParamChecks } from './abc/produceParamChecks' for syntax;
import { produceInnerChecks } from './abc/produceInnerChecks' for syntax;
syntax function = function(ctx) {
let funcName = ctx.next().value;
let funcParams = ctx.next().value;
let funcBody = ctx.next().value;
//produce the normal params array
var normalParams = produceNormalParams(funcParams);
//produce the checks
var paramChecks = produceParamChecks(funcParams);
//produce the original funcBody code
var inner = produceInnerChecks(funcParams);
var someArbitrarySyntax = #`console.log('hey')`;
//put them together as the final result
var result = #`function ${funcName} ${normalParams} {
${someArbitrarySyntax}
${paramChecks}
${inner}
}`;
return result
}
注意导入语句后面的 for syntax
。这些是导入在编译时可用所必需的。
如何在 sweet.js 生成的输出中插入任意字符串?
这对于以编程方式执行字符串根据各种条件不同的操作非常有用。
例如,在下面代码的第 25 行,我想插入一个字符串作为结果。
sweet.js代码:
import { produceNormalParams } from './abc/produceNormalParams'
import { produceParamChecks } from './abc/produceParamChecks'
import { produceInnerChecks } from './abc/produceInnerChecks'
syntax function = function(ctx) {
let funcName = ctx.next().value;
let funcParams = ctx.next().value;
let funcBody = ctx.next().value;
//produce the normal params array
var normalParams = produceNormalParams(funcParams)
//produce the checks
var paramChecks = produceParamChecks(funcParams)
//produce the original funcBody code
var inner = produceInnerChecks(funcParams)
var someArbitraryString = "console.log('hey')"
//put them together as the final result
var result = #`function ${funcName} ${normalParams} {
${someArbitraryString}
${paramChecks}
${inner}
}`
return result
}
示例输入:
module.exports = multiply
function multiply(a:array,b,c:array) {
return a * c
}
示例输出:
// Example Output
module.exports = multiply;
function multiply(a_31, b_32, c_33) {
console.log('hey')
if (Object.prototype.toString.call(a_31) !== "[object Array]") throw new Error("Must be array:" + a_31);
if (Object.prototype.toString.call(c_33) !== "[object Array]") throw new Error("Must be array:" + c_33);
return a_31 * c_33;
}
虽然不能将任意字符串插入语法模板,但可以插入其他语法模板。
import { produceNormalParams } from './abc/produceNormalParams' for syntax;
import { produceParamChecks } from './abc/produceParamChecks' for syntax;
import { produceInnerChecks } from './abc/produceInnerChecks' for syntax;
syntax function = function(ctx) {
let funcName = ctx.next().value;
let funcParams = ctx.next().value;
let funcBody = ctx.next().value;
//produce the normal params array
var normalParams = produceNormalParams(funcParams);
//produce the checks
var paramChecks = produceParamChecks(funcParams);
//produce the original funcBody code
var inner = produceInnerChecks(funcParams);
var someArbitrarySyntax = #`console.log('hey')`;
//put them together as the final result
var result = #`function ${funcName} ${normalParams} {
${someArbitrarySyntax}
${paramChecks}
${inner}
}`;
return result
}
注意导入语句后面的 for syntax
。这些是导入在编译时可用所必需的。