summaryrefslogtreecommitdiff
path: root/core/source_map.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2022-04-15 15:08:09 +0100
committerGitHub <noreply@github.com>2022-04-15 16:08:09 +0200
commit8b31fc23cd80de9baa62535e95367da7a21c9cfd (patch)
tree994748bd06ed5b4953929392107b6beaa1c1c337 /core/source_map.rs
parentb4af648c1515a8e79d7a5d1b14d8a4ba9d966a72 (diff)
refactor: Move source map lookups to core (#14274)
The following transformations gradually faced by "JsError" have all been moved up front to "JsError::from_v8_exception()": - finding the first non-"deno:" source line; - moving "JsError::script_resource_name" etc. into the first error stack in case of syntax errors; - source mapping "JsError::script_resource_name" etc. when wrapping the error even though the frame locations are source mapped earlier; - removing "JsError::{script_resource_name,line_number,start_column,end_column}" entirely in favour of "js_error.frames.get(0)". We also no longer pass a js-side callback to "core/02_error.js" from cli. I avoided doing this on previous occasions because the source map lookups were in an awkward place.
Diffstat (limited to 'core/source_map.rs')
-rw-r--r--core/source_map.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/core/source_map.rs b/core/source_map.rs
new file mode 100644
index 000000000..d33b66586
--- /dev/null
+++ b/core/source_map.rs
@@ -0,0 +1,99 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+//! This mod provides functions to remap a `JsError` based on a source map.
+
+use crate::resolve_url;
+pub use sourcemap::SourceMap;
+use std::collections::HashMap;
+use std::str;
+
+pub trait SourceMapGetter: Sync + Send {
+ /// Returns the raw source map file.
+ fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>>;
+ fn get_source_line(
+ &self,
+ file_name: &str,
+ line_number: usize,
+ ) -> Option<String>;
+}
+
+/// Cached filename lookups. The key can be None if a previous lookup failed to
+/// find a SourceMap.
+pub type CachedMaps = HashMap<String, Option<SourceMap>>;
+
+pub fn apply_source_map<G: SourceMapGetter + ?Sized>(
+ file_name: String,
+ line_number: i64,
+ column_number: i64,
+ mappings_map: &mut CachedMaps,
+ getter: &G,
+) -> (String, i64, i64, Option<String>) {
+ // Lookup expects 0-based line and column numbers, but ours are 1-based.
+ let line_number = line_number - 1;
+ let column_number = column_number - 1;
+
+ let default_pos = (file_name.clone(), line_number, column_number, None);
+ let maybe_source_map = get_mappings(&file_name, mappings_map, getter);
+ let (file_name, line_number, column_number, source_line) =
+ match maybe_source_map {
+ None => default_pos,
+ Some(source_map) => {
+ match source_map.lookup_token(line_number as u32, column_number as u32)
+ {
+ None => default_pos,
+ Some(token) => match token.get_source() {
+ None => default_pos,
+ Some(source_file_name) => {
+ // The `source_file_name` written by tsc in the source map is
+ // sometimes only the basename of the URL, or has unwanted `<`/`>`
+ // around it. Use the `file_name` we get from V8 if
+ // `source_file_name` does not parse as a URL.
+ let file_name = match resolve_url(source_file_name) {
+ Ok(m) if m.scheme() == "blob" => file_name,
+ Ok(m) => m.to_string(),
+ Err(_) => file_name,
+ };
+ let source_line =
+ if let Some(source_view) = token.get_source_view() {
+ source_view
+ .get_line(token.get_src_line())
+ .map(|s| s.to_string())
+ } else {
+ None
+ };
+ (
+ file_name,
+ i64::from(token.get_src_line()),
+ i64::from(token.get_src_col()),
+ source_line,
+ )
+ }
+ },
+ }
+ }
+ };
+ let source_line = source_line
+ .or_else(|| getter.get_source_line(&file_name, line_number as usize));
+ (file_name, line_number + 1, column_number + 1, source_line)
+}
+
+fn get_mappings<'a, G: SourceMapGetter + ?Sized>(
+ file_name: &str,
+ mappings_map: &'a mut CachedMaps,
+ getter: &G,
+) -> &'a Option<SourceMap> {
+ mappings_map
+ .entry(file_name.to_string())
+ .or_insert_with(|| parse_map_string(file_name, getter))
+}
+
+// TODO(kitsonk) parsed source maps should probably be cached in state in
+// the module meta data.
+fn parse_map_string<G: SourceMapGetter + ?Sized>(
+ file_name: &str,
+ getter: &G,
+) -> Option<SourceMap> {
+ getter
+ .get_source_map(file_name)
+ .and_then(|raw_source_map| SourceMap::from_slice(&raw_source_map).ok())
+}