當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。