本文整理汇总了TypeScript中babel-types.isStringLiteral函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isStringLiteral函数的具体用法?TypeScript isStringLiteral怎么用?TypeScript isStringLiteral使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isStringLiteral函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: codeFrameError
attributes: attributes.reduce((obj, attr) => {
if (t.isJSXSpreadAttribute(attr)) {
throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
}
const name = attr.name.name === 'className' ? 'class' : attr.name.name
let value: string | boolean = true
let attrValue = attr.value
if (typeof name === 'string') {
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (t.isJSXExpressionContainer(attrValue)) {
const isBindEvent =
name.startsWith('bind') || name.startsWith('catch')
let { code } = generate(attrValue.expression)
code = code
.replace(/(this\.props\.)|(this\.state\.)/, '')
.replace(/this\./, '')
value = isBindEvent ? code : `{{${code}}}`
if (t.isStringLiteral(attrValue.expression)) {
value = attrValue.expression.value
}
}
if (
componentSpecialProps &&
componentSpecialProps.has(name)
) {
obj[name] = value
} else {
obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
}
}
return obj
}, {}),
示例2: getWXS
function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports: Imports[]): WXS {
let moduleName: string | null = null
let src: string | null = null
for (const attr of attrs) {
if (t.isJSXIdentifier(attr.name)) {
const attrName = attr.name.name
const attrValue = attr.value
let value: string | null = null
if (attrValue === null) {
throw new Error('WXS 标签的属性值不得为空')
}
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (
t.isJSXExpressionContainer(attrValue) &&
t.isStringLiteral(attrValue.expression)
) {
value = attrValue.expression.value
}
if (attrName === 'module') {
moduleName = value
}
if (attrName === 'src') {
src = value
}
}
}
if (!src) {
const { children: [ script ] } = path.node
if (!t.isJSXText(script)) {
throw new Error('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。')
}
src = './wxs__' + moduleName
imports.push({
ast: parseCode(script.value),
name: moduleName as string,
wxs: true
})
}
if (!moduleName || !src) {
throw new Error('一个 WXS 需要同时存在两个属性:`wxs`, `src`')
}
path.remove()
return {
module: moduleName,
src
}
}
示例3: codeFrameError
attributesTrans = attributes.reduce((obj, attr) => {
if (t.isJSXSpreadAttribute(attr)) {
throw codeFrameError(attr.loc, 'JSX 参数暂不支持 ...spread 表达式')
}
let name = attr.name.name
if (DEFAULT_Component_SET.has(componentName)) {
if (name === 'className') {
name = 'class'
}
}
let value: string | boolean = true
let attrValue = attr.value
if (typeof name === 'string') {
const isAlipayEvent = Adapter.type === Adapters.alipay && /(^on[A-Z_])|(^catch[A-Z_])/.test(name)
if (t.isStringLiteral(attrValue)) {
value = attrValue.value
} else if (t.isJSXExpressionContainer(attrValue)) {
let isBindEvent =
(name.startsWith('bind') && name !== 'bind') || (name.startsWith('catch') && name !== 'catch')
const code = decodeUnicode(generate(attrValue.expression, {
quotes: 'single',
concise: true
}).code)
.replace(/"/g, "'")
.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/this\./g, '')
value = isBindEvent || isAlipayEvent ? code : `{{${code}}}`
if (Adapter.type === Adapters.swan && name === Adapter.for) {
value = code
}
if (t.isStringLiteral(attrValue.expression)) {
value = attrValue.expression.value
}
} else if (attrValue === null && name !== Adapter.else) {
value = `{{true}}`
}
if ((componentName === 'Input' || componentName === 'input') && name === 'maxLength') {
obj['maxlength'] = value
} else if (
componentSpecialProps && componentSpecialProps.has(name) ||
name.startsWith('__fn_') ||
isAlipayEvent
) {
obj[name] = value
} else {
obj[isDefaultComponent && !name.includes('-') && !name.includes(':') ? kebabCase(name) : name] = value
}
}
if (!isDefaultComponent && !specialComponentName.includes(componentName)) {
obj[TRIGGER_OBSERER] = '{{ _triggerObserer }}'
}
return obj
}, {})
示例4: codeFrameError
.forEach(prop => {
if (t.isObjectProperty(prop)) {
let propKey: string | null = null
if (t.isStringLiteral(prop.key)) {
propKey = prop.key.value
}
if (t.isIdentifier(prop.key)) {
propKey = prop.key.name
// propsKeys.push(prop.key.name)
}
if (t.isObjectExpression(prop.value) && propKey) {
for (const p of prop.value.properties) {
if (t.isObjectProperty(p)) {
let key: string | null = null
if (t.isStringLiteral(p.key)) {
key = p.key.value
}
if (t.isIdentifier(p.key)) {
key = p.key.name
}
if (key === 'value') {
defaultProps.push({
name: propKey,
value: p.value
})
} else if (key === 'observer') {
observeProps.push({
name: propKey,
observer: p.value
})
}
if (!isValidVarName(propKey)) {
throw codeFrameError(prop, `${propKey} 不是一个合法的 JavaScript 变量名`)
}
}
if (t.isObjectMethod(p) && t.isIdentifier(p.key, { name: 'observer' })) {
observeProps.push({
name: propKey,
observer: t.arrowFunctionExpression(p.params, p.body, p.async)
})
}
}
}
if (propKey) {
propsKeys.push(propKey)
}
}
})
示例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: 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);
}
}
});
}
示例7: getEs6ImportResolutions
getEs6ImportResolutions(document: Document): Map<string, ResolvedUrl> {
const jsImports = document.getFeatures({
kind: 'js-import',
imported: false,
externalPackages: true,
excludeBackreferences: true,
});
const resolutions = new Map<string, ResolvedUrl>();
for (const jsImport of jsImports) {
const node = jsImport.astNode.node;
if ('source' in node) {
if (node.source && jsImport.document !== undefined) {
resolutions.set(node.source.value, jsImport.document.url);
}
} else if (
node.callee && node.callee.type + '' === 'Import' &&
jsImport.document !== undefined) {
const source = node.arguments[0];
if (source) {
if (babel.isStringLiteral(source)) {
resolutions.set(source.value, jsImport.document.url);
}
}
}
}
return resolutions;
}
示例8: _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();
}
}
}
});
}
示例9: Error
.forEach(p => {
const node = p.node
if (node.name.name === WX_FOR_ITEM) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_ITEM + ' 的值必须是一个字符串')
}
item = node.value
p.remove()
}
if (node.name.name === WX_FOR_INDEX) {
if (!node.value || !t.isStringLiteral(node.value)) {
throw new Error(WX_FOR_INDEX + ' 的值必须是一个字符串')
}
index = node.value
p.remove()
}
})
示例10: _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));
}
}
});
}