fix(es/parser): Fix span of ExportDefaultDeclaration (#1818)

This commit is contained in:
Vladimir Guguiev 2021-06-27 04:02:45 +02:00 committed by GitHub
parent 33f2ab2d79
commit 7488950f90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 113 additions and 54 deletions

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.61.0"
version = "0.61.1"
[features]
default = []

View File

@ -10,38 +10,40 @@ impl<'a, I: Tokens> Parser<I> {
pub(super) fn parse_async_fn_expr(&mut self) -> PResult<Box<Expr>> {
let start = cur_pos!(self);
expect!(self, "async");
self.parse_fn(Some(start), vec![])
self.parse_fn(None, Some(start), vec![])
}
/// Parse function expression
pub(super) fn parse_fn_expr(&mut self) -> PResult<Box<Expr>> {
self.parse_fn(None, vec![])
self.parse_fn(None, None, vec![])
}
pub(super) fn parse_async_fn_decl(&mut self, decorators: Vec<Decorator>) -> PResult<Decl> {
let start = cur_pos!(self);
expect!(self, "async");
self.parse_fn(Some(start), decorators)
self.parse_fn(None, Some(start), decorators)
}
pub(super) fn parse_fn_decl(&mut self, decorators: Vec<Decorator>) -> PResult<Decl> {
self.parse_fn(None, decorators)
self.parse_fn(None, None, decorators)
}
pub(super) fn parse_default_async_fn(
&mut self,
start: BytePos,
decorators: Vec<Decorator>,
) -> PResult<ExportDefaultDecl> {
let start = cur_pos!(self);
let start_of_async = cur_pos!(self);
expect!(self, "async");
self.parse_fn(Some(start), decorators)
self.parse_fn(Some(start), Some(start_of_async), decorators)
}
pub(super) fn parse_default_fn(
&mut self,
start: BytePos,
decorators: Vec<Decorator>,
) -> PResult<ExportDefaultDecl> {
self.parse_fn(None, decorators)
self.parse_fn(Some(start), None, decorators)
}
pub(super) fn parse_class_decl(
@ -951,6 +953,7 @@ impl<'a, I: Tokens> Parser<I> {
fn parse_fn<T>(
&mut self,
start_of_output_type: Option<BytePos>,
start_of_async: Option<BytePos>,
decorators: Vec<Decorator>,
) -> PResult<T>
@ -1016,7 +1019,11 @@ impl<'a, I: Tokens> Parser<I> {
// let body = p.parse_fn_body(is_async, is_generator)?;
Ok(T::finish_fn(span!(p, start), ident, f))
Ok(T::finish_fn(
span!(p, start_of_output_type.unwrap_or(start)),
ident,
f,
))
})
}
@ -1327,7 +1334,7 @@ impl OutputType for Decl {
i.sym == js_word!("constructor")
}
fn finish_fn(span: Span, ident: Ident, function: Function) -> Self {
fn finish_fn(_span: Span, ident: Ident, function: Function) -> Self {
Decl::Fn(FnDecl {
declare: false,
ident,

View File

@ -384,10 +384,10 @@ impl<'a, I: Tokens> Parser<I> {
&& peeked_is!(self, "function")
&& !self.input.has_linebreak_between_cur_and_peeked()
{
let decl = self.parse_default_async_fn(decorators)?;
let decl = self.parse_default_async_fn(start, decorators)?;
return Ok(ModuleDecl::ExportDefaultDecl(decl));
} else if is!(self, "function") {
let decl = self.parse_default_fn(decorators)?;
let decl = self.parse_default_fn(start, decorators)?;
return Ok(ModuleDecl::ExportDefaultDecl(decl));
} else if self.input.syntax().export_default_from()
&& (is!(self, "from") || (is!(self, ',') && peeked_is!(self, '{')))

View File

@ -1,4 +1,5 @@
use crate::test_parser;
use swc_common::BytePos;
use swc_ecma_ast::*;
fn program(src: &'static str) -> Program {
@ -81,3 +82,54 @@ fn issue_1813() {
},
)
}
fn parse_module_export_named_span() {
let m = module("export function foo() {}");
if let ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { span, .. })) = &m.body[0] {
assert_eq!(span.lo, BytePos(0));
} else {
panic!("expected ExportDecl");
}
}
#[test]
fn parse_module_export_default_fn_span() {
let m = module("export default function foo() {}");
if let ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
span, ..
})) = &m.body[0]
{
assert_eq!(span.lo, BytePos(0));
assert_eq!(span.hi, BytePos(32));
} else {
panic!("expected ExportDefaultDecl");
}
}
#[test]
fn parse_module_export_default_async_fn_span() {
let m = module("export default async function foo() {}");
if let ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
span, ..
})) = &m.body[0]
{
assert_eq!(span.lo, BytePos(0));
assert_eq!(span.hi, BytePos(38));
} else {
panic!("expected ExportDefaultDecl");
}
}
#[test]
fn parse_module_export_default_class_span() {
let m = module("export default class Foo {}");
if let ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
span, ..
})) = &m.body[0]
{
assert_eq!(span.lo, BytePos(0));
assert_eq!(span.hi, BytePos(27));
} else {
panic!("expected ExportDefaultDecl");
}
}

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 15,
"start": 0,
"end": 42,
"ctxt": 0
},

