Skip to content

Commit

Permalink
fix(es/typescript): Handle accessibility in private method (#8689)
Browse files Browse the repository at this point in the history
**Related issue:**

- Closes #8687
  • Loading branch information
magic-akari authored Mar 4, 2024
1 parent 5ae0a2f commit baba663
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pub enum SyntaxError {
TS2703,
TS4112,
TS8038,
TS18010,
TSTypeAnnotationAfterAssign,
TsNonNullAssertionNotAllowed(JsWord),

Expand Down Expand Up @@ -720,6 +721,9 @@ impl SyntaxError {
SyntaxError::TS8038 => "Decorators may not appear after `export` or `export default` \
if they also appear before `export`."
.into(),
SyntaxError::TS18010 => {
"An accessibility modifier cannot be used with a private identifier.".into()
}
SyntaxError::TSTypeAnnotationAfterAssign => {
"Type annotations must come before default assignments".into()
}
Expand Down
62 changes: 38 additions & 24 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,20 +1097,27 @@ impl<I: Tokens> Parser<I> {
}

Ok(match key {
Key::Private(key) => PrivateProp {
span: span!(p, start),
key,
value,
is_static,
decorators,
accessibility,
is_optional,
is_override,
readonly,
type_ann,
definite,
Key::Private(key) => {
let span = span!(p, start);
if accessibility.is_some() {
p.emit_err(span.with_hi(key.span_hi()), SyntaxError::TS18010);
}

PrivateProp {
span: span!(p, start),
key,
value,
is_static,
decorators,
accessibility,
is_optional,
is_override,
readonly,
type_ann,
definite,
}
.into()
}
.into(),
Key::Public(key) => {
let span = span!(p, start);
if is_abstract && value.is_some() {
Expand Down Expand Up @@ -1446,20 +1453,27 @@ impl<I: Tokens> Parser<I> {
}

match key {
Key::Private(key) => Ok(PrivateMethod {
span: span!(self, start),
Key::Private(key) => {
let span = span!(self, start);
if accessibility.is_some() {
self.emit_err(span.with_hi(key.span_hi()), SyntaxError::TS18010);
}

accessibility,
is_abstract,
is_optional,
is_override,
Ok(PrivateMethod {
span,

is_static,
key,
function,
kind,
accessibility,
is_abstract,
is_optional,
is_override,

is_static,
key,
function,
kind,
}
.into())
}
.into()),
Key::Public(key) => {
let span = span!(self, start);
if is_abstract && function.body.is_some() {
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/tests/errors/issue-8687/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {
private #p1() { }
private #p2: number;
}
16 changes: 16 additions & 0 deletions crates/swc_ecma_parser/tests/errors/issue-8687/input.ts.swc-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

x An accessibility modifier cannot be used with a private identifier.
,-[$DIR/tests/errors/issue-8687/input.ts:1:1]
1 | class Foo {
2 | private #p1() { }
: ^^^^^^^^^^^
3 | private #p2: number;
`----

x An accessibility modifier cannot be used with a private identifier.
,-[$DIR/tests/errors/issue-8687/input.ts:2:1]
2 | private #p1() { }
3 | private #p2: number;
: ^^^^^^^^^^^
4 | }
`----
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/strip_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ impl VisitMut for StripType {
prop.visit_mut_children_with(self);
}

fn visit_mut_private_method(&mut self, n: &mut PrivateMethod) {
n.accessibility = None;
n.is_abstract = false;
n.is_optional = false;
n.is_override = false;
n.visit_mut_children_with(self);
}

fn visit_mut_constructor(&mut self, n: &mut Constructor) {
n.accessibility = None;
n.visit_mut_children_with(self);
Expand Down

0 comments on commit baba663

Please sign in to comment.