• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
2// See LICENSE.txt in the project root for complete license information.
3
4///<reference path='typescript.ts' />
5
6module TypeScript.AstWalkerWithDetailCallback {
7    export interface AstWalkerDetailCallback {
8        EmptyCallback? (pre, ast: AST): boolean;
9        EmptyExprCallback? (pre, ast: AST): boolean;
10        TrueCallback? (pre, ast: AST): boolean;
11        FalseCallback? (pre, ast: AST): boolean;
12        ThisCallback? (pre, ast: AST): boolean;
13        SuperCallback? (pre, ast: AST): boolean;
14        QStringCallback? (pre, ast: AST): boolean;
15        RegexCallback? (pre, ast: AST): boolean;
16        NullCallback? (pre, ast: AST): boolean;
17        ArrayLitCallback? (pre, ast: AST): boolean;
18        ObjectLitCallback? (pre, ast: AST): boolean;
19        VoidCallback? (pre, ast: AST): boolean;
20        CommaCallback? (pre, ast: AST): boolean;
21        PosCallback? (pre, ast: AST): boolean;
22        NegCallback? (pre, ast: AST): boolean;
23        DeleteCallback? (pre, ast: AST): boolean;
24        AwaitCallback? (pre, ast: AST): boolean;
25        InCallback? (pre, ast: AST): boolean;
26        DotCallback? (pre, ast: AST): boolean;
27        FromCallback? (pre, ast: AST): boolean;
28        IsCallback? (pre, ast: AST): boolean;
29        InstOfCallback? (pre, ast: AST): boolean;
30        TypeofCallback? (pre, ast: AST): boolean;
31        NumberLitCallback? (pre, ast: AST): boolean;
32        NameCallback? (pre, identifierAst: Identifier): boolean;
33        TypeRefCallback? (pre, ast: AST): boolean;
34        IndexCallback? (pre, ast: AST): boolean;
35        CallCallback? (pre, ast: AST): boolean;
36        NewCallback? (pre, ast: AST): boolean;
37        AsgCallback? (pre, ast: AST): boolean;
38        AsgAddCallback? (pre, ast: AST): boolean;
39        AsgSubCallback? (pre, ast: AST): boolean;
40        AsgDivCallback? (pre, ast: AST): boolean;
41        AsgMulCallback? (pre, ast: AST): boolean;
42        AsgModCallback? (pre, ast: AST): boolean;
43        AsgAndCallback? (pre, ast: AST): boolean;
44        AsgXorCallback? (pre, ast: AST): boolean;
45        AsgOrCallback? (pre, ast: AST): boolean;
46        AsgLshCallback? (pre, ast: AST): boolean;
47        AsgRshCallback? (pre, ast: AST): boolean;
48        AsgRs2Callback? (pre, ast: AST): boolean;
49        QMarkCallback? (pre, ast: AST): boolean;
50        LogOrCallback? (pre, ast: AST): boolean;
51        LogAndCallback? (pre, ast: AST): boolean;
52        OrCallback? (pre, ast: AST): boolean;
53        XorCallback? (pre, ast: AST): boolean;
54        AndCallback? (pre, ast: AST): boolean;
55        EqCallback? (pre, ast: AST): boolean;
56        NeCallback? (pre, ast: AST): boolean;
57        EqvCallback? (pre, ast: AST): boolean;
58        NEqvCallback? (pre, ast: AST): boolean;
59        LtCallback? (pre, ast: AST): boolean;
60        LeCallback? (pre, ast: AST): boolean;
61        GtCallback? (pre, ast: AST): boolean;
62        GeCallback? (pre, ast: AST): boolean;
63        AddCallback? (pre, ast: AST): boolean;
64        SubCallback? (pre, ast: AST): boolean;
65        MulCallback? (pre, ast: AST): boolean;
66        DivCallback? (pre, ast: AST): boolean;
67        ModCallback? (pre, ast: AST): boolean;
68        LshCallback? (pre, ast: AST): boolean;
69        RshCallback? (pre, ast: AST): boolean;
70        Rs2Callback? (pre, ast: AST): boolean;
71        NotCallback? (pre, ast: AST): boolean;
72        LogNotCallback? (pre, ast: AST): boolean;
73        IncPreCallback? (pre, ast: AST): boolean;
74        DecPreCallback? (pre, ast: AST): boolean;
75        IncPostCallback? (pre, ast: AST): boolean;
76        DecPostCallback? (pre, ast: AST): boolean;
77        TypeAssertionCallback? (pre, ast: AST): boolean;
78        FuncDeclCallback? (pre, funcDecl: FuncDecl): boolean;
79        MemberCallback? (pre, ast: AST): boolean;
80        VarDeclCallback? (pre, varDecl: VarDecl): boolean;
81        ArgDeclCallback? (pre, ast: AST): boolean;
82        ReturnCallback? (pre, ast: AST): boolean;
83        BreakCallback? (pre, ast: AST): boolean;
84        ContinueCallback? (pre, ast: AST): boolean;
85        ThrowCallback? (pre, ast: AST): boolean;
86        ForCallback? (pre, ast: AST): boolean;
87        ForInCallback? (pre, ast: AST): boolean;
88        IfCallback? (pre, ast: AST): boolean;
89        WhileCallback? (pre, ast: AST): boolean;
90        DoWhileCallback? (pre, ast: AST): boolean;
91        BlockCallback? (pre, block: Block): boolean;
92        CaseCallback? (pre, ast: AST): boolean;
93        SwitchCallback? (pre, ast: AST): boolean;
94        TryCallback? (pre, ast: AST): boolean;
95        TryCatchCallback? (pre, ast: AST): boolean;
96        TryFinallyCallback? (pre, ast: AST): boolean;
97        FinallyCallback? (pre, ast: AST): boolean;
98        CatchCallback? (pre, ast: AST): boolean;
99        ListCallback? (pre, astList: ASTList): boolean;
100        ScriptCallback? (pre, script: Script): boolean;
101        ClassDeclarationCallback? (pre, ast: AST): boolean;
102        InterfaceDeclarationCallback? (pre, interfaceDecl: InterfaceDeclaration): boolean;
103        ModuleDeclarationCallback? (pre, moduleDecl: ModuleDeclaration): boolean;
104        ImportDeclarationCallback? (pre, ast: AST): boolean;
105        WithCallback? (pre, ast: AST): boolean;
106        LabelCallback? (pre, labelAST: AST): boolean;
107        LabeledStatementCallback? (pre, ast: AST): boolean;
108        EBStartCallback? (pre, ast: AST): boolean;
109        GotoEBCallback? (pre, ast: AST): boolean;
110        EndCodeCallback? (pre, ast: AST): boolean;
111        ErrorCallback? (pre, ast: AST): boolean;
112        CommentCallback? (pre, ast: AST): boolean;
113        DebuggerCallback? (pre, ast: AST): boolean;
114        DefaultCallback? (pre, ast: AST): boolean;
115    }
116
117    export function walk(script: Script, callback: AstWalkerDetailCallback): void {
118        var pre = (cur: AST, parent: AST) => {
119            walker.options.goChildren = AstWalkerCallback(true, cur, callback);
120            return cur;
121        }
122
123        var post = (cur: AST, parent: AST) => {
124            AstWalkerCallback(false, cur, callback);
125            return cur;
126        }
127
128        var walker = TypeScript.getAstWalkerFactory().getWalker(pre, post);
129        walker.walk(script, null);
130    }
131
132    function AstWalkerCallback(pre: boolean, ast: AST, callback: AstWalkerDetailCallback): boolean {
133        // See if the Callback needs to be handled using specific one or default one
134        var nodeType = ast.nodeType;
135        var callbackString = (<any>NodeType)._map[nodeType] + "Callback";
136        if (callback[callbackString]) {
137            return callback[callbackString](pre, ast);
138        }
139
140        if (callback.DefaultCallback) {
141            return callback.DefaultCallback(pre, ast);
142        }
143
144        return true;
145    }
146}