[rust][sema] Test for function hoisting

This commit is contained in:
Joe Savona
2023-08-14 17:36:54 -07:00
parent 1ad0460693
commit f2a1a6a6e4
2 changed files with 279 additions and 0 deletions
@@ -0,0 +1,22 @@
function Component(props) {
foo();
if (props) {
{
for (;;) {
while (true) {
do {
function foo() {
bar();
if (props) {
const _ = function bar() {};
}
}
break;
} while (true);
break;
}
break;
}
}
}
}
@@ -0,0 +1,257 @@
---
source: crates/forget_semantic_analysis/tests/analysis_test.rs
expression: "format!(\"Input:\\n{input}\\n\\nAnalysis:\\n{output}\")"
input_file: crates/forget_semantic_analysis/tests/fixtures/function-hoisting.js
---
Input:
function Component(props) {
foo();
if (props) {
{
for (;;) {
while (true) {
do {
function foo() {
bar();
if (props) {
const _ = function bar() {};
}
}
break;
} while (true);
break;
}
break;
}
}
}
}
Analysis:
Scope {
id: ScopeId(
0,
),
kind: Global,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
1,
),
kind: Module,
declarations: {
"Component": Declaration {
id: DeclarationId(
0,
),
kind: FunctionDeclaration,
scope: ScopeId(
1,
),
},
},
references: [],
children: [
Scope {
id: ScopeId(
2,
),
kind: Function,
declarations: {
"props": Declaration {
id: DeclarationId(
1,
),
kind: FunctionDeclaration,
scope: ScopeId(
2,
),
},
"foo": Declaration {
id: DeclarationId(
2,
),
kind: FunctionDeclaration,
scope: ScopeId(
2,
),
},
},
references: [
Reference {
id: ReferenceId(
1,
),
kind: Read,
declaration: DeclarationId(
2,
),
declaration (name): "foo",
scope: ScopeId(
2,
),
},
Reference {
id: ReferenceId(
2,
),
kind: Read,
declaration: DeclarationId(
1,
),
declaration (name): "props",
scope: ScopeId(
2,
),
},
],
children: [
Scope {
id: ScopeId(
3,
),
kind: Block,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
4,
),
kind: Block,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
5,
),
kind: Block,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
6,
),
kind: Block,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
7,
),
kind: Block,
declarations: {},
references: [],
children: [
Scope {
id: ScopeId(
8,
),
kind: Function,
declarations: {},
references: [
Reference {
id: ReferenceId(
0,
),
kind: Read,
declaration: DeclarationId(
1,
),
declaration (name): "props",
scope: ScopeId(
8,
),
},
],
children: [
Scope {
id: ScopeId(
9,
),
kind: Block,
declarations: {
"_": Declaration {
id: DeclarationId(
3,
),
kind: Const,
scope: ScopeId(
9,
),
},
},
references: [],
children: [
Scope {
id: ScopeId(
10,
),
kind: Function,
declarations: {
"bar": Declaration {
id: DeclarationId(
4,
),
kind: FunctionDeclaration,
scope: ScopeId(
10,
),
},
},
references: [],
children: [
Scope {
id: ScopeId(
11,
),
kind: Function,
declarations: {},
references: [],
children: [],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
}
Diagnostic(
DiagnosticData {
message: "Undefined variable",
span: Some(
SourceSpan {
offset: SourceOffset(
0,
),
length: 1,
},
),
related_information: [],
severity: InvalidSyntax,
data: [],
},
)