<commit_before>"use strict";
import * as fs from "fs";
import * as path from "path";
import * as ast from "cowlick-analyzer";
import * as kag from "../resources/kag";

interface ParseResult {
  scene: ast.Scene;
  dependencies: string[];
}

function parseScene(target: string): ParseResult {
  const input = fs.readFileSync(target, "utf8");
  const result = kag.parse(input);
  const scene: ast.Scene = {
    label: ast.filename(target),
    frames: result.frames
  };
  return {
    scene,
    dependencies: result.dependencies
  };
}

export function parse(baseDir: string): ast.Scenario {
  let targets = ["first.ks"];
  const scenario: ast.Scene[] = [];
  while (targets.length !== 0) {
    const filePath = path.resolve(baseDir, targets.pop());
    const result = parseScene(filePath);
    scenario.push(result.scene);

    // ファイル登場順に1度だけ解析対象として追加する
    for (const d of result.dependencies) {
      if (targets.findIndex(t => t === d) === -1) {
        targets.push(d);
      }
    }
  }
  return scenario;
}
<commit_msg>解析中に無限ループに陥る問題を修正<commit_after>"use strict";
import * as fs from "fs";
import * as path from "path";
import * as ast from "cowlick-analyzer";
import * as kag from "../resources/kag";

interface ParseResult {
  scene: ast.Scene;
  dependencies: string[];
}

function parseScene(target: string): ParseResult {
  const input = fs.readFileSync(target, "utf8");
  const result = kag.parse(input);
  const scene: ast.Scene = {
    label: ast.filename(target),
    frames: result.frames
  };
  return {
    scene,
    dependencies: result.dependencies
  };
}

export function parse(baseDir: string): ast.Scenario {
  let targets = ["first.ks"];
  const scenario: ast.Scene[] = [];
  while (targets.length !== 0) {
    const filePath = path.resolve(baseDir, targets.pop());
    const result = parseScene(filePath);
    scenario.push(result.scene);

    // ファイル登場順に1度だけ解析対象として追加する
    for (const d of result.dependencies) {
      if (targets.findIndex(t => t === d) === -1 && scenario.findIndex(s => s.label === ast.filename(d)) === -1) {
        targets.push(d);
      }
    }
  }
  return scenario;
}

{'fraction_non_alphanumeric': 0.14992927864214992, 'fraction_numerical': 0.004243281471004243, 'mean_word_length': 3.393374741200828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '3389848', 'n_tokens_mistral': 730, 'n_tokens_neox': 727, 'n_words': 176}