How to use the typescript.isStringLiteral 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 angular / angular / packages / compiler-cli / src / ngtsc / metadata / src / util.ts View on Github external
function extractTemplateGuard(member: ClassMember): TemplateGuardMeta|null {
  if (!member.name.startsWith('ngTemplateGuard_')) {
    return null;
  }
  const inputName = afterUnderscore(member.name);
  if (member.kind === ClassMemberKind.Property) {
    let type: string|null = null;
    if (member.type !== null && ts.isLiteralTypeNode(member.type) &&
        ts.isStringLiteral(member.type.literal)) {
      type = member.type.literal.text;
    }

    // Only property members with string literal type 'binding' are considered as template guard.
    if (type !== 'binding') {
      return null;
    }
    return {inputName, type};
  } else if (member.kind === ClassMemberKind.Method) {
    return {inputName, type: 'invocation'};
  } else {
    return null;
  }
}
github aurelia / aurelia / packages / plugin-conventions / dist / umd / preprocess-resource.js View on Github external
function captureImport(s, lib, code) {
        if (ts.isImportDeclaration(s) &&
            ts.isStringLiteral(s.moduleSpecifier) &&
            s.moduleSpecifier.text === lib &&
            s.importClause &&
            s.importClause.namedBindings &&
            ts.isNamedImports(s.importClause.namedBindings)) {
            return {
                names: s.importClause.namedBindings.elements.map(e => e.name.text),
                start: ensureTokenStart(s.pos, code),
                end: s.end
            };
        }
    }
    // This method mutates runtimeExports.
