Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span of nodes with decorators, parameter properties, and class members with accessibility #581

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions ecmascript/parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,8 @@ impl<'a, I: Tokens> Parser<'a, I> {
}

fn parse_class_member(&mut self) -> PResult<'a, ClassMember> {
let decorators = self.parse_decorators(false)?;

let start = cur_pos!();
let decorators = self.parse_decorators(false)?;

if eat!("declare") {
self.emit_err(self.input.prev_span(), SyntaxError::TS1031);
Expand Down Expand Up @@ -364,19 +363,18 @@ impl<'a, I: Tokens> Parser<'a, I> {
}
}

self.parse_class_member_with_is_static(accessibility, static_token, decorators)
self.parse_class_member_with_is_static(start, accessibility, static_token, decorators)
}

#[allow(clippy::cognitive_complexity)]
fn parse_class_member_with_is_static(
&mut self,
start: BytePos,
accessibility: Option<Accessibility>,
static_token: Option<Span>,
decorators: Vec<Decorator>,
) -> PResult<'a, ClassMember> {
let is_static = static_token.is_some();
let start = static_token.map(|s| s.lo()).unwrap_or(cur_pos!());

let modifier = self.parse_ts_modifier(&["abstract", "readonly"])?;
let (is_abstract, readonly) = match modifier {
Some("abstract") => (true, self.parse_ts_modifier(&["readonly"])?.is_some()),
Expand Down
13 changes: 7 additions & 6 deletions ecmascript/parser/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ impl<'a, I: Tokens> Parser<'a, I> {
}

fn parse_constructor_param(&mut self) -> PResult<'a, PatOrTsParamProp> {
let start = cur_pos!();
let decorators = self.parse_decorators(false)?;

let start = cur_pos!();
let (accessibility, readonly) = if self.input.syntax().typescript() {
let accessibility = self.parse_access_modifier()?;
(
Expand All @@ -290,16 +290,17 @@ impl<'a, I: Tokens> Parser<'a, I> {
if accessibility == None && !readonly {
self.parse_formal_param().map(PatOrTsParamProp::from)
} else {
let param = match self.parse_formal_param()? {
Pat::Ident(i) => TsParamPropParam::Ident(i),
Pat::Assign(a) => TsParamPropParam::Assign(a),
node => syntax_error!(node.span(), SyntaxError::TsInvalidParamPropPat),
};
Ok(PatOrTsParamProp::TsParamProp(TsParamProp {
span: span!(start),
accessibility,
readonly,
decorators,
param: match self.parse_formal_param()? {
Pat::Ident(i) => TsParamPropParam::Ident(i),
Pat::Assign(a) => TsParamPropParam::Assign(a),
node => syntax_error!(node.span(), SyntaxError::TsInvalidParamPropPat),
},
param,
}))
}
}
Expand Down
6 changes: 3 additions & 3 deletions ecmascript/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ impl<'a, I: Tokens> Parser<'a, I> {
Self: StmtLikeParser<'a, Type>,
Type: IsDirective + From<Stmt>,
{
let start = cur_pos!();
let decorators = self.parse_decorators(true)?;

if is_one_of!("import", "export") {
return self.handle_import_export(top_level, decorators);
}

self.parse_stmt_internal(include_decl, top_level, decorators)
self.parse_stmt_internal(start, include_decl, top_level, decorators)
.map(From::from)
}

/// `parseStatementContent`
#[allow(clippy::cognitive_complexity)]
fn parse_stmt_internal(
&mut self,
start: BytePos,
include_decl: bool,
top_level: bool,
decorators: Vec<Decorator>,
) -> PResult<'a, Stmt> {
let start = cur_pos!();

if self.input.syntax().typescript() && is!("const") && peeked_is!("enum") {
assert_and_bump!("const");
let _ = cur!(true)?;
Expand Down
18 changes: 18 additions & 0 deletions ecmascript/parser/tests/typescript/class/decorators/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@dec
class C {
constructor(@dec public param: string) {
}

@dec
@dec2
method() {}

@dec
prop;

@dec
get prop2() { return 5; }

@dec
static method() {}
}
Loading