本文整理汇总了TypeScript中babel-traverse.default函数的典型用法代码示例。如果您正苦于以下问题:TypeScript default函数的具体用法?TypeScript default怎么用?TypeScript default使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: rewriteExportAllToNamedExports
rewriteExportAllToNamedExports(node: babel.Node, analysis: Analysis) {
traverse(node, {
noScope: true,
ExportAllDeclaration: {
enter(path: NodePath<babel.ExportAllDeclaration>) {
const exportAllDeclaration = path.node;
const sourceUrl =
babel.isStringLiteral(exportAllDeclaration.source) &&
exportAllDeclaration.source.value;
if (!sourceUrl) {
return;
}
const sourceDocument = getAnalysisDocument(analysis, sourceUrl);
const documentExports = sourceDocument.getFeatures({kind: 'export'});
const specifiers: babel.ExportSpecifier[] = [];
for (const documentExport of documentExports) {
for (const exportIdentifier of documentExport.identifiers) {
const identifierValue = exportIdentifier.valueOf();
// It does not appear that `export * from` should re-export
// the default module export of a module.
if (identifierValue !== 'default') {
specifiers.push(babel.exportSpecifier(
babel.identifier(identifierValue),
babel.identifier(identifierValue)));
}
}
}
const namedExportDeclaration = babel.exportNamedDeclaration(
undefined, specifiers, babel.stringLiteral(sourceUrl));
rewriteObject(exportAllDeclaration, namedExportDeclaration);
}
}
});
}
示例2: _rewriteImportStatements
/**
* Rewrite import declarations source URLs reference the bundle URL for
* bundled files and import names to correspond to names as exported by
* bundles.
*/
private _rewriteImportStatements(baseUrl: ResolvedUrl, node: babel.Node) {
const this_ = this;
traverse(node, {
noScope: true,
// Dynamic import() syntax doesn't have full type support yet, so we
// have to use generic `enter` and walk all nodes until that's fixed.
// TODO(usergenic): Switch this to the `Import: { enter }` style
// after dynamic imports fully supported.
enter(path: NodePath) {
if (path.node.type === 'Import') {
this_._rewriteDynamicImport(baseUrl, node, path);
}
},
});
traverse(node, {
noScope: true,
ImportDeclaration: {
enter(path: NodePath) {
const importDeclaration = path.node as babel.ImportDeclaration;
if (!babel.isStringLiteral(importDeclaration.source)) {
// We can't actually handle values which are not string literals, so
// we'll skip them.
return;
}
const source = importDeclaration.source.value as ResolvedUrl;
const sourceBundle = this_.manifest.getBundleForFile(source);
// If there is no import bundle, then this URL is not bundled (maybe
// excluded or something) so we should just ensure the URL is
// converted back to a relative URL.
if (!sourceBundle) {
importDeclaration.source.value =
this_.bundler.analyzer.urlResolver.relative(baseUrl, source);
return;
}
for (const specifier of importDeclaration.specifiers) {
if (babel.isImportSpecifier(specifier)) {
this_._rewriteImportSpecifierName(
specifier, source, sourceBundle);
}
if (babel.isImportDefaultSpecifier(specifier)) {
this_._rewriteImportDefaultSpecifier(
specifier, source, sourceBundle);
}
if (babel.isImportNamespaceSpecifier(specifier)) {
this_._rewriteImportNamespaceSpecifier(
specifier, source, sourceBundle);
}
}
importDeclaration.source.value =
ensureLeadingDot(this_.bundler.analyzer.urlResolver.relative(
baseUrl, sourceBundle.url));
}
}
});
}
示例3: isFileToBeTaroComponent
export function isFileToBeTaroComponent (
code: string,
sourcePath: string,
outputPath: string
) {
const {
buildAdapter,
sourceDir,
constantsReplaceList,
jsxAttributeNameReplace
} = getBuildData()
const transformResult: IWxTransformResult = wxTransformer({
code,
sourcePath: sourcePath,
sourceDir,
outputPath: outputPath,
isNormal: true,
isTyped: REG_TYPESCRIPT.test(sourcePath),
adapter: buildAdapter,
env: constantsReplaceList,
jsxAttributeNameReplace
})
const { ast }: IWxTransformResult = transformResult
let isTaroComponent = false
traverse(ast, {
ClassDeclaration (astPath) {
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
},
ClassExpression (astPath) {
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
}
})
return {
isTaroComponent,
transformResult
}
}
示例4: _deduplicateImportStatements
/**
* Attempts to reduce the number of distinct import declarations by combining
* those referencing the same source into the same declaration. Results in
* deduplication of imports of the same item as well.
*
* Before:
* import {a} from './module-1.js';
* import {b} from './module-1.js';
* import {c} from './module-2.js';
* After:
* import {a,b} from './module-1.js';
* import {c} from './module-2.js';
*/
private _deduplicateImportStatements(node: babel.Node) {
const importDeclarations = new Map<string, babel.ImportDeclaration>();
traverse(node, {
noScope: true,
ImportDeclaration: {
enter(path: NodePath) {
const importDeclaration = path.node;
if (!babel.isImportDeclaration(importDeclaration)) {
return;
}
const source = babel.isStringLiteral(importDeclaration.source) &&
importDeclaration.source.value;
if (!source) {
return;
}
const hasNamespaceSpecifier = importDeclaration.specifiers.some(
(s) => babel.isImportNamespaceSpecifier(s));
const hasDefaultSpecifier = importDeclaration.specifiers.some(
(s) => babel.isImportDefaultSpecifier(s));
if (!importDeclarations.has(source) && !hasNamespaceSpecifier &&
!hasDefaultSpecifier) {
importDeclarations.set(source, importDeclaration);
} else if (importDeclarations.has(source)) {
const existingDeclaration = importDeclarations.get(source)!;
for (const specifier of importDeclaration.specifiers) {
existingDeclaration.specifiers.push(specifier);
}
path.remove();
}
}
}
});
}
示例5: _rewriteExportStatements
/**
* Rewrite export declarations source URLs to reference the bundle URL for
* bundled files.
*/
private _rewriteExportStatements(baseUrl: ResolvedUrl, node: babel.Node) {
const this_ = this;
traverse(node, {
noScope: true,
ExportNamedDeclaration: {
enter(path: NodePath<babel.ExportNamedDeclaration>) {
const exportNamedDeclaration = path.node;
if (!exportNamedDeclaration.source ||
!babel.isStringLiteral(exportNamedDeclaration.source)) {
// We can't rewrite a source if there isn't one or if it isn't a
// string literal in the first place.
return;
}
const source = exportNamedDeclaration.source.value as ResolvedUrl;
const sourceBundle = this_.manifest.getBundleForFile(source);
// If there is no bundle associated with the export from statement
// then the URL is not bundled (perhaps it was excluded) so we will
// just ensure the URL is converted back to a relative URL.
if (!sourceBundle) {
exportNamedDeclaration.source.value = ensureLeadingDot(
this_.bundler.analyzer.urlResolver.relative(baseUrl, source));
return;
}
exportNamedDeclaration.source.value =
ensureLeadingDot(this_.bundler.analyzer.urlResolver.relative(
baseUrl, sourceBundle.url));
}
}
});
}
示例6: evalClass
export function evalClass (ast: t.File, props = '', isRequire = false) {
let mainClass!: t.ClassDeclaration
const statements = new Set<t.ExpressionStatement>([
template('Current.inst = this;')() as any
])
traverse(ast, {
ClassDeclaration (path) {
mainClass = path.node
},
/**
* 目前 node 的版本支持不了 class-properties
* 但 babel 又有 bug,某些情况竟然把转换后的 class-properties 编译到 super 之前
* 不然用 babel.transformFromAst 就完事了
* 现在只能自己实现这个 feature 的部分功能了,真 tm 麻烦
* @TODO 有空再给他们提 PR 吧
*/
ClassProperty (path) {
const { key, value } = path.node
statements.add(t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.thisExpression(),
key
),
value
)))
path.remove()
}
})
for (const method of mainClass.body.body) {
// constructor 即便没有被定义也会被加上
if (t.isClassMethod(method) && method.kind === 'constructor') {
const index = method.body.body.findIndex(node => t.isSuper(node))
method.body.body.push(
t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.thisExpression(),
t.identifier('state')
),
t.callExpression(t.memberExpression(t.thisExpression(), t.identifier('_createData')), [])
))
)
method.body.body.splice(index, 0, ...statements)
}
}
let code = `function f() {};` +
generate(t.classDeclaration(t.identifier('Test'), t.identifier('f'), mainClass.body, [])).code +
';' + `var classInst = new Test(${props});classInst`
code = internalFunction + code
// tslint:disable-next-line
return eval(code)
}
示例7: parseWXML
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
refIds: Set<string>
} {
if (!parseImport) {
errors.length = 0
usedComponents.clear()
}
usedComponents.add('Block')
let wxses: WXS[] = []
let imports: Imports[] = []
const refIds = new Set<string>()
const loopIds = new Set<string>()
if (!wxml) {
return {
wxses,
imports,
refIds,
wxml: t.nullLiteral()
}
}
const nodes = removEmptyTextAndComment(parse(wxml.trim()))
const ast = t.file(
t.program(
[
t.expressionStatement(parseNode(
buildElement('block', nodes as Node[])
) as t.Expression)
],
[]
)
)
traverse(ast, createWxmlVistor(loopIds, refIds, dirPath, wxses, imports))
refIds.forEach(id => {
if (loopIds.has(id) || imports.filter(i => i.wxs).map(i => i.name).includes(id)) {
refIds.delete(id)
}
})
return {
wxses,
imports,
wxml: hydrate(ast),
refIds
}
}
示例8: traverse
const pickTheReturnOfWebpackResult = (ast: Node): Node => {
let expr = null;
traverse(ast, {
AssignmentExpression(nodePath) {
if (
nodePath.parentPath.parentPath.isProgram() &&
nodePath.get("left").get("object").equals("name", "module") &&
nodePath.get("left").get("property").equals("name", "exports")
) {
expr = nodePath.node.right;
}
},
});
return expr;
};
示例9: moduleResolve
captureImports(ast) {
let imports = [];
let exports = [];
let reexports = [];
let exportAllSources = [];
traverse(ast, {
enter(path) {
if (path.isImportDeclaration()) {
imports.push(path.node);
}
}
});
return imports.map((imp) => {
return moduleResolve(imp.source.value, this.id);
});
}
示例10: enter
rewriteEs6SourceUrlsToResolved(
node: babel.Node, jsImportResolvedUrls: Map<string, ResolvedUrl>) {
const rewriteDeclarationSource = {
enter(
path:
NodePath<babel.ExportAllDeclaration|
babel.ExportNamedDeclaration|babel.ImportDeclaration>) {
const declaration = path.node;
const source = declaration.source &&
babel.isStringLiteral(declaration.source) &&
declaration.source.value;
if (!source) {
return;
}
const resolution = jsImportResolvedUrls.get(source);
if (resolution) {
declaration.source!.value = resolution;
}
}
};
traverse(node, {
noScope: true,
ExportAllDeclaration: rewriteDeclarationSource,
ExportNamedDeclaration: rewriteDeclarationSource,
ImportDeclaration: rewriteDeclarationSource,
CallExpression: {
enter(path: NodePath<babel.Node>) {
const callExpression = path.node;
const callee = callExpression['callee']!;
const callArguments = callExpression['arguments']!;
if (!callee || !callArguments || callArguments.length < 1 ||
!babel.isStringLiteral(callArguments[0]!)) {
return;
}
if (callee.type === 'Import') {
callArguments[0].value =
jsImportResolvedUrls.get(callArguments[0].value);
}
}
}
});
}