github bang88 / typescript-react-intl / lib / index.js View on Github external
element.attributes.properties.forEach(function (attr) {
                if (!ts.isJsxAttribute(attr) || !attr.initializer) {
                    // Either JsxSpreadAttribute, or JsxAttribute without initializer.
                    return;
                }
                var key = attr.name.text;
                var init = attr.initializer;
                var text;
                if (ts.isStringLiteral(init)) {
                    text = init.text;
                }
                else if (ts.isJsxExpression(init)) {
                    if (init.expression &&
                        (ts.isStringLiteral(init.expression) ||
                            ts.isNoSubstitutionTemplateLiteral(init.expression))) {
                        text = init.expression.text;
                    }
                    else {
                        // Either the JsxExpression has no expression (?)
                        // or a non-StringLiteral expression.
                        return;
                    }
                }
                else {
                    // Should be a StringLiteral or JsxExpression, but it's not!
github nrwl / nx / packages / workspace / src / core / project-graph / build-dependencies / typescript-import-locator.ts View on Github external
) => void
  ): void {
    if (
      ts.isImportDeclaration(node) ||
      (ts.isExportDeclaration(node) && node.moduleSpecifier)
    ) {
      const imp = this.getStringLiteralValue(node.moduleSpecifier);
      visitor(imp, filePath, DependencyType.static);
      return; // stop traversing downwards
    }

    if (
      ts.isCallExpression(node) &&
      node.expression.kind === ts.SyntaxKind.ImportKeyword &&
      node.arguments.length === 1 &&
      ts.isStringLiteral(node.arguments[0])
    ) {
      const imp = this.getStringLiteralValue(node.arguments[0]);
      visitor(imp, filePath, DependencyType.dynamic);
      return;
    }

    if (node.kind === ts.SyntaxKind.PropertyAssignment) {
      const name = this.getPropertyAssignmentName(
        (node as ts.PropertyAssignment).name
      );
      if (name === 'loadChildren') {
        const init = (node as ts.PropertyAssignment).initializer;
        if (init.kind === ts.SyntaxKind.StringLiteral) {
          const childrenExpr = this.getStringLiteralValue(init);
          visitor(childrenExpr, filePath, DependencyType.dynamic);
          return; // stop traversing downwards
github zerkalica / zerollup / packages / ts-transform-paths / src / importPathVisitor.ts View on Github external
expression.kind !== ts.SyntaxKind.ImportKeyword
    )
      return
    importValue = stripQuotes(arg.getText(sf))
    nodeToFix = arg
    // import, export
  } else if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
    if (!node.moduleSpecifier || !ts.isStringLiteral(node.moduleSpecifier))
      return
    // do not use getFullText() here, bug in watch mode, https://github.com/zerkalica/zerollup/issues/12
    importValue = node.moduleSpecifier.text
    nodeToFix = node.moduleSpecifier
  } else if (
    ts.isImportTypeNode(node) &&
    ts.isLiteralTypeNode(node.argument) &&
    ts.isStringLiteral(node.argument.literal)
  ) {
    importValue = node.argument.literal.text
  } else if (ts.isModuleDeclaration(node)) {
    if (!ts.isStringLiteral(node.name)) return
    importValue = node.name.text
    nodeToFix = node.name
  } else {
    return
  }

  const newImport = resolver.resolveImport(
    importValue,
    path.dirname(sf.fileName)
  )

  if (!newImport || newImport === importValue) return
github anz-bank / sysl / pkg / lspimpl / lspframework / lsp / protocol / typescript / util.ts View on Github external
function f(n: ts.Node) {
    seenAdd(kinds(n))
    if (ts.isIdentifier(n)) {
      pr(`${indent} ${loc(n)} ${strKind(n)} ${n.text} \n`)
    }
    else if (ts.isPropertySignature(n) || ts.isEnumMember(n)) {
      pra(`${indent} ${loc(n)} ${strKind(n)} \n`)
    }
    else if (ts.isTypeLiteralNode(n)) {
      let m = n.members
      pr(`${indent} ${loc(n)} ${strKind(n)} ${m.length} \n`)
    }
    else if (ts.isStringLiteral(n)) {
      pr(`${indent} ${loc(n)} ${strKind(n)} ${n.text} \n`)
    }
    else {pr(`${indent} ${loc(n)} ${strKind(n)} \n`)};
    indent += ' .'
    ts.forEachChild(n, f)
    indent = indent.slice(0, indent.length - 2)
  }
  f(node)
github fuse-box / fuse-box / src / production / transformation / conditionUnwrapper.ts View on Github external
function extractValue(node: Node) {
  let value;
  let text = node.getText();

  if (ts.isStringLiteral(node.compilerNode)) {
    value = node.compilerNode.text;
  } else if (ts.isNumericLiteral(node.compilerNode)) {
    value = parseInt(node.compilerNode.text);
  } else {
    if (text === 'true') {
      value = true;
    } else if (text === 'false') {
      value = false;
    }
  }
  return value;
}
github ionic-team / stencil / src / compiler / transpile / transformers / module-imports.ts View on Github external
function visitImport(moduleFile: d.ModuleFile, dirPath: string, importNode: ts.ImportDeclaration) {
      if (importNode.moduleSpecifier && ts.isStringLiteral(importNode.moduleSpecifier)) {
        let importPath = importNode.moduleSpecifier.text;

        if (config.sys.path.isAbsolute(importPath)) {
          importPath = normalizePath(importPath);
          moduleFile.localImports.push(importPath);

        } else if (importPath.startsWith('.')) {
          importPath = normalizePath(config.sys.path.resolve(dirPath, importPath));
          moduleFile.localImports.push(importPath);

        } else {
          moduleFile.externalImports.push(importPath);
        }
      }

      return importNode;
github ng-qt / ng-qt / packages / devkit / src / transformers / inline-assets-transformer.ts View on Github external
function transformPropertyAssignmentForJest(node: PropertyAssignment) {
    const mutableAssignment = ts.getMutableClone(node);

    function createDefaultRequireCall(literal: ts.StringLiteral) {
      return ts.createPropertyAccess(
        ts.createCall(ts.createIdentifier(REQUIRE), undefined, [literal]),
        EXPORT_DEFAULT,
      );
    }

    const assignmentNameText = (mutableAssignment.name as Identifier).text;
    const pathLiteral = mutableAssignment.initializer;
    switch (assignmentNameText) {
      case TEMPLATE_URL:
        if (ts.isStringLiteral(pathLiteral)) {
          mutableAssignment.name = ts.createIdentifier(TEMPLATE);
          mutableAssignment.initializer = createDefaultRequireCall(pathLiteral);
        }
        break;

      case STYLE_URLS:
        if (ts.isArrayLiteralExpression(pathLiteral)) {
          mutableAssignment.name = ts.createIdentifier(STYLES);
          mutableAssignment.initializer = ts.createArrayLiteral(
            pathLiteral.elements.reduce(
              (literals, literal) => {
                if (ts.isStringLiteral(literal)) {
                  const styleRequire = createDefaultRequireCall(literal);
                  return [...literals, styleRequire];
                }
github tensorflow / tensorboard / tensorboard / tools / migration / src / polymerelementify.ts View on Github external
function initializerValue(initializer: ts.Expression): string {
  if (ts.isIdentifier(initializer)) {
    return initializer.text;
  }
  if (ts.isStringLiteral(initializer)) {
    return initializer.text;
  }
  return initializer.getText();
}