aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/get_source_range_snippet.ts
blob: 30d6ec802c7e1eb882cc22fc882f4199c270086a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';
import * as Parser from 'solidity-parser-antlr';

import { SingleFileSourceRange, SourceRange, SourceSnippet } from './types';
import { utils } from './utils';

interface ASTInfo {
    type: string;
    node: Parser.ASTNode;
    name: string | null;
    range?: SingleFileSourceRange;
}

// Parsing source code for each transaction/code is slow and therefore we cache it
const parsedSourceByHash: { [sourceHash: string]: Parser.ASTNode } = {};

export function getSourceRangeSnippet(sourceRange: SourceRange, sourceCode: string): SourceSnippet | null {
    const sourceHash = ethUtil.sha3(sourceCode).toString('hex');
    if (_.isUndefined(parsedSourceByHash[sourceHash])) {
        parsedSourceByHash[sourceHash] = Parser.parse(sourceCode, { loc: true });
    }
    const astNode = parsedSourceByHash[sourceHash];
    const visitor = new ASTInfoVisitor();
    Parser.visit(astNode, visitor);
    const astInfo = visitor.getASTInfoForRange(sourceRange);
    if (astInfo === null) {
        return null;
    }
    const sourceCodeInRange = utils.getRange(sourceCode, sourceRange.location);
    return {
        ...astInfo,
        range: astInfo.range as SingleFileSourceRange,
        source: sourceCodeInRange,
        fileName: sourceRange.fileName,
    };
}

// A visitor which collects ASTInfo for most nodes in the AST.
class ASTInfoVisitor {
    private _astInfos: ASTInfo[] = [];
    public getASTInfoForRange(sourceRange: SourceRange): ASTInfo | null {
        // HACK(albrow): Sometimes the source range doesn't exactly match that
        // of astInfo. To work around that we try with a +/-1 offset on
        // end.column. If nothing matches even with the offset, we return null.
        const offset = {
            start: {
                line: 0,
                column: 0,
            },
            end: {
                line: 0,
                column: 0,
            },
        };
        let astInfo = this._getASTInfoForRange(sourceRange, offset);
        if (astInfo !== null) {
            return astInfo;
        }
        offset.end.column += 1;
        astInfo = this._getASTInfoForRange(sourceRange, offset);
        if (astInfo !== null) {
            return astInfo;
        }
        offset.end.column -= 2;
        astInfo = this._getASTInfoForRange(sourceRange, offset);
        if (astInfo !== null) {
            return astInfo;
        }
        return null;
    }
    public ContractDefinition(ast: Parser.ContractDefinition): void {
        this._visitContractDefinition(ast);
    }
    public IfStatement(ast: Parser.IfStatement): void {
        this._visitStatement(ast);
    }
    public FunctionDefinition(ast: Parser.FunctionDefinition): void {
        this._visitFunctionLikeDefinition(ast);
    }
    public ModifierDefinition(ast: Parser.ModifierDefinition): void {
        this._visitFunctionLikeDefinition(ast);
    }
    public ForStatement(ast: Parser.ForStatement): void {
        this._visitStatement(ast);
    }
    public ReturnStatement(ast: Parser.ReturnStatement): void {
        this._visitStatement(ast);
    }
    public BreakStatement(ast: Parser.BreakStatement): void {
        this._visitStatement(ast);
    }
    public ContinueStatement(ast: Parser.ContinueStatement): void {
        this._visitStatement(ast);
    }
    public EmitStatement(ast: any /* TODO: Parser.EmitStatement */): void {
        this._visitStatement(ast);
    }
    public VariableDeclarationStatement(ast: Parser.VariableDeclarationStatement): void {
        this._visitStatement(ast);
    }
    public Statement(ast: Parser.Statement): void {
        this._visitStatement(ast);
    }
    public WhileStatement(ast: Parser.WhileStatement): void {
        this._visitStatement(ast);
    }
    public SimpleStatement(ast: Parser.SimpleStatement): void {
        this._visitStatement(ast);
    }
    public ThrowStatement(ast: Parser.ThrowStatement): void {
        this._visitStatement(ast);
    }
    public DoWhileStatement(ast: Parser.DoWhileStatement): void {
        this._visitStatement(ast);
    }
    public ExpressionStatement(ast: Parser.ExpressionStatement): void {
        this._visitStatement(ast.expression);
    }
    public InlineAssemblyStatement(ast: Parser.InlineAssemblyStatement): void {
        this._visitStatement(ast);
    }
    public ModifierInvocation(ast: Parser.ModifierInvocation): void {
        const BUILTIN_MODIFIERS = ['public', 'view', 'payable', 'external', 'internal', 'pure', 'constant'];
        if (!_.includes(BUILTIN_MODIFIERS, ast.name)) {
            this._visitStatement(ast);
        }
    }
    private _visitStatement(ast: Parser.ASTNode): void {
        this._astInfos.push({
            type: ast.type,
            node: ast,
            name: null,
            range: ast.loc,
        });
    }
    private _visitFunctionLikeDefinition(ast: Parser.ModifierDefinition | Parser.FunctionDefinition): void {
        this._astInfos.push({
            type: ast.type,
            node: ast,
            name: ast.name,
            range: ast.loc,
        });
    }
    private _visitContractDefinition(ast: Parser.ContractDefinition): void {
        this._astInfos.push({
            type: ast.type,
            node: ast,
            name: ast.name,
            range: ast.loc,
        });
    }
    private _getASTInfoForRange(sourceRange: SourceRange, offset: SingleFileSourceRange): ASTInfo | null {
        const offsetSourceRange = {
            ...sourceRange,
            location: {
                start: {
                    line: sourceRange.location.start.line + offset.start.line,
                    column: sourceRange.location.start.column + offset.start.column,
                },
                end: {
                    line: sourceRange.location.end.line + offset.end.line,
                    column: sourceRange.location.end.column + offset.end.column,
                },
            },
        };
        for (const astInfo of this._astInfos) {
            const astInfoRange = astInfo.range as SingleFileSourceRange;
            if (
                astInfoRange.start.column === offsetSourceRange.location.start.column &&
                astInfoRange.start.line === offsetSourceRange.location.start.line &&
                astInfoRange.end.column === offsetSourceRange.location.end.column &&
                astInfoRange.end.line === offsetSourceRange.location.end.line
            ) {
                return astInfo;
            }
        }
        return null;
    }
}