/**
 * Creates `traversals.ts` from the source typescript.d.ts file for guards and the `find` function
 */
import fs from 'fs';
import path from 'path';
///** Create the guard fn
var source = fs
    .readFileSync(path.join(__dirname, '../../../node_modules/typescript/lib/typescript.d.ts'))
    .toString();
var lines = source.split('\n');
var syntaxKinds = source.match(/export enum SyntaxKind \{([^}]+)\}/m);
var pairs = syntaxKinds[1].match(/([a-z]+) = [\d]+/gi);
var guardPairs = [];
pairs === null || pairs === void 0 ? void 0 : pairs.forEach(function (pair) {
    var matches = pair.match(/([a-z]+) = [0-9]+/i);
    if (matches) {
        var syntaxKind_1 = matches[1];
        var lineIndex = lines.findIndex(function (l) { return l.includes("kind: SyntaxKind." + syntaxKind_1 + ";"); });
        if (lineIndex > 0) {
            var matches_1 = lines[lineIndex - 1].match(/export interface ([a-z]+)\s/i);
            if (matches_1) {
                guardPairs.push([syntaxKind_1, matches_1[1]]);
            }
        }
    }
});
var guardFns = guardPairs.map(function (_a) {
    var kind = _a[0], typeName = _a[1];
    return "is" + kind + "(node: ts.Node): node is ts." + typeName + " {\n    return node.kind === ts.SyntaxKind." + kind + ";\n  }";
});
var kindMap = guardPairs.map(function (_a) {
    var kind = _a[0], typeName = _a[1];
    return kind + ": {} as any as ts." + typeName;
});
var kindToString = guardPairs.map(function (_a) {
    var kind = _a[0];
    return "[ts.SyntaxKind." + kind + "]: '" + kind + "'";
});
var output = "\n// This file is auto-generated from the createTraversals.ts file. Do not modify contents\nimport ts from 'typescript';\n\nexport const guards = {\n  " + guardFns.join(',\n  ') + "\n}\n\nexport const kindsMap = {\n  " + kindMap.join(',\n  ') + "\n}\n\nconst kindToString = {\n  " + kindToString.join(',\n  ') + "\n}\n\nexport function getKindNameFromNode(node: ts.Node): string {\n  // @ts-ignore\n  return kindToString[node.kind] || String(node.kind);\n}\n";
fs.writeFileSync(path.join(__dirname, 'traversals.ts'), output);
