本文整理匯總了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; } });