Skip to content

Commit

Permalink
refactor(fe): create Used_Variable_Kind for 'export default'
Browse files Browse the repository at this point in the history
In order to implement some TypeScript-specific 'export default' logic,
we need to know if a variable use came from 'export default'. Track
this information with a new Used_Variable_Kind.
  • Loading branch information
strager committed Nov 21, 2023
1 parent 4c221b8 commit df1cb1e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/quick-lint-js/fe/variable-analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ void Variable_Analyzer::declare_variable(Scope &scope, Identifier name,
switch (used_var.kind) {
case Used_Variable_Kind::assignment:
break;
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::use:
if (kind == Variable_Kind::_class || kind == Variable_Kind::_const ||
Expand Down Expand Up @@ -399,6 +400,7 @@ void Variable_Analyzer::declare_variable(Scope &scope, Identifier name,
}
break;
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_export_default:
// TODO(strager): This shouldn't happen. export statements are
// not allowed inside functions.
break;
Expand Down Expand Up @@ -473,7 +475,7 @@ void Variable_Analyzer::visit_variable_delete_use(
}

void Variable_Analyzer::visit_variable_export_default_use(Identifier name) {
this->visit_variable_use(name, Used_Variable_Kind::use);
this->visit_variable_use(name, Used_Variable_Kind::_export_default);
}

void Variable_Analyzer::visit_variable_export_use(Identifier name) {
Expand Down Expand Up @@ -579,8 +581,9 @@ void Variable_Analyzer::visit_end_of_module() {
// propagate_variable_uses_to_parent_scope should have already removed it
// from variables_used and variables_used_in_descendant_scope.
switch (var.kind) {
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_delete:
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::assignment:
case Used_Variable_Kind::use:
Expand Down Expand Up @@ -612,6 +615,7 @@ void Variable_Analyzer::visit_end_of_module() {
Diag_Use_Of_Undeclared_Type{.name = used_var.name.span()});
break;
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::use:
this->diag_reporter_->report(
Diag_Use_Of_Undeclared_Variable{.name = used_var.name.span()});
Expand Down Expand Up @@ -693,6 +697,7 @@ void Variable_Analyzer::propagate_variable_uses_to_parent_scope(
var = parent_scope.declared_variables.find(used_var.name);
break;
case Used_Variable_Kind::_delete:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::assignment:
case Used_Variable_Kind::use:
Expand Down Expand Up @@ -735,6 +740,7 @@ void Variable_Analyzer::propagate_variable_uses_to_parent_scope(
var = parent_scope.declared_variables.find(used_var.name);
break;
case Used_Variable_Kind::_delete:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::assignment:
case Used_Variable_Kind::use:
Expand Down Expand Up @@ -1144,6 +1150,7 @@ bool Variable_Analyzer::Used_Variable::is_runtime() const {
switch (this->kind) {
case Used_Variable_Kind::_delete:
case Used_Variable_Kind::_export:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::assignment:
case Used_Variable_Kind::use:
Expand All @@ -1160,6 +1167,7 @@ bool Variable_Analyzer::Used_Variable::is_type() const {
case Used_Variable_Kind::type:
return true;
case Used_Variable_Kind::_delete:
case Used_Variable_Kind::_export_default:
case Used_Variable_Kind::_typeof:
case Used_Variable_Kind::assignment:
case Used_Variable_Kind::use:
Expand Down
1 change: 1 addition & 0 deletions src/quick-lint-js/fe/variable-analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Variable_Analyzer final : public Parse_Visitor_Base {
enum class Used_Variable_Kind {
_delete,
_export,
_export_default,
_typeof,
assignment,
type, // TypeScript only.
Expand Down

0 comments on commit df1cb1e

Please sign in to comment.