2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-02-16 13:14:19 -05:00
|
|
|
|
|
|
|
use deno_ast::LineAndColumnIndex;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_ast::SourceTextInfo;
|
2022-08-22 12:14:59 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-09-27 14:01:43 -04:00
|
|
|
use deno_graph::MediaType;
|
2022-02-16 13:14:19 -05:00
|
|
|
use deno_graph::Module;
|
|
|
|
use deno_graph::ModuleGraph;
|
|
|
|
use deno_graph::Position;
|
|
|
|
use deno_graph::Range;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::Resolution;
|
2022-06-14 10:05:37 -04:00
|
|
|
use import_map::ImportMap;
|
|
|
|
use import_map::SpecifierMap;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use log::warn;
|
2022-02-16 13:14:19 -05:00
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
|
|
|
|
2022-02-16 13:14:19 -05:00
|
|
|
use super::mappings::Mappings;
|
|
|
|
use super::specifiers::is_remote_specifier;
|
|
|
|
use super::specifiers::is_remote_specifier_text;
|
|
|
|
|
|
|
|
struct ImportMapBuilder<'a> {
|
2022-06-14 10:05:37 -04:00
|
|
|
base_dir: &'a ModuleSpecifier,
|
2022-02-16 13:14:19 -05:00
|
|
|
mappings: &'a Mappings,
|
|
|
|
imports: ImportsBuilder<'a>,
|
2022-06-14 10:05:37 -04:00
|
|
|
scopes: IndexMap<String, ImportsBuilder<'a>>,
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ImportMapBuilder<'a> {
|
2022-06-14 10:05:37 -04:00
|
|
|
pub fn new(base_dir: &'a ModuleSpecifier, mappings: &'a Mappings) -> Self {
|
2022-02-16 13:14:19 -05:00
|
|
|
ImportMapBuilder {
|
2022-06-14 10:05:37 -04:00
|
|
|
base_dir,
|
2022-02-16 13:14:19 -05:00
|
|
|
mappings,
|
2022-06-14 10:05:37 -04:00
|
|
|
imports: ImportsBuilder::new(base_dir, mappings),
|
2022-02-16 13:14:19 -05:00
|
|
|
scopes: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
pub fn base_dir(&self) -> &ModuleSpecifier {
|
|
|
|
self.base_dir
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:14:19 -05:00
|
|
|
pub fn scope(
|
|
|
|
&mut self,
|
|
|
|
base_specifier: &ModuleSpecifier,
|
|
|
|
) -> &mut ImportsBuilder<'a> {
|
|
|
|
self
|
|
|
|
.scopes
|
|
|
|
.entry(
|
|
|
|
self
|
|
|
|
.mappings
|
2022-06-14 10:05:37 -04:00
|
|
|
.relative_specifier_text(self.base_dir, base_specifier),
|
2022-02-16 13:14:19 -05:00
|
|
|
)
|
2022-06-14 10:05:37 -04:00
|
|
|
.or_insert_with(|| ImportsBuilder::new(self.base_dir, self.mappings))
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
pub fn into_import_map(
|
|
|
|
self,
|
|
|
|
original_import_map: Option<&ImportMap>,
|
|
|
|
) -> ImportMap {
|
|
|
|
fn get_local_imports(
|
|
|
|
new_relative_path: &str,
|
|
|
|
original_imports: &SpecifierMap,
|
|
|
|
) -> Vec<(String, String)> {
|
|
|
|
let mut result = Vec::new();
|
|
|
|
for entry in original_imports.entries() {
|
|
|
|
if let Some(raw_value) = entry.raw_value {
|
|
|
|
if raw_value.starts_with("./") || raw_value.starts_with("../") {
|
|
|
|
let sub_index = raw_value.find('/').unwrap() + 1;
|
|
|
|
result.push((
|
|
|
|
entry.raw_key.to_string(),
|
|
|
|
format!("{}{}", new_relative_path, &raw_value[sub_index..]),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_local_imports<'a>(
|
|
|
|
new_relative_path: &str,
|
|
|
|
original_imports: &SpecifierMap,
|
|
|
|
get_new_imports: impl FnOnce() -> &'a mut SpecifierMap,
|
|
|
|
) {
|
|
|
|
let local_imports =
|
|
|
|
get_local_imports(new_relative_path, original_imports);
|
|
|
|
if !local_imports.is_empty() {
|
|
|
|
let new_imports = get_new_imports();
|
|
|
|
for (key, value) in local_imports {
|
|
|
|
if let Err(warning) = new_imports.append(key, value) {
|
|
|
|
warn!("{}", warning);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut import_map = ImportMap::new(self.base_dir.clone());
|
|
|
|
|
|
|
|
if let Some(original_im) = original_import_map {
|
|
|
|
let original_base_dir = ModuleSpecifier::from_directory_path(
|
|
|
|
original_im
|
|
|
|
.base_url()
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let new_relative_path = self
|
|
|
|
.mappings
|
|
|
|
.relative_specifier_text(self.base_dir, &original_base_dir);
|
|
|
|
// add the imports
|
|
|
|
add_local_imports(&new_relative_path, original_im.imports(), || {
|
|
|
|
import_map.imports_mut()
|
|
|
|
});
|
|
|
|
|
|
|
|
for scope in original_im.scopes() {
|
|
|
|
if scope.raw_key.starts_with("./") || scope.raw_key.starts_with("../") {
|
|
|
|
let sub_index = scope.raw_key.find('/').unwrap() + 1;
|
|
|
|
let new_key =
|
|
|
|
format!("{}{}", new_relative_path, &scope.raw_key[sub_index..]);
|
|
|
|
add_local_imports(&new_relative_path, scope.imports, || {
|
|
|
|
import_map.get_or_append_scope_mut(&new_key).unwrap()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
let imports = import_map.imports_mut();
|
|
|
|
for (key, value) in self.imports.imports {
|
|
|
|
if !imports.contains(&key) {
|
|
|
|
imports.append(key, value).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (scope_key, scope_value) in self.scopes {
|
|
|
|
if !scope_value.imports.is_empty() {
|
|
|
|
let imports = import_map.get_or_append_scope_mut(&scope_key).unwrap();
|
|
|
|
for (key, value) in scope_value.imports {
|
|
|
|
if !imports.contains(&key) {
|
|
|
|
imports.append(key, value).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
import_map
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ImportsBuilder<'a> {
|
2022-06-14 10:05:37 -04:00
|
|
|
base_dir: &'a ModuleSpecifier,
|
2022-02-16 13:14:19 -05:00
|
|
|
mappings: &'a Mappings,
|
2022-06-14 10:05:37 -04:00
|
|
|
imports: IndexMap<String, String>,
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ImportsBuilder<'a> {
|
2022-06-14 10:05:37 -04:00
|
|
|
pub fn new(base_dir: &'a ModuleSpecifier, mappings: &'a Mappings) -> Self {
|
2022-02-16 13:14:19 -05:00
|
|
|
Self {
|
2022-06-14 10:05:37 -04:00
|
|
|
base_dir,
|
2022-02-16 13:14:19 -05:00
|
|
|
mappings,
|
|
|
|
imports: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&mut self, key: String, specifier: &ModuleSpecifier) {
|
2022-05-23 12:49:28 -04:00
|
|
|
let value = self
|
|
|
|
.mappings
|
2022-06-14 10:05:37 -04:00
|
|
|
.relative_specifier_text(self.base_dir, specifier);
|
2022-05-23 12:49:28 -04:00
|
|
|
|
|
|
|
// skip creating identity entries
|
|
|
|
if key != value {
|
|
|
|
self.imports.insert(key, value);
|
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_import_map(
|
2022-06-14 10:05:37 -04:00
|
|
|
base_dir: &ModuleSpecifier,
|
2022-02-16 13:14:19 -05:00
|
|
|
graph: &ModuleGraph,
|
|
|
|
modules: &[&Module],
|
|
|
|
mappings: &Mappings,
|
2022-06-14 10:05:37 -04:00
|
|
|
original_import_map: Option<&ImportMap>,
|
2022-08-22 12:14:59 -04:00
|
|
|
parsed_source_cache: &ParsedSourceCache,
|
|
|
|
) -> Result<String, AnyError> {
|
2022-06-14 10:05:37 -04:00
|
|
|
let mut builder = ImportMapBuilder::new(base_dir, mappings);
|
2022-08-22 12:14:59 -04:00
|
|
|
visit_modules(graph, modules, mappings, &mut builder, parsed_source_cache)?;
|
2022-02-16 13:14:19 -05:00
|
|
|
|
|
|
|
for base_specifier in mappings.base_specifiers() {
|
2022-06-14 10:05:37 -04:00
|
|
|
builder
|
2022-02-16 13:14:19 -05:00
|
|
|
.imports
|
|
|
|
.add(base_specifier.to_string(), base_specifier);
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
Ok(builder.into_import_map(original_import_map).to_json())
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_modules(
|
|
|
|
graph: &ModuleGraph,
|
|
|
|
modules: &[&Module],
|
|
|
|
mappings: &Mappings,
|
|
|
|
import_map: &mut ImportMapBuilder,
|
2022-08-22 12:14:59 -04:00
|
|
|
parsed_source_cache: &ParsedSourceCache,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-02-16 13:14:19 -05:00
|
|
|
for module in modules {
|
2022-09-27 14:01:43 -04:00
|
|
|
if module.media_type == MediaType::Json {
|
|
|
|
// skip visiting Json modules as they are leaves
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
let text_info =
|
|
|
|
match parsed_source_cache.get_parsed_source_from_module(module)? {
|
|
|
|
Some(source) => source.text_info().clone(),
|
|
|
|
None => continue,
|
|
|
|
};
|
2022-02-16 13:14:19 -05:00
|
|
|
let source_text = match &module.maybe_source {
|
|
|
|
Some(source) => source,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
for dep in module.dependencies.values() {
|
2023-02-09 22:00:23 -05:00
|
|
|
visit_resolution(
|
2022-02-16 13:14:19 -05:00
|
|
|
&dep.maybe_code,
|
|
|
|
graph,
|
|
|
|
import_map,
|
|
|
|
&module.specifier,
|
|
|
|
mappings,
|
2022-08-22 12:14:59 -04:00
|
|
|
&text_info,
|
2022-02-16 13:14:19 -05:00
|
|
|
source_text,
|
|
|
|
);
|
2023-02-09 22:00:23 -05:00
|
|
|
visit_resolution(
|
2022-02-16 13:14:19 -05:00
|
|
|
&dep.maybe_type,
|
|
|
|
graph,
|
|
|
|
import_map,
|
|
|
|
&module.specifier,
|
|
|
|
mappings,
|
2022-08-22 12:14:59 -04:00
|
|
|
&text_info,
|
2022-02-16 13:14:19 -05:00
|
|
|
source_text,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
if let Some(types_dep) = &module.maybe_types_dependency {
|
|
|
|
visit_resolution(
|
|
|
|
&types_dep.dependency,
|
2022-02-16 13:14:19 -05:00
|
|
|
graph,
|
|
|
|
import_map,
|
|
|
|
&module.specifier,
|
|
|
|
mappings,
|
2022-08-22 12:14:59 -04:00
|
|
|
&text_info,
|
2022-02-16 13:14:19 -05:00
|
|
|
source_text,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
|
|
|
|
Ok(())
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
fn visit_resolution(
|
|
|
|
resolution: &Resolution,
|
2022-02-16 13:14:19 -05:00
|
|
|
graph: &ModuleGraph,
|
|
|
|
import_map: &mut ImportMapBuilder,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
mappings: &Mappings,
|
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
source_text: &str,
|
|
|
|
) {
|
2023-02-09 22:00:23 -05:00
|
|
|
if let Some(resolved) = resolution.ok() {
|
|
|
|
let text = text_from_range(text_info, source_text, &resolved.range);
|
2022-02-16 13:14:19 -05:00
|
|
|
// if the text is empty then it's probably an x-TypeScript-types
|
|
|
|
if !text.is_empty() {
|
|
|
|
handle_dep_specifier(
|
2023-02-09 22:00:23 -05:00
|
|
|
text,
|
|
|
|
&resolved.specifier,
|
|
|
|
graph,
|
|
|
|
import_map,
|
|
|
|
referrer,
|
|
|
|
mappings,
|
2022-02-16 13:14:19 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_dep_specifier(
|
|
|
|
text: &str,
|
|
|
|
unresolved_specifier: &ModuleSpecifier,
|
|
|
|
graph: &ModuleGraph,
|
|
|
|
import_map: &mut ImportMapBuilder,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
mappings: &Mappings,
|
|
|
|
) {
|
2023-02-15 11:30:54 -05:00
|
|
|
let specifier = match graph.get(unresolved_specifier) {
|
|
|
|
Some(module) => module.specifier.clone(),
|
|
|
|
// Ignore when None. The graph was previous validated so this is a
|
|
|
|
// dynamic import that was missing and is ignored for vendoring
|
|
|
|
None => return,
|
|
|
|
};
|
2022-06-14 10:05:37 -04:00
|
|
|
// check if it's referencing a remote module
|
|
|
|
if is_remote_specifier(&specifier) {
|
|
|
|
handle_remote_dep_specifier(
|
|
|
|
text,
|
|
|
|
unresolved_specifier,
|
|
|
|
&specifier,
|
|
|
|
import_map,
|
|
|
|
referrer,
|
|
|
|
mappings,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
handle_local_dep_specifier(
|
|
|
|
text,
|
|
|
|
unresolved_specifier,
|
|
|
|
&specifier,
|
|
|
|
import_map,
|
|
|
|
referrer,
|
|
|
|
mappings,
|
|
|
|
);
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
2022-06-14 10:05:37 -04:00
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
fn handle_remote_dep_specifier(
|
|
|
|
text: &str,
|
|
|
|
unresolved_specifier: &ModuleSpecifier,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
import_map: &mut ImportMapBuilder,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
mappings: &Mappings,
|
|
|
|
) {
|
2022-02-16 13:14:19 -05:00
|
|
|
if is_remote_specifier_text(text) {
|
2022-06-14 10:05:37 -04:00
|
|
|
let base_specifier = mappings.base_specifier(specifier);
|
2022-02-16 13:14:19 -05:00
|
|
|
if !text.starts_with(base_specifier.as_str()) {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Expected {text} to start with {base_specifier}");
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let sub_path = &text[base_specifier.as_str().len()..];
|
2022-06-14 10:05:37 -04:00
|
|
|
let relative_text =
|
|
|
|
mappings.relative_specifier_text(base_specifier, specifier);
|
|
|
|
let expected_sub_path = relative_text.trim_start_matches("./");
|
|
|
|
if expected_sub_path != sub_path {
|
|
|
|
import_map.imports.add(text.to_string(), specifier);
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let expected_relative_specifier_text =
|
2022-06-14 10:05:37 -04:00
|
|
|
mappings.relative_specifier_text(referrer, specifier);
|
2022-02-16 13:14:19 -05:00
|
|
|
if expected_relative_specifier_text == text {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
if !is_remote_specifier(referrer) {
|
|
|
|
// local module referencing a remote module using
|
|
|
|
// non-remote specifier text means it was something in
|
|
|
|
// the original import map, so add a mapping to it
|
|
|
|
import_map.imports.add(text.to_string(), specifier);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-04 09:39:14 -04:00
|
|
|
let base_referrer = mappings.base_specifier(referrer);
|
2022-06-14 10:05:37 -04:00
|
|
|
let base_dir = import_map.base_dir().clone();
|
2022-08-04 09:39:14 -04:00
|
|
|
let imports = import_map.scope(base_referrer);
|
2022-05-23 12:49:28 -04:00
|
|
|
if text.starts_with("./") || text.starts_with("../") {
|
2022-02-16 13:14:19 -05:00
|
|
|
// resolve relative specifier key
|
2022-08-04 09:39:14 -04:00
|
|
|
let mut local_base_specifier = mappings.local_uri(base_referrer);
|
2022-02-16 13:14:19 -05:00
|
|
|
local_base_specifier = local_base_specifier
|
2022-05-02 09:07:41 -04:00
|
|
|
// path includes "/" so make it relative
|
|
|
|
.join(&format!(".{}", unresolved_specifier.path()))
|
2022-02-16 13:14:19 -05:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
"Error joining {} to {}",
|
|
|
|
unresolved_specifier.path(),
|
|
|
|
local_base_specifier
|
|
|
|
)
|
|
|
|
});
|
|
|
|
local_base_specifier.set_query(unresolved_specifier.query());
|
2022-05-23 12:49:28 -04:00
|
|
|
|
|
|
|
imports.add(
|
2022-06-14 10:05:37 -04:00
|
|
|
mappings.relative_specifier_text(&base_dir, &local_base_specifier),
|
|
|
|
specifier,
|
2022-05-23 12:49:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// add a mapping that uses the local directory name and the remote
|
|
|
|
// filename in order to support files importing this relatively
|
|
|
|
imports.add(
|
|
|
|
{
|
2022-06-14 10:05:37 -04:00
|
|
|
let local_path = mappings.local_path(specifier);
|
2022-05-23 12:49:28 -04:00
|
|
|
let mut value =
|
|
|
|
ModuleSpecifier::from_directory_path(local_path.parent().unwrap())
|
|
|
|
.unwrap();
|
|
|
|
value.set_query(specifier.query());
|
|
|
|
value.set_path(&format!(
|
|
|
|
"{}{}",
|
|
|
|
value.path(),
|
|
|
|
specifier.path_segments().unwrap().last().unwrap(),
|
|
|
|
));
|
2022-06-14 10:05:37 -04:00
|
|
|
mappings.relative_specifier_text(&base_dir, &value)
|
2022-05-23 12:49:28 -04:00
|
|
|
},
|
2022-06-14 10:05:37 -04:00
|
|
|
specifier,
|
2022-05-23 12:49:28 -04:00
|
|
|
);
|
2022-02-16 13:14:19 -05:00
|
|
|
} else {
|
|
|
|
// absolute (`/`) or bare specifier should be left as-is
|
2022-06-14 10:05:37 -04:00
|
|
|
imports.add(text.to_string(), specifier);
|
2022-05-23 12:49:28 -04:00
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
fn handle_local_dep_specifier(
|
|
|
|
text: &str,
|
|
|
|
unresolved_specifier: &ModuleSpecifier,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
import_map: &mut ImportMapBuilder,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
mappings: &Mappings,
|
|
|
|
) {
|
|
|
|
if !is_remote_specifier(referrer) {
|
|
|
|
// do not handle local modules referencing local modules
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The remote module is referencing a local file. This could occur via an
|
|
|
|
// existing import map. In this case, we'll have to add an import map
|
|
|
|
// entry in order to map the path back to the local path once vendored.
|
|
|
|
let base_dir = import_map.base_dir().clone();
|
|
|
|
let base_specifier = mappings.base_specifier(referrer);
|
|
|
|
let imports = import_map.scope(base_specifier);
|
|
|
|
|
|
|
|
if text.starts_with("./") || text.starts_with("../") {
|
|
|
|
let referrer_local_uri = mappings.local_uri(referrer);
|
|
|
|
let mut specifier_local_uri =
|
|
|
|
referrer_local_uri.join(text).unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
"Error joining {} to {}",
|
|
|
|
unresolved_specifier.path(),
|
|
|
|
referrer_local_uri
|
|
|
|
)
|
|
|
|
});
|
|
|
|
specifier_local_uri.set_query(unresolved_specifier.query());
|
|
|
|
|
|
|
|
imports.add(
|
|
|
|
mappings.relative_specifier_text(&base_dir, &specifier_local_uri),
|
|
|
|
specifier,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
imports.add(text.to_string(), specifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:14:19 -05:00
|
|
|
fn text_from_range<'a>(
|
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
text: &'a str,
|
|
|
|
range: &Range,
|
|
|
|
) -> &'a str {
|
|
|
|
let result = &text[byte_range(text_info, range)];
|
|
|
|
if result.starts_with('"') || result.starts_with('\'') {
|
|
|
|
// remove the quotes
|
|
|
|
&result[1..result.len() - 1]
|
|
|
|
} else {
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn byte_range(
|
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
range: &Range,
|
|
|
|
) -> std::ops::Range<usize> {
|
|
|
|
let start = byte_index(text_info, &range.start);
|
|
|
|
let end = byte_index(text_info, &range.end);
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
|
|
|
|
fn byte_index(text_info: &SourceTextInfo, pos: &Position) -> usize {
|
|
|
|
// todo(https://github.com/denoland/deno_graph/issues/79): use byte indexes all the way down
|
2022-05-20 16:40:55 -04:00
|
|
|
text_info.loc_to_source_pos(LineAndColumnIndex {
|
|
|
|
line_index: pos.line,
|
|
|
|
column_index: pos.character,
|
|
|
|
}) - text_info.range().start
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|