🎉

typescript-eslint/typescript-estree デバッグスクリプト

2022/04/17に公開

@typescript-eslint/typescript-estree をデバッグしたいことが誰しもあると思う。

console.log デバッグ大好き君であるところの自分はこんなスクリプトを用意している。

このスクリプトは ./packages/typescript-estree 直下に配置する。名前はなんでもいいけど、自分はマシンのグローバルの .gitignore_tmptmptmp.* というのを入れていて、雑なデバッグ用スクリプトみたいなのをその名前にするようにしている。

なのでこいつの名前は _tmptmptmp.js である。

で、typescript-estree のディレクトリで、ウォッチモードで tsc を動かしながらこのスクリプトを実行しながらデバッグする。

--omit-loc オプションをつけると、ロケーション情報を消した AST を jq で整形してだしてくれる。

_tmptmptmp.js
/* eslint-disable */
const { execSync } = require('child_process');

const { parseAndGenerateServices } = require('./dist/index');

const args = process.argv.slice(2);
const modeArg = args[0];

const codes = [
  `type X3<T> = T extends [infer U extends number] ? MustBeNumber<U> : never;`,
];

const result = parseAndGenerateServices(codes[0], {
  errorOnTypeScriptSyntacticAndSemanticIssues: true,
  loc: true,
  range: true,
  tokens: false,
  comment: false,
  errorOnUnknownASTType: true,
});

if (modeArg === '--omit-loc') {
  const string = JSON.stringify(result);
  execSync(
    `echo '${string}' | jq '.ast | walk(if type == "object" and has("loc") then del(.loc, .start, .end, .range)  else . end)'`,
    { shell: true, stdio: 'inherit' },
  );
} else {
  console.log(JSON.stringify(result));
}

Discussion