Skip to content

Commit 1816368

Browse files
bvanjoikdy1
authored andcommitted
refactor(es/parser): Reduce input_mut() calls (#10899)
1 parent 54c866a commit 1816368

File tree

13 files changed

+249
-258
lines changed

13 files changed

+249
-258
lines changed

.changeset/dry-onions-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_ecma_lexer: major
3+
---
4+
5+
refactor(es/parser): less input mut

crates/swc_ecma_lexer/src/common/parser/class_and_fn.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ pub fn parse_maybe_opt_binding_ident<'a>(
6363
}
6464

6565
fn parse_maybe_decorator_args<'a, P: Parser<'a>>(p: &mut P, expr: Box<Expr>) -> PResult<Box<Expr>> {
66-
let type_args = if p.input().syntax().typescript() && p.input_mut().is(&P::Token::LESS) {
66+
let type_args = if p.input().syntax().typescript() && p.input().is(&P::Token::LESS) {
6767
let ret = parse_ts_type_args(p)?;
6868
p.assert_and_bump(&P::Token::GREATER);
6969
Some(ret)
7070
} else {
7171
None
7272
};
7373

74-
if type_args.is_none() && !p.input_mut().is(&P::Token::LPAREN) {
74+
if type_args.is_none() && !p.input().is(&P::Token::LPAREN) {
7575
return Ok(expr);
7676
}
7777

@@ -97,14 +97,14 @@ pub fn parse_decorators<'a, P: Parser<'a>>(
9797
let mut decorators = Vec::new();
9898
let start = p.cur_pos();
9999

100-
while p.input_mut().is(&P::Token::AT) {
100+
while p.input().is(&P::Token::AT) {
101101
decorators.push(parse_decorator(p)?);
102102
}
103103
if decorators.is_empty() {
104104
return Ok(decorators);
105105
}
106106

107-
if p.input_mut().is(&P::Token::EXPORT) {
107+
if p.input().is(&P::Token::EXPORT) {
108108
if !p.ctx().contains(Context::InClass)
109109
&& !p.ctx().contains(Context::InFunction)
110110
&& !allow_export
@@ -118,7 +118,7 @@ pub fn parse_decorators<'a, P: Parser<'a>>(
118118
{
119119
syntax_error!(p, p.span(start), SyntaxError::DecoratorOnExport);
120120
}
121-
} else if !p.input_mut().is(&P::Token::CLASS) {
121+
} else if !p.input().is(&P::Token::CLASS) {
122122
// syntax_error!(p, p.span(start),
123123
// SyntaxError::InvalidLeadingDecorator)
124124
}
@@ -179,7 +179,7 @@ pub fn parse_super_class<'a, P: Parser<'a>>(
179179
// because in some cases "super class" returned by `parse_lhs_expr`
180180
// may not include `TsExprWithTypeArgs`
181181
// but it's a super class with type params, for example, in JSX.
182-
if p.syntax().typescript() && p.input_mut().is(&P::Token::LESS) {
182+
if p.syntax().typescript() && p.input().is(&P::Token::LESS) {
183183
let ret = parse_ts_type_args(p)?;
184184
p.assert_and_bump(&P::Token::GREATER);
185185
Ok((super_class, Some(ret)))
@@ -208,7 +208,7 @@ pub fn is_class_property<'a, P: Parser<'a>>(p: &mut P, asi: bool) -> bool {
208208
}
209209

210210
pub fn parse_class_prop_name<'a, P: Parser<'a>>(p: &mut P) -> PResult<Key> {
211-
if p.input_mut().is(&P::Token::HASH) {
211+
if p.input().is(&P::Token::HASH) {
212212
let name = parse_private_name(p)?;
213213
if name.name == "constructor" {
214214
p.emit_err(name.span, SyntaxError::PrivateConstructor);
@@ -237,9 +237,9 @@ where
237237
p.in_type(|p| {
238238
trace_cur!(p, parse_fn_args_body__type_params);
239239

240-
Ok(if p.input_mut().is(&P::Token::LESS) {
240+
Ok(if p.input().is(&P::Token::LESS) {
241241
Some(parse_ts_type_params(p, false, true)?)
242-
} else if p.input_mut().is(&P::Token::JSX_TAG_START) {
242+
} else if p.input().is(&P::Token::JSX_TAG_START) {
243243
debug_assert_eq!(
244244
p.input().token_context().current(),
245245
Some(TokenContext::JSXOpeningTag)
@@ -283,7 +283,7 @@ where
283283
expect!(p, &P::Token::RPAREN);
284284

285285
// typescript extension
286-
let return_type = if p.syntax().typescript() && p.input_mut().is(&P::Token::COLON) {
286+
let return_type = if p.syntax().typescript() && p.input().is(&P::Token::COLON) {
287287
parse_ts_type_or_type_predicate_ann(p, &P::Token::COLON).map(Some)?
288288
} else {
289289
None
@@ -599,7 +599,7 @@ pub fn parse_fn_block_or_expr_body<'a, P: Parser<'a>>(
599599
is_arrow_function,
600600
is_simple_parameter_list,
601601
|p, is_simple_parameter_list| {
602-
if p.input_mut().is(&P::Token::LBRACE) {
602+
if p.input().is(&P::Token::LBRACE) {
603603
parse_block(p, false)
604604
.map(|block_stmt| {
605605
if !is_simple_parameter_list {
@@ -629,7 +629,7 @@ fn parse_fn_body<'a, P: Parser<'a>, T>(
629629
) -> PResult<T> {
630630
if p.ctx().contains(Context::InDeclare)
631631
&& p.syntax().typescript()
632-
&& p.input_mut().is(&P::Token::LBRACE)
632+
&& p.input().is(&P::Token::LBRACE)
633633
{
634634
// p.emit_err(
635635
// p.ctx().span_of_fn_name.expect("we are not in function"),
@@ -692,7 +692,7 @@ pub(super) fn parse_fn_block_body<'a, P: Parser<'a>>(
692692
|p, is_simple_parameter_list| {
693693
// allow omitting body and allow placing `{` on next line
694694
if p.input().syntax().typescript()
695-
&& !p.input_mut().is(&P::Token::LBRACE)
695+
&& !p.input().is(&P::Token::LBRACE)
696696
&& p.eat_general_semi()
697697
{
698698
return Ok(None);
@@ -746,7 +746,7 @@ fn make_property<'a, P: Parser<'a>>(
746746
let type_ann = try_parse_ts_type_ann(p)?;
747747

748748
p.do_inside_of_context(Context::IncludeInExpr.union(Context::InClassField), |p| {
749-
let value = if p.input_mut().is(&P::Token::EQUAL) {
749+
let value = if p.input().is(&P::Token::EQUAL) {
750750
p.assert_and_bump(&P::Token::EQUAL);
751751
Some(parse_assignment_expr(p)?)
752752
} else {
@@ -926,7 +926,7 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
926926
}
927927
});
928928

929-
if is_static && p.input_mut().is(&P::Token::LBRACE) {
929+
if is_static && p.input().is(&P::Token::LBRACE) {
930930
if let Some(span) = declare_token {
931931
p.emit_err(span, SyntaxError::TS1184);
932932
}
@@ -935,7 +935,7 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
935935
}
936936
return parse_static_block(p, start);
937937
}
938-
if p.input_mut().is(&P::Token::STATIC) && peek!(p).is_some_and(|cur| cur.is_lbrace()) {
938+
if p.input().is(&P::Token::STATIC) && peek!(p).is_some_and(|cur| cur.is_lbrace()) {
939939
// For "readonly", "abstract" and "override"
940940
if let Some(span) = modifier_span {
941941
p.emit_err(span, SyntaxError::TS1184);
@@ -1013,7 +1013,7 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
10131013
p.emit_err(p.span(start), SyntaxError::TS1089(atom!("override")));
10141014
}
10151015

1016-
if p.syntax().typescript() && p.input_mut().is(&P::Token::LESS) {
1016+
if p.syntax().typescript() && p.input().is(&P::Token::LESS) {
10171017
let start = p.cur_pos();
10181018
if peek!(p).is_some_and(|cur| cur.is_less()) {
10191019
p.assert_and_bump(&P::Token::LESS);
@@ -1037,7 +1037,7 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
10371037
let params = parse_constructor_params(p)?;
10381038
expect!(p, &P::Token::RPAREN);
10391039

1040-
if p.syntax().typescript() && p.input_mut().is(&P::Token::COLON) {
1040+
if p.syntax().typescript() && p.input().is(&P::Token::COLON) {
10411041
let start = p.cur_pos();
10421042
let type_ann = parse_ts_type_ann(p, true, start)?;
10431043

@@ -1122,7 +1122,7 @@ fn parse_class_member_with_is_static<'a, P: Parser<'a>>(
11221122
}
11231123

11241124
let is_next_line_generator =
1125-
p.input_mut().had_line_break_before_cur() && p.input_mut().is(&P::Token::MUL);
1125+
p.input_mut().had_line_break_before_cur() && p.input().is(&P::Token::MUL);
11261126
let getter_or_setter_ident = match key {
11271127
// `get\n*` is an uninitialized property named 'get' followed by a generator.
11281128
Key::Public(PropName::Ident(ref i))
@@ -1317,7 +1317,7 @@ fn parse_class_member<'a, P: Parser<'a>>(p: &mut P) -> PResult<ClassMember> {
13171317
},
13181318
);
13191319
} else if is_class_property(p, /* asi */ true)
1320-
|| (p.syntax().typescript() && p.input_mut().is(&P::Token::QUESTION))
1320+
|| (p.syntax().typescript() && p.input().is(&P::Token::QUESTION))
13211321
{
13221322
// Property named `declare`
13231323

@@ -1395,7 +1395,7 @@ fn parse_class_member<'a, P: Parser<'a>>(p: &mut P) -> PResult<ClassMember> {
13951395
},
13961396
);
13971397
} else if is_class_property(p, /* asi */ true)
1398-
|| (p.syntax().typescript() && p.input_mut().is(&P::Token::QUESTION))
1398+
|| (p.syntax().typescript() && p.input().is(&P::Token::QUESTION))
13991399
{
14001400
// Property named `accessor`
14011401

@@ -1450,14 +1450,14 @@ fn parse_class_member<'a, P: Parser<'a>>(p: &mut P) -> PResult<ClassMember> {
14501450
},
14511451
);
14521452
} else if is_class_property(p, /* asi */ false)
1453-
|| (p.syntax().typescript() && p.input_mut().is(&P::Token::QUESTION))
1453+
|| (p.syntax().typescript() && p.input().is(&P::Token::QUESTION))
14541454
{
14551455
// Property named `static`
14561456

14571457
// Avoid to parse
14581458
// static
14591459
// {}
1460-
let is_parsing_static_blocks = p.input_mut().is(&P::Token::LBRACE);
1460+
let is_parsing_static_blocks = p.input().is(&P::Token::LBRACE);
14611461
if !is_parsing_static_blocks {
14621462
let key = Key::Public(PropName::Ident(IdentName::new(
14631463
atom!("static"),
@@ -1499,7 +1499,7 @@ fn parse_class_member<'a, P: Parser<'a>>(p: &mut P) -> PResult<ClassMember> {
14991499
fn parse_class_body<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<ClassMember>> {
15001500
let mut elems = Vec::with_capacity(32);
15011501
let mut has_constructor_with_body = false;
1502-
while !eof!(p) && !p.input_mut().is(&P::Token::RBRACE) {
1502+
while !eof!(p) && !p.input().is(&P::Token::RBRACE) {
15031503
if p.input_mut().eat(&P::Token::SEMI) {
15041504
let span = p.input().prev_span();
15051505
debug_assert!(span.lo <= span.hi);

0 commit comments

Comments
 (0)