本文整理汇总了TypeScript中@babel/parser.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: rewriteBareModuleSpecifiers
export function rewriteBareModuleSpecifiers(
code: string, packageVersions: PackageVersionMap, rootPackage: string):
string {
const jsAST = babelParser.parse(
code, {sourceType: 'module', plugins: ['dynamicImport']});
for (const node of jsAST.program.body) {
if ((node.type === 'ImportDeclaration' ||
node.type === 'ExportNamedDeclaration' ||
node.type === 'ExportAllDeclaration') &&
node.source) {
if (isBareModuleSpecifier(node.source.value)) {
const parsedPackage = parsePackageName(node.source.value);
const version = packageVersions[parsedPackage.package];
const versionString = version ? '@' + version : '';
const queryString = rootPackage ? '?' + rootPackage : '';
node.source.value = `/${parsedPackage.package}${versionString}${
parsedPackage.path}${queryString}`;
} else {
// Append rootPackage to relative URLs.
const parsedUrl = url.parse(node.source.value);
if (!parsedUrl.protocol) {
parsedUrl.search = rootPackage || '';
node.source.value = url.format(parsedUrl);
}
}
}
}
const outputJs = babelGenerate(jsAST, {retainLines: true}, code);
return outputJs.code;
}
示例2: function
exit: function(path) {
const babelPluginOptions = plugin["babelPluginOptions"];
let usableHelperCode;
if (babelPluginOptions) {
const { accessToken, backendPort } = babelPluginOptions;
usableHelperCode = helperCode;
usableHelperCode = usableHelperCode.replace(
/ACCESS_TOKEN_PLACEHOLDER/g,
accessToken
);
usableHelperCode = usableHelperCode.replace(
/BACKEND_PORT_PLACEHOLDER/g,
backendPort
);
} else {
usableHelperCode = helperCode;
}
// console.log({ enter, enterNotIgnored });
var initCodeAstNodes = babylon
.parse(usableHelperCode)
.program.body.reverse();
initCodeAstNodes.forEach(node => {
path.node.body.unshift(node);
});
}
示例3: tryParse
function tryParse(code: any, fullPath: any) {
var parseOptions = getOptions(fullPath);
try {
return babelParse(code, parseOptions).program;
} catch (error) {
// If parsing fails, check options.json to see if the failure was
// expected.
try {
var options = JSON.parse(fs.readFileSync(
path.join(path.dirname(fullPath), "options.json")).toString());
} catch (optionsError) {
console.error(optionsError.message);
}
if (options &&
options.throws === error.message) {
return null;
}
throw error;
}
}
示例4: check
function check(source: string, expected: Array<Insertion>) {
const ast = parse(source, { sourceType: 'module', tokens: true });
const { insertions } = process(source, ast);
expect(expected).toEqual(insertions);
}
示例5: 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 = 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)) {
示例6: assert
import { parse, parseExpression } from "@babel/parser";
declare function assert(expr: boolean): void;
const code = `function square(n) {
return n * n;
}`;
const node = parse(code);
assert(node.type === "File");
assert(node.start === 0);
assert(node.end === 38);
assert(!!node.loc && node.loc.start > node.loc.end);
parse(code, {
sourceType: "module", // default: "script"
plugins: ["jsx"] // default: []
});
示例7: function
fs.readFile(fullPath, "utf8", function (error, code) {
if (error) {
throw error;
}
var program = babelParse(code, {
sourceType: "module",
plugins: [
"typescript",
"objectRestSpread",
"classProperties",
"optionalCatchBinding",
"numericSeparator",
]
}).program;
tsTypes.namedTypes.Program.assert(program, true);
done();
});
示例8: describe
describe('scope', () => {
const scope = [
"type Foo = {}",
"interface Bar {}"
];
const ast = babelParse(scope.join("\n"), {
plugins: ['typescript']
});
it("should register typescript types with the scope", function() {
visit(ast, {
visitProgram(path) {
assert(path.scope.declaresType('Foo'));
assert(path.scope.declaresType('Bar'));
assert.equal(path.scope.lookupType('Foo').getTypes()['Foo'][0].parent.node.type, 'TSTypeAliasDeclaration');
assert.equal(path.scope.lookupType('Bar').getTypes()['Bar'][0].parent.node.type, 'TSInterfaceDeclaration');
return false;
}
});
});
});