Skip to content

Commit

Permalink
refactor(fe): pass object instead of parts
Browse files Browse the repository at this point in the history
report_error_if_variable_declaration_conflicts_in_scope accepts various
parts of Declared_Variable. Make callers pass the Declared_Variable
aggregate, simplifying code a bit.
  • Loading branch information
strager committed Oct 14, 2023
1 parent fde3478 commit f6098c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
28 changes: 13 additions & 15 deletions src/quick-lint-js/fe/variable-analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,6 @@ void Variable_Analyzer::declare_variable(Scope &scope, Identifier name,
QLJS_ASSERT(is_function_or_var);
}

this->report_error_if_variable_declaration_conflicts_in_scope(
scope, name, kind, flags, declared_scope);

if (is_function_or_var && name.normalized_name() == u8"eval"_sv) {
scope.used_eval_in_this_scope = false;
}

Declared_Variable declared = {
.declaration = name,
.kind = kind,
Expand All @@ -319,6 +312,13 @@ void Variable_Analyzer::declare_variable(Scope &scope, Identifier name,
.flags = flags,
};

this->report_error_if_variable_declaration_conflicts_in_scope(scope,
declared);

if (is_function_or_var && name.normalized_name() == u8"eval"_sv) {
scope.used_eval_in_this_scope = false;
}

erase_if(scope.variables_used, [&](const Used_Variable &used_var) {
if (name.normalized_name() != used_var.name.normalized_name()) {
return false;
Expand Down Expand Up @@ -933,22 +933,20 @@ void Variable_Analyzer::report_errors_for_variable_use(
}

void Variable_Analyzer::report_error_if_variable_declaration_conflicts_in_scope(
const Variable_Analyzer::Scope &scope, Identifier name, Variable_Kind kind,
Variable_Declaration_Flags flags,
Variable_Analyzer::Declared_Variable_Scope declaration_scope) const {
const Variable_Analyzer::Scope &scope, const Declared_Variable &var) const {
const Declared_Variable *already_declared_variable =
scope.declared_variables.find(name);
scope.declared_variables.find(var.declaration);
if (already_declared_variable) {
this->report_error_if_variable_declaration_conflicts(
/*already_declared=*/&already_declared_variable->declaration,
/*already_declared_kind=*/already_declared_variable->kind,
/*already_declared_flags=*/already_declared_variable->flags,
/*already_declared_declaration_scope=*/
already_declared_variable->declaration_scope,
/*newly_declared_name=*/name,
/*newly_declared_kind=*/kind,
/*newly_declared_flags=*/flags,
/*newly_declared_declaration_scope=*/declaration_scope);
/*newly_declared_name=*/var.declaration,
/*newly_declared_kind=*/var.kind,
/*newly_declared_flags=*/var.flags,
/*newly_declared_declaration_scope=*/var.declaration_scope);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/quick-lint-js/fe/variable-analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ class Variable_Analyzer final : public Parse_Visitor_Base {
bool use_is_before_declaration) const;

void report_error_if_variable_declaration_conflicts_in_scope(
const Scope &scope, Identifier name, Variable_Kind kind,
Variable_Declaration_Flags flags,
Declared_Variable_Scope declaration_scope) const;
const Scope &scope, const Declared_Variable &var) const;
void report_error_if_variable_declaration_conflicts_in_scope(
const Global_Scope &scope, const Declared_Variable &var) const;
void report_error_if_variable_declaration_conflicts(
Expand Down

0 comments on commit f6098c5

Please sign in to comment.