本文整理汇总了TypeScript中babylon.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('returns events from a comment', () => {
const node = parse(`
class Foo {
/**
* This is an event
*
* @event event-name
* @param {Event} event
*/
myMethod() { }
/**
* @event descriptionless-event
*/
anotherMethod() { }
}`);
const events = [...getEventComments(node).values()];
const eventMatches = events.map((ev) => ({
description: ev.description,
name: ev.name,
params: ev.params,
warnings: ev.warnings
}));
assert.deepEqual(eventMatches, [
{
description: 'descriptionless-event',
name: 'descriptionless-event',
params: [],
warnings: []
},
{
description: 'This is an event',
name: 'event-name',
params: [{desc: '', name: 'event', type: 'Event'}],
warnings: []
}
]);
});
示例2: run
static run(content: string): StageResult {
let log = logger(this.name);
log(content);
let editor = new MagicString(content);
let ast = parse(content, {
sourceType: 'module',
plugins: BABYLON_PLUGINS,
allowReturnOutsideFunction: true,
tokens: true
} as any); // tslint:disable-line no-any
let config = buildConfig(content, ast);
asi(config);
config.insertions.forEach(({ index, content }: Insertion) => editor.appendLeft(index, content));
config.removals.forEach(({ start, end }: Removal) => editor.remove(start, end));
return {
code: editor.toString(),
suggestions: []
};
}
示例3: assert
/// <reference types="babel-types" />
/// <reference types="babel-types" />
// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babylon
import * as babylon from "babylon";
declare function assert(expr: boolean): void;
const code = `function square(n) {
return n * n;
}`;
let node = babylon.parse(code);
assert(node.type === "File");
assert(node.start === 0);
assert(node.end === 38);
assert(node.loc.start > node.loc.end);
babylon.parse(code, {
sourceType: "module", // default: "script"
plugins: ["jsx"] // default: []
});
示例4:
const parse = (source: string) =>
babylon.parse(source, {
sourceType: 'module',
plugins: ['jsx', 'typescript', 'classProperties', 'objectRestSpread']
});
示例5: parse
/// <reference types="babylon" />
// Example from https://github.com/babel/babel/tree/master/packages/babel-generator
import {parse} from 'babylon';
import generate from 'babel-generator';
const code = 'class Example {}';
const ast = parse(code);
ast.type;
ast.loc.start;
const output = generate(ast, { /* options */ }, code);
// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-generator
let result = generate(ast, {
retainLines: false,
compact: "auto",
concise: false,
quotes: "double",
// ...
}, code);
result.code;
result.map;
示例6: Identifier
}
}
};
const MyVisitor2: Visitor = {
Identifier(path) {
console.log("Visiting: " + path.node.name);
}
};
// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse
const code = `function square(n) {
return n * n;
}`;
const ast = babylon.parse(code);
traverse(ast, {
enter(path) {
const node = path.node;
if (t.isIdentifier(node) && node.name === "n") {
node.name = "x";
}
}
});
// Examples from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#writing-your-first-babel-plugin
const v1: Visitor = {
BinaryExpression(path) {
if (t.isIdentifier(path.node.left)) {
示例7: jsTransform
export function jsTransform(js: string, options: JsTransformOptions): string {
// Even with no transform plugins, parsing and serializing with Babel will
// make some minor formatting changes to the code. Skip Babel altogether
// if we have no meaningful changes to make.
let doBabelTransform = false;
// Note that Babel plugins run in this order:
// 1) plugins, first to last
// 2) presets, last to first
const plugins = [...babelSyntaxPlugins];
const presets = [];
if (options.externalHelpers) {
plugins.push(babelExternalHelpersPlugin);
}
if (options.minify) {
doBabelTransform = true;
// Minify last, so push first.
presets.push(babelPresetMinify);
}
if (options.compile === true || options.compile === 'es5') {
doBabelTransform = true;
plugins.push(...babelTransformEs2015);
plugins.push(...babelTransformEs2016);
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2015') {
doBabelTransform = true;
plugins.push(...babelTransformEs2016);
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2016') {
doBabelTransform = true;
plugins.push(...babelTransformEs2017);
plugins.push(...babelTransformEs2018);
} else if (options.compile === 'es2017') {
doBabelTransform = true;
plugins.push(...babelTransformEs2018);
}
if (options.moduleResolution === 'node') {
if (!options.filePath) {
throw new Error(
'Cannot perform node module resolution without filePath.');
}
doBabelTransform = true;
plugins.push(resolveBareSpecifiers(
options.filePath,
!!options.isComponentRequest,
options.packageName,
options.componentDir,
options.rootDir));
}
// When the AMD option is "auto", these options will change based on whether
// we have a module or not (unless they are already definitely true).
let transformModulesToAmd = options.transformModulesToAmd;
if (transformModulesToAmd === true) {
doBabelTransform = true;
}
const maybeDoBabelTransform =
doBabelTransform || transformModulesToAmd === 'auto';
if (maybeDoBabelTransform) {
let ast;
try {
ast = babylon.parse(js, {
// TODO(aomarks) Remove any when typings are updated for babylon 7.
// tslint:disable-next-line: no-any
sourceType: transformModulesToAmd === 'auto' ? 'unambiguous' as any :
'module',
plugins: [
'asyncGenerators',
'dynamicImport',
// tslint:disable-next-line: no-any
'importMeta' as any,
'objectRestSpread',
],
});
} catch (e) {
if (options.softSyntaxError && e.constructor.name === 'SyntaxError') {
console.error(
'ERROR [polymer-build]: failed to parse JavaScript' +
(options.filePath ? ` (${options.filePath}):` : ':'),
e);
return js;
} else {
throw e;
}
}
if (transformModulesToAmd === 'auto' &&
ast.program.sourceType === 'module') {
transformModulesToAmd = true;
}
if (transformModulesToAmd) {
doBabelTransform = true;
plugins.push(...babelTransformModulesAmd);
}
//.........这里部分代码省略.........
示例8: parseNumber
/**
* Parses JavaScript source representing a number.
*/
export default function parseNumber(string: string): number {
let expressionStatement = parse(`(${string})`).program.body[0] as ExpressionStatement;
let literal = expressionStatement.expression as NumericLiteral;
return literal.value;
}
示例9: parseRegExp
/**
* Parses JavaScript source representing a regular expression.
*/
export default function parseRegExp(string: string): { pattern: string, flags?: string } {
let expressionStatement = parse(`(${string})`).program.body[0] as ExpressionStatement;
let literal = expressionStatement.expression as RegExpLiteral;
return literal;
}