typescript#SourceFile TypeScript Examples

The following examples show how to use typescript#SourceFile. 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: ng-module.ts    From cli with Apache License 2.0 6 votes vote down vote up
export function getAdditionalImports(
  source: SourceFile,
  appModulePath: string
) {
  const modules = {
    AppRoutingModule: './app-routing.module',
    CoveoComponentsModule: './coveo.components.module',
  };

  const changes: InsertChange[] = [];

  Object.entries(modules).map(([key, value]) =>
    changes.push(
      ...(addImportToModule(
        source,
        appModulePath,
        key,
        value
      ) as InsertChange[])
    )
  );

  return changes;
}
Example #2
Source File: index.ts    From edit-in-place with MIT License 6 votes vote down vote up
export function getModuleFile(host: Tree, options: SchemaOptions): SourceFile {
  const modulePath = options.module;

  if (!host.exists(modulePath)) {
    throw new SchematicsException(`File ${modulePath} does not exist.`);
  }

  const text = host.read(modulePath);
  if (text === null) {
    throw new SchematicsException(`File ${modulePath} does not exist.`);
  }
  const sourceText = text.toString('utf-8');

  return createSourceFile(modulePath, sourceText, ScriptTarget.Latest, true);
}
Example #3
Source File: dangerfile.ts    From autocomplete with MIT License 6 votes vote down vote up
specTransformer: TransformerFactory<SourceFile> = (context) => {
  return (sourceFile) => {
    const visitor = (node: Node) => {
      if (isPropertyAssignment(node)) {
        const typedNode = node as PropertyAssignment;
        if (typedNode.name.getText() === "script") {
          console.log(typedNode.initializer.getText());
        }
      }
      return visitEachChild(node, visitor, context);
    };
    return visitNode(sourceFile, visitor);
  };
}
Example #4
Source File: sourceFilesHelper.ts    From elephize with MIT License 5 votes vote down vote up
sourceFiles: { [key: string]: SourceFile | null } = {}
Example #5
Source File: ast-converter.d.ts    From amazon-kinesis-video-streams-webrtc-sdk-js-with-amazon-cognito with MIT No Attribution 5 votes vote down vote up
export declare function astConverter(ast: SourceFile, extra: Extra, shouldPreserveNodeMaps: boolean): {
    estree: TSESTree.Program;
    astMaps: ASTMaps | undefined;
};
Example #6
Source File: measurePerf.ts    From DefinitelyTyped-tools with MIT License 4 votes vote down vote up
export async function measurePerf({
  packageName,
  packageVersion,
  typeScriptVersion,
  definitelyTypedRootPath,
  allPackages,
  maxRunSeconds = Infinity,
  progress,
  nProcesses,
  iterations,
  tsPath,
  ts,
  batchRunStart,
  failOnErrors,
}: MeasurePerfOptions) {
  let duration = NaN;
  const sourceVersion = execSync("git rev-parse HEAD", { cwd: definitelyTypedRootPath, encoding: "utf8" }).trim();
  const observer = new PerformanceObserver((list) => {
    const totalMeasurement = list.getEntriesByName("benchmark")[0];
    duration = totalMeasurement.duration;
  });

  observer.observe({ entryTypes: ["measure"] });
  performance.mark("benchmarkStart");
  const typesPath = path.join(definitelyTypedRootPath, "types");

  const typings = allPackages.getTypingsData({
    name: packageName,
    version: packageVersion,
  });
  const packagePath = path.join(typesPath, typings.subDirectoryPath);
  const typesVersion = getLatestTypesVersionForTypeScriptVersion(typings.typesVersions, typeScriptVersion);
  const latestTSTypesDir = path.resolve(packagePath, typesVersion ? `ts${typesVersion}` : ".");
  await installDependencies(allPackages, typings.id, typesPath);

  const commandLine = getParsedCommandLineForPackage(ts, latestTSTypesDir);
  const testPaths = getTestFileNames(commandLine.fileNames);

  let done = 0;
  let lastUpdate = Date.now();
  let languageServiceCrashed = false;
  const testMatrix = createLanguageServiceTestMatrix(testPaths, latestTSTypesDir, commandLine.options, iterations);
  if (progress) {
    updateProgress(
      `${toPackageKey(packageName, packageVersion)}: benchmarking over ${nProcesses} processes`,
      0,
      testMatrix.inputs.length
    );
  }

  await runWithListeningChildProcesses({
    inputs: testMatrix.inputs,
    commandLineArgs: [],
    workerFile: measureLanguageServiceWorkerFilename,
    nProcesses,
    crashRecovery: !failOnErrors,
    cwd: process.cwd(),
    softTimeoutMs: maxRunSeconds * 1000,
    handleCrash: (input) => {
      languageServiceCrashed = true;
      console.error("Failed measurement on request:", JSON.stringify(input, undefined, 2));
    },
    handleOutput: (measurement: LanguageServiceSingleMeasurement) => {
      testMatrix.addMeasurement(measurement);
      done++;
      if (progress) {
        updateProgress(
          `${toPackageKey(packageName, packageVersion)}: benchmarking over ${nProcesses} processes`,
          done,
          testMatrix.inputs.length
        );
      } else if (Date.now() - lastUpdate > 1000 * 60 * 5) {
        // Log every 5 minutes or so to make sure Pipelines doesn’t shut us down
        console.log(((100 * done) / testMatrix.inputs.length).toFixed(1) + "% done...");
        lastUpdate = Date.now();
      }
    },
  });

  if (progress && done !== testMatrix.inputs.length) {
    updateProgress(`${toPackageKey(packageName, packageVersion)}: timed out`, done, testMatrix.inputs.length);
    process.stdout.write(os.EOL);
  }

  const batchCompilationInput: MeasureBatchCompilationChildProcessArgs = {
    tsPath,
    fileNames: commandLine.fileNames,
    options: commandLine.options,
  };

  let batchCompilationResult: MeasureBatchCompilationChildProcessResult | undefined;
  await runWithChildProcesses({
    inputs: [batchCompilationInput],
    workerFile: measureBatchCompilationWorkerFilename,
    commandLineArgs: [],
    nProcesses: 1,
    handleOutput: (result: MeasureBatchCompilationChildProcessResult) => {
      batchCompilationResult = result;
    },
  });

  if (!batchCompilationResult) {
    throw new Error("Failed to get batch compilation metrics");
  }

  performance.mark("benchmarkEnd");
  performance.measure("benchmark", "benchmarkStart", "benchmarkEnd");

  const measurement: PackageBenchmark = {
    ...batchCompilationResult,
    benchmarkDuration: duration,
    sourceVersion,
    packageName,
    packageVersionMajor: packageVersion.major,
    packageVersionMinor: packageVersion.minor,
    typeScriptVersion,
    typeScriptVersionMajorMinor: ts.versionMajorMinor,
    languageServiceBenchmarks: testMatrix.getAllBenchmarks(),
    requestedLanguageServiceTestIterations: iterations,
    languageServiceCrashed,
    testIdentifierCount: testMatrix.uniquePositionCount,
    batchRunStart,
  };

  return measurement;

  function getIdentifiers(sourceFile: SourceFile) {
    const identifiers: Node[] = [];
    ts.forEachChild(sourceFile, function visit(node) {
      if (ts.isIdentifier(node)) {
        identifiers.push(node);
      } else {
        ts.forEachChild(node, visit);
      }
    });
    return identifiers;
  }

  function getTestFileNames(fileNames: readonly string[]) {
    return fileNames.filter((name) => {
      const ext = path.extname(name);
      return (ext === Extension.Ts || ext === Extension.Tsx) && !name.endsWith(Extension.Dts);
    });
  }

  function createLanguageServiceTestMatrix(
    testPaths: string[],
    packageDirectory: string,
    compilerOptions: CompilerOptions,
    iterations: number
  ) {
    const fileMap = new Map<string, Map<number, LanguageServiceBenchmark>>();
    const inputs: MeasureLanguageServiceChildProcessArgs[] = [];
    let uniquePositionCount = 0;
    for (const testPath of testPaths) {
      const positionMap = new Map<number, LanguageServiceBenchmark>();
      fileMap.set(testPath, positionMap);
      const sourceFile = ts.createSourceFile(
        testPath,
        ts.sys.readFile(testPath)!,
        compilerOptions.target || ts.ScriptTarget.Latest
      );
      // Reverse: more complex examples are usually near the end of test files,
      // so prioritize those.
      const identifiers = getIdentifiers(sourceFile).reverse();
      uniquePositionCount += identifiers.length;
      // Do the loops in this order so that a single child process doesn’t
      // run iterations of the same exact measurement back-to-back to avoid
      // v8 optimizing a significant chunk of the work away.
      for (let i = 0; i < iterations; i++) {
        for (const identifier of identifiers) {
          const start = identifier.getStart(sourceFile);
          if (i === 0) {
            const lineAndCharacter = ts.getLineAndCharacterOfPosition(sourceFile, start);
            const benchmark: LanguageServiceBenchmark = {
              fileName: path.relative(definitelyTypedRootPath, testPath),
              start,
              end: identifier.getEnd(),
              identifierText: identifier.getText(sourceFile),
              line: lineAndCharacter.line + 1,
              offset: lineAndCharacter.character + 1,
              completionsDurations: [],
              quickInfoDurations: [],
            };
            positionMap.set(start, benchmark);
          }
          inputs.push({
            fileName: testPath,
            start,
            packageDirectory,
            tsPath,
          });
        }
      }
    }
    return {
      inputs,
      uniquePositionCount,
      addMeasurement: (measurement: LanguageServiceSingleMeasurement) => {
        const benchmark = fileMap.get(measurement.fileName)!.get(measurement.start)!;
        benchmark.completionsDurations.push(measurement.completionsDuration);
        benchmark.quickInfoDurations.push(measurement.quickInfoDuration);
      },
      getAllBenchmarks: () => {
        return Array.prototype.concat
          .apply(
            [],
            Array.from(fileMap.values()).map((map) => Array.from(map.values()))
          )
          .filter(
            (benchmark: LanguageServiceBenchmark) =>
              benchmark.completionsDurations.length > 0 || benchmark.quickInfoDurations.length > 0
          );
      },
    };
  }
}