mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 04:39:06 +03:00
feat(es/parser): Allow parsing files with missing close braces (#4187)
This commit is contained in:
parent
b67c104877
commit
cffe5ca58e
@ -157,7 +157,16 @@ impl<'a, I: Tokens> Parser<I> {
|
|||||||
..p.ctx()
|
..p.ctx()
|
||||||
})
|
})
|
||||||
.parse_class_body()?;
|
.parse_class_body()?;
|
||||||
|
|
||||||
|
if p.input.cur().is_none() {
|
||||||
|
let eof_text = p.input.dump_cur();
|
||||||
|
p.emit_err(
|
||||||
|
p.input.cur_span(),
|
||||||
|
SyntaxError::Expected(&Token::RBrace, eof_text),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
expect!(p, '}');
|
expect!(p, '}');
|
||||||
|
}
|
||||||
let end = last_pos!(p);
|
let end = last_pos!(p);
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
|
@ -16,7 +16,7 @@ impl<'a, I: Tokens> Parser<I> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
mut allow_directives: bool,
|
mut allow_directives: bool,
|
||||||
top_level: bool,
|
top_level: bool,
|
||||||
end: Option<&Token>,
|
end: Option<&'static Token>,
|
||||||
) -> PResult<Vec<Type>>
|
) -> PResult<Vec<Type>>
|
||||||
where
|
where
|
||||||
Self: StmtLikeParser<'a, Type>,
|
Self: StmtLikeParser<'a, Type>,
|
||||||
@ -28,8 +28,17 @@ impl<'a, I: Tokens> Parser<I> {
|
|||||||
|
|
||||||
let stmts = Arena::new();
|
let stmts = Arena::new();
|
||||||
while {
|
while {
|
||||||
|
if self.input.cur().is_none() && end.is_some() {
|
||||||
|
let eof_text = self.input.dump_cur();
|
||||||
|
self.emit_err(
|
||||||
|
self.input.cur_span(),
|
||||||
|
SyntaxError::Expected(end.unwrap(), eof_text),
|
||||||
|
);
|
||||||
|
false
|
||||||
|
} else {
|
||||||
let c = cur!(self, false).ok();
|
let c = cur!(self, false).ok();
|
||||||
c != end
|
c != end
|
||||||
|
}
|
||||||
} {
|
} {
|
||||||
let stmt = self.parse_stmt_like(true, top_level)?;
|
let stmt = self.parse_stmt_like(true, top_level)?;
|
||||||
if allow_directives {
|
if allow_directives {
|
||||||
@ -54,7 +63,7 @@ impl<'a, I: Tokens> Parser<I> {
|
|||||||
stmts.alloc(stmt);
|
stmts.alloc(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
if end.is_some() {
|
if self.input.cur().is_some() && end.is_some() {
|
||||||
bump!(self);
|
bump!(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
class Test {
|
||||||
|
|
||||||
|
method() {}
|
@ -0,0 +1,84 @@
|
|||||||
|
|
||||||
|
x Module
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x ModuleItem
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Stmt
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Decl
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x ClassDecl
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Ident
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | class Test {
|
||||||
|
: ^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Class
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:1:1]
|
||||||
|
1 | ,-> class Test {
|
||||||
|
2 | |
|
||||||
|
3 | `-> method() {}
|
||||||
|
`----
|
||||||
|
|
||||||
|
x ClassMember
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^^^^^^^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x ClassMethod
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^^^^^^^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x PropName
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Ident
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x Function
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^^^^^^^^^^
|
||||||
|
`----
|
||||||
|
|
||||||
|
x BlockStmt
|
||||||
|
,-[$DIR/tests/span/js/decl/class-no-close-brace.js:3:3]
|
||||||
|
3 | method() {}
|
||||||
|
: ^^
|
||||||
|
`----
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
x Unexpected eof
|
x Expected '}', got '<eof>'
|
||||||
,-[$DIR/tests/test262-parser/fail/021fb596db81e6d0.js:1:1]
|
,-[$DIR/tests/test262-parser/fail/021fb596db81e6d0.js:1:1]
|
||||||
1 | {
|
1 | {
|
||||||
: ^
|
: ^
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
x Unexpected eof
|
x Expected '}', got '<eof>'
|
||||||
,-[$DIR/tests/test262-parser/fail/4d579849c75cfef9.js:4:1]
|
,-[$DIR/tests/test262-parser/fail/4d579849c75cfef9.js:4:1]
|
||||||
4 | {
|
4 | {
|
||||||
: ^
|
: ^
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
x Unexpected eof
|
x Expected '}', got '<eof>'
|
||||||
,-[$DIR/tests/test262-parser/fail/50efa1f220e37136.js:1:1]
|
,-[$DIR/tests/test262-parser/fail/50efa1f220e37136.js:1:1]
|
||||||
1 | { ; ;
|
1 | { ; ;
|
||||||
: ^
|
: ^
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
x Unexpected eof
|
x Expected '}', got '<eof>'
|
||||||
,-[$DIR/tests/test262-parser/fail/77fe5a8d6ae33dd1.js:1:1]
|
,-[$DIR/tests/test262-parser/fail/77fe5a8d6ae33dd1.js:1:1]
|
||||||
1 | function t() { ; ;
|
1 | function t() { ; ;
|
||||||
: ^
|
: ^
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
const t = () => {
|
||||||
|
console.log(5);
|
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
x Expected '}', got '<eof>'
|
||||||
|
,-[$DIR/tests/typescript-errors/arrow-function/missing-closing-brace/input.ts:2:3]
|
||||||
|
2 | console.log(5);
|
||||||
|
: ^
|
||||||
|
`----
|
@ -0,0 +1,2 @@
|
|||||||
|
class Class {
|
||||||
|
prop: string;
|
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
x Expected '}', got '<eof>'
|
||||||
|
,-[$DIR/tests/typescript-errors/class/missing-closing-brace/input.ts:2:3]
|
||||||
|
2 | prop: string;
|
||||||
|
: ^
|
||||||
|
`----
|
Loading…
Reference in New Issue
Block a user