Skip to main content

bynk_ts/program/
spans.rs

1use super::{TsArrowBody, TsDecl, TsExpr, TsObjectEntry, TsStmt, TsStmtKind};
2
3impl TsStmt {
4    /// This statement and every statement nested in it, with no source span. A statement lowered
5    /// from one source file that is printed into a module whose map belongs to another (a
6    /// contract attack's `requires`, from the target's source, in a test module) clears its
7    /// spans so that the map records nothing for it instead of a position in the wrong file.
8    pub fn clear_spans(&mut self) {
9        self.span = None;
10        clear_stmt_kind(&mut self.kind);
11    }
12}
13
14fn clear_stmts(stmts: &mut [TsStmt]) {
15    for s in stmts {
16        s.clear_spans();
17    }
18}
19
20fn clear_stmt_kind(kind: &mut TsStmtKind) {
21    match kind {
22        TsStmtKind::Verbatim { .. }
23        | TsStmtKind::Continue(_)
24        | TsStmtKind::Break(_)
25        | TsStmtKind::Comment(_)
26        | TsStmtKind::DocComment(_)
27        | TsStmtKind::Blank
28        | TsStmtKind::Raw(..) => {}
29        TsStmtKind::Decl(decl) => clear_decl(decl),
30        TsStmtKind::Const { init, .. } => clear_expr(init),
31        TsStmtKind::Let { init, .. } => {
32            if let Some(e) = init {
33                clear_expr(e);
34            }
35        }
36        TsStmtKind::ExprStmt(e) | TsStmtKind::Throw(e) | TsStmtKind::Increment(e) => clear_expr(e),
37        TsStmtKind::Return(e) => {
38            if let Some(e) = e {
39                clear_expr(e);
40            }
41        }
42        TsStmtKind::If {
43            cond,
44            then_branch,
45            else_branch,
46            ..
47        } => {
48            clear_expr(cond);
49            then_branch.clear_spans();
50            if let Some(e) = else_branch {
51                e.clear_spans();
52            }
53        }
54        TsStmtKind::ForOf { iter, body, .. } => {
55            clear_expr(iter);
56            body.clear_spans();
57        }
58        TsStmtKind::While { cond, body } => {
59            clear_expr(cond);
60            body.clear_spans();
61        }
62        TsStmtKind::Labelled { body, .. } => body.clear_spans(),
63        TsStmtKind::LetList(decls) => {
64            for d in decls {
65                if let Some(e) = &mut d.init {
66                    clear_expr(e);
67                }
68            }
69        }
70        TsStmtKind::For {
71            init, test, body, ..
72        } => {
73            clear_expr(init);
74            clear_expr(test);
75            body.clear_spans();
76        }
77        TsStmtKind::TryCatch {
78            try_block,
79            catch_block,
80            finally_block,
81            ..
82        } => {
83            try_block.clear_spans();
84            if let Some(c) = catch_block {
85                c.clear_spans();
86            }
87            if let Some(f) = finally_block {
88                f.clear_spans();
89            }
90        }
91        TsStmtKind::Block(stmts) | TsStmtKind::InlineBlock(stmts) => clear_stmts(stmts),
92        TsStmtKind::Assign { target, value } => {
93            clear_expr(target);
94            clear_expr(value);
95        }
96        TsStmtKind::Switch {
97            discriminant,
98            cases,
99        } => {
100            clear_expr(discriminant);
101            for case in cases {
102                case.span = None;
103                for t in &mut case.labels {
104                    clear_expr(t);
105                }
106                clear_stmts(&mut case.body);
107            }
108        }
109    }
110}
111
112fn clear_decl(decl: &mut TsDecl) {
113    match decl {
114        TsDecl::Import { .. }
115        | TsDecl::ImportNamespace { .. }
116        | TsDecl::ImportDefault { .. }
117        | TsDecl::ReExport { .. }
118        | TsDecl::ExportNames { .. }
119        | TsDecl::ReExportAll { .. }
120        | TsDecl::Interface { .. }
121        | TsDecl::TypeAlias { .. }
122        | TsDecl::DeclareConst { .. } => {}
123        TsDecl::Export(inner) => clear_decl(inner),
124        TsDecl::ConstDecl { init, .. } | TsDecl::ExportDefault(init) => clear_expr(init),
125        TsDecl::Class {
126            constructor,
127            methods,
128            end_span,
129            ..
130        } => {
131            *end_span = None;
132            if let Some(ctor) = constructor {
133                clear_stmts(&mut ctor.body);
134            }
135            for m in methods {
136                m.span = None;
137                clear_stmts(&mut m.body);
138            }
139        }
140        TsDecl::Function { body, .. } => clear_stmts(body),
141    }
142}
143
144fn clear_arrow_body(body: &mut TsArrowBody) {
145    match body {
146        TsArrowBody::Expr(e) => clear_expr(e),
147        TsArrowBody::Block(stmts) | TsArrowBody::Multiline(stmts) | TsArrowBody::Flat(stmts) => {
148            clear_stmts(stmts);
149        }
150    }
151}
152
153fn clear_expr(expr: &mut TsExpr) {
154    match expr {
155        TsExpr::Ident(_) | TsExpr::RawUsing(..) | TsExpr::VerbatimExpr(..) | TsExpr::Lit(_) => {}
156        TsExpr::Member { object, .. } | TsExpr::OptionalMember { object, .. } => clear_expr(object),
157        TsExpr::TypeApp { callee, .. } => clear_expr(callee),
158        TsExpr::Index { object, index } | TsExpr::OptionalIndex { object, index } => {
159            clear_expr(object);
160            clear_expr(index);
161        }
162        TsExpr::Arrow { body, .. } => clear_arrow_body(body),
163        TsExpr::DefaultIife { default, body, .. } => {
164            clear_expr(default);
165            clear_stmts(body);
166        }
167        TsExpr::Call { callee, args }
168        | TsExpr::New { callee, args }
169        | TsExpr::OptionalCall { callee, args } => {
170            clear_expr(callee);
171            for a in args {
172                clear_expr(a);
173            }
174        }
175        TsExpr::Object { entries, .. } => {
176            for e in entries {
177                match e {
178                    TsObjectEntry::Prop { value, .. } => clear_expr(value),
179                    TsObjectEntry::Shorthand(_) => {}
180                    TsObjectEntry::Method { body, .. } => clear_stmts(body),
181                    TsObjectEntry::Spread(e) => clear_expr(e),
182                }
183            }
184        }
185        TsExpr::Array { items, .. } | TsExpr::LinesArray(items) | TsExpr::Sequence(items) => {
186            for i in items {
187                clear_expr(i);
188            }
189        }
190        TsExpr::TemplateLit { exprs, .. } => {
191            for e in exprs {
192                clear_expr(e);
193            }
194        }
195        TsExpr::Await(e)
196        | TsExpr::AsConst(e)
197        | TsExpr::Paren(e)
198        | TsExpr::Spread(e)
199        | TsExpr::NonNull(e)
200        | TsExpr::Yield(e) => clear_expr(e),
201        TsExpr::As { expr, .. } | TsExpr::Unary { expr, .. } => clear_expr(expr),
202        TsExpr::Binary { left, right, .. } => {
203            clear_expr(left);
204            clear_expr(right);
205        }
206        TsExpr::Conditional {
207            test,
208            consequent,
209            alternate,
210        }
211        | TsExpr::MultilineConditional {
212            test,
213            consequent,
214            alternate,
215        } => {
216            clear_expr(test);
217            clear_expr(consequent);
218            clear_expr(alternate);
219        }
220        TsExpr::Assign { target, value }
221        | TsExpr::AddAssign { target, value }
222        | TsExpr::NullishAssign { target, value } => {
223            clear_expr(target);
224            clear_expr(value);
225        }
226        TsExpr::Function { body, .. } => clear_stmts(body),
227    }
228}