@samitouri / QOS-React-1 / commits / dafa468765

[rust][sema] Allow defining/resolving known globals

Adds an option to pass a list of known globals into the semantic analyzer so that references to globals can be checked. As a follow-up we'll need to distinguish between different types of semantic analysis errors, so that callers which don't want to validate globals can ignore "unknown variable" reference errors while still handling definite errors such as duplicate declarations.

Joe Savona committed Aug 16, 2023 at 16:44 UTC dafa4687659d6ae33a90b80ba5e2e92cb73a077d
15 files changed +2188 -1629
compiler/forget/crates/forget_fixtures/tests/fixtures_test.rs
+1 -1
@@ -21,7 +21,7 @@ fn fixtures() {
21
22 let mut output = String::new();
23
24 - let mut analysis = analyze(&ast);
24 + let mut analysis = analyze(&ast, Default::default());
25 let diagnostics = analysis.diagnostics();
26 if !diagnostics.is_empty() {
27 for diagnostic in diagnostics {
compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs
+9 -4
@@ -11,12 +11,17 @@ use crate::{
11 ScopeKind, ScopeManager,
12 };
13
14 -pub fn analyze(ast: &Program) -> ScopeManager {
15 - let mut analyzer = Analyzer::new(ast);
14 +pub fn analyze(ast: &Program, options: AnalyzeOptions) -> ScopeManager {
15 + let mut analyzer = Analyzer::new(ast, options);
16 analyzer.visit_program(ast);
17 analyzer.complete()
18 }
19
20 +#[derive(Debug, Default)]
21 +pub struct AnalyzeOptions {
22 + pub globals: Vec<String>,
23 +}
24 +
25 struct Analyzer {
26 manager: ScopeManager,
27 labels: Vec<LabelId>,
@@ -39,8 +44,8 @@ pub struct UnresolvedReference {
44 }
45
46 impl Analyzer {
42 - fn new(program: &Program) -> Self {
43 - let manager = ScopeManager::new(program.source_type);
47 + fn new(program: &Program, options: AnalyzeOptions) -> Self {
48 + let manager = ScopeManager::new(program.source_type, options.globals);
49 let current = manager.root_id();
50 let labels = Default::default();
51 Self {
compiler/forget/crates/forget_semantic_analysis/src/lib.rs
+1 -1
@@ -2,6 +2,6 @@ mod analyzer;
2 mod scope_manager;
3 mod scope_view;
4
5 -pub use analyzer::analyze;
5 +pub use analyzer::*;
6 pub use scope_manager::*;
7 pub use scope_view::*;
compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs
+37 -10
@@ -7,9 +7,11 @@ use forget_utils::PointerAddress;
7 use indexmap::IndexMap;
8
9 use crate::scope_view::{DeclarationView, ReferenceView, ScopeView};
10 +use crate::ScopeManagerView;
11
12 pub struct ScopeManager {
13 root: ScopeId,
14 + globals: IndexMap<String, DeclarationId>,
15
16 // Storage of the semantic information
17 scopes: Vec<Scope>,
@@ -33,14 +35,15 @@ impl std::fmt::Debug for ScopeManager {
35 }
36
37 impl ScopeManager {
36 - pub(crate) fn new(source_type: SourceType) -> Self {
38 + pub(crate) fn new(source_type: SourceType, globals: Vec<String>) -> Self {
39 let root_id = ScopeId(0);
40 let root_kind = match source_type {
41 SourceType::Module => ScopeKind::Module,
42 SourceType::Script => ScopeKind::Global,
43 };
42 - Self {
44 + let mut manager = Self {
45 root: root_id,
46 + globals: IndexMap::with_capacity(globals.len()),
47 scopes: vec![Scope {
48 id: root_id,
49 kind: root_kind,
@@ -57,21 +60,33 @@ impl ScopeManager {
60 node_declarations: Default::default(),
61 node_references: Default::default(),
62 diagnostics: Default::default(),
63 + };
64 + for global in globals {
65 + let id = DeclarationId(manager.declarations.len());
66 + manager.globals.insert(global.clone(), id);
67 + manager.declarations.push(Declaration {
68 + id,
69 + kind: DeclarationKind::Global,
70 + name: global,
71 + scope: manager.root,
72 + });
73 }
74 +
75 + manager
76 }
77
63 - pub fn debug(&self) -> ScopeView<'_> {
64 - let root = self.root();
65 - ScopeView {
66 - manager: self,
67 - scope: root,
68 - }
78 + pub fn debug(&self) -> ScopeManagerView<'_> {
79 + ScopeManagerView { manager: self }
80 }
81
82 pub fn diagnostics(&mut self) -> Vec<Diagnostic> {
83 std::mem::take(&mut self.diagnostics)
84 }
85
86 + pub fn globals(&self) -> impl Iterator<Item = (&String, &DeclarationId)> {
87 + self.globals.iter()
88 + }
89 +
90 pub fn root(&self) -> &Scope {
91 &self.scopes[self.root.0]
92 }
@@ -198,7 +213,7 @@ impl ScopeManager {
213 return None;
214 }
215 }
201 - return Some(&self.declarations[id.0]);
216 + return Some(declaration);
217 }
218 if let Some(parent) = current.parent {
219 // When leaving a function scope, clear the tdz limit.
@@ -210,6 +225,11 @@ impl ScopeManager {
225 }
226 current = &self.scopes[parent.0];
227 } else {
228 + // Maybe it's a global!
229 + if let Some(id) = self.globals.get(name) {
230 + let declaration = self.declaration(*id);
231 + return Some(declaration);
232 + }
233 return None;
234 }
235 }
@@ -322,6 +342,9 @@ impl ScopeManager {
342 .push(Diagnostic::invalid_syntax("Duplicate declaration", range));
343 }
344 }
345 + DeclarationKind::Global => {
346 + unreachable!("Unexpected explicit declaration of global")
347 + }
348 }
349
350 // Always create a new declaration and id...
@@ -369,6 +392,9 @@ impl ScopeManager {
392 }
393 }
394 }
395 + DeclarationKind::Global => {
396 + unreachable!("Unexpected explicit declaration of global")
397 + }
398 }
399 }
400
@@ -402,7 +428,7 @@ fn is_block_scoped_declaration(kind: DeclarationKind) -> bool {
428 | DeclarationKind::Class
429 | DeclarationKind::Function
430 | DeclarationKind::CatchClause => true,
405 - DeclarationKind::Var => false,
431 + DeclarationKind::Var | DeclarationKind::Global => false,
432 }
433 }
434
@@ -457,6 +483,7 @@ pub struct Label {
483
484 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)]
485 pub enum DeclarationKind {
486 + Global,
487 Class,
488 Const,
489 Var,
compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs
+26
@@ -5,6 +5,32 @@ use crate::{
5 Scope, ScopeId, ScopeKind, ScopeManager,
6 };
7
8 +#[derive(Clone, Copy)]
9 +pub struct ScopeManagerView<'m> {
10 + pub(crate) manager: &'m ScopeManager,
11 +}
12 +
13 +impl<'m> ScopeManagerView<'m> {
14 + pub fn root(&self) -> ScopeView<'m> {
15 + ScopeView {
16 + manager: &self.manager,
17 + scope: self.manager.scope(self.manager.root_id()),
18 + }
19 + }
20 +
21 + pub fn globals(&self) -> impl Iterator<Item = (&String, &DeclarationId)> {
22 + self.manager.globals()
23 + }
24 +}
25 +
26 +impl<'m> std::fmt::Debug for ScopeManagerView<'m> {
27 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 + f.debug_struct("ScopeManager")
29 + .field("globals", &self.globals().collect::<Vec<_>>())
30 + .field("root", &self.root())
31 + .finish()
32 + }
33 +}
34 #[derive(Clone, Copy)]
35 pub struct ScopeView<'m> {
36 pub(crate) manager: &'m ScopeManager,
compiler/forget/crates/forget_semantic_analysis/tests/analysis_test.rs
+17 -2
@@ -1,7 +1,7 @@
1 use std::fmt::Write;
2
3 use forget_hermes_parser::parse;
4 -use forget_semantic_analysis::analyze;
4 +use forget_semantic_analysis::{analyze, AnalyzeOptions};
5 use insta::{assert_snapshot, glob};
6 use miette::{NamedSource, Report};
7
@@ -11,7 +11,22 @@ fn fixtures() {
11 println!("fixture {}", path.to_str().unwrap());
12 let input = std::fs::read_to_string(path).unwrap();
13 let ast = parse(&input, path.to_str().unwrap()).unwrap();
14 - let mut analysis = analyze(&ast);
14 + let mut analysis = analyze(
15 + &ast,
16 + AnalyzeOptions {
17 + globals: vec![
18 + "Array".to_string(),
19 + "Boolean".to_string(),
20 + "console".to_string(),
21 + "global".to_string(),
22 + "Math".to_string(),
23 + "Number".to_string(),
24 + "setInterval".to_string(),
25 + "setTimeout".to_string(),
26 + "String".to_string(),
27 + ],
28 + },
29 + );
30
31 let mut output = String::new();
32 writeln!(&mut output, "{:#?}", analysis.debug()).unwrap();
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@block-item-duplication.js.snap
+207 -149
@@ -32,166 +32,224 @@ const x = false; // error
32
33
34 Analysis:
35 -Scope {
36 - id: ScopeId(
37 - 0,
38 - ),
39 - kind: Module,
40 - declarations: {
41 - "Component": Declaration {
42 - id: DeclarationId(
35 +ScopeManager {
36 + globals: [
37 + (
38 + "Array",
39 + DeclarationId(
40 0,
41 ),
45 - kind: Function,
46 - scope: ScopeId(
47 - 0,
42 + ),
43 + (
44 + "Boolean",
45 + DeclarationId(
46 + 1,
47 ),
49 - },
50 - "x": Declaration {
51 - id: DeclarationId(
52 - 12,
48 + ),
49 + (
50 + "console",
51 + DeclarationId(
52 + 2,
53 ),
54 - kind: Const,
55 - scope: ScopeId(
56 - 0,
54 + ),
55 + (
56 + "global",
57 + DeclarationId(
58 + 3,
59 ),
58 - },
59 - },
60 - references: [],
61 - children: [
62 - Scope {
63 - id: ScopeId(
64 - 1,
60 + ),
61 + (
62 + "Math",
63 + DeclarationId(
64 + 4,
65 ),
66 - kind: Function,
67 - declarations: {
68 - "a": Declaration {
69 - id: DeclarationId(
70 - 1,
71 - ),
72 - kind: Let,
73 - scope: ScopeId(
74 - 1,
75 - ),
76 - },
77 - "b": Declaration {
78 - id: DeclarationId(
79 - 3,
80 - ),
81 - kind: Const,
82 - scope: ScopeId(
83 - 1,
84 - ),
85 - },
86 - "foo": Declaration {
87 - id: DeclarationId(
88 - 5,
89 - ),
90 - kind: Function,
91 - scope: ScopeId(
92 - 1,
93 - ),
94 - },
95 - "c": Declaration {
96 - id: DeclarationId(
97 - 10,
98 - ),
99 - kind: Function,
100 - scope: ScopeId(
101 - 1,
102 - ),
103 - },
66 + ),
67 + (
68 + "Number",
69 + DeclarationId(
70 + 5,
71 + ),
72 + ),
73 + (
74 + "setInterval",
75 + DeclarationId(
76 + 6,
77 + ),
78 + ),
79 + (
80 + "setTimeout",
81 + DeclarationId(
82 + 7,
83 + ),
84 + ),
85 + (
86 + "String",
87 + DeclarationId(
88 + 8,
89 + ),
90 + ),
91 + ],
92 + root: Scope {
93 + id: ScopeId(
94 + 0,
95 + ),
96 + kind: Module,
97 + declarations: {
98 + "Component": Declaration {
99 + id: DeclarationId(
100 + 9,
101 + ),
102 + kind: Function,
103 + scope: ScopeId(
104 + 0,
105 + ),
106 },
105 - references: [],
106 - children: [
107 - Scope {
108 - id: ScopeId(
109 - 2,
110 - ),
111 - kind: Function,
112 - declarations: {},
113 - references: [],
114 - children: [],
115 - },
116 - Scope {
117 - id: ScopeId(
118 - 3,
119 - ),
120 - kind: Function,
121 - declarations: {},
122 - references: [],
123 - children: [],
124 - },
125 - Scope {
126 - id: ScopeId(
127 - 4,
128 - ),
129 - kind: Block,
130 - declarations: {},
131 - references: [],
132 - children: [],
107 + "x": Declaration {
108 + id: DeclarationId(
109 + 21,
110 + ),
111 + kind: Const,
112 + scope: ScopeId(
113 + 0,
114 + ),
115 + },
116 + },
117 + references: [],
118 + children: [
119 + Scope {
120 + id: ScopeId(
121 + 1,
122 + ),
123 + kind: Function,
124 + declarations: {
125 + "a": Declaration {
126 + id: DeclarationId(
127 + 10,
128 + ),
129 + kind: Let,
130 + scope: ScopeId(
131 + 1,
132 + ),
133 + },
134 + "b": Declaration {
135 + id: DeclarationId(
136 + 12,
137 + ),
138 + kind: Const,
139 + scope: ScopeId(
140 + 1,
141 + ),
142 + },
143 + "foo": Declaration {
144 + id: DeclarationId(
145 + 14,
146 + ),
147 + kind: Function,
148 + scope: ScopeId(
149 + 1,
150 + ),
151 + },
152 + "c": Declaration {
153 + id: DeclarationId(
154 + 19,
155 + ),
156 + kind: Function,
157 + scope: ScopeId(
158 + 1,
159 + ),
160 + },
161 },
134 - Scope {
135 - id: ScopeId(
136 - 5,
137 - ),
138 - kind: CatchClause,
139 - declarations: {
140 - "c": Declaration {
141 - id: DeclarationId(
142 - 7,
143 - ),
144 - kind: CatchClause,
145 - scope: ScopeId(
146 - 5,
147 - ),
148 - },
162 + references: [],
163 + children: [
164 + Scope {
165 + id: ScopeId(
166 + 2,
167 + ),
168 + kind: Function,
169 + declarations: {},
170 + references: [],
171 + children: [],
172 },
150 - references: [],
151 - children: [
152 - Scope {
153 - id: ScopeId(
154 - 6,
155 - ),
156 - kind: Block,
157 - declarations: {
158 - "c": Declaration {
159 - id: DeclarationId(
160 - 8,
161 - ),
162 - kind: Let,
163 - scope: ScopeId(
164 - 6,
165 - ),
166 - },
173 + Scope {
174 + id: ScopeId(
175 + 3,
176 + ),
177 + kind: Function,
178 + declarations: {},
179 + references: [],
180 + children: [],
181 + },
182 + Scope {
183 + id: ScopeId(
184 + 4,
185 + ),
186 + kind: Block,
187 + declarations: {},
188 + references: [],
189 + children: [],
190 + },
191 + Scope {
192 + id: ScopeId(
193 + 5,
194 + ),
195 + kind: CatchClause,
196 + declarations: {
197 + "c": Declaration {
198 + id: DeclarationId(
199 + 16,
200 + ),
201 + kind: CatchClause,
202 + scope: ScopeId(
203 + 5,
204 + ),
205 },
168 - references: [],
169 - children: [
170 - Scope {
171 - id: ScopeId(
172 - 7,
173 - ),
174 - kind: Function,
175 - declarations: {},
176 - references: [],
177 - children: [],
178 - },
179 - ],
206 },
181 - ],
182 - },
183 - ],
184 - },
185 - Scope {
186 - id: ScopeId(
187 - 8,
188 - ),
189 - kind: Function,
190 - declarations: {},
191 - references: [],
192 - children: [],
193 - },
194 - ],
207 + references: [],
208 + children: [
209 + Scope {
210 + id: ScopeId(
211 + 6,
212 + ),
213 + kind: Block,
214 + declarations: {
215 + "c": Declaration {
216 + id: DeclarationId(
217 + 17,
218 + ),
219 + kind: Let,
220 + scope: ScopeId(
221 + 6,
222 + ),
223 + },
224 + },
225 + references: [],
226 + children: [
227 + Scope {
228 + id: ScopeId(
229 + 7,
230 + ),
231 + kind: Function,
232 + declarations: {},
233 + references: [],
234 + children: [],
235 + },
236 + ],
237 + },
238 + ],
239 + },
240 + ],
241 + },
242 + Scope {
243 + id: ScopeId(
244 + 8,
245 + ),
246 + kind: Function,
247 + declarations: {},
248 + references: [],
249 + children: [],
250 + },
251 + ],
252 + },
253 }
254 Diagnostic(
255 DiagnosticData {
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@function-hoisting.js.snap
+247 -189
@@ -29,204 +29,262 @@ function Component(props) {
29
30
31 Analysis:
32 -Scope {
33 - id: ScopeId(
34 - 0,
35 - ),
36 - kind: Module,
37 - declarations: {
38 - "Component": Declaration {
39 - id: DeclarationId(
32 +ScopeManager {
33 + globals: [
34 + (
35 + "Array",
36 + DeclarationId(
37 0,
38 ),
42 - kind: Function,
43 - scope: ScopeId(
44 - 0,
45 - ),
46 - },
47 - },
48 - references: [],
49 - children: [
50 - Scope {
51 - id: ScopeId(
39 + ),
40 + (
41 + "Boolean",
42 + DeclarationId(
43 1,
44 ),
54 - kind: Function,
55 - declarations: {
56 - "props": Declaration {
57 - id: DeclarationId(
58 - 1,
59 - ),
60 - kind: Function,
61 - scope: ScopeId(
62 - 1,
63 - ),
64 - },
65 - "foo": Declaration {
66 - id: DeclarationId(
67 - 2,
68 - ),
69 - kind: Function,
70 - scope: ScopeId(
71 - 1,
72 - ),
73 - },
45 + ),
46 + (
47 + "console",
48 + DeclarationId(
49 + 2,
50 + ),
51 + ),
52 + (
53 + "global",
54 + DeclarationId(
55 + 3,
56 + ),
57 + ),
58 + (
59 + "Math",
60 + DeclarationId(
61 + 4,
62 + ),
63 + ),
64 + (
65 + "Number",
66 + DeclarationId(
67 + 5,
68 + ),
69 + ),
70 + (
71 + "setInterval",
72 + DeclarationId(
73 + 6,
74 + ),
75 + ),
76 + (
77 + "setTimeout",
78 + DeclarationId(
79 + 7,
80 + ),
81 + ),
82 + (
83 + "String",
84 + DeclarationId(
85 + 8,
86 + ),
87 + ),
88 + ],
89 + root: Scope {
90 + id: ScopeId(
91 + 0,
92 + ),
93 + kind: Module,
94 + declarations: {
95 + "Component": Declaration {
96 + id: DeclarationId(
97 + 9,
98 + ),
99 + kind: Function,
100 + scope: ScopeId(
101 + 0,
102 + ),
103 },
75 - references: [
76 - Reference {
77 - id: ReferenceId(
78 - 0,
79 - ),
80 - kind: Read,
81 - declaration: DeclarationId(
82 - 2,
83 - ),
84 - declaration (name): "foo",
85 - scope: ScopeId(
86 - 1,
87 - ),
88 - },
89 - Reference {
90 - id: ReferenceId(
91 - 1,
92 - ),
93 - kind: Read,
94 - declaration: DeclarationId(
95 - 1,
96 - ),
97 - declaration (name): "props",
98 - scope: ScopeId(
99 - 1,
100 - ),
104 + },
105 + references: [],
106 + children: [
107 + Scope {
108 + id: ScopeId(
109 + 1,
110 + ),
111 + kind: Function,
112 + declarations: {
113 + "props": Declaration {
114 + id: DeclarationId(
115 + 10,
116 + ),
117 + kind: Function,
118 + scope: ScopeId(
119 + 1,
120 + ),
121 + },
122 + "foo": Declaration {
123 + id: DeclarationId(
124 + 11,
125 + ),
126 + kind: Function,
127 + scope: ScopeId(
128 + 1,
129 + ),
130 + },
131 },
102 - ],
103 - children: [
104 - Scope {
105 - id: ScopeId(
106 - 2,
107 - ),
108 - kind: Block,
109 - declarations: {},
110 - references: [],
111 - children: [
112 - Scope {
113 - id: ScopeId(
114 - 3,
115 - ),
116 - kind: Block,
117 - declarations: {},
118 - references: [],
119 - children: [
120 - Scope {
121 - id: ScopeId(
122 - 4,
123 - ),
124 - kind: Block,
125 - declarations: {},
126 - references: [],
127 - children: [
128 - Scope {
129 - id: ScopeId(
130 - 5,
131 - ),
132 - kind: Block,
133 - declarations: {},
134 - references: [],
135 - children: [
136 - Scope {
137 - id: ScopeId(
138 - 6,
139 - ),
140 - kind: Block,
141 - declarations: {},
142 - references: [],
143 - children: [
144 - Scope {
145 - id: ScopeId(
146 - 7,
147 - ),
148 - kind: Function,
149 - declarations: {},
150 - references: [
151 - Reference {
152 - id: ReferenceId(
153 - 2,
154 - ),
155 - kind: Read,
156 - declaration: DeclarationId(
157 - 1,
158 - ),
159 - declaration (name): "props",
160 - scope: ScopeId(
161 - 7,
162 - ),
163 - },
164 - ],
165 - children: [
166 - Scope {
167 - id: ScopeId(
168 - 8,
169 - ),
170 - kind: Block,
171 - declarations: {
172 - "_": Declaration {
173 - id: DeclarationId(
174 - 3,
175 - ),
176 - kind: Const,
177 - scope: ScopeId(
178 - 8,
179 - ),
180 - },
132 + references: [
133 + Reference {
134 + id: ReferenceId(
135 + 0,
136 + ),
137 + kind: Read,
138 + declaration: DeclarationId(
139 + 11,
140 + ),
141 + declaration (name): "foo",
142 + scope: ScopeId(
143 + 1,
144 + ),
145 + },
146 + Reference {
147 + id: ReferenceId(
148 + 1,
149 + ),
150 + kind: Read,
151 + declaration: DeclarationId(
152 + 10,
153 + ),
154 + declaration (name): "props",
155 + scope: ScopeId(
156 + 1,
157 + ),
158 + },
159 + ],
160 + children: [
161 + Scope {
162 + id: ScopeId(
163 + 2,
164 + ),
165 + kind: Block,
166 + declarations: {},
167 + references: [],
168 + children: [
169 + Scope {
170 + id: ScopeId(
171 + 3,
172 + ),
173 + kind: Block,
174 + declarations: {},
175 + references: [],
176 + children: [
177 + Scope {
178 + id: ScopeId(
179 + 4,
180 + ),
181 + kind: Block,
182 + declarations: {},
183 + references: [],
184 + children: [
185 + Scope {
186 + id: ScopeId(
187 + 5,
188 + ),
189 + kind: Block,
190 + declarations: {},
191 + references: [],
192 + children: [
193 + Scope {
194 + id: ScopeId(
195 + 6,
196 + ),
197 + kind: Block,
198 + declarations: {},
199 + references: [],
200 + children: [
201 + Scope {
202 + id: ScopeId(
203 + 7,
204 + ),
205 + kind: Function,
206 + declarations: {},
207 + references: [
208 + Reference {
209 + id: ReferenceId(
210 + 2,
211 + ),
212 + kind: Read,
213 + declaration: DeclarationId(
214 + 10,
215 + ),
216 + declaration (name): "props",
217 + scope: ScopeId(
218 + 7,
219 + ),
220 },
182 - references: [],
183 - children: [
184 - Scope {
185 - id: ScopeId(
186 - 9,
187 - ),
188 - kind: Function,
189 - declarations: {
190 - "bar": Declaration {
191 - id: DeclarationId(
192 - 4,
193 - ),
194 - kind: Function,
195 - scope: ScopeId(
196 - 9,
197 - ),
198 - },
221 + ],
222 + children: [
223 + Scope {
224 + id: ScopeId(
225 + 8,
226 + ),
227 + kind: Block,
228 + declarations: {
229 + "_": Declaration {
230 + id: DeclarationId(
231 + 12,
232 + ),
233 + kind: Const,
234 + scope: ScopeId(
235 + 8,
236 + ),
237 },
200 - references: [],
201 - children: [
202 - Scope {
203 - id: ScopeId(
204 - 10,
205 - ),
206 - kind: Function,
207 - declarations: {},
208 - references: [],
209 - children: [],
210 - },
211 - ],
238 },
213 - ],
214 - },
215 - ],
216 - },
217 - ],
218 - },
219 - ],
220 - },
221 - ],
222 - },
223 - ],
224 - },
225 - ],
226 - },
227 - ],
228 - },
229 - ],
239 + references: [],
240 + children: [
241 + Scope {
242 + id: ScopeId(
243 + 9,
244 + ),
245 + kind: Function,
246 + declarations: {
247 + "bar": Declaration {
248 + id: DeclarationId(
249 + 13,
250 + ),
251 + kind: Function,
252 + scope: ScopeId(
253 + 9,
254 + ),
255 + },
256 + },
257 + references: [],
258 + children: [
259 + Scope {
260 + id: ScopeId(
261 + 10,
262 + ),
263 + kind: Function,
264 + declarations: {},
265 + references: [],
266 + children: [],
267 + },
268 + ],
269 + },
270 + ],
271 + },
272 + ],
273 + },
274 + ],
275 + },
276 + ],
277 + },
278 + ],
279 + },
280 + ],
281 + },
282 + ],
283 + },
284 + ],
285 + },
286 + ],
287 + },
288 }
289 Diagnostic(
290 DiagnosticData {
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@globals-and-imports.js.snap
+355 -321
@@ -24,350 +24,384 @@ function Component(props) {
24
25
26 Analysis:
27 -Scope {
28 - id: ScopeId(
29 - 0,
30 - ),
31 - kind: Module,
32 - declarations: {
33 - "Foo": Declaration {
34 - id: DeclarationId(
27 +ScopeManager {
28 + globals: [
29 + (
30 + "Array",
31 + DeclarationId(
32 0,
33 ),
37 - kind: Import,
38 - scope: ScopeId(
39 - 0,
40 - ),
41 - },
42 - "Bar": Declaration {
43 - id: DeclarationId(
34 + ),
35 + (
36 + "Boolean",
37 + DeclarationId(
38 1,
39 ),
46 - kind: Import,
47 - scope: ScopeId(
48 - 0,
49 - ),
50 - },
51 - "Baz": Declaration {
52 - id: DeclarationId(
40 + ),
41 + (
42 + "console",
43 + DeclarationId(
44 2,
45 ),
55 - kind: Import,
56 - scope: ScopeId(
57 - 0,
58 - ),
59 - },
60 - "Component": Declaration {
61 - id: DeclarationId(
46 + ),
47 + (
48 + "global",
49 + DeclarationId(
50 3,
51 ),
64 - kind: Function,
65 - scope: ScopeId(
66 - 0,
52 + ),
53 + (
54 + "Math",
55 + DeclarationId(
56 + 4,
57 ),
68 - },
69 - },
70 - references: [],
71 - children: [
72 - Scope {
73 - id: ScopeId(
74 - 1,
58 + ),
59 + (
60 + "Number",
61 + DeclarationId(
62 + 5,
63 ),
76 - kind: Function,
77 - declarations: {
78 - "props": Declaration {
79 - id: DeclarationId(
80 - 4,
81 - ),
82 - kind: Function,
83 - scope: ScopeId(
84 - 1,
85 - ),
86 - },
87 - "g": Declaration {
88 - id: DeclarationId(
89 - 5,
90 - ),
91 - kind: Let,
92 - scope: ScopeId(
93 - 1,
94 - ),
95 - },
96 - "y": Declaration {
97 - id: DeclarationId(
98 - 6,
99 - ),
100 - kind: Let,
101 - scope: ScopeId(
102 - 1,
103 - ),
104 - },
105 - "s": Declaration {
106 - id: DeclarationId(
107 - 7,
108 - ),
109 - kind: Let,
110 - scope: ScopeId(
111 - 1,
112 - ),
113 - },
114 - "b": Declaration {
115 - id: DeclarationId(
116 - 8,
117 - ),
118 - kind: Let,
119 - scope: ScopeId(
120 - 1,
121 - ),
122 - },
123 - "n": Declaration {
124 - id: DeclarationId(
125 - 9,
126 - ),
127 - kind: Let,
128 - scope: ScopeId(
129 - 1,
130 - ),
131 - },
132 - "x": Declaration {
133 - id: DeclarationId(
134 - 10,
135 - ),
136 - kind: Let,
137 - scope: ScopeId(
138 - 1,
139 - ),
140 - },
141 - },
142 - references: [
143 - Reference {
144 - id: ReferenceId(
145 - 0,
146 - ),
147 - kind: Read,
148 - declaration: DeclarationId(
149 - 4,
150 - ),
151 - declaration (name): "props",
152 - scope: ScopeId(
153 - 1,
154 - ),
155 - },
156 - Reference {
157 - id: ReferenceId(
158 - 1,
159 - ),
160 - kind: Read,
161 - declaration: DeclarationId(
162 - 4,
163 - ),
164 - declaration (name): "props",
165 - scope: ScopeId(
166 - 1,
167 - ),
168 - },
169 - Reference {
170 - id: ReferenceId(
171 - 2,
172 - ),
173 - kind: Read,
174 - declaration: DeclarationId(
175 - 4,
176 - ),
177 - declaration (name): "props",
178 - scope: ScopeId(
179 - 1,
180 - ),
181 - },
182 - Reference {
183 - id: ReferenceId(
184 - 3,
185 - ),
186 - kind: Read,
187 - declaration: DeclarationId(
188 - 0,
189 - ),
190 - declaration (name): "Foo",
191 - scope: ScopeId(
192 - 1,
193 - ),
194 - },
195 - Reference {
196 - id: ReferenceId(
197 - 4,
198 - ),
199 - kind: Read,
200 - declaration: DeclarationId(
201 - 1,
202 - ),
203 - declaration (name): "Bar",
204 - scope: ScopeId(
205 - 1,
206 - ),
207 - },
208 - Reference {
209 - id: ReferenceId(
210 - 5,
211 - ),
212 - kind: Read,
213 - declaration: DeclarationId(
214 - 2,
215 - ),
216 - declaration (name): "Baz",
217 - scope: ScopeId(
218 - 1,
219 - ),
220 - },
221 - ],
222 - children: [
223 - Scope {
224 - id: ScopeId(
225 - 2,
226 - ),
227 - kind: Function,
228 - declarations: {},
229 - references: [],
230 - children: [],
231 - },
232 - Scope {
233 - id: ScopeId(
234 - 3,
235 - ),
236 - kind: Function,
237 - declarations: {},
238 - references: [],
239 - children: [],
240 - },
241 - ],
242 - },
64 + ),
65 + (
66 + "setInterval",
67 + DeclarationId(
68 + 6,
69 + ),
70 + ),
71 + (
72 + "setTimeout",
73 + DeclarationId(
74 + 7,
75 + ),
76 + ),
77 + (
78 + "String",
79 + DeclarationId(
80 + 8,
81 + ),
82 + ),
83 ],
244 -}
245 -Diagnostic(
246 - DiagnosticData {
247 - message: "Undefined variable",
248 - span: Some(
249 - SourceSpan {
250 - offset: SourceOffset(
251 - 117,
252 - ),
253 - length: 6,
254 - },
84 + root: Scope {
85 + id: ScopeId(
86 + 0,
87 ),
256 - related_information: [],
257 - severity: InvalidSyntax,
258 - data: [],
259 - },
260 -)
261 -Diagnostic(
262 - DiagnosticData {
263 - message: "Undefined variable",
264 - span: Some(
265 - SourceSpan {
266 - offset: SourceOffset(
267 - 139,
88 + kind: Module,
89 + declarations: {
90 + "Foo": Declaration {
91 + id: DeclarationId(
92 + 9,
93 ),
269 - length: 5,
270 - },
271 - ),
272 - related_information: [],
273 - severity: InvalidSyntax,
274 - data: [],
275 - },
276 -)
277 -Diagnostic(
278 - DiagnosticData {
279 - message: "Undefined variable",
280 - span: Some(
281 - SourceSpan {
282 - offset: SourceOffset(
283 - 169,
94 + kind: Import,
95 + scope: ScopeId(
96 + 0,
97 ),
285 - length: 6,
98 },
287 - ),
288 - related_information: [],
289 - severity: InvalidSyntax,
290 - data: [],
291 - },
292 -)
293 -Diagnostic(
294 - DiagnosticData {
295 - message: "Undefined variable",
296 - span: Some(
297 - SourceSpan {
298 - offset: SourceOffset(
299 - 196,
99 + "Bar": Declaration {
100 + id: DeclarationId(
101 + 10,
102 ),
301 - length: 7,
302 - },
303 - ),
304 - related_information: [],
305 - severity: InvalidSyntax,
306 - data: [],
307 - },
308 -)
309 -Diagnostic(
310 - DiagnosticData {
311 - message: "Undefined variable",
312 - span: Some(
313 - SourceSpan {
314 - offset: SourceOffset(
315 - 221,
103 + kind: Import,
104 + scope: ScopeId(
105 + 0,
106 ),
317 - length: 6,
107 },
319 - ),
320 - related_information: [],
321 - severity: InvalidSyntax,
322 - data: [],
323 - },
324 -)
325 -Diagnostic(
326 - DiagnosticData {
327 - message: "Undefined variable",
328 - span: Some(
329 - SourceSpan {
330 - offset: SourceOffset(
331 - 242,
108 + "Baz": Declaration {
109 + id: DeclarationId(
110 + 11,
111 + ),
112 + kind: Import,
113 + scope: ScopeId(
114 + 0,
115 ),
333 - length: 4,
116 },
335 - ),
336 - related_information: [],
337 - severity: InvalidSyntax,
338 - data: [],
339 - },
340 -)
341 -Diagnostic(
342 - DiagnosticData {
343 - message: "Undefined variable",
344 - span: Some(
345 - SourceSpan {
346 - offset: SourceOffset(
347 - 272,
117 + "Component": Declaration {
118 + id: DeclarationId(
119 + 12,
120 + ),
121 + kind: Function,
122 + scope: ScopeId(
123 + 0,
124 ),
349 - length: 10,
125 },
351 - ),
352 - related_information: [],
353 - severity: InvalidSyntax,
354 - data: [],
355 - },
356 -)
357 -Diagnostic(
358 - DiagnosticData {
359 - message: "Undefined variable",
360 - span: Some(
361 - SourceSpan {
362 - offset: SourceOffset(
363 - 299,
126 + },
127 + references: [],
128 + children: [
129 + Scope {
130 + id: ScopeId(
131 + 1,
132 ),
365 - length: 11,
133 + kind: Function,
134 + declarations: {
135 + "props": Declaration {
136 + id: DeclarationId(
137 + 13,
138 + ),
139 + kind: Function,
140 + scope: ScopeId(
141 + 1,
142 + ),
143 + },
144 + "g": Declaration {
145 + id: DeclarationId(
146 + 14,
147 + ),
148 + kind: Let,
149 + scope: ScopeId(
150 + 1,
151 + ),
152 + },
153 + "y": Declaration {
154 + id: DeclarationId(
155 + 15,
156 + ),
157 + kind: Let,
158 + scope: ScopeId(
159 + 1,
160 + ),
161 + },
162 + "s": Declaration {
163 + id: DeclarationId(
164 + 16,
165 + ),
166 + kind: Let,
167 + scope: ScopeId(
168 + 1,
169 + ),
170 + },
171 + "b": Declaration {
172 + id: DeclarationId(
173 + 17,
174 + ),
175 + kind: Let,
176 + scope: ScopeId(
177 + 1,
178 + ),
179 + },
180 + "n": Declaration {
181 + id: DeclarationId(
182 + 18,
183 + ),
184 + kind: Let,
185 + scope: ScopeId(
186 + 1,
187 + ),
188 + },
189 + "x": Declaration {
190 + id: DeclarationId(
191 + 19,
192 + ),
193 + kind: Let,
194 + scope: ScopeId(
195 + 1,
196 + ),
197 + },
198 + },
199 + references: [
200 + Reference {
201 + id: ReferenceId(
202 + 0,
203 + ),
204 + kind: Read,
205 + declaration: DeclarationId(
206 + 3,
207 + ),
208 + declaration (name): "global",
209 + scope: ScopeId(
210 + 1,
211 + ),
212 + },
213 + Reference {
214 + id: ReferenceId(
215 + 1,
216 + ),
217 + kind: Read,
218 + declaration: DeclarationId(
219 + 0,
220 + ),
221 + declaration (name): "Array",
222 + scope: ScopeId(
223 + 1,
224 + ),
225 + },
226 + Reference {
227 + id: ReferenceId(
228 + 2,
229 + ),
230 + kind: Read,
231 + declaration: DeclarationId(
232 + 13,
233 + ),
234 + declaration (name): "props",
235 + scope: ScopeId(
236 + 1,
237 + ),
238 + },
239 + Reference {
240 + id: ReferenceId(
241 + 3,
242 + ),
243 + kind: Read,
244 + declaration: DeclarationId(
245 + 8,
246 + ),
247 + declaration (name): "String",
248 + scope: ScopeId(
249 + 1,
250 + ),
251 + },
252 + Reference {
253 + id: ReferenceId(
254 + 4,
255 + ),
256 + kind: Read,
257 + declaration: DeclarationId(
258 + 1,
259 + ),
260 + declaration (name): "Boolean",
261 + scope: ScopeId(
262 + 1,
263 + ),
264 + },
265 + Reference {
266 + id: ReferenceId(
267 + 5,
268 + ),
269 + kind: Read,
270 + declaration: DeclarationId(
271 + 5,
272 + ),
273 + declaration (name): "Number",
274 + scope: ScopeId(
275 + 1,
276 + ),
277 + },
278 + Reference {
279 + id: ReferenceId(
280 + 6,
281 + ),
282 + kind: Read,
283 + declaration: DeclarationId(
284 + 4,
285 + ),
286 + declaration (name): "Math",
287 + scope: ScopeId(
288 + 1,
289 + ),
290 + },
291 + Reference {
292 + id: ReferenceId(
293 + 7,
294 + ),
295 + kind: Read,
296 + declaration: DeclarationId(
297 + 13,
298 + ),
299 + declaration (name): "props",
300 + scope: ScopeId(
301 + 1,
302 + ),
303 + },
304 + Reference {
305 + id: ReferenceId(
306 + 8,
307 + ),
308 + kind: Read,
309 + declaration: DeclarationId(
310 + 13,
311 + ),
312 + declaration (name): "props",
313 + scope: ScopeId(
314 + 1,
315 + ),
316 + },
317 + Reference {
318 + id: ReferenceId(
319 + 9,
320 + ),
321 + kind: Read,
322 + declaration: DeclarationId(
323 + 7,
324 + ),
325 + declaration (name): "setTimeout",
326 + scope: ScopeId(
327 + 1,
328 + ),
329 + },
330 + Reference {
331 + id: ReferenceId(
332 + 10,
333 + ),
334 + kind: Read,
335 + declaration: DeclarationId(
336 + 6,
337 + ),
338 + declaration (name): "setInterval",
339 + scope: ScopeId(
340 + 1,
341 + ),
342 + },
343 + Reference {
344 + id: ReferenceId(
345 + 11,
346 + ),
347 + kind: Read,
348 + declaration: DeclarationId(
349 + 9,
350 + ),
351 + declaration (name): "Foo",
352 + scope: ScopeId(
353 + 1,
354 + ),
355 + },
356 + Reference {
357 + id: ReferenceId(
358 + 12,
359 + ),
360 + kind: Read,
361 + declaration: DeclarationId(
362 + 10,
363 + ),
364 + declaration (name): "Bar",
365 + scope: ScopeId(
366 + 1,
367 + ),
368 + },
369 + Reference {
370 + id: ReferenceId(
371 + 13,
372 + ),
373 + kind: Read,
374 + declaration: DeclarationId(
375 + 11,
376 + ),
377 + declaration (name): "Baz",
378 + scope: ScopeId(
379 + 1,
380 + ),
381 + },
382 + ],
383 + children: [
384 + Scope {
385 + id: ScopeId(
386 + 2,
387 + ),
388 + kind: Function,
389 + declarations: {},
390 + references: [],
391 + children: [],
392 + },
393 + Scope {
394 + id: ScopeId(
395 + 3,
396 + ),
397 + kind: Function,
398 + declarations: {},
399 + references: [],
400 + children: [],
401 + },
402 + ],
403 },
367 - ),
368 - related_information: [],
369 - severity: InvalidSyntax,
370 - data: [],
404 + ],
405 },
372 -)
406 +}
407
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@labels.js.snap
+240 -182
@@ -20,195 +20,253 @@ function Component(props) {
20
21
22 Analysis:
23 -Scope {
24 - id: ScopeId(
25 - 0,
26 - ),
27 - kind: Module,
28 - declarations: {
29 - "Component": Declaration {
30 - id: DeclarationId(
23 +ScopeManager {
24 + globals: [
25 + (
26 + "Array",
27 + DeclarationId(
28 0,
29 ),
33 - kind: Function,
34 - scope: ScopeId(
35 - 0,
36 - ),
37 - },
38 - },
39 - references: [],
40 - children: [
41 - Scope {
42 - id: ScopeId(
30 + ),
31 + (
32 + "Boolean",
33 + DeclarationId(
34 1,
35 ),
45 - kind: Function,
46 - declarations: {
47 - "props": Declaration {
48 - id: DeclarationId(
49 - 1,
50 - ),
51 - kind: Function,
52 - scope: ScopeId(
53 - 1,
54 - ),
55 - },
56 - "y": Declaration {
57 - id: DeclarationId(
58 - 2,
59 - ),
60 - kind: Let,
61 - scope: ScopeId(
62 - 1,
63 - ),
64 - },
36 + ),
37 + (
38 + "console",
39 + DeclarationId(
40 + 2,
41 + ),
42 + ),
43 + (
44 + "global",
45 + DeclarationId(
46 + 3,
47 + ),
48 + ),
49 + (
50 + "Math",
51 + DeclarationId(
52 + 4,
53 + ),
54 + ),
55 + (
56 + "Number",
57 + DeclarationId(
58 + 5,
59 + ),
60 + ),
61 + (
62 + "setInterval",
63 + DeclarationId(
64 + 6,
65 + ),
66 + ),
67 + (
68 + "setTimeout",
69 + DeclarationId(
70 + 7,
71 + ),
72 + ),
73 + (
74 + "String",
75 + DeclarationId(
76 + 8,
77 + ),
78 + ),
79 + ],
80 + root: Scope {
81 + id: ScopeId(
82 + 0,
83 + ),
84 + kind: Module,
85 + declarations: {
86 + "Component": Declaration {
87 + id: DeclarationId(
88 + 9,
89 + ),
90 + kind: Function,
91 + scope: ScopeId(
92 + 0,
93 + ),
94 },
66 - references: [
67 - Reference {
68 - id: ReferenceId(
69 - 6,
70 - ),
71 - kind: Read,
72 - declaration: DeclarationId(
73 - 1,
74 - ),
75 - declaration (name): "props",
76 - scope: ScopeId(
77 - 1,
78 - ),
95 + },
96 + references: [],
97 + children: [
98 + Scope {
99 + id: ScopeId(
100 + 1,
101 + ),
102 + kind: Function,
103 + declarations: {
104 + "props": Declaration {
105 + id: DeclarationId(
106 + 10,
107 + ),
108 + kind: Function,
109 + scope: ScopeId(
110 + 1,
111 + ),
112 + },
113 + "y": Declaration {
114 + id: DeclarationId(
115 + 11,
116 + ),
117 + kind: Let,
118 + scope: ScopeId(
119 + 1,
120 + ),
121 + },
122 },
80 - ],
81 - children: [
82 - Scope {
83 - id: ScopeId(
84 - 2,
85 - ),
86 - kind: For,
87 - declarations: {
88 - "x": Declaration {
89 - id: DeclarationId(
90 - 3,
91 - ),
92 - kind: Let,
93 - scope: ScopeId(
94 - 2,
95 - ),
96 - },
123 + references: [
124 + Reference {
125 + id: ReferenceId(
126 + 6,
127 + ),
128 + kind: Read,
129 + declaration: DeclarationId(
130 + 10,
131 + ),
132 + declaration (name): "props",
133 + scope: ScopeId(
134 + 1,
135 + ),
136 },
98 - references: [
99 - Reference {
100 - id: ReferenceId(
101 - 0,
102 - ),
103 - kind: Read,
104 - declaration: DeclarationId(
105 - 3,
106 - ),
107 - declaration (name): "x",
108 - scope: ScopeId(
109 - 2,
110 - ),
137 + ],
138 + children: [
139 + Scope {
140 + id: ScopeId(
141 + 2,
142 + ),
143 + kind: For,
144 + declarations: {
145 + "x": Declaration {
146 + id: DeclarationId(
147 + 12,
148 + ),
149 + kind: Let,
150 + scope: ScopeId(
151 + 2,
152 + ),
153 + },
154 },
112 - Reference {
113 - id: ReferenceId(
114 - 1,
115 - ),
116 - kind: Read,
117 - declaration: DeclarationId(
118 - 3,
119 - ),
120 - declaration (name): "x",
121 - scope: ScopeId(
122 - 2,
123 - ),
124 - },
125 - ],
126 - children: [
127 - Scope {
128 - id: ScopeId(
129 - 3,
130 - ),
131 - kind: Block,
132 - declarations: {},
133 - references: [
134 - Reference {
135 - id: ReferenceId(
136 - 2,
137 - ),
138 - kind: Read,
139 - declaration: DeclarationId(
140 - 3,
141 - ),
142 - declaration (name): "x",
143 - scope: ScopeId(
144 - 3,
145 - ),
146 - },
147 - Reference {
148 - id: ReferenceId(
149 - 3,
150 - ),
151 - kind: Write,
152 - declaration: DeclarationId(
153 - 2,
154 - ),
155 - declaration (name): "y",
156 - scope: ScopeId(
157 - 3,
158 - ),
159 - },
160 - Reference {
161 - id: ReferenceId(
162 - 4,
163 - ),
164 - kind: Read,
165 - declaration: DeclarationId(
166 - 3,
167 - ),
168 - declaration (name): "x",
169 - scope: ScopeId(
170 - 3,
171 - ),
172 - },
173 - Reference {
174 - id: ReferenceId(
175 - 5,
176 - ),
177 - kind: Read,
178 - declaration: DeclarationId(
179 - 2,
180 - ),
181 - declaration (name): "y",
182 - scope: ScopeId(
183 - 3,
184 - ),
185 - },
186 - ],
187 - children: [
188 - Scope {
189 - id: ScopeId(
190 - 4,
191 - ),
192 - kind: Block,
193 - declarations: {},
194 - references: [],
195 - children: [],
196 - },
197 - ],
198 - },
199 - ],
200 - },
201 - Scope {
202 - id: ScopeId(
203 - 5,
204 - ),
205 - kind: Block,
206 - declarations: {},
207 - references: [],
208 - children: [],
209 - },
210 - ],
211 - },
212 - ],
155 + references: [
156 + Reference {
157 + id: ReferenceId(
158 + 0,
159 + ),
160 + kind: Read,
161 + declaration: DeclarationId(
162 + 12,
163 + ),
164 + declaration (name): "x",
165 + scope: ScopeId(
166 + 2,
167 + ),
168 + },
169 + Reference {
170 + id: ReferenceId(
171 + 1,
172 + ),
173 + kind: Read,
174 + declaration: DeclarationId(
175 + 12,
176 + ),
177 + declaration (name): "x",
178 + scope: ScopeId(
179 + 2,
180 + ),
181 + },
182 + ],
183 + children: [
184 + Scope {
185 + id: ScopeId(
186 + 3,
187 + ),
188 + kind: Block,
189 + declarations: {},
190 + references: [
191 + Reference {
192 + id: ReferenceId(
193 + 2,
194 + ),
195 + kind: Read,
196 + declaration: DeclarationId(
197 + 12,
198 + ),
199 + declaration (name): "x",
200 + scope: ScopeId(
201 + 3,
202 + ),
203 + },
204 + Reference {
205 + id: ReferenceId(
206 + 3,
207 + ),
208 + kind: Write,
209 + declaration: DeclarationId(
210 + 11,
211 + ),
212 + declaration (name): "y",
213 + scope: ScopeId(
214 + 3,
215 + ),
216 + },
217 + Reference {
218 + id: ReferenceId(
219 + 4,
220 + ),
221 + kind: Read,
222 + declaration: DeclarationId(
223 + 12,
224 + ),
225 + declaration (name): "x",
226 + scope: ScopeId(
227 + 3,
228 + ),
229 + },
230 + Reference {
231 + id: ReferenceId(
232 + 5,
233 + ),
234 + kind: Read,
235 + declaration: DeclarationId(
236 + 11,
237 + ),
238 + declaration (name): "y",
239 + scope: ScopeId(
240 + 3,
241 + ),
242 + },
243 + ],
244 + children: [
245 + Scope {
246 + id: ScopeId(
247 + 4,
248 + ),
249 + kind: Block,
250 + declarations: {},
251 + references: [],
252 + children: [],
253 + },
254 + ],
255 + },
256 + ],
257 + },
258 + Scope {
259 + id: ScopeId(
260 + 5,
261 + ),
262 + kind: Block,
263 + declarations: {},
264 + references: [],
265 + children: [],
266 + },
267 + ],
268 + },
269 + ],
270 + },
271 }
272
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@let-const-hoisting.js.snap
+145 -87
@@ -15,98 +15,156 @@ function Component() {
15
16
17 Analysis:
18 -Scope {
19 - id: ScopeId(
20 - 0,
21 - ),
22 - kind: Module,
23 - declarations: {
24 - "Component": Declaration {
25 - id: DeclarationId(
18 +ScopeManager {
19 + globals: [
20 + (
21 + "Array",
22 + DeclarationId(
23 0,
24 ),
28 - kind: Function,
29 - scope: ScopeId(
30 - 0,
31 - ),
32 - },
33 - },
34 - references: [],
35 - children: [
36 - Scope {
37 - id: ScopeId(
25 + ),
26 + (
27 + "Boolean",
28 + DeclarationId(
29 1,
30 ),
40 - kind: Function,
41 - declarations: {
42 - "foo": Declaration {
43 - id: DeclarationId(
44 - 1,
45 - ),
46 - kind: Function,
47 - scope: ScopeId(
48 - 1,
49 - ),
50 - },
51 - "a": Declaration {
52 - id: DeclarationId(
53 - 2,
54 - ),
55 - kind: Let,
56 - scope: ScopeId(
57 - 1,
58 - ),
59 - },
60 - "b": Declaration {
61 - id: DeclarationId(
62 - 3,
63 - ),
64 - kind: Const,
65 - scope: ScopeId(
66 - 1,
67 - ),
68 - },
31 + ),
32 + (
33 + "console",
34 + DeclarationId(
35 + 2,
36 + ),
37 + ),
38 + (
39 + "global",
40 + DeclarationId(
41 + 3,
42 + ),
43 + ),
44 + (
45 + "Math",
46 + DeclarationId(
47 + 4,
48 + ),
49 + ),
50 + (
51 + "Number",
52 + DeclarationId(
53 + 5,
54 + ),
55 + ),
56 + (
57 + "setInterval",
58 + DeclarationId(
59 + 6,
60 + ),
61 + ),
62 + (
63 + "setTimeout",
64 + DeclarationId(
65 + 7,
66 + ),
67 + ),
68 + (
69 + "String",
70 + DeclarationId(
71 + 8,
72 + ),
73 + ),
74 + ],
75 + root: Scope {
76 + id: ScopeId(
77 + 0,
78 + ),
79 + kind: Module,
80 + declarations: {
81 + "Component": Declaration {
82 + id: DeclarationId(
83 + 9,
84 + ),
85 + kind: Function,
86 + scope: ScopeId(
87 + 0,
88 + ),
89 },
70 - references: [],
71 - children: [
72 - Scope {
73 - id: ScopeId(
74 - 2,
75 - ),
76 - kind: Function,
77 - declarations: {},
78 - references: [
79 - Reference {
80 - id: ReferenceId(
81 - 0,
82 - ),
83 - kind: Read,
84 - declaration: DeclarationId(
85 - 2,
86 - ),
87 - declaration (name): "a",
88 - scope: ScopeId(
89 - 2,
90 - ),
91 - },
92 - Reference {
93 - id: ReferenceId(
94 - 1,
95 - ),
96 - kind: Read,
97 - declaration: DeclarationId(
98 - 3,
99 - ),
100 - declaration (name): "b",
101 - scope: ScopeId(
102 - 2,
103 - ),
104 - },
105 - ],
106 - children: [],
107 - },
108 - ],
90 },
110 - ],
91 + references: [],
92 + children: [
93 + Scope {
94 + id: ScopeId(
95 + 1,
96 + ),
97 + kind: Function,
98 + declarations: {
99 + "foo": Declaration {
100 + id: DeclarationId(
101 + 10,
102 + ),
103 + kind: Function,
104 + scope: ScopeId(
105 + 1,
106 + ),
107 + },
108 + "a": Declaration {
109 + id: DeclarationId(
110 + 11,
111 + ),
112 + kind: Let,
113 + scope: ScopeId(
114 + 1,
115 + ),
116 + },
117 + "b": Declaration {
118 + id: DeclarationId(
119 + 12,
120 + ),
121 + kind: Const,
122 + scope: ScopeId(
123 + 1,
124 + ),
125 + },
126 + },
127 + references: [],
128 + children: [
129 + Scope {
130 + id: ScopeId(
131 + 2,
132 + ),
133 + kind: Function,
134 + declarations: {},
135 + references: [
136 + Reference {
137 + id: ReferenceId(
138 + 0,
139 + ),
140 + kind: Read,
141 + declaration: DeclarationId(
142 + 11,
143 + ),
144 + declaration (name): "a",
145 + scope: ScopeId(
146 + 2,
147 + ),
148 + },
149 + Reference {
150 + id: ReferenceId(
151 + 1,
152 + ),
153 + kind: Read,
154 + declaration: DeclarationId(
155 + 12,
156 + ),
157 + declaration (name): "b",
158 + scope: ScopeId(
159 + 2,
160 + ),
161 + },
162 + ],
163 + children: [],
164 + },
165 + ],
166 + },
167 + ],
168 + },
169 }
170
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@simple-function.js.snap
+233 -178
@@ -16,193 +16,248 @@ function Component(a) {
16
17
18 Analysis:
19 -Scope {
20 - id: ScopeId(
21 - 0,
22 - ),
23 - kind: Module,
24 - declarations: {
25 - "Component": Declaration {
26 - id: DeclarationId(
19 +ScopeManager {
20 + globals: [
21 + (
22 + "Array",
23 + DeclarationId(
24 0,
25 ),
29 - kind: Function,
30 - scope: ScopeId(
31 - 0,
32 - ),
33 - },
34 - },
35 - references: [],
36 - children: [
37 - Scope {
38 - id: ScopeId(
26 + ),
27 + (
28 + "Boolean",
29 + DeclarationId(
30 1,
31 ),
41 - kind: Function,
42 - declarations: {
43 - "a": Declaration {
44 - id: DeclarationId(
45 - 1,
46 - ),
47 - kind: Function,
48 - scope: ScopeId(
49 - 1,
50 - ),
51 - },
52 - "b": Declaration {
53 - id: DeclarationId(
54 - 2,
55 - ),
56 - kind: Let,
57 - scope: ScopeId(
58 - 1,
59 - ),
60 - },
61 - "foo": Declaration {
62 - id: DeclarationId(
63 - 3,
64 - ),
65 - kind: Const,
66 - scope: ScopeId(
67 - 1,
68 - ),
69 - },
32 + ),
33 + (
34 + "console",
35 + DeclarationId(
36 + 2,
37 + ),
38 + ),
39 + (
40 + "global",
41 + DeclarationId(
42 + 3,
43 + ),
44 + ),
45 + (
46 + "Math",
47 + DeclarationId(
48 + 4,
49 + ),
50 + ),
51 + (
52 + "Number",
53 + DeclarationId(
54 + 5,
55 + ),
56 + ),
57 + (
58 + "setInterval",
59 + DeclarationId(
60 + 6,
61 + ),
62 + ),
63 + (
64 + "setTimeout",
65 + DeclarationId(
66 + 7,
67 + ),
68 + ),
69 + (
70 + "String",
71 + DeclarationId(
72 + 8,
73 + ),
74 + ),
75 + ],
76 + root: Scope {
77 + id: ScopeId(
78 + 0,
79 + ),
80 + kind: Module,
81 + declarations: {
82 + "Component": Declaration {
83 + id: DeclarationId(
84 + 9,
85 + ),
86 + kind: Function,
87 + scope: ScopeId(
88 + 0,
89 + ),
90 },
71 - references: [
72 - Reference {
73 - id: ReferenceId(
74 - 4,
75 - ),
76 - kind: Read,
77 - declaration: DeclarationId(
78 - 3,
79 - ),
80 - declaration (name): "foo",
81 - scope: ScopeId(
82 - 1,
83 - ),
91 + },
92 + references: [],
93 + children: [
94 + Scope {
95 + id: ScopeId(
96 + 1,
97 + ),
98 + kind: Function,
99 + declarations: {
100 + "a": Declaration {
101 + id: DeclarationId(
102 + 10,
103 + ),
104 + kind: Function,
105 + scope: ScopeId(
106 + 1,
107 + ),
108 + },
109 + "b": Declaration {
110 + id: DeclarationId(
111 + 11,
112 + ),
113 + kind: Let,
114 + scope: ScopeId(
115 + 1,
116 + ),
117 + },
118 + "foo": Declaration {
119 + id: DeclarationId(
120 + 12,
121 + ),
122 + kind: Const,
123 + scope: ScopeId(
124 + 1,
125 + ),
126 + },
127 },
85 - ],
86 - children: [
87 - Scope {
88 - id: ScopeId(
89 - 2,
90 - ),
91 - kind: Function,
92 - declarations: {
93 - "foo_": Declaration {
94 - id: DeclarationId(
95 - 4,
96 - ),
97 - kind: Function,
98 - scope: ScopeId(
99 - 2,
100 - ),
101 - },
128 + references: [
129 + Reference {
130 + id: ReferenceId(
131 + 0,
132 + ),
133 + kind: Read,
134 + declaration: DeclarationId(
135 + 4,
136 + ),
137 + declaration (name): "Math",
138 + scope: ScopeId(
139 + 1,
140 + ),
141 },
103 - references: [],
104 - children: [
105 - Scope {
106 - id: ScopeId(
107 - 3,
108 - ),
109 - kind: Function,
110 - declarations: {
111 - "c": Declaration {
112 - id: DeclarationId(
113 - 5,
114 - ),
115 - kind: Function,
116 - scope: ScopeId(
117 - 3,
118 - ),
119 - },
120 - "d": Declaration {
121 - id: DeclarationId(
122 - 6,
123 - ),
124 - kind: Let,
125 - scope: ScopeId(
126 - 3,
127 - ),
128 - },
142 + Reference {
143 + id: ReferenceId(
144 + 5,
145 + ),
146 + kind: Read,
147 + declaration: DeclarationId(
148 + 12,
149 + ),
150 + declaration (name): "foo",
151 + scope: ScopeId(
152 + 1,
153 + ),
154 + },
155 + ],
156 + children: [
157 + Scope {
158 + id: ScopeId(
159 + 2,
160 + ),
161 + kind: Function,
162 + declarations: {
163 + "foo_": Declaration {
164 + id: DeclarationId(
165 + 13,
166 + ),
167 + kind: Function,
168 + scope: ScopeId(
169 + 2,
170 + ),
171 },
130 - references: [
131 - Reference {
132 - id: ReferenceId(
133 - 0,
134 - ),
135 - kind: Read,
136 - declaration: DeclarationId(
137 - 1,
138 - ),
139 - declaration (name): "a",
140 - scope: ScopeId(
141 - 3,
142 - ),
143 - },
144 - Reference {
145 - id: ReferenceId(
146 - 1,
147 - ),
148 - kind: Read,
149 - declaration: DeclarationId(
150 - 2,
151 - ),
152 - declaration (name): "b",
153 - scope: ScopeId(
154 - 3,
155 - ),
156 - },
157 - Reference {
158 - id: ReferenceId(
159 - 2,
160 - ),
161 - kind: Read,
162 - declaration: DeclarationId(
163 - 5,
164 - ),
165 - declaration (name): "c",
166 - scope: ScopeId(
167 - 3,
168 - ),
169 - },
170 - Reference {
171 - id: ReferenceId(
172 - 3,
173 - ),
174 - kind: Read,
175 - declaration: DeclarationId(
176 - 6,
177 - ),
178 - declaration (name): "d",
179 - scope: ScopeId(
180 - 3,
181 - ),
182 - },
183 - ],
184 - children: [],
172 },
186 - ],
187 - },
188 - ],
189 - },
190 - ],
191 -}
192 -Diagnostic(
193 - DiagnosticData {
194 - message: "Undefined variable",
195 - span: Some(
196 - SourceSpan {
197 - offset: SourceOffset(
198 - 26,
199 - ),
200 - length: 4,
173 + references: [],
174 + children: [
175 + Scope {
176 + id: ScopeId(
177 + 3,
178 + ),
179 + kind: Function,
180 + declarations: {
181 + "c": Declaration {
182 + id: DeclarationId(
183 + 14,
184 + ),
185 + kind: Function,
186 + scope: ScopeId(
187 + 3,
188 + ),
189 + },
190 + "d": Declaration {
191 + id: DeclarationId(
192 + 15,
193 + ),
194 + kind: Let,
195 + scope: ScopeId(
196 + 3,
197 + ),
198 + },
199 + },
200 + references: [
201 + Reference {
202 + id: ReferenceId(
203 + 1,
204 + ),
205 + kind: Read,
206 + declaration: DeclarationId(
207 + 10,
208 + ),
209 + declaration (name): "a",
210 + scope: ScopeId(
211 + 3,
212 + ),
213 + },
214 + Reference {
215 + id: ReferenceId(
216 + 2,
217 + ),
218 + kind: Read,
219 + declaration: DeclarationId(
220 + 11,
221 + ),
222 + declaration (name): "b",
223 + scope: ScopeId(
224 + 3,
225 + ),
226 + },
227 + Reference {
228 + id: ReferenceId(
229 + 3,
230 + ),
231 + kind: Read,
232 + declaration: DeclarationId(
233 + 14,
234 + ),
235 + declaration (name): "c",
236 + scope: ScopeId(
237 + 3,
238 + ),
239 + },
240 + Reference {
241 + id: ReferenceId(
242 + 4,
243 + ),
244 + kind: Read,
245 + declaration: DeclarationId(
246 + 15,
247 + ),
248 + declaration (name): "d",
249 + scope: ScopeId(
250 + 3,
251 + ),
252 + },
253 + ],
254 + children: [],
255 + },
256 + ],
257 + },
258 + ],
259 },
202 - ),
203 - related_information: [],
204 - severity: InvalidSyntax,
205 - data: [],
260 + ],
261 },
207 -)
262 +}
263
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@tdz.js.snap
+155 -97
@@ -21,109 +21,167 @@ function Component() {
21
22
23 Analysis:
24 -Scope {
25 - id: ScopeId(
26 - 0,
27 - ),
28 - kind: Module,
29 - declarations: {
30 - "Component": Declaration {
31 - id: DeclarationId(
24 +ScopeManager {
25 + globals: [
26 + (
27 + "Array",
28 + DeclarationId(
29 0,
30 ),
34 - kind: Function,
35 - scope: ScopeId(
36 - 0,
37 - ),
38 - },
39 - },
40 - references: [],
41 - children: [
42 - Scope {
43 - id: ScopeId(
31 + ),
32 + (
33 + "Boolean",
34 + DeclarationId(
35 1,
36 ),
46 - kind: Function,
47 - declarations: {
48 - "foo": Declaration {
49 - id: DeclarationId(
50 - 1,
51 - ),
52 - kind: Function,
53 - scope: ScopeId(
54 - 1,
55 - ),
56 - },
57 - "a": Declaration {
58 - id: DeclarationId(
59 - 2,
60 - ),
61 - kind: Let,
62 - scope: ScopeId(
63 - 1,
64 - ),
65 - },
37 + ),
38 + (
39 + "console",
40 + DeclarationId(
41 + 2,
42 + ),
43 + ),
44 + (
45 + "global",
46 + DeclarationId(
47 + 3,
48 + ),
49 + ),
50 + (
51 + "Math",
52 + DeclarationId(
53 + 4,
54 + ),
55 + ),
56 + (
57 + "Number",
58 + DeclarationId(
59 + 5,
60 + ),
61 + ),
62 + (
63 + "setInterval",
64 + DeclarationId(
65 + 6,
66 + ),
67 + ),
68 + (
69 + "setTimeout",
70 + DeclarationId(
71 + 7,
72 + ),
73 + ),
74 + (
75 + "String",
76 + DeclarationId(
77 + 8,
78 + ),
79 + ),
80 + ],
81 + root: Scope {
82 + id: ScopeId(
83 + 0,
84 + ),
85 + kind: Module,
86 + declarations: {
87 + "Component": Declaration {
88 + id: DeclarationId(
89 + 9,
90 + ),
91 + kind: Function,
92 + scope: ScopeId(
93 + 0,
94 + ),
95 },
67 - references: [
68 - Reference {
69 - id: ReferenceId(
70 - 1,
71 - ),
72 - kind: Read,
73 - declaration: DeclarationId(
74 - 1,
75 - ),
76 - declaration (name): "foo",
77 - scope: ScopeId(
78 - 1,
79 - ),
80 - },
81 - ],
82 - children: [
83 - Scope {
84 - id: ScopeId(
85 - 2,
86 - ),
87 - kind: Block,
88 - declarations: {},
89 - references: [],
90 - children: [],
91 - },
92 - Scope {
93 - id: ScopeId(
94 - 3,
95 - ),
96 - kind: Block,
97 - declarations: {},
98 - references: [],
99 - children: [],
100 - },
101 - Scope {
102 - id: ScopeId(
103 - 4,
104 - ),
105 - kind: Function,
106 - declarations: {},
107 - references: [
108 - Reference {
109 - id: ReferenceId(
110 - 0,
111 - ),
112 - kind: Read,
113 - declaration: DeclarationId(
114 - 2,
115 - ),
116 - declaration (name): "a",
117 - scope: ScopeId(
118 - 4,
119 - ),
120 - },
121 - ],
122 - children: [],
123 - },
124 - ],
96 },
126 - ],
97 + references: [],
98 + children: [
99 + Scope {
100 + id: ScopeId(
101 + 1,
102 + ),
103 + kind: Function,
104 + declarations: {
105 + "foo": Declaration {
106 + id: DeclarationId(
107 + 10,
108 + ),
109 + kind: Function,
110 + scope: ScopeId(
111 + 1,
112 + ),
113 + },
114 + "a": Declaration {
115 + id: DeclarationId(
116 + 11,
117 + ),
118 + kind: Let,
119 + scope: ScopeId(
120 + 1,
121 + ),
122 + },
123 + },
124 + references: [
125 + Reference {
126 + id: ReferenceId(
127 + 1,
128 + ),
129 + kind: Read,
130 + declaration: DeclarationId(
131 + 10,
132 + ),
133 + declaration (name): "foo",
134 + scope: ScopeId(
135 + 1,
136 + ),
137 + },
138 + ],
139 + children: [
140 + Scope {
141 + id: ScopeId(
142 + 2,
143 + ),
144 + kind: Block,
145 + declarations: {},
146 + references: [],
147 + children: [],
148 + },
149 + Scope {
150 + id: ScopeId(
151 + 3,
152 + ),
153 + kind: Block,
154 + declarations: {},
155 + references: [],
156 + children: [],
157 + },
158 + Scope {
159 + id: ScopeId(
160 + 4,
161 + ),
162 + kind: Function,
163 + declarations: {},
164 + references: [
165 + Reference {
166 + id: ReferenceId(
167 + 0,
168 + ),
169 + kind: Read,
170 + declaration: DeclarationId(
171 + 11,
172 + ),
173 + declaration (name): "a",
174 + scope: ScopeId(
175 + 4,
176 + ),
177 + },
178 + ],
179 + children: [],
180 + },
181 + ],
182 + },
183 + ],
184 + },
185 }
186 Diagnostic(
187 DiagnosticData {
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@var-duplication.js.snap
+258 -209
@@ -36,177 +36,274 @@ function Component() {
36
37
38 Analysis:
39 -Scope {
40 - id: ScopeId(
41 - 0,
42 - ),
43 - kind: Module,
44 - declarations: {
45 - "Component": Declaration {
46 - id: DeclarationId(
39 +ScopeManager {
40 + globals: [
41 + (
42 + "Array",
43 + DeclarationId(
44 0,
45 ),
49 - kind: Function,
50 - scope: ScopeId(
51 - 0,
52 - ),
53 - },
54 - },
55 - references: [],
56 - children: [
57 - Scope {
58 - id: ScopeId(
46 + ),
47 + (
48 + "Boolean",
49 + DeclarationId(
50 1,
51 ),
61 - kind: Function,
62 - declarations: {
63 - "a": Declaration {
64 - id: DeclarationId(
65 - 1,
66 - ),
67 - kind: Let,
68 - scope: ScopeId(
69 - 1,
70 - ),
71 - },
72 - "b": Declaration {
73 - id: DeclarationId(
74 - 3,
75 - ),
76 - kind: Const,
77 - scope: ScopeId(
78 - 1,
79 - ),
80 - },
81 - "c": Declaration {
82 - id: DeclarationId(
83 - 6,
84 - ),
85 - kind: Var,
86 - scope: ScopeId(
87 - 1,
88 - ),
89 - },
90 - "d": Declaration {
91 - id: DeclarationId(
92 - 8,
93 - ),
94 - kind: Var,
95 - scope: ScopeId(
96 - 1,
97 - ),
98 - },
99 - "e": Declaration {
100 - id: DeclarationId(
101 - 9,
102 - ),
103 - kind: Var,
104 - scope: ScopeId(
105 - 1,
106 - ),
107 - },
52 + ),
53 + (
54 + "console",
55 + DeclarationId(
56 + 2,
57 + ),
58 + ),
59 + (
60 + "global",
61 + DeclarationId(
62 + 3,
63 + ),
64 + ),
65 + (
66 + "Math",
67 + DeclarationId(
68 + 4,
69 + ),
70 + ),
71 + (
72 + "Number",
73 + DeclarationId(
74 + 5,
75 + ),
76 + ),
77 + (
78 + "setInterval",
79 + DeclarationId(
80 + 6,
81 + ),
82 + ),
83 + (
84 + "setTimeout",
85 + DeclarationId(
86 + 7,
87 + ),
88 + ),
89 + (
90 + "String",
91 + DeclarationId(
92 + 8,
93 + ),
94 + ),
95 + ],
96 + root: Scope {
97 + id: ScopeId(
98 + 0,
99 + ),
100 + kind: Module,
101 + declarations: {
102 + "Component": Declaration {
103 + id: DeclarationId(
104 + 9,
105 + ),
106 + kind: Function,
107 + scope: ScopeId(
108 + 0,
109 + ),
110 },
109 - references: [
110 - Reference {
111 - id: ReferenceId(
112 - 0,
113 - ),
114 - kind: Read,
115 - declaration: DeclarationId(
116 - 9,
117 - ),
118 - declaration (name): "e",
119 - scope: ScopeId(
120 - 1,
121 - ),
122 - },
123 - Reference {
124 - id: ReferenceId(
125 - 1,
126 - ),
127 - kind: Read,
128 - declaration: DeclarationId(
129 - 9,
130 - ),
131 - declaration (name): "e",
132 - scope: ScopeId(
133 - 1,
134 - ),
135 - },
136 - Reference {
137 - id: ReferenceId(
138 - 2,
139 - ),
140 - kind: Read,
141 - declaration: DeclarationId(
142 - 9,
143 - ),
144 - declaration (name): "e",
145 - scope: ScopeId(
146 - 1,
147 - ),
148 - },
149 - ],
150 - children: [
151 - Scope {
152 - id: ScopeId(
153 - 2,
154 - ),
155 - kind: Block,
156 - declarations: {},
157 - references: [],
158 - children: [],
159 - },
160 - Scope {
161 - id: ScopeId(
162 - 3,
163 - ),
164 - kind: Block,
165 - declarations: {},
166 - references: [],
167 - children: [],
111 + },
112 + references: [],
113 + children: [
114 + Scope {
115 + id: ScopeId(
116 + 1,
117 + ),
118 + kind: Function,
119 + declarations: {
120 + "a": Declaration {
121 + id: DeclarationId(
122 + 10,
123 + ),
124 + kind: Let,
125 + scope: ScopeId(
126 + 1,
127 + ),
128 + },
129 + "b": Declaration {
130 + id: DeclarationId(
131 + 12,
132 + ),
133 + kind: Const,
134 + scope: ScopeId(
135 + 1,
136 + ),
137 + },
138 + "c": Declaration {
139 + id: DeclarationId(
140 + 15,
141 + ),
142 + kind: Var,
143 + scope: ScopeId(
144 + 1,
145 + ),
146 + },
147 + "d": Declaration {
148 + id: DeclarationId(
149 + 17,
150 + ),
151 + kind: Var,
152 + scope: ScopeId(
153 + 1,
154 + ),
155 + },
156 + "e": Declaration {
157 + id: DeclarationId(
158 + 18,
159 + ),
160 + kind: Var,
161 + scope: ScopeId(
162 + 1,
163 + ),
164 + },
165 },
169 - Scope {
170 - id: ScopeId(
171 - 4,
172 - ),
173 - kind: Block,
174 - declarations: {
175 - "c": Declaration {
176 - id: DeclarationId(
177 - 5,
178 - ),
179 - kind: Let,
180 - scope: ScopeId(
181 - 4,
182 - ),
166 + references: [
167 + Reference {
168 + id: ReferenceId(
169 + 0,
170 + ),
171 + kind: Read,
172 + declaration: DeclarationId(
173 + 2,
174 + ),
175 + declaration (name): "console",
176 + scope: ScopeId(
177 + 1,
178 + ),
179 + },
180 + Reference {
181 + id: ReferenceId(
182 + 1,
183 + ),
184 + kind: Read,
185 + declaration: DeclarationId(
186 + 18,
187 + ),
188 + declaration (name): "e",
189 + scope: ScopeId(
190 + 1,
191 + ),
192 + },
193 + Reference {
194 + id: ReferenceId(
195 + 2,
196 + ),
197 + kind: Read,
198 + declaration: DeclarationId(
199 + 2,
200 + ),
201 + declaration (name): "console",
202 + scope: ScopeId(
203 + 1,
204 + ),
205 + },
206 + Reference {
207 + id: ReferenceId(
208 + 3,
209 + ),
210 + kind: Read,
211 + declaration: DeclarationId(
212 + 18,
213 + ),
214 + declaration (name): "e",
215 + scope: ScopeId(
216 + 1,
217 + ),
218 + },
219 + Reference {
220 + id: ReferenceId(
221 + 4,
222 + ),
223 + kind: Read,
224 + declaration: DeclarationId(
225 + 2,
226 + ),
227 + declaration (name): "console",
228 + scope: ScopeId(
229 + 1,
230 + ),
231 + },
232 + Reference {
233 + id: ReferenceId(
234 + 5,
235 + ),
236 + kind: Read,
237 + declaration: DeclarationId(
238 + 18,
239 + ),
240 + declaration (name): "e",
241 + scope: ScopeId(
242 + 1,
243 + ),
244 + },
245 + ],
246 + children: [
247 + Scope {
248 + id: ScopeId(
249 + 2,
250 + ),
251 + kind: Block,
252 + declarations: {},
253 + references: [],
254 + children: [],
255 + },
256 + Scope {
257 + id: ScopeId(
258 + 3,
259 + ),
260 + kind: Block,
261 + declarations: {},
262 + references: [],
263 + children: [],
264 + },
265 + Scope {
266 + id: ScopeId(
267 + 4,
268 + ),
269 + kind: Block,
270 + declarations: {
271 + "c": Declaration {
272 + id: DeclarationId(
273 + 14,
274 + ),
275 + kind: Let,
276 + scope: ScopeId(
277 + 4,
278 + ),
279 + },
280 },
281 + references: [],
282 + children: [],
283 },
185 - references: [],
186 - children: [],
187 - },
188 - Scope {
189 - id: ScopeId(
190 - 5,
191 - ),
192 - kind: Block,
193 - declarations: {
194 - "d": Declaration {
195 - id: DeclarationId(
196 - 7,
197 - ),
198 - kind: Const,
199 - scope: ScopeId(
200 - 5,
201 - ),
284 + Scope {
285 + id: ScopeId(
286 + 5,
287 + ),
288 + kind: Block,
289 + declarations: {
290 + "d": Declaration {
291 + id: DeclarationId(
292 + 16,
293 + ),
294 + kind: Const,
295 + scope: ScopeId(
296 + 5,
297 + ),
298 + },
299 },
300 + references: [],
301 + children: [],
302 },
204 - references: [],
205 - children: [],
206 - },
207 - ],
208 - },
209 - ],
303 + ],
304 + },
305 + ],
306 + },
307 }
308 Diagnostic(
309 DiagnosticData {
@@ -272,52 +369,4 @@ Diagnostic(
369 data: [],
370 },
371 )
275 -Diagnostic(
276 - DiagnosticData {
277 - message: "Undefined variable",
278 - span: Some(
279 - SourceSpan {
280 - offset: SourceOffset(
281 - 305,
282 - ),
283 - length: 7,
284 - },
285 - ),
286 - related_information: [],
287 - severity: InvalidSyntax,
288 - data: [],
289 - },
290 -)
291 -Diagnostic(
292 - DiagnosticData {
293 - message: "Undefined variable",
294 - span: Some(
295 - SourceSpan {
296 - offset: SourceOffset(
297 - 341,
298 - ),
299 - length: 7,
300 - },
301 - ),
302 - related_information: [],
303 - severity: InvalidSyntax,
304 - data: [],
305 - },
306 -)
307 -Diagnostic(
308 - DiagnosticData {
309 - message: "Undefined variable",
310 - span: Some(
311 - SourceSpan {
312 - offset: SourceOffset(
313 - 373,
314 - ),
315 - length: 7,
316 - },
317 - ),
318 - related_information: [],
319 - severity: InvalidSyntax,
320 - data: [],
321 - },
322 -)
372
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@var-hoisting.js.snap
+257 -199
@@ -26,215 +26,273 @@ var baz;
26
27
28 Analysis:
29 -Scope {
30 - id: ScopeId(
31 - 0,
32 - ),
33 - kind: Module,
34 - declarations: {
35 - "Component": Declaration {
36 - id: DeclarationId(
29 +ScopeManager {
30 + globals: [
31 + (
32 + "Array",
33 + DeclarationId(
34 0,
35 ),
39 - kind: Function,
40 - scope: ScopeId(
41 - 0,
36 + ),
37 + (
38 + "Boolean",
39 + DeclarationId(
40 + 1,
41 ),
43 - },
44 - "baz": Declaration {
45 - id: DeclarationId(
42 + ),
43 + (
44 + "console",
45 + DeclarationId(
46 + 2,
47 + ),
48 + ),
49 + (
50 + "global",
51 + DeclarationId(
52 + 3,
53 + ),
54 + ),
55 + (
56 + "Math",
57 + DeclarationId(
58 + 4,
59 + ),
60 + ),
61 + (
62 + "Number",
63 + DeclarationId(
64 5,
65 ),
48 - kind: Var,
49 - scope: ScopeId(
50 - 0,
66 + ),
67 + (
68 + "setInterval",
69 + DeclarationId(
70 + 6,
71 ),
52 - },
53 - },
54 - references: [],
55 - children: [
56 - Scope {
57 - id: ScopeId(
58 - 1,
72 + ),
73 + (
74 + "setTimeout",
75 + DeclarationId(
76 + 7,
77 ),
60 - kind: Function,
61 - declarations: {
62 - "props": Declaration {
63 - id: DeclarationId(
64 - 1,
65 - ),
66 - kind: Function,
67 - scope: ScopeId(
68 - 1,
69 - ),
70 - },
71 - "foo": Declaration {
72 - id: DeclarationId(
73 - 2,
74 - ),
75 - kind: Function,
76 - scope: ScopeId(
77 - 1,
78 - ),
79 - },
80 - "bar": Declaration {
81 - id: DeclarationId(
82 - 4,
83 - ),
84 - kind: Var,
85 - scope: ScopeId(
86 - 1,
87 - ),
88 - },
78 + ),
79 + (
80 + "String",
81 + DeclarationId(
82 + 8,
83 + ),
84 + ),
85 + ],
86 + root: Scope {
87 + id: ScopeId(
88 + 0,
89 + ),
90 + kind: Module,
91 + declarations: {
92 + "Component": Declaration {
93 + id: DeclarationId(
94 + 9,
95 + ),
96 + kind: Function,
97 + scope: ScopeId(
98 + 0,
99 + ),
100 },
90 - references: [
91 - Reference {
92 - id: ReferenceId(
93 - 0,
94 - ),
95 - kind: Read,
96 - declaration: DeclarationId(
97 - 4,
98 - ),
99 - declaration (name): "bar",
100 - scope: ScopeId(
101 - 1,
102 - ),
103 - },
104 - Reference {
105 - id: ReferenceId(
106 - 1,
107 - ),
108 - kind: Write,
109 - declaration: DeclarationId(
110 - 4,
111 - ),
112 - declaration (name): "bar",
113 - scope: ScopeId(
114 - 1,
115 - ),
116 - },
117 - Reference {
118 - id: ReferenceId(
119 - 2,
120 - ),
121 - kind: Read,
122 - declaration: DeclarationId(
123 - 5,
124 - ),
125 - declaration (name): "baz",
126 - scope: ScopeId(
127 - 1,
128 - ),
129 - },
130 - Reference {
131 - id: ReferenceId(
132 - 3,
133 - ),
134 - kind: Write,
135 - declaration: DeclarationId(
136 - 5,
137 - ),
138 - declaration (name): "baz",
139 - scope: ScopeId(
140 - 1,
141 - ),
142 - },
143 - Reference {
144 - id: ReferenceId(
145 - 7,
146 - ),
147 - kind: Read,
148 - declaration: DeclarationId(
149 - 1,
150 - ),
151 - declaration (name): "props",
152 - scope: ScopeId(
153 - 1,
154 - ),
101 + "baz": Declaration {
102 + id: DeclarationId(
103 + 14,
104 + ),
105 + kind: Var,
106 + scope: ScopeId(
107 + 0,
108 + ),
109 + },
110 + },
111 + references: [],
112 + children: [
113 + Scope {
114 + id: ScopeId(
115 + 1,
116 + ),
117 + kind: Function,
118 + declarations: {
119 + "props": Declaration {
120 + id: DeclarationId(
121 + 10,
122 + ),
123 + kind: Function,
124 + scope: ScopeId(
125 + 1,
126 + ),
127 + },
128 + "foo": Declaration {
129 + id: DeclarationId(
130 + 11,
131 + ),
132 + kind: Function,
133 + scope: ScopeId(
134 + 1,
135 + ),
136 + },
137 + "bar": Declaration {
138 + id: DeclarationId(
139 + 13,
140 + ),
141 + kind: Var,
142 + scope: ScopeId(
143 + 1,
144 + ),
145 + },
146 },
156 - ],
157 - children: [
158 - Scope {
159 - id: ScopeId(
160 - 2,
161 - ),
162 - kind: Function,
163 - declarations: {
164 - "bar": Declaration {
165 - id: DeclarationId(
166 - 3,
167 - ),
168 - kind: Var,
169 - scope: ScopeId(
170 - 2,
171 - ),
172 - },
147 + references: [
148 + Reference {
149 + id: ReferenceId(
150 + 0,
151 + ),
152 + kind: Read,
153 + declaration: DeclarationId(
154 + 13,
155 + ),
156 + declaration (name): "bar",
157 + scope: ScopeId(
158 + 1,
159 + ),
160 },
174 - references: [
175 - Reference {
176 - id: ReferenceId(
177 - 4,
178 - ),
179 - kind: Read,
180 - declaration: DeclarationId(
181 - 3,
182 - ),
183 - declaration (name): "bar",
184 - scope: ScopeId(
185 - 2,
186 - ),
187 - },
188 - Reference {
189 - id: ReferenceId(
190 - 5,
191 - ),
192 - kind: Write,
193 - declaration: DeclarationId(
194 - 3,
195 - ),
196 - declaration (name): "bar",
197 - scope: ScopeId(
198 - 2,
199 - ),
200 - },
201 - Reference {
202 - id: ReferenceId(
203 - 6,
204 - ),
205 - kind: Read,
206 - declaration: DeclarationId(
207 - 1,
208 - ),
209 - declaration (name): "props",
210 - scope: ScopeId(
211 - 2,
212 - ),
213 - },
214 - ],
215 - children: [
216 - Scope {
217 - id: ScopeId(
218 - 3,
219 - ),
220 - kind: Block,
221 - declarations: {},
222 - references: [],
223 - children: [],
161 + Reference {
162 + id: ReferenceId(
163 + 1,
164 + ),
165 + kind: Write,
166 + declaration: DeclarationId(
167 + 13,
168 + ),
169 + declaration (name): "bar",
170 + scope: ScopeId(
171 + 1,
172 + ),
173 + },
174 + Reference {
175 + id: ReferenceId(
176 + 2,
177 + ),
178 + kind: Read,
179 + declaration: DeclarationId(
180 + 14,
181 + ),
182 + declaration (name): "baz",
183 + scope: ScopeId(
184 + 1,
185 + ),
186 + },
187 + Reference {
188 + id: ReferenceId(
189 + 3,
190 + ),
191 + kind: Write,
192 + declaration: DeclarationId(
193 + 14,
194 + ),
195 + declaration (name): "baz",
196 + scope: ScopeId(
197 + 1,
198 + ),
199 + },
200 + Reference {
201 + id: ReferenceId(
202 + 7,
203 + ),
204 + kind: Read,
205 + declaration: DeclarationId(
206 + 10,
207 + ),
208 + declaration (name): "props",
209 + scope: ScopeId(
210 + 1,
211 + ),
212 + },
213 + ],
214 + children: [
215 + Scope {
216 + id: ScopeId(
217 + 2,
218 + ),
219 + kind: Function,
220 + declarations: {
221 + "bar": Declaration {
222 + id: DeclarationId(
223 + 12,
224 + ),
225 + kind: Var,
226 + scope: ScopeId(
227 + 2,
228 + ),
229 + },
230 },
225 - ],
226 - },
227 - Scope {
228 - id: ScopeId(
229 - 4,
230 - ),
231 - kind: Block,
232 - declarations: {},
233 - references: [],
234 - children: [],
235 - },
236 - ],
237 - },
238 - ],
231 + references: [
232 + Reference {
233 + id: ReferenceId(
234 + 4,
235 + ),
236 + kind: Read,
237 + declaration: DeclarationId(
238 + 12,
239 + ),
240 + declaration (name): "bar",
241 + scope: ScopeId(
242 + 2,
243 + ),
244 + },
245 + Reference {
246 + id: ReferenceId(
247 + 5,
248 + ),
249 + kind: Write,
250 + declaration: DeclarationId(
251 + 12,
252 + ),
253 + declaration (name): "bar",
254 + scope: ScopeId(
255 + 2,
256 + ),
257 + },
258 + Reference {
259 + id: ReferenceId(
260 + 6,
261 + ),
262 + kind: Read,
263 + declaration: DeclarationId(
264 + 10,
265 + ),
266 + declaration (name): "props",
267 + scope: ScopeId(
268 + 2,
269 + ),
270 + },
271 + ],
272 + children: [
273 + Scope {
274 + id: ScopeId(
275 + 3,
276 + ),
277 + kind: Block,
278 + declarations: {},
279 + references: [],
280 + children: [],
281 + },
282 + ],
283 + },
284 + Scope {
285 + id: ScopeId(
286 + 4,
287 + ),
288 + kind: Block,
289 + declarations: {},
290 + references: [],
291 + children: [],
292 + },
293 + ],
294 + },
295 + ],
296 + },
297 }
298