1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::pass::Pass;
use ast::*;
use std::sync::Arc;
use swc_common::{FileName, Fold, SourceMap, DUMMY_SP};
#[cfg(test)]
mod tests;
pub fn jsx_src(dev: bool, cm: Arc<SourceMap>) -> impl Pass {
JsxSrc { cm, dev }
}
struct JsxSrc {
cm: Arc<SourceMap>,
dev: bool,
}
impl Fold<JSXOpeningElement> for JsxSrc {
fn fold(&mut self, mut e: JSXOpeningElement) -> JSXOpeningElement {
if !self.dev || e.span == DUMMY_SP {
return e;
}
let file_lines = match self.cm.span_to_lines(e.span) {
Ok(v) => v,
_ => return e,
};
e.attrs.push(JSXAttrOrSpread::JSXAttr(JSXAttr {
span: DUMMY_SP,
name: JSXAttrName::Ident(quote_ident!("__source")),
value: Some(
box ObjectLit {
span: DUMMY_SP,
props: vec![
PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("fileName")),
value: box Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: match file_lines.file.name {
FileName::Real(ref p) => p.display().to_string().into(),
_ => unimplemented!("file name for other than real files"),
},
has_escape: false,
})),
})),
PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!("lineNumber")),
value: box Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: (file_lines.lines[0].line_index + 1) as _,
})),
})),
],
}
.into(),
),
}));
e
}
}