How to use the ts-morph.TypeGuards.isStringLiteral 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 / neu / parsers / endpoint-parser.ts View on Github external
function extractEndpointTags(
  decoratorConfig: ObjectLiteralExpression
): Result {
  const tagsProp = getObjLiteralProp(decoratorConfig, "tags");
  if (tagsProp === undefined) return ok([]);

  const tagsLiteral = getPropValueAsArrayOrThrow(tagsProp);
  const tags: string[] = [];

  for (const elementExpr of tagsLiteral.getElements()) {
    // Sanity check, typesafety should prevent any non-string tags
    if (!TypeGuards.isStringLiteral(elementExpr)) {
      return err(
        new ParserError("endpoint tag must be a string", {
          file: elementExpr.getSourceFile().getFilePath(),
          position: elementExpr.getPos()
        })
      );
    }
    const tag = elementExpr.getLiteralText().trim();
    if (tag.length === 0) {
      return err(
        new ParserError("endpoint tag cannot be blank", {
          file: elementExpr.getSourceFile().getFilePath(),
          position: elementExpr.getPos()
        })
      );
    }
github airtasker / spot / lib / src / neu / parsers / type-parser.ts View on Github external
function parseLiteralType(
  typeNode: LiteralTypeNode
): Result<
  BooleanLiteralType | StringLiteralType | FloatLiteralType | IntLiteralType,
  ParserError
> {
  const literal = typeNode.getLiteral();
  if (TypeGuards.isBooleanLiteral(literal)) {
    return ok(booleanLiteralType(literal.getLiteralValue()));
  } else if (TypeGuards.isStringLiteral(literal)) {
    return ok(stringLiteralType(literal.getLiteralText()));
  } else if (TypeGuards.isNumericLiteral(literal)) {
    const numericValue = literal.getLiteralValue();
    return ok(
      Number.isInteger(numericValue)
        ? intLiteralType(numericValue)
        : floatLiteralType(numericValue)
    );
  } else {
    return err(
      new TypeNotAllowedError("unexpected literal type", {
        file: typeNode.getSourceFile().getFilePath(),
        position: typeNode.getPos()
      })
    );
  }
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
  if (literalChain.some(literal => !TypeGuards.isStringLiteral(literal))) {
    throw new Error("indexed access type error: not a string literal");
github airtasker / spot / lib / src / parsers / utilities / expression-parser.ts View on Github external
export function parseExpression(expression: Expression): DataExpression {
  if (TypeGuards.isNullLiteral(expression)) {
    return nullExpression();
  } else if (TypeGuards.isBooleanLiteral(expression)) {
    return booleanExpression(expression.getLiteralValue());
  } else if (TypeGuards.isStringLiteral(expression)) {
    return stringExpression(expression.getLiteralValue());
  } else if (TypeGuards.isPrefixUnaryExpression(expression)) {
    const operand = expression.getOperand();
    if (!TypeGuards.isNumericLiteral(operand)) {
      throw new Error("unary operators may only be used with numeric literals");
    }
    switch (expression.getOperatorToken()) {
      case SyntaxKind.MinusToken:
        return numberExpression(-operand.getLiteralValue());
      case SyntaxKind.PlusToken:
        return numberExpression(operand.getLiteralValue());
      default:
        throw new Error("unknown prefix operator token");
    }
  } else if (TypeGuards.isNumericLiteral(expression)) {
    return numberExpression(expression.getLiteralValue());
github mono / TsToCSharp / src / GeneratorHelpers.ts View on Github external
export function isMap(node: (sast.InterfaceDeclaration | sast.ClassDeclaration | sast.TypeLiteralNode)) : boolean {
  const members = node.getMembers();
  
  if (members.length === 0)
    return false;

  let isMap : boolean = true;
  for (let m = 0; m < members.length; m++)
  {
    const member = members[m];
    if (TypeGuards.isPropertySignature(member))
    {
      const name = member.getName();
      const memberType = member.getNameNode();
      if (!TypeGuards.isStringLiteral(memberType))
      {
        isMap = false;
        break;
      }
      
    }
    else
    {
      isMap = false;
      break;
    }
  }
  return isMap;
}
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
function parseLiteralType(typeNode: LiteralTypeNode): PrimitiveLiteral {
  const literal = typeNode.getLiteral();
  if (TypeGuards.isBooleanLiteral(literal)) {
    return booleanLiteral(literal.getLiteralValue());
  } else if (TypeGuards.isStringLiteral(literal)) {
    return stringLiteral(literal.getLiteralText());
  } else if (TypeGuards.isNumericLiteral(literal)) {
    return numberLiteral(literal.getLiteralValue());
  } else {
    throw new Error("unexpected literal type");
  }
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
const value = literal.getElements().map(e => {
    if (TypeGuards.isStringLiteral(e)) {
      return e.getLiteralText();
    } else {
      throw new Error(`expected string literal`);
    }
  });
  const location = property.getSourceFile().getFilePath();