<commit_before>import { Project, ts } from 'ts-morph';

import { UriUtils } from '../../common/view/uri-utils';
import {
  DocumentSettings,
  ExtensionSettings,
} from '../../feature/configuration/DocumentSettings';

export class TsMorphProject {
  public project: Project;

  private readonly tsconfigPath: string = '';

  constructor(public readonly documentSettings: DocumentSettings) {
    const settings = this.documentSettings.getSettings();
    const targetSourceDirectory = getTargetSourceDirectory(settings);

    this.tsconfigPath =
      // eslint-disable-next-line
      settings.pathToTsConfig ||
      (ts.findConfigFile(
        /* searchPath */ UriUtils.toPath(targetSourceDirectory),
        ts.sys.fileExists,
        'tsconfig.json'
      ) ??
        '');
  }

  public create(): Project {
    // const compilerSettings: ts.CompilerOptions = {
    //   module: ts.ModuleKind.CommonJS,
    //   target: ts.ScriptTarget.ESNext,
    //   // outDir: 'dist',
    //   // emitDecoratorMetadata: true,
    //   // experimentalDecorators: true,
    //   // lib: ['es2017.object', 'es7', 'dom'],
    //   sourceMap: true,
    //   rootDir: '.',
    // };

    const project = createTsMorphProject({
      // customCompilerOptions: {
      //   ...compilerSettings,
      // },
      tsConfigPath: this.tsconfigPath,
    });

    this.set(project);

    return project;
  }

  public get() {
    return this.project;
  }

  public set(tsMorphProject: Project) {
    this.project = tsMorphProject;
  }
}

function getTargetSourceDirectory(settings: ExtensionSettings) {
  let targetSourceDirectory = '';
  if (settings?.aureliaProject?.rootDirectory !== undefined) {
    targetSourceDirectory = settings.aureliaProject.rootDirectory;
  } else {
    targetSourceDirectory = ts.sys.getCurrentDirectory();
  }

  return targetSourceDirectory;
}

export function createTsMorphProject(
  customProjectSettings: {
    customCompilerOptions?: ts.CompilerOptions;
    tsConfigPath?: string;
  } = {
    customCompilerOptions: {},
    tsConfigPath: undefined,
  }
) {
  const { customCompilerOptions, tsConfigPath } = customProjectSettings;
  const project = new Project({
    compilerOptions: customCompilerOptions,
  });

  if (tsConfigPath !== undefined) {
    project.addSourceFilesFromTsConfig(tsConfigPath);
  }

  return project;
}
<commit_msg>feat(core): add files without tsconfig.json<commit_after>import { Project, ts } from 'ts-morph';

import { UriUtils } from '../../common/view/uri-utils';
import {
  DocumentSettings,
  ExtensionSettings,
} from '../../feature/configuration/DocumentSettings';

export class TsMorphProject {
  public project: Project;

  private readonly tsconfigPath: string | undefined;
  private targetSourceDirectory: string;

  constructor(public readonly documentSettings: DocumentSettings) {
    const settings = this.documentSettings.getSettings();
    const targetSourceDirectory = getTargetSourceDirectory(settings);
    this.targetSourceDirectory = targetSourceDirectory;

    const potentialTsConfigPath =
      // eslint-disable-next-line
      settings.pathToTsConfig ||
      (ts.findConfigFile(
        /* searchPath */ UriUtils.toPath(targetSourceDirectory),
        ts.sys.fileExists,
        'tsconfig.json'
      ) ??
        '');

    const sourceDirHasTsConfig = potentialTsConfigPath.includes(
      targetSourceDirectory
    );

    if (sourceDirHasTsConfig) {
      this.tsconfigPath = potentialTsConfigPath;
    }
  }

  public create(): Project {
    // const compilerSettings: ts.CompilerOptions = {
    //   module: ts.ModuleKind.CommonJS,
    //   target: ts.ScriptTarget.ESNext,
    //   // outDir: 'dist',
    //   // emitDecoratorMetadata: true,
    //   // experimentalDecorators: true,
    //   // lib: ['es2017.object', 'es7', 'dom'],
    //   sourceMap: true,
    //   rootDir: '.',
    // };

    const project = createTsMorphProject({
      // customCompilerOptions: {
      //   ...compilerSettings,
      // },
      targetSourceDirectory: this.targetSourceDirectory,
      tsConfigPath: this.tsconfigPath,
    });

    this.set(project);

    return project;
  }

  public get() {
    return this.project;
  }

  public set(tsMorphProject: Project) {
    this.project = tsMorphProject;
  }
}

function getTargetSourceDirectory(settings: ExtensionSettings) {
  let targetSourceDirectory = '';
  if (settings?.aureliaProject?.projectDirectory !== undefined) {
    targetSourceDirectory = settings.aureliaProject.projectDirectory;
  } else {
    targetSourceDirectory = ts.sys.getCurrentDirectory();
  }

  return targetSourceDirectory;
}

export function createTsMorphProject(
  customProjectSettings: {
    customCompilerOptions?: ts.CompilerOptions;
    tsConfigPath?: string;
    targetSourceDirectory?: string;
  } = {
    customCompilerOptions: {},
    tsConfigPath: undefined,
  }
) {
  const { customCompilerOptions, tsConfigPath, targetSourceDirectory } =
    customProjectSettings;

  const project = new Project({
    compilerOptions: customCompilerOptions,
  });

  if (tsConfigPath != null) {
    project.addSourceFilesFromTsConfig(tsConfigPath);
  }
  // No tsconfigPath means js project?!
  else if (targetSourceDirectory) {
    const pathGlob = `${targetSourceDirectory}/**/*.{j,t}s`;
    project.addSourceFilesAtPaths(pathGlob);
  }

  return project;
}

{'fraction_non_alphanumeric': 0.12013473053892215, 'fraction_numerical': 0.0018712574850299401, 'mean_word_length': 3.225296442687747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5438758', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1505, 'n_words': 288}