fix(es/codegen): Emit sourcemap of key-value properties correctly (#4166)

This commit is contained in:
RiESAEX 2022-03-27 13:26:53 +08:00 committed by GitHub
parent 3761ba4c98
commit ea0de90137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 15 deletions

View File

@ -1,5 +1,5 @@
{
"mappings": "AAQA,cAAe,CARiF,AAQ/EA,CAAC,CARR,sFAAsF,AAQ/EA,CALsJ,AAKnJC,CAAC,CALX,mJAA6J,AAKnJA,CAAG,AAAC",
"mappings": "AAQA,cAAe,CAAEA,CAAC,CARR,sFAAsF,CAQ5EC,CAAC,CALX,mJAA6J,CAKhJ,AAAC",
"names": [
"a",
"b"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -11,7 +11,7 @@ use std::{
use anyhow::{Context, Error};
use swc::{
config::{Config, IsModule, Options, SourceMapsConfig},
config::{Config, IsModule, ModuleConfig, Options, SourceMapsConfig},
Compiler,
};
use testing::{assert_eq, NormalizedOutput, StdErr, Tester};
@ -227,3 +227,73 @@ fn extract_node_stack_trace(output: Output) -> NormalizedOutput {
stacks.into()
}
#[test]
fn issue_4112() {
Tester::new()
.print_errors(|cm, handler| {
let c = Compiler::new(cm.clone());
let fm = cm.new_source_file(
swc_common::FileName::Real("./browser.js".into()),
r#""use strict";
export { default as Selection } from "./selection";
export { default as RichTextarea } from "./richTextarea";
"#
.to_string(),
);
let output1 = c
.process_js_file(
fm,
&handler,
&Options {
config: Config {
module: Some(ModuleConfig::CommonJs(Default::default())),
..Default::default()
},
source_maps: Some(SourceMapsConfig::Bool(true)),
..Default::default()
},
)
.expect("failed to process js file");
let fm2 = cm.new_source_file(
swc_common::FileName::Real("./preamble.js".into()),
r#""use strict";
import { React, window } from "easy";
window.assign({
React
});
"#
.to_string(),
);
let output2 = c
.process_js_file(
fm2,
&handler,
&Options {
config: Config {
module: Some(ModuleConfig::CommonJs(Default::default())),
..Default::default()
},
source_maps: Some(SourceMapsConfig::Bool(true)),
..Default::default()
},
)
.expect("failed to process js file");
let source_count = sourcemap::SourceMap::from_slice(output2.map.unwrap().as_bytes())
.expect("failed to deserialize sourcemap")
.get_source_count();
if source_count == 1 {
return Ok(());
}
panic!(
"Validation failed, should has 1 source, but {}",
source_count
);
})
.unwrap()
}

View File

@ -93,12 +93,12 @@ pub struct CompileOptions {
/// `trace-{unix epoch time}.json` will be used by default.
#[clap(group = "experimental_trace", long)]
trace_out_file: Option<String>,
//Flags legacy @swc/cli supports, might need some thoughts if we need support same.
//log_watch_compilation: bool,
//copy_files: bool,
//include_dotfiles: bool,
//only: Option<String>,
//no_swcrc: bool,
/*Flags legacy @swc/cli supports, might need some thoughts if we need support same.
*log_watch_compilation: bool,
*copy_files: bool,
*include_dotfiles: bool,
*only: Option<String>,
*no_swcrc: bool, */
}
static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {

View File

@ -1826,15 +1826,25 @@ where
#[emitter]
fn emit_kv_prop(&mut self, node: &KeyValueProp) -> Result {
self.emit_leading_comments_of_span(node.span(), false)?;
srcmap!(node, true);
let key_span = node.key.span();
let value_span = node.value.span();
if !key_span.is_dummy() {
self.wr.add_srcmap(key_span.lo)?;
}
emit!(node.key);
if !key_span.is_dummy() && value_span.is_dummy() {
self.wr.add_srcmap(key_span.hi)?;
}
punct!(":");
formatting_space!();
if key_span.is_dummy() && !value_span.is_dummy() {
self.wr.add_srcmap(value_span.lo)?;
}
emit!(node.value);
srcmap!(node, false);
if !value_span.is_dummy() {
self.wr.add_srcmap(value_span.hi)?;
}
}
#[emitter]

View File

@ -11,6 +11,7 @@ use crate::{
/// Methods related to option `dead_code`.
impl Pure<'_> {
///
/// - Removes `L1: break L1`
pub(super) fn drop_instant_break(&mut self, s: &mut Stmt) {
if let Stmt::Labeled(ls) = s {

View File

@ -517,7 +517,11 @@ impl Scope {
// import * as foo from 'foo';
Ok(obj)
} else {
Ok(obj.make_member(Ident::new(prop, DUMMY_SP)))
Ok(Expr::Member(MemberExpr {
obj: Box::new(obj),
span: orig_span,
prop: Ident::new(prop, DUMMY_SP).into(),
}))
}
}
}