本文整理汇总了TypeScript中decaffeinate-parser.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: statement
function statement(code: string): Node {
let body = parse(code).body;
if (!body) {
throw new Error('Expected non-null body.');
}
return body.statements[0];
}
示例2: it
it('does simple counting within a program', () => {
let ast = parse(`
x = 1
for x in y
console.log x
`);
strictEqual(countVariableUsages(ast, 'x'), 3);
strictEqual(countVariableUsages(ast, 'y'), 1);
strictEqual(countVariableUsages(ast, 'z'), 0);
});
示例3: it
it('formats an AST for normal CoffeeScript code', () => {
let source = stripSharedIndent(`
loop
x = a()
break
`);
let formattedTokens = formatDecaffeinateParserAst(parse(source), new CodeContext(source));
strictEqual(
formattedTokens,
stripSharedIndent(`
Program [1:1(0)-3:8(22)] {
body: Block [1:1(0)-3:8(22)] {
inline: false
statements: [
Loop [1:1(0)-3:8(22)] {
body: Block [2:3(7)-3:8(22)] {
inline: false
statements: [
AssignOp [2:3(7)-2:10(14)] {
assignee: Identifier [2:3(7)-2:4(8)] {
data: "x"
}
expression: FunctionApplication [2:7(11)-2:10(14)] {
function: Identifier [2:7(11)-2:8(12)] {
data: "a"
}
arguments: []
}
}
Break [3:3(17)-3:8(22)] {
}
]
}
}
]
}
}
`) + '\n'
);
});
示例4: convertCustomStage
function convertCustomStage(source: string, stageName: string, useCS2: boolean): ConversionResult {
let context = new CodeContext(source);
if (stageName === 'coffeescript-lexer') {
let tokens = useCS2 ? getCoffee2Tokens(source) : getCoffee1Tokens(source);
return {
code: formatCoffeeScriptLexerTokens(tokens, context)
};
} else if (stageName === 'coffeescript-parser') {
let nodes = useCS2 ? getCoffee2Nodes(source) : getCoffee1Nodes(source);
return {
code: formatCoffeeScriptAst(nodes, context)
};
} else if (stageName === 'coffee-lex') {
return {
code: formatCoffeeLexTokens(lex(source, { useCS2 }), context)
};
} else if (stageName === 'decaffeinate-parser') {
return {
code: formatDecaffeinateParserAst(decaffeinateParse(source, { useCS2 }), context)
};
} else {
throw new Error(`Unrecognized stage name: ${stageName}`);
}
}
示例5: create
static create(source: string, useCS2: boolean): DecaffeinateContext {
let program = decaffeinateParse(source, { useCS2 });
return new DecaffeinateContext(
program,
source,
program.context.sourceTokens,
program.context.ast,
program.context.linesAndColumns,
computeParentMap(program),
computeScopeMap(program)
);
}