View File

@ -33,7 +33,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 110,
"start": 95,
"end": 123,
"ctxt": 0
},

View File

@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 114,
"start": 99,
"end": 131,
"ctxt": 0
},

View File

@ -158,7 +158,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 260,
"start": 245,
"end": 289,
"ctxt": 0
},

View File

@ -33,7 +33,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 115,
"start": 100,
"end": 128,
"ctxt": 0
},

View File

@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 119,
"start": 104,
"end": 136,
"ctxt": 0
},

View File

@ -33,7 +33,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 113,
"start": 98,
"end": 126,
"ctxt": 0
},

View File

@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 117,
"start": 102,
"end": 134,
"ctxt": 0
},

View File

@ -158,7 +158,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 266,
"start": 251,
"end": 295,
"ctxt": 0
},

View File

@ -33,7 +33,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 110,
"start": 95,
"end": 123,
"ctxt": 0
},

View File

@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 114,
"start": 99,
"end": 131,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 53,
"start": 38,
"end": 66,
"ctxt": 0
},
@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 82,
"start": 67,
"end": 104,
"ctxt": 0
},
@ -112,7 +112,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 120,
"start": 105,
"end": 150,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 73,
"start": 58,
"end": 106,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 53,
"start": 38,
"end": 71,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 66,
"start": 51,
"end": 83,
"ctxt": 0
},
@ -94,7 +94,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 143,
"start": 128,
"end": 170,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 71,
"start": 56,
"end": 88,
"ctxt": 0
},
@ -94,7 +94,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 148,
"start": 133,
"end": 173,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 66,
"start": 51,
"end": 83,
"ctxt": 0
},
@ -94,7 +94,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 143,
"start": 128,
"end": 170,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 71,
"start": 56,
"end": 88,
"ctxt": 0
},
@ -94,7 +94,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 148,
"start": 133,
"end": 175,
"ctxt": 0
},

View File

@ -42,7 +42,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 104,
"start": 89,
"end": 123,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 73,
"start": 58,
"end": 92,
"ctxt": 0
},
@ -50,7 +50,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 109,
"start": 94,
"end": 128,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 53,
"start": 38,
"end": 69,
"ctxt": 0
},
@ -50,7 +50,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 86,
"start": 71,
"end": 102,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 73,
"start": 58,
"end": 89,
"ctxt": 0
},

View File

@ -167,7 +167,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 114,
"start": 99,
"end": 136,
"ctxt": 0
},

View File

@ -167,7 +167,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 114,
"start": 99,
"end": 136,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 15,
"start": 0,
"end": 38,
"ctxt": 0
},

View File

@ -54,7 +54,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 65,
"start": 50,
"end": 83,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 15,
"start": 0,
"end": 33,
"ctxt": 0
},

View File

@ -26,7 +26,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 175,
"start": 160,
"end": 209,
"ctxt": 0
},
@ -515,7 +515,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 725,
"start": 710,
"end": 743,
"ctxt": 0
},

View File

@ -165,7 +165,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 240,
"start": 225,
"end": 499,
"ctxt": 0
},

View File

@ -165,7 +165,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 240,
"start": 225,
"end": 386,
"ctxt": 0
},

View File

@ -165,7 +165,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 240,
"start": 225,
"end": 376,
"ctxt": 0
},

View File

@ -165,7 +165,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 240,
"start": 225,
"end": 380,
"ctxt": 0
},

View File

@ -9,7 +9,7 @@
{
"type": "ExportDefaultDeclaration",
"span": {
"start": 103,
"start": 88,
"end": 125,
"ctxt": 0
},