本文整理汇总了TypeScript中@babel/types.isObjectExpression函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isObjectExpression函数的具体用法?TypeScript isObjectExpression怎么用?TypeScript isObjectExpression使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isObjectExpression函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('produces expected names', function() {
const objectLiteralCode = `
({
'foo': 1,
bar: 2,
[10]: 3,
[10 + 20]: 4,
['hi' + ' there']: 5,
[identifier]: 6,
});
`;
const statement = babylon.parse(objectLiteralCode).program.body[0];
if (!babel.isExpressionStatement(statement)) {
throw new Error('');
}
const expr = statement.expression;
if (!babel.isObjectExpression(expr)) {
throw new Error('');
}
assert.deepEqual(
[...getSimpleObjectProperties(expr)].map(
(prop) => getPropertyName(prop)),
['foo', 'bar', '10', '30', 'hi there', undefined]);
});
示例2: analyzeProperties
export function analyzeProperties(
node: babel.Node, document: JavaScriptDocument): ScannedPolymerProperty[] {
const analyzedProps: ScannedPolymerProperty[] = [];
if (!babel.isObjectExpression(node)) {
return analyzedProps;
}
for (const property of esutil.getSimpleObjectProperties(node)) {
const prop = toScannedPolymerProperty(
property, document.sourceRangeForNode(property)!, document);
if (prop === undefined) {
continue;
}
// toScannedPolymerProperty does the wrong thing for us with type. We want
// type to be undefined unless there's a positive signal for the type.
// toScannedPolymerProperty will give Object because it infers based on the
// property declaration.
prop.type = undefined;
const typeTag = jsdoc.getTag(prop.jsdoc, 'type');
if (typeTag) {
prop.type =
typeTag.type ? doctrine.type.stringify(typeTag.type) : undefined;
}
prop.published = true;
let isComputed = false;
const value = property.value;
if (babel.isIdentifier(value)) {
// Polymer supports this simple syntax, where only the attribute
// deserializer is specified.
prop.attributeType = value.name;
} else if (!babel.isObjectExpression(value)) {
continue;
} else {
/**
* Parse the expression inside a property object block. e.g.
* property: {
* key: {
* type: String,
* notify: true,
* value: -1,
* readOnly: true,
* reflectToAttribute: true
* }
* }
*/
for (const propertyArg of esutil.getSimpleObjectProperties(value)) {
const propertyKey = esutil.getPropertyName(propertyArg);
switch (propertyKey) {
case 'type':
prop.attributeType = astValue.getIdentifierName(propertyArg.value);
if (prop.attributeType === undefined && prop.type === undefined) {
prop.warnings.push(new Warning({
code: 'invalid-property-type',
message: 'Invalid type in property object.',
severity: Severity.WARNING,
sourceRange: document.sourceRangeForNode(propertyArg)!,
parsedDocument: document
}));
}
break;
case 'notify':
prop.notify = !!astValue.expressionToValue(propertyArg.value);
break;
case 'observer':
const val = astValue.expressionToValue(propertyArg.value);
prop.observerNode = propertyArg.value;
const parseResult = parseExpressionInJsStringLiteral(
document, propertyArg.value, 'identifierOnly');
prop.warnings.push(...parseResult.warnings);
prop.observerExpression = parseResult.databinding;
if (val === undefined) {
prop.observer = astValue.CANT_CONVERT;
} else {
prop.observer = JSON.stringify(val);
}
break;
case 'readOnly':
prop.readOnly = !!astValue.expressionToValue(propertyArg.value);
break;
case 'reflectToAttribute':
prop.reflectToAttribute =
!!astValue.expressionToValue(propertyArg.value);
break;
case 'computed':
isComputed = true;
const computedParseResult = parseExpressionInJsStringLiteral(
document, propertyArg.value, 'callExpression');
prop.warnings.push(...computedParseResult.warnings);
prop.computedExpression = computedParseResult.databinding;
break;
case 'value':
prop.default =
JSON.stringify(astValue.expressionToValue(propertyArg.value));
//.........这里部分代码省略.........
示例3: declarationPropertyHandlers
export function declarationPropertyHandlers(
declaration: ScannedPolymerElement,
document: JavaScriptDocument,
path: NodePath): PropertyHandlers {
return {
is(node: babel.Node) {
if (babel.isLiteral(node)) {
declaration.tagName = '' + astValue.expressionToValue(node);
}
},
properties(node: babel.Node) {
for (const prop of analyzeProperties(node, document)) {
declaration.addProperty(prop);
}
},
behaviors(node: babel.Node) {
if (!babel.isArrayExpression(node)) {
return;
}
for (const element of node.elements) {
const result = getBehaviorReference(element, document, path);
if (result.successful === false) {
declaration.warnings.push(result.error);
} else {
declaration.behaviorAssignments.push(result.value);
}
}
},
observers(node: babel.Node) {
const observers = extractObservers(node, document);
if (!observers) {
return;
}
declaration.warnings = declaration.warnings.concat(observers.warnings);
declaration.observers = declaration.observers.concat(observers.observers);
},
listeners(node: babel.Node) {
if (!babel.isObjectExpression(node)) {
declaration.warnings.push(new Warning({
code: 'invalid-listeners-declaration',
message: '`listeners` property should be an object expression',
severity: Severity.WARNING,
sourceRange: document.sourceRangeForNode(node)!,
parsedDocument: document
}));
return;
}
for (const p of getSimpleObjectProperties(node)) {
const evtName =
babel.isLiteral(p.key) && astValue.expressionToValue(p.key) ||
babel.isIdentifier(p.key) && p.key.name;
const handler =
!babel.isLiteral(p.value) || astValue.expressionToValue(p.value);
if (typeof evtName !== 'string' || typeof handler !== 'string') {
// TODO (maklesoft): Notifiy the user somehow that a listener entry
// was not extracted
// because the event or handler namecould not be statically analyzed.
// E.g. add a low-severity
// warning once opting out of rules is supported.
continue;
}
declaration.listeners.push({event: evtName, handler: handler});
}
}
};
}