Skip to content

Commit

Permalink
fix(semantic): syntax error for undeclared private field access
Browse files Browse the repository at this point in the history
closes #7582
  • Loading branch information
Boshen committed Dec 4, 2024
1 parent 2158c38 commit 5e00c58
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
14 changes: 5 additions & 9 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,13 @@ fn private_field_undeclared(x0: &str, span1: Span) -> OxcDiagnostic {

fn check_private_identifier(ctx: &SemanticBuilder<'_>) {
if let Some(class_id) = ctx.class_table_builder.current_class_id {
ctx.class_table_builder.classes.iter_private_identifiers(class_id).for_each(|reference| {
if reference.element_ids.is_empty()
&& !ctx.class_table_builder.classes.ancestors(class_id).skip(1).any(|class_id| {
ctx.class_table_builder
.classes
.has_private_definition(class_id, &reference.name)
})
{
for reference in ctx.class_table_builder.classes.iter_private_identifiers(class_id) {
if !ctx.class_table_builder.classes.ancestors(class_id).any(|class_id| {
ctx.class_table_builder.classes.has_private_definition(class_id, &reference.name)
}) {
ctx.error(private_field_undeclared(&reference.name, reference.span));
}
});
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions crates/oxc_semantic/src/class/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ pub struct ClassTable {
pub parent_ids: FxHashMap<ClassId, ClassId>,
pub declarations: IndexVec<ClassId, NodeId>,
pub elements: IndexVec<ClassId, IndexVec<ElementId, Element>>,
// PrivateIdentifier reference
pub private_identifiers: IndexVec<ClassId, Vec<PrivateIdentifierReference>>,
pub private_identifier_references: IndexVec<ClassId, Vec<PrivateIdentifierReference>>,
}

impl ClassTable {
Expand All @@ -70,7 +69,7 @@ impl ClassTable {
&self,
class_id: ClassId,
) -> impl Iterator<Item = &PrivateIdentifierReference> + '_ {
self.private_identifiers[class_id].iter()
self.private_identifier_references[class_id].iter()
}

pub fn get_node_id(&self, class_id: ClassId) -> NodeId {
Expand Down Expand Up @@ -108,7 +107,7 @@ impl ClassTable {
self.parent_ids.insert(class_id, parent_id);
};
self.elements.push(IndexVec::default());
self.private_identifiers.push(Vec::new());
self.private_identifier_references.push(Vec::new());
class_id
}

Expand All @@ -121,6 +120,6 @@ impl ClassTable {
class_id: ClassId,
private_identifier_reference: PrivateIdentifierReference,
) {
self.private_identifiers[class_id].push(private_identifier_reference);
self.private_identifier_references[class_id].push(private_identifier_reference);
}
}
3 changes: 2 additions & 1 deletion crates/oxc_semantic/tests/integration/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ impl<'a> SemanticTester<'a> {
match (self.expect_errors, semantic_ret.errors.is_empty()) {
(true, true) => panic!("Expected errors, but none were produced"),
(false, false) => panic!(
"Semantic analysis failed:\n\n{}",
"Semantic analysis failed:\n\n{}\n\n{}",
self.source_text,
semantic_ret
.errors
.iter()
Expand Down
6 changes: 6 additions & 0 deletions tasks/coverage/misc/fail/oxc-7582.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class C {
x = 1;
method() {
obj.#x;
}
}
10 changes: 9 additions & 1 deletion tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_misc Summary:
AST Parsed : 30/30 (100.00%)
Positive Passed: 30/30 (100.00%)
Negative Passed: 25/25 (100.00%)
Negative Passed: 26/26 (100.00%)

× Unexpected token
╭─[misc/fail/oxc-169.js:2:1]
Expand Down Expand Up @@ -264,6 +264,14 @@ Negative Passed: 25/25 (100.00%)
3 │ }
╰────

× Private field 'x' must be declared in an enclosing class
╭─[misc/fail/oxc-7582.js:4:9]
3 │ method() {
4obj.#x;
· ──
5 │ }
╰────

× The keyword 'let' is reserved
╭─[misc/fail/oxc.js:3:1]
2
Expand Down

0 comments on commit 5e00c58

Please sign in to comment.