How to use the ts-morph.TypeGuards.isPropertyAssignment function in ts-morph

To help you get started, we’ve selected a few ts-morph examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github airtasker / spot / lib / src / parsers / nodes / test-parser.ts View on Github external
return expression.getProperties().map(property => {
    if (TypeGuards.isPropertyAssignment(property)) {
      const name = property
        .getNameNode()
        .getSymbolOrThrow()
        .getEscapedName();
      return {
        name,
        expression: parseExpression(property.getInitializerOrThrow())
      };
    } else {
      throw new Error("expected property assignment");
    }
  });
}
github airtasker / spot / lib / src / parsers / nodes / test-parser.ts View on Github external
return expression.getElements().map(exp => {
    if (TypeGuards.isObjectLiteralExpression(exp)) {
      const nameProperty = exp.getPropertyOrThrow("name");
      if (!TypeGuards.isPropertyAssignment(nameProperty)) {
        throw new Error("expected property assignment");
      }
      const name = nameProperty
        .getInitializerIfKindOrThrow(ts.SyntaxKind.StringLiteral)
        .getLiteralValue();

      const paramsProperty = extractOptionalObjectProperty(exp, "params");
      const params =
        paramsProperty && objectExpressionToProperties(paramsProperty.value);

      return {
        name,
        params
      };
    } else {
      throw new Error("expected object literal expression");
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractObjectProperty(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): Locatable {
  const property = objectLiteral.getPropertyOrThrow(propertyName);
  if (!TypeGuards.isPropertyAssignment(property)) {
    throw new Error("expected property assignment");
  }
  const value = property.getInitializerIfKindOrThrow(
    ts.SyntaxKind.ObjectLiteralExpression
  );
  const location = property.getSourceFile().getFilePath();
  const line = value.getStartLineNumber();

  return { value, location, line };
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractStringProperty(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): string {
  const property = objectLiteral.getPropertyOrThrow(propertyName);
  if (!TypeGuards.isPropertyAssignment(property)) {
    throw new Error("expected property assignment");
  }
  return property
    .getInitializerIfKindOrThrow(ts.SyntaxKind.StringLiteral)
    .getLiteralText();
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractOptionalObjectProperty(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): Locatable | undefined {
  const property = objectLiteral.getProperty(propertyName);
  if (property) {
    if (!TypeGuards.isPropertyAssignment(property)) {
      throw new Error("expected property assignment");
    }
    const value = property.getInitializerIfKindOrThrow(
      ts.SyntaxKind.ObjectLiteralExpression
    );
    const location = property.getSourceFile().getFilePath();
    const line = value.getStartLineNumber();

    return { value, location, line };
  } else {
    return undefined;
  }
}
github airtasker / spot / lib / src / parsers / utilities / expression-parser.ts View on Github external
.map(property => {
        if (TypeGuards.isPropertyAssignment(property)) {
          const nameNode = property.getNameNode();
          let name;
          if (TypeGuards.isIdentifier(nameNode)) {
            name = nameNode.getText();
          } else if (TypeGuards.isStringLiteral(nameNode)) {
            name = nameNode.getLiteralValue();
          } else {
            throw new Error(
              "only identifiers and string literals are valid object property keys"
            );
          }
          return {
            name,
            expression: parseExpression(property.getInitializerOrThrow())
          };
        } else {
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractBooleanProperty(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): Locatable {
  const property = objectLiteral.getPropertyOrThrow(propertyName);
  if (!TypeGuards.isPropertyAssignment(property)) {
    throw new Error("expected property assignment");
  }
  const literal = property.getInitializerOrThrow();
  if (!TypeGuards.isBooleanLiteral(literal)) {
    throw new Error("expected boolean literal");
  }
  const value = literal.getLiteralValue();
  const location = property.getSourceFile().getFilePath();
  const line = literal.getStartLineNumber();

  return { value, location, line };
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractStringPropertyValueLocatable(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): Locatable {
  const property = objectLiteral.getPropertyOrThrow(propertyName);
  if (!TypeGuards.isPropertyAssignment(property)) {
    throw new Error("expected property assignment");
  }
  const literal = property.getInitializerIfKindOrThrow(
    ts.SyntaxKind.StringLiteral
  );
  const value = literal.getLiteralText();
  const location = property.getSourceFile().getFilePath();
  const line = literal.getStartLineNumber();

  return { value, location, line };
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractOptionalArrayProperty(
  objectLiteral: ObjectLiteralExpression,
  propertyName: string
): Locatable | undefined {
  const property = objectLiteral.getProperty(propertyName);
  if (property) {
    if (!TypeGuards.isPropertyAssignment(property)) {
      throw new Error("expected property assignment");
    }
    const value = property.getInitializerIfKindOrThrow(
      ts.SyntaxKind.ArrayLiteralExpression
    );
    const location = property.getSourceFile().getFilePath();
    const line = value.getStartLineNumber();

    return { value, location, line };
  } else {
    return undefined;
  }
}
github airtasker / spot / lib / src / neu / parsers / parser-helpers.ts View on Github external
export function getObjLiteralProp(
  objectLiteral: ObjectLiteralExpression,
  propertyName: Extract
): PropertyAssignment | undefined {
  const property = objectLiteral.getProperty(propertyName);
  if (!property) {
    return undefined;
  }
  if (!TypeGuards.isPropertyAssignment(property)) {
    throw new Error("expected property assignment");
  }
  return property;
}