feat: specifier position in DependencyDescriptor (#1260)

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
Luca Casonato 2020-12-09 03:27:09 +01:00 committed by GitHub
parent 01b6a0a90f
commit 8ba2ae959d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 23 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecmascript"
repository = "https://github.com/swc-project/swc.git"
version = "0.14.5"
version = "0.15.0"
[features]
codegen = ["swc_ecma_codegen"]
@ -22,7 +22,7 @@ react = ["swc_ecma_transforms", "swc_ecma_transforms/react"]
[dependencies]
swc_ecma_ast = {version = "0.35.0", path = "./ast"}
swc_ecma_codegen = {version = "0.41.1", path = "./codegen", optional = true}
swc_ecma_dep_graph = {version = "0.9.0", path = "./dep-graph", optional = true}
swc_ecma_dep_graph = {version = "0.10.0", path = "./dep-graph", optional = true}
swc_ecma_parser = {version = "0.43.0", path = "./parser", optional = true}
swc_ecma_transforms = {version = "0.30.3", path = "./transforms", optional = true}
swc_ecma_utils = {version = "0.25.0", path = "./utils", optional = true}

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_dep_graph"
repository = "https://github.com/swc-project/swc.git"
version = "0.9.0"
version = "0.10.0"
[dependencies]
swc_atoms = {version = "0.2", path = "../../atoms"}

View File

@ -39,10 +39,13 @@ pub struct DependencyDescriptor {
/// further processing of supported pragma that impact the dependency.
pub leading_comments: Vec<Comment>,
/// The location of the import/export statement.
pub col: usize,
pub line: usize,
pub col: usize,
/// The text specifier associated with the import/export statement.
pub specifier: JsWord,
/// The location of the specifier.
pub specifier_line: usize,
pub specifier_col: usize,
}
struct DependencyCollector<'a> {
@ -55,12 +58,13 @@ struct DependencyCollector<'a> {
}
impl<'a> DependencyCollector<'a> {
fn get_location_and_comments(&self, span: Span) -> (Loc, Vec<Comment>) {
let location = self.source_map.lookup_char_pos(span.lo);
let leading_comments = self
.comments
.with_leading(span.lo, |comments| comments.to_vec());
(location, leading_comments)
fn get_location(&self, span: Span) -> Loc {
self.source_map.lookup_char_pos(span.lo)
}
fn get_leading_comments(&self, span: Span) -> Vec<Comment> {
self.comments
.with_leading(span.lo, |comments| comments.to_vec())
}
}
@ -68,7 +72,9 @@ impl<'a> Visit for DependencyCollector<'a> {
fn visit_import_decl(&mut self, node: &ast::ImportDecl, _parent: &dyn Node) {
let specifier = node.src.value.clone();
let span = node.span;
let (location, leading_comments) = self.get_location_and_comments(span);
let location = self.get_location(span);
let leading_comments = self.get_leading_comments(span);
let specifier_location = self.get_location(node.src.span);
let kind = if node.type_only {
DependencyKind::ImportType
} else {
@ -81,6 +87,8 @@ impl<'a> Visit for DependencyCollector<'a> {
col: location.col_display,
line: location.line,
specifier,
specifier_col: specifier_location.col_display,
specifier_line: specifier_location.line,
});
}
@ -88,7 +96,9 @@ impl<'a> Visit for DependencyCollector<'a> {
if let Some(src) = &node.src {
let specifier = src.value.clone();
let span = node.span;
let (location, leading_comments) = self.get_location_and_comments(span);
let location = self.get_location(span);
let leading_comments = self.get_leading_comments(span);
let specifier_location = self.get_location(src.span);
let kind = if node.type_only {
DependencyKind::ExportType
} else {
@ -101,6 +111,8 @@ impl<'a> Visit for DependencyCollector<'a> {
col: location.col_display,
line: location.line,
specifier,
specifier_col: specifier_location.col_display,
specifier_line: specifier_location.line,
});
}
}
@ -108,7 +120,9 @@ impl<'a> Visit for DependencyCollector<'a> {
fn visit_export_all(&mut self, node: &ast::ExportAll, _parent: &dyn Node) {
let specifier = node.src.value.clone();
let span = node.span;
let (location, leading_comments) = self.get_location_and_comments(span);
let location = self.get_location(span);
let leading_comments = self.get_leading_comments(span);
let specifier_location = self.get_location(node.src.span);
self.items.push(DependencyDescriptor {
kind: DependencyKind::Export,
is_dynamic: false,
@ -116,13 +130,17 @@ impl<'a> Visit for DependencyCollector<'a> {
col: location.col_display,
line: location.line,
specifier,
specifier_col: specifier_location.col_display,
specifier_line: specifier_location.line,
});
}
fn visit_ts_import_type(&mut self, node: &ast::TsImportType, _parent: &dyn Node) {
let specifier = node.arg.value.clone();
let span = node.span;
let (location, leading_comments) = self.get_location_and_comments(span);
let location = self.get_location(span);
let leading_comments = self.get_leading_comments(span);
let specifier_location = self.get_location(node.arg.span);
self.items.push(DependencyDescriptor {
kind: DependencyKind::ImportType,
is_dynamic: false,
@ -130,6 +148,8 @@ impl<'a> Visit for DependencyCollector<'a> {
col: location.col_display,
line: location.line,
specifier,
specifier_col: specifier_location.col_display,
specifier_line: specifier_location.line,
});
}
@ -169,7 +189,9 @@ impl<'a> Visit for DependencyCollector<'a> {
if let ast::Lit::Str(str_) = lit {
let specifier = str_.value.clone();
let span = node.span;
let (location, leading_comments) = self.get_location_and_comments(span);
let location = self.get_location(span);
let leading_comments = self.get_leading_comments(span);
let specifier_location = self.get_location(str_.span);
self.items.push(DependencyDescriptor {
kind,
is_dynamic,
@ -177,6 +199,8 @@ impl<'a> Visit for DependencyCollector<'a> {
col: location.col_display,
line: location.line,
specifier,
specifier_col: specifier_location.col_display,
specifier_line: specifier_location.line,
});
}
}
@ -279,7 +303,9 @@ try {
leading_comments: Vec::new(),
col: 0,
line: 1,
specifier: JsWord::from("./test.ts")
specifier: JsWord::from("./test.ts"),
specifier_col: 21,
specifier_line: 1,
},
DependencyDescriptor {
kind: DependencyKind::ImportType,
@ -291,7 +317,9 @@ try {
}],
col: 0,
line: 3,
specifier: JsWord::from("./foo.d.ts")
specifier: JsWord::from("./foo.d.ts"),
specifier_col: 25,
specifier_line: 3,
},
DependencyDescriptor {
kind: DependencyKind::Export,
@ -303,7 +331,9 @@ try {
}],
col: 0,
line: 5,
specifier: JsWord::from("./buzz.ts")
specifier: JsWord::from("./buzz.ts"),
specifier_col: 22,
specifier_line: 5,
},
DependencyDescriptor {
kind: DependencyKind::ExportType,
@ -322,7 +352,9 @@ try {
],
col: 0,
line: 10,
specifier: JsWord::from("./fizz.d.ts")
specifier: JsWord::from("./fizz.d.ts"),
specifier_col: 26,
specifier_line: 10,
},
DependencyDescriptor {
kind: DependencyKind::Require,
@ -330,7 +362,9 @@ try {
leading_comments: Vec::new(),
col: 17,
line: 11,
specifier: JsWord::from("path")
specifier: JsWord::from("path"),
specifier_col: 25,
specifier_line: 11,
},
DependencyDescriptor {
kind: DependencyKind::Import,
@ -338,7 +372,9 @@ try {
leading_comments: Vec::new(),
col: 6,
line: 14,
specifier: JsWord::from("./foo1.ts")
specifier: JsWord::from("./foo1.ts"),
specifier_col: 13,
specifier_line: 14,
},
DependencyDescriptor {
kind: DependencyKind::Import,
@ -346,7 +382,9 @@ try {
leading_comments: Vec::new(),
col: 22,
line: 17,
specifier: JsWord::from("./foo.ts")
specifier: JsWord::from("./foo.ts"),
specifier_col: 29,
specifier_line: 17,
},
DependencyDescriptor {
kind: DependencyKind::Require,
@ -354,7 +392,9 @@ try {
leading_comments: Vec::new(),
col: 16,
line: 23,
specifier: JsWord::from("some_package")
specifier: JsWord::from("some_package"),
specifier_col: 24,
specifier_line: 23,
}
]
);