當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript types.isObjectExpression函數代碼示例

本文整理匯總了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]);
 });
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:24,代碼來源:esutil_test.ts

示例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));
//.........這裏部分代碼省略.........
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:101,代碼來源:analyze-properties.ts

示例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});
      }
    }
  };
}
開發者ID:MehdiRaash,項目名稱:tools,代碼行數:69,代碼來源:declaration-property-handlers.ts


注:本文中的@babel/types.isObjectExpression函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。