How to use the typescript.createLiteral function in typescript

To help you get started, we’ve selected a few typescript 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 relay-tools / relay-compiler-language-typescript / transform / src / createClassicNode.ts View on Github external
function parseValue(value: any) {
  switch (value.kind) {
    case 'BooleanValue':
      return ts.createLiteral(value.value);
    case 'IntValue':
      return ts.createLiteral(parseInt(value.value, 10));
    case 'FloatValue':
      return ts.createLiteral(parseFloat(value.value));
    case 'StringValue':
      return ts.createLiteral(value.value);
    case 'EnumValue':
      return ts.createLiteral(value.value);
    case 'ListValue':
      return ts.createArrayLiteral(value.values.map((item: any) => parseValue(item)), /* multiLine */ true);
    default:
      throw new Error(
        'TSTransformRelay: Unsupported literal type `' + value.kind + '`.',
      );
  }
}
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
// static reaodnly _capnp = { displayName: 'MyStruct', id: '4732bab4310f81', size = new __O(8, 8) };
  members.push(
    ts.createProperty(
      __,
      [STATIC, READONLY],
      "_capnp",
      __,
      __,
      ts.createObjectLiteral(
        [
          ts.createPropertyAssignment(
            "displayName",
            ts.createLiteral(displayNamePrefix)
          ),
          ts.createPropertyAssignment("id", ts.createLiteral(nodeIdHex)),
          ts.createPropertyAssignment(
            "size",
            ts.createNew(OBJECT_SIZE, __, [
              ts.createNumericLiteral(dataByteLength.toString()),
              ts.createNumericLiteral(pointerCount.toString())
            ])
          )
        ].concat(defaultValues)
      )
    )
  );

  // private static _ConcreteListClass: MyStruct_ConcreteListClass;
  members.push(...concreteLists.map(f => createConcreteListProperty(ctx, f)));

  // getFoo() { ... } initFoo() { ... } setFoo() { ... }
github angular / angular-cli / packages / angular_devkit / build_optimizer / src / transforms / import-tslib.ts View on Github external
function createTslibImport(
  name: string,
  aliases?: string,
  useRequire = false,
): ts.VariableStatement | ts.ImportDeclaration {
  if (useRequire) {
    // Use `var __helper = /*@__PURE__*/ require("tslib").__helper`.
    const requireCall = ts.createCall(ts.createIdentifier('require'), undefined,
      [ts.createLiteral('tslib')]);
    const pureRequireCall = ts.addSyntheticLeadingComment(
      requireCall, ts.SyntaxKind.MultiLineCommentTrivia, '@__PURE__', false);
    const helperAccess = ts.createPropertyAccess(pureRequireCall, name);
    const variableDeclaration = ts.createVariableDeclaration(aliases || name, undefined, helperAccess);
    const variableStatement = ts.createVariableStatement(undefined, [variableDeclaration]);

    return variableStatement;
  } else {
    // Use `import { __helper } from "tslib"`.
    const namedImports = ts.createNamedImports([
      ts.createImportSpecifier(aliases ? ts.createIdentifier(name) : undefined, ts.createIdentifier(aliases || name)),
    ]);
    const importClause = ts.createImportClause(undefined, namedImports);
    const newNode = ts.createImportDeclaration(undefined, undefined, importClause,
      ts.createLiteral('tslib'));
github Bearer / bearer-js / packages / transpiler / src / transformers / bearer-state-injector.ts View on Github external
propsDecoratedMeta.map(meta =>
              ts.createStatement(
                ts.createAssignment(
                  ts.createPropertyAccess(ts.createThis(), meta.componentPropName),
                  ts.createElementAccess(state, ts.createLiteral(meta.statePropName))
                )
              )
            ),
github strothj / react-docgen-typescript-loader / src / generateDocgenCodeBlock.ts View on Github external
function setDisplayName(d: ComponentDoc): ts.Statement {
  return insertTsIgnoreBeforeStatement(
    ts.createStatement(
      ts.createBinary(
        ts.createPropertyAccess(
          ts.createIdentifier(d.displayName),
          ts.createIdentifier("displayName"),
        ),
        ts.SyntaxKind.EqualsToken,
        ts.createLiteral(d.displayName),
      ),
    ),
  );
}
github angular / angular-cli / packages / ngtools / webpack / src / transformers / replace_resources.ts View on Github external
function createRequireExpression(node: ts.Expression, loader?: string): ts.Expression {
  const url = getResourceUrl(node, loader);
  if (!url) {
    return node;
  }

  const callExpression = ts.createCall(
    ts.createIdentifier('require'),
    undefined,
    [ts.createLiteral(url)],
  );

  return ts.createPropertyAccess(
    ts.createCall(
      ts.setEmitFlags(
        ts.createIdentifier('__importDefault'),
        ts.EmitFlags.HelperName | ts.EmitFlags.AdviseOnEmitNode,
      ),
      undefined,
      [callExpression],
    ),
    'default',
  );
}
github ionic-team / stencil / src / compiler / transformers / decorators-to-static / deprecated-prop.ts View on Github external
function replace(prop: ts.PropertyDeclaration, functionName: string, arg: string, newMembers: ts.ClassElement[]) {
  const memberIndex = newMembers.findIndex(m => m === prop);
  newMembers[memberIndex] = ts.createProperty(
    undefined,
    undefined,
    prop.name.getText(),
    undefined,
    undefined,
    ts.createCall(
      ts.createIdentifier(functionName),
      undefined,
      [
        ts.createLiteral(arg),
        ts.createThis()
      ]
    )
  );
}
github angular / angular / packages / compiler-cli / src / ngtsc / translator / src / translator.ts View on Github external
visitJSDocCommentStmt(stmt: JSDocCommentStmt, context: Context): ts.NotEmittedStatement {
    const commentStmt = ts.createNotEmittedStatement(ts.createLiteral(''));
    const text = stmt.toString();
    const kind = ts.SyntaxKind.MultiLineCommentTrivia;
    ts.setSyntheticLeadingComments(commentStmt, [{kind, text, pos: -1, end: -1}]);
    return commentStmt;
  }
github cuba-platform / front-generator / src / generators / sdk / model / entity-views-generation.ts View on Github external
(typeExpr: ts.TypeNode, view): ts.TypeNode => {
        return ts.createConditionalTypeNode(
          ts.createTypeReferenceNode(VIEW_NAME_TYPE_PARAMETER, undefined),
          ts.createLiteralTypeNode(
            ts.createLiteral(view.name)
          ),
          createPickPropertiesType(className, view, allowedAttrs),
          typeExpr
        )
      },
      ts.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword)
github urish / typewiz / packages / typewiz-core / src / transformer.ts View on Github external
function createTwizInstrumentStatement(name: string, fileOffset: number, filename: string, opts: IExtraOptions) {
    return ts.createStatement(
        ts.createCall(
            ts.createIdentifier('$_$twiz'),
            [],
            [
                ts.createLiteral(name),
                ts.createIdentifier(name),
                ts.createNumericLiteral(fileOffset.toString()),
                ts.createLiteral(filename),
                ts.createLiteral(JSON.stringify(opts)),
            ],
        ),
    );
}