mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 12:49:21 +03:00
Fix class and interface spans (#566)
The interface span for something like `interface Test {}` was `Test {}`. Also, for stuff like `export default abstract class Test{}` it was `class Test{}` instead of `abstract class Test {}`.
This commit is contained in:
parent
3e0f4a5bd7
commit
8e3827403e
@ -45,31 +45,43 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
self.parse_fn(None, decorators)
|
||||
}
|
||||
|
||||
pub(super) fn parse_class_decl(&mut self, decorators: Vec<Decorator>) -> PResult<'a, Decl> {
|
||||
self.parse_class(decorators)
|
||||
pub(super) fn parse_class_decl(
|
||||
&mut self,
|
||||
start: BytePos,
|
||||
class_start: BytePos,
|
||||
decorators: Vec<Decorator>,
|
||||
) -> PResult<'a, Decl> {
|
||||
self.parse_class(start, class_start, decorators)
|
||||
}
|
||||
|
||||
pub(super) fn parse_class_expr(
|
||||
&mut self,
|
||||
start: BytePos,
|
||||
decorators: Vec<Decorator>,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
self.parse_class(decorators)
|
||||
self.parse_class(start, start, decorators)
|
||||
}
|
||||
|
||||
pub(super) fn parse_default_class(
|
||||
&mut self,
|
||||
start: BytePos,
|
||||
class_start: BytePos,
|
||||
decorators: Vec<Decorator>,
|
||||
) -> PResult<'a, ExportDefaultDecl> {
|
||||
self.parse_class(decorators)
|
||||
self.parse_class(start, class_start, decorators)
|
||||
}
|
||||
|
||||
fn parse_class<T>(&mut self, decorators: Vec<Decorator>) -> PResult<'a, T>
|
||||
fn parse_class<T>(
|
||||
&mut self,
|
||||
start: BytePos,
|
||||
class_start: BytePos,
|
||||
decorators: Vec<Decorator>,
|
||||
) -> PResult<'a, T>
|
||||
where
|
||||
T: OutputType,
|
||||
Self: MaybeOptionalIdentParser<'a, T::Ident>,
|
||||
{
|
||||
self.strict_mode().parse_with(|p| {
|
||||
let start = cur_pos!();
|
||||
expect!("class");
|
||||
|
||||
let ident = p.parse_maybe_opt_binding_ident()?;
|
||||
@ -156,7 +168,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
span!(start),
|
||||
ident,
|
||||
Class {
|
||||
span: Span::new(start, end, Default::default()),
|
||||
span: Span::new(class_start, end, Default::default()),
|
||||
decorators,
|
||||
is_abstract: false,
|
||||
type_params,
|
||||
|
@ -253,7 +253,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
if is!("function") {
|
||||
return self.parse_fn_expr();
|
||||
} else if is!("class") {
|
||||
return self.parse_class_expr(decorators);
|
||||
return self.parse_class_expr(start, decorators);
|
||||
}
|
||||
|
||||
// Literals
|
||||
|
@ -164,7 +164,9 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
if !include_decl {
|
||||
unexpected!()
|
||||
}
|
||||
return self.parse_class_decl(decorators).map(Stmt::from);
|
||||
return self
|
||||
.parse_class_decl(start, start, decorators)
|
||||
.map(Stmt::from);
|
||||
}
|
||||
|
||||
if is!("if") {
|
||||
@ -313,7 +315,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
if eat!("interface") {
|
||||
self.emit_err(i.span, SyntaxError::TS2427);
|
||||
return self
|
||||
.parse_ts_interface_decl()
|
||||
.parse_ts_interface_decl(start)
|
||||
.map(Decl::from)
|
||||
.map(Stmt::from);
|
||||
}
|
||||
|
@ -157,13 +157,14 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
let start = cur_pos!();
|
||||
assert_and_bump!("export");
|
||||
let _ = cur!(true);
|
||||
let after_export_start = cur_pos!();
|
||||
|
||||
// "export declare" is equivalent to just "export".
|
||||
let declare = self.input.syntax().typescript() && eat!("declare");
|
||||
|
||||
if declare {
|
||||
// TODO: Remove
|
||||
if let Some(decl) = self.try_parse_ts_declare(start, decorators.clone())? {
|
||||
if let Some(decl) = self.try_parse_ts_declare(after_export_start, decorators.clone())? {
|
||||
return Ok(ModuleDecl::ExportDecl(ExportDecl {
|
||||
span: span!(start),
|
||||
decl,
|
||||
@ -250,11 +251,11 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
if export_ns.is_none() && eat!("default") {
|
||||
if self.input.syntax().typescript() {
|
||||
if is!("abstract") && peeked_is!("class") {
|
||||
let start = cur_pos!();
|
||||
let class_start = cur_pos!();
|
||||
assert_and_bump!("abstract");
|
||||
let _ = cur!(true);
|
||||
|
||||
let mut class = self.parse_default_class(decorators)?;
|
||||
let mut class = self.parse_default_class(start, class_start, decorators)?;
|
||||
match class {
|
||||
ExportDefaultDecl {
|
||||
decl: DefaultDecl::Class(ClassExpr { ref mut class, .. }),
|
||||
@ -265,8 +266,12 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
return Ok(class.into());
|
||||
}
|
||||
|
||||
if eat!("interface") {
|
||||
let decl = self.parse_ts_interface_decl().map(DefaultDecl::from)?;
|
||||
if is!("interface") {
|
||||
let interface_start = cur_pos!();
|
||||
assert_and_bump!("interface");
|
||||
let decl = self
|
||||
.parse_ts_interface_decl(interface_start)
|
||||
.map(DefaultDecl::from)?;
|
||||
return Ok(ExportDefaultDecl {
|
||||
span: span!(start),
|
||||
decl,
|
||||
@ -276,7 +281,8 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
}
|
||||
|
||||
if is!("class") {
|
||||
let decl = self.parse_default_class(decorators)?;
|
||||
let class_start = cur_pos!();
|
||||
let decl = self.parse_default_class(start, class_start, decorators)?;
|
||||
return Ok(ModuleDecl::ExportDefaultDecl(decl));
|
||||
} else if is!("async")
|
||||
&& peeked_is!("function")
|
||||
@ -302,7 +308,8 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
}
|
||||
|
||||
let decl = if is!("class") {
|
||||
self.parse_class_decl(decorators)?
|
||||
let class_start = cur_pos!();
|
||||
self.parse_class_decl(start, class_start, decorators)?
|
||||
} else if is!("async")
|
||||
&& peeked_is!("function")
|
||||
&& !self.input.has_linebreak_between_cur_and_peeked()
|
||||
|
@ -859,11 +859,12 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
})
|
||||
}
|
||||
/// `tsParseInterfaceDeclaration`
|
||||
pub(super) fn parse_ts_interface_decl(&mut self) -> PResult<'a, TsInterfaceDecl> {
|
||||
pub(super) fn parse_ts_interface_decl(
|
||||
&mut self,
|
||||
start: BytePos,
|
||||
) -> PResult<'a, TsInterfaceDecl> {
|
||||
debug_assert!(self.input.syntax().typescript());
|
||||
|
||||
let start = cur_pos!();
|
||||
|
||||
let id = self.parse_ident_name()?;
|
||||
match id.sym {
|
||||
js_word!("string")
|
||||
@ -1925,7 +1926,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
|
||||
if is!("class") {
|
||||
return p
|
||||
.parse_class_decl(decorators)
|
||||
.parse_class_decl(start, start, decorators)
|
||||
.map(|decl| match decl {
|
||||
Decl::Class(c) => Decl::Class(ClassDecl { declare: true, ..c }),
|
||||
_ => decl,
|
||||
@ -2007,7 +2008,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
if next {
|
||||
bump!();
|
||||
}
|
||||
let mut decl = self.parse_class_decl(decorators)?;
|
||||
let mut decl = self.parse_class_decl(start, start, decorators)?;
|
||||
match decl {
|
||||
Decl::Class(ClassDecl {
|
||||
class:
|
||||
@ -2041,7 +2042,10 @@ impl<'a, I: Tokens> Parser<'a, I> {
|
||||
bump!();
|
||||
}
|
||||
|
||||
return self.parse_ts_interface_decl().map(From::from).map(Some);
|
||||
return self
|
||||
.parse_ts_interface_decl(start)
|
||||
.map(From::from)
|
||||
.map(Some);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": false,
|
||||
"span": {
|
||||
"start": 9,
|
||||
"start": 0,
|
||||
"end": 19,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -48,7 +48,7 @@
|
||||
},
|
||||
"declare": true,
|
||||
"span": {
|
||||
"start": 37,
|
||||
"start": 20,
|
||||
"end": 47,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -82,7 +82,7 @@
|
||||
},
|
||||
"declare": false,
|
||||
"span": {
|
||||
"start": 64,
|
||||
"start": 55,
|
||||
"end": 74,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -98,7 +98,7 @@
|
||||
{
|
||||
"type": "ExportDefaultDeclaration",
|
||||
"span": {
|
||||
"start": 154,
|
||||
"start": 130,
|
||||
"end": 163,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -106,7 +106,7 @@
|
||||
"type": "ClassExpression",
|
||||
"identifier": null,
|
||||
"span": {
|
||||
"start": 154,
|
||||
"start": 145,
|
||||
"end": 163,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -122,7 +122,7 @@
|
||||
{
|
||||
"type": "ExportDefaultDeclaration",
|
||||
"span": {
|
||||
"start": 188,
|
||||
"start": 164,
|
||||
"end": 199,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -140,7 +140,7 @@
|
||||
"optional": false
|
||||
},
|
||||
"span": {
|
||||
"start": 188,
|
||||
"start": 179,
|
||||
"end": 199,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": true,
|
||||
"span": {
|
||||
"start": 8,
|
||||
"start": 0,
|
||||
"end": 87,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": true,
|
||||
"span": {
|
||||
"start": 8,
|
||||
"start": 0,
|
||||
"end": 39,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": false,
|
||||
"span": {
|
||||
"start": 49,
|
||||
"start": 40,
|
||||
"end": 295,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": false,
|
||||
"span": {
|
||||
"start": 51,
|
||||
"start": 42,
|
||||
"end": 256,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -21,7 +21,7 @@
|
||||
},
|
||||
"declare": false,
|
||||
"span": {
|
||||
"start": 9,
|
||||
"start": 0,
|
||||
"end": 396,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 18,
|
||||
"start": 0,
|
||||
"end": 22,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -16,7 +16,7 @@
|
||||
"declaration": {
|
||||
"type": "TsEnumDeclaration",
|
||||
"span": {
|
||||
"start": 0,
|
||||
"start": 7,
|
||||
"end": 30,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 11,
|
||||
"start": 1,
|
||||
"end": 188,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -16,7 +16,7 @@
|
||||
"decl": {
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 25,
|
||||
"start": 15,
|
||||
"end": 31,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -136,7 +136,7 @@
|
||||
},
|
||||
"declare": true,
|
||||
"span": {
|
||||
"start": 82,
|
||||
"start": 74,
|
||||
"end": 92,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -159,7 +159,7 @@
|
||||
"declaration": {
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 118,
|
||||
"start": 100,
|
||||
"end": 122,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -198,7 +198,7 @@
|
||||
"declaration": {
|
||||
"type": "TsTypeAliasDeclaration",
|
||||
"span": {
|
||||
"start": 123,
|
||||
"start": 130,
|
||||
"end": 154,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -236,7 +236,7 @@
|
||||
"declaration": {
|
||||
"type": "TsModuleDeclaration",
|
||||
"span": {
|
||||
"start": 155,
|
||||
"start": 162,
|
||||
"end": 181,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -274,7 +274,7 @@
|
||||
"declaration": {
|
||||
"type": "TsModuleDeclaration",
|
||||
"span": {
|
||||
"start": 182,
|
||||
"start": 189,
|
||||
"end": 211,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 38,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 42,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -16,7 +16,7 @@
|
||||
"declaration": {
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 17,
|
||||
"start": 7,
|
||||
"end": 21,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -55,7 +55,7 @@
|
||||
"decl": {
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 47,
|
||||
"start": 37,
|
||||
"end": 51,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 48,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 40,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 80,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 61,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 31,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 65,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 39,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 53,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 78,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 32,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 40,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -125,7 +125,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 51,
|
||||
"start": 41,
|
||||
"end": 80,
|
||||
"ctxt": 0
|
||||
},
|
||||
@ -241,7 +241,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 91,
|
||||
"start": 81,
|
||||
"end": 130,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -84,7 +84,7 @@
|
||||
"declaration": {
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 58,
|
||||
"start": 48,
|
||||
"end": 62,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
{
|
||||
"type": "TsInterfaceDeclaration",
|
||||
"span": {
|
||||
"start": 10,
|
||||
"start": 0,
|
||||
"end": 40,
|
||||
"ctxt": 0
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user