ts 源码解析

compile flow

flow chart

sourceCode--scanner--token--parse--ast--binder--checker--transformer--emitter

debugger flow

yarn 
yarn run build:compiler

.vscode/launch.json

launch.json config

{
    "configurations": [
        {
            "name": "调试 ts 源码",
            "program": "${workspaceFolder}/test.js",
            "request": "launch",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "args": [],
            "type": "node"
        }
    ]
}

test.js

const ts = require("./built/local/typescript");

const filename = "./input.ts";
const program = ts.createProgram([filename], {
    allowJs: false
});
const sourceFile = program.getSourceFile(filename);
const typeChecker = program.getTypeChecker();

function visitNode(node) {
    if (node.kind === ts.SyntaxKind.TypeReference)  {
        const type = typeChecker.getTypeFromTypeNode(node);

        debugger;
    }

    node.forEachChild(child =>
        visitNode(child)
    );
}

visitNode(sourceFile);
上次更新:
贡献者: Joe