本文整理汇总了TypeScript中astring.generate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript generate函数的具体用法?TypeScript generate怎么用?TypeScript generate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了generate函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: elaborate
elaborate() {
return stripIndent`
This "if" block requires corresponding "else" block which will be
evaluated when ${generate(this.node.test)} expression evaluates to false.
Later in the course we will lift this restriction and allow "if" without
else.
`
}
示例2: elaborate
elaborate() {
let ifOrElse
let header
let body
if (this.type === 'consequent') {
ifOrElse = 'if'
header = `if (${generate(this.node.test)})`
body = this.node.consequent
} else {
ifOrElse = header = 'else'
body = this.node.alternate
}
return stripIndent`
${ifOrElse} block need to be enclosed with a pair of curly braces.
${header} {
${generate(body)}
}
An exception is when you have an "if" followed by "else if", in this case
"else if" block does not need to be surrounded by curly braces.
if (someCondition) {
// ...
} else /* notice missing { here */ if (someCondition) {
// ...
} else {
// ...
}
Rationale: Readability in dense packed code.
In the snippet below, for instance, with poor indentation it is easy to
mistaken hello() and world() to belong to the same branch of logic.
if (someCondition) {
2;
} else
hello();
world();
`
}
示例3: astToHDWalletProvider
function astToHDWalletProvider(node: ESTree.NewExpression): IProvider {
const provider: IProvider = {
raw: generate(node),
};
const mnemonicNode = node.arguments[0];
if (mnemonicNode && mnemonicNode.type === 'Literal') {
provider.mnemonic = '' + mnemonicNode.value;
}
const urlNode = node.arguments[1];
if (urlNode && urlNode.type === 'Literal') {
provider.url = '' + urlNode.value;
}
if (urlNode && urlNode.type !== 'Literal') {
provider.url = generate(urlNode);
}
return provider;
}
示例4: while
tests.forEach(t => {
const generator = evaluators[t.statement.type](t.statement, state)
let isDone = false
while (!isDone) {
const g = generator.next()
isDone = g.done
state = g.value
}
if (state.value !== t.expectedValue) {
// tslint:disable-next-line
console.log(`L${t.statement.loc!.start.line}: ${generate(t.statement)}`)
}
expect(state.value).toBe(t.expectedValue)
})
示例5: createInterpreter
export const testVisualizer = (program: string, expectedOutput: string[]) => {
let state = createInterpreter()
let visualizer = create()
const node = parse(program, 3).parser.program!
const evaluator = evalProgram(node, state)
const prints = []
let gen = evaluator.next()
while (gen) {
state = gen.value
if (!state) {
break
}
const prev = visualizer
visualizer = next(visualizer, state)
if (visualizer.root && visualizer.id !== prev.id) {
prints.push(generate(visualizer.root))
}
gen = evaluator.next()
}
expect(prints).toEqual(expectedOutput)
}
示例6: generate
const fixs = this.fixs.map(n => '\t' + generate(n)).join('\n')
示例7: writeAST
public async writeAST(): Promise<void> {
return fs.writeFile(this.filePath, generate(this.ast as ESTree.Node, {comments: true}));
}
示例8: generate
import { baseGenerator, generate } from "astring";
import { FunctionExpression, MemberExpression, Program } from "estree";
import { Stream } from "stream";
const ast: Program = null;
const functionE: FunctionExpression = null;
const memberE: MemberExpression = null;
// should accept different nodes
generate(ast);
generate(functionE);
generate(memberE);
// options without output option should generate string
const string: string = generate(ast, {
comments: true,
generator: baseGenerator,
indent: "\t",
lineEnd: "\n",
startingIndentLevel: 42,
sourceMap: null
});
// options with output option should return Stream
const stream: Stream = generate(ast, {
output: new Stream()
});
// Generator should map node types to functions whose first parameter is same node type
baseGenerator.Program(ast, { write(s: string) { return; } });
baseGenerator.FunctionExpression(functionE, { write(s: string) { return; } });