当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Project.getSourceFileOrThrow方法代码示例

本文整理汇总了TypeScript中ts-simple-ast.Project.getSourceFileOrThrow方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Project.getSourceFileOrThrow方法的具体用法?TypeScript Project.getSourceFileOrThrow怎么用?TypeScript Project.getSourceFileOrThrow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ts-simple-ast.Project的用法示例。


在下文中一共展示了Project.getSourceFileOrThrow方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: createDeclarationFile

export function createDeclarationFile(project: Project) {
    const mainFile = project.getSourceFileOrThrow("src/main.ts");
    const outputFiles = mainFile.getEmitOutput({ emitOnlyDtsFiles: true }).getOutputFiles();
    if (outputFiles.length !== 1)
        throw new Error(`Expected 1 file when emitting, but had ${outputFiles.length}`);

    const declarationFile = project.createSourceFile("ts-nameof.d.ts", outputFiles[0].getText(), { overwrite: true });

    removePreceedingCommentReference();
    commentExternalTypes();
    removeTypeScriptImport();
    wrapInGlobalModule();
    addGlobalDeclarations();

    function removePreceedingCommentReference() {
        const firstChild = declarationFile.getFirstChildOrThrow();
        declarationFile.removeText(0, firstChild.getStart());
    }

    function commentExternalTypes() {
        // these types are made to be any so that this library will work when included in
        // web projects and NodeJS does not exist. See issue #22.
        const typesToComment = [
            "ts.TransformerFactory<ts.SourceFile>",
            "NodeJS.ErrnoException"
        ];
        declarationFile.forEachDescendant(descendant => {
            if (typesToComment.indexOf(descendant.getText()) >= 0)
                descendant.replaceWithText(`any /* ${descendant.getText()} */`);
        });
    }

    function removeTypeScriptImport() {
        declarationFile.getImportDeclarationOrThrow("typescript").remove();
    }

    function wrapInGlobalModule() {
        const fileText = declarationFile.getText();
        declarationFile.removeText();
        const apiModule = declarationFile.addNamespace({
            hasDeclareKeyword: true,
            declarationKind: NamespaceDeclarationKind.Module,
            name: `"ts-nameof"`
        });
        apiModule.setBodyText(fileText);
        apiModule.getVariableStatementOrThrow(s => s.getDeclarations().some(d => d.getName() === "api"))
            .setHasDeclareKeyword(false);
    }

    function addGlobalDeclarations() {
        const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
        declarationFile.addStatements(writer => {
            writer.newLine();
            writer.write(globalFile.getText().replace(/\r?\n$/, ""));
        });
    }
}
开发者ID:dsherret,项目名称:ts-nameof,代码行数:57,代码来源:createDeclarationFile.ts

示例2: main


//.........这里部分代码省略.........
    .filter(filePath => !filePath.startsWith(jsPath));
  loadFiles(declarationProject, inputProjectFiles);

  // now we add the emitted declaration files from the input project
  for (const { filePath, text } of inputEmitResult.getFiles()) {
    declarationProject.createSourceFile(filePath, text);
  }

  // the outputProject will contain the final library file we are looking to
  // build
  const outputProject = new Project({
    compilerOptions: {
      baseUrl: buildPath,
      moduleResolution: ModuleResolutionKind.NodeJs,
      noLib: true,
      strict: true,
      target: ScriptTarget.ESNext
    },
    useVirtualFileSystem: true
  });

  // There are files we need to load into memory, so that the project "compiles"
  loadDtsFiles(outputProject);

  // libDts is the final output file we are looking to build and we are not
  // actually creating it, only in memory at this stage.
  const libDTs = outputProject.createSourceFile(outFile);

  // Deal with `js/deno.ts`

  // `gen/msg_generated.d.ts` contains too much exported information that is not
  // part of the public API surface of Deno, so we are going to extract just the
  // information we need.
  const msgGeneratedDts = inputProject.getSourceFileOrThrow(
    `${buildPath}${MSG_GENERATED_PATH}`
  );
  const msgGeneratedDtsText = extract(msgGeneratedDts, MSG_GENERATED_ENUMS);

  // Generate a object hash of substitutions of modules to use when flattening
  const customSources = {
    [msgGeneratedDts.getFilePath()]: `${
      debug ? getSourceComment(msgGeneratedDts, basePath) : ""
    }${msgGeneratedDtsText}\n`
  };

  mergeGlobal({
    basePath,
    debug,
    declarationProject,
    filePath: `${basePath}/js/globals.ts`,
    globalVarName: "window",
    inputProject,
    ignore: ["Deno"],
    interfaceName: "Window",
    targetSourceFile: libDTs
  });

  if (!silent) {
    console.log(`Merged "globals" into global scope.`);
  }

  flatten({
    basePath,
    customSources,
    debug,
    declarationProject,
开发者ID:F001,项目名称:deno,代码行数:67,代码来源:build_library.ts


注:本文中的ts-simple-ast.Project.getSourceFileOrThrow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。