ts-morph#VariableDeclarationKind TypeScript Examples

The following examples show how to use ts-morph#VariableDeclarationKind. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: export.ts    From cli with MIT License 7 votes vote down vote up
// 新增 export const x = {}
export function addConstExport(
  source: SourceFile,
  key: string,
  value: any,
  useStringify = true,
  apply = true
) {
  // 又可以抽离成export相关的方法
  // 拿到所有export const 检查是否已存在
  // 不允许value是函数
  // 添加类型提示的方式
  // const exports

  const exportVars = getExportVariableIdentifiers(source);

  if (exportVars.includes(key)) {
    consola.error(`export ${key} exist!`);
    return;
  }

  source
    .addVariableStatement({
      declarationKind: VariableDeclarationKind.Const,
      declarations: [
        {
          name: key,
          initializer: writer =>
            writer.write(useStringify ? JSON.stringify(value) : value),
        },
      ],
    })
    .setIsExported(true);

  apply && source.saveSync();
}
Example #2
Source File: transformer.ts    From telefunc with MIT License 5 votes vote down vote up
generateShield = (
  telefuncFileCode: string
): string => {
  const project = new Project({
    compilerOptions: {
      strict: true
    }
  })

  project.createSourceFile("types.ts", typesSrc)
  const telefuncFileSource = project.createSourceFile("telefunc.ts", telefuncFileCode)
  // this source file is used for evaluating the template literal types' values
  const shieldStrSource = project.createSourceFile("shield-str.ts")

  shieldStrSource.addImportDeclaration({
    moduleSpecifier: "./types",
    namedImports: ["ShieldArrStr"]
  })

  const telefunctions = telefuncFileSource.getFunctions().filter(f => f.isExported())
  const telefunctionNames = telefunctions.flatMap(telefunction => {
    const name = telefunction.getName()
    if (!name) return []
    return [name]
  })
  shieldStrSource.addImportDeclaration({
    moduleSpecifier: "./telefunc",
    namedImports: telefunctionNames
  })

  // assign the template literal type to a string
  // then diagnostics are used to get the value of the template literal type
  for (const telefunctionName of telefunctionNames) {
    shieldStrSource.addTypeAlias({
      name: `${telefunctionName}Shield`,
      type: `ShieldArrStr<Parameters<typeof ${telefunctionName}>>`
    })
  }

  const shieldAlias = '__shieldGenerator_shield'  // alias for shield
  telefuncFileSource.addImportDeclaration({
    moduleSpecifier: 'telefunc',
    namedImports: [{
      name: 'shield',
      alias: shieldAlias
    }]
  })
  telefuncFileSource.addVariableStatement({
    declarationKind: VariableDeclarationKind.Const,
    declarations: [{
      name: tAlias,
      initializer: `${shieldAlias}.type`,
    }]
  })

  for (const telefunctionName of telefunctionNames) {
    const typeAlias = shieldStrSource.getTypeAlias(`${telefunctionName}Shield`)
    assert(typeAlias, `Failed to get typeAlias '${telefunctionName}Shield'.`)

    const shieldStr = typeAlias.getType().getLiteralValue()

    if (!shieldStr || typeof shieldStr !== 'string') {
      assertWarning(false, `Failed to generate shield() for telefunction ${telefunctionName}()`, { onlyOnce: false })
      continue
    }
    const shieldStrWithAlias = replaceShieldTypeAlias(shieldStr)
    telefuncFileSource.addStatements(`${shieldAlias}(${telefunctionName}, ${shieldStrWithAlias}, { __autoGenerated: true })`)
  }

  return telefuncFileSource.getText()
}