Unverified Commit b90559cd authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Remove a bunch of dead code from the source map implementation (#84318)

The `SourceMap::Sectioned` enum was only used recursively.  once i noticed that a bunch of dead code fell out.

Part-of PACK-417
parent 031b8fb8
...@@ -1491,12 +1491,10 @@ pub async fn project_trace_source_operation( ...@@ -1491,12 +1491,10 @@ pub async fn project_trace_source_operation(
return Ok(Vc::cell(None)); return Ok(Vc::cell(None));
}; };
let token = map let token = map.lookup_token(
.lookup_token(
line.saturating_sub(1), line.saturating_sub(1),
frame.column.unwrap_or(1).saturating_sub(1), frame.column.unwrap_or(1).saturating_sub(1),
) );
.await?;
let (original_file, line, column, method_name) = match token { let (original_file, line, column, method_name) = match token {
Token::Original(token) => ( Token::Original(token) => (
......
...@@ -63,14 +63,18 @@ pub trait GenerateSourceMap { ...@@ -63,14 +63,18 @@ pub trait GenerateSourceMap {
/// The distinction between the source map spec's [sourcemap::Index] and our /// The distinction between the source map spec's [sourcemap::Index] and our
/// [SourceMap::Sectioned] is whether the sections are represented with Vcs /// [SourceMap::Sectioned] is whether the sections are represented with Vcs
/// pointers. /// pointers.
#[turbo_tasks::value(shared, cell = "new")] #[turbo_tasks::value(shared, cell = "new", eq = "manual")]
#[derive(Debug)] #[derive(Debug)]
pub enum SourceMap { pub struct SourceMap {
/// A decoded source map contains no Vcs. /// A decoded source map contains no Vcs.
Decoded(#[turbo_tasks(trace_ignore)] InnerSourceMap), #[turbo_tasks(trace_ignore)]
/// A sectioned source map contains many (possibly recursive) maps covering map: Arc<CrateMapWrapper>,
/// different regions of the file. }
Sectioned(#[turbo_tasks(trace_ignore)] SectionedSourceMap), impl Eq for SourceMap {}
impl PartialEq for SourceMap {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.map, &other.map)
}
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
...@@ -207,49 +211,23 @@ impl TryInto<swc_sourcemap::RawToken> for Token { ...@@ -207,49 +211,23 @@ impl TryInto<swc_sourcemap::RawToken> for Token {
} }
impl SourceMap { impl SourceMap {
pub fn empty_uncelled() -> Self {
let mut builder = SourceMapBuilder::new(None);
builder.add(0, 0, 0, 0, None, None, false);
SourceMap::new_regular(builder.into_sourcemap())
}
/// Creates a new SourceMap::Decoded Vc out of a [RegularMap] instance. /// Creates a new SourceMap::Decoded Vc out of a [RegularMap] instance.
pub fn new_regular(map: RegularMap) -> Self { fn new_regular(map: RegularMap) -> Self {
Self::new_decoded(DecodedMap::Regular(map)) Self::new_decoded(DecodedMap::Regular(map))
} }
/// Creates a new SourceMap::Decoded Vc out of a [DecodedMap] instance. /// Creates a new SourceMap::Decoded Vc out of a [DecodedMap] instance.
pub fn new_decoded(map: DecodedMap) -> Self { fn new_decoded(map: DecodedMap) -> Self {
SourceMap::Decoded(InnerSourceMap::new(map)) SourceMap {
map: Arc::new(CrateMapWrapper(map)),
} }
/// Creates a new SourceMap::Sectioned Vc out of a collection of source map
/// sections.
pub fn new_sectioned(sections: Vec<SourceMapSection>) -> Self {
SourceMap::Sectioned(SectionedSourceMap::new(sections))
} }
pub fn new_from_rope(content: &Rope) -> Result<Option<Self>> { pub fn new_from_rope(content: &Rope) -> Result<Option<Self>> {
let Ok(map) = DecodedMap::from_reader(content.read()) else { let Ok(map) = DecodedMap::from_reader(content.read()) else {
return Ok(None); return Ok(None);
}; };
Ok(Some(SourceMap::Decoded(InnerSourceMap::new(map)))) Ok(Some(SourceMap::new_decoded(map)))
}
pub async fn new_from_file(file: FileSystemPath) -> Result<Option<Self>> {
let read = file.read();
Self::new_from_file_content(read).await
}
pub async fn new_from_file_content(content: Vc<FileContent>) -> Result<Option<Self>> {
let content = &content.await?;
let Some(contents) = content.as_content() else {
return Ok(None);
};
let Ok(map) = DecodedMap::from_reader(contents.read()) else {
return Ok(None);
};
Ok(Some(SourceMap::Decoded(InnerSourceMap::new(map))))
} }
} }
...@@ -269,27 +247,8 @@ impl SourceMap { ...@@ -269,27 +247,8 @@ impl SourceMap {
} }
impl SourceMap { impl SourceMap {
pub async fn to_source_map(&self) -> Result<Arc<CrateMapWrapper>> { pub fn to_source_map(&self) -> Arc<CrateMapWrapper> {
Ok(match self { self.map.clone()
Self::Decoded(m) => m.map.clone(),
Self::Sectioned(m) => {
let wrapped = m.to_crate_wrapper().await?;
let sections = wrapped
.sections
.iter()
.map(|s| {
swc_sourcemap::SourceMapSection::new(
(s.offset.line, s.offset.column),
None,
Some(s.map.0.clone()),
)
})
.collect::<Vec<swc_sourcemap::SourceMapSection>>();
Arc::new(CrateMapWrapper(DecodedMap::Index(SourceMapIndex::new(
None, sections,
))))
}
})
} }
} }
...@@ -356,50 +315,31 @@ impl SourceMap { ...@@ -356,50 +315,31 @@ impl SourceMap {
rope += "}"; rope += "}";
} }
rope += "] rope += "]";
}";
rope += "\n}";
Ok(rope.build()) Ok(rope.build())
} }
/// Stringifies the source map into JSON bytes. /// Stringifies the source map into JSON bytes.
pub async fn to_rope(&self) -> Result<Rope> { pub fn to_rope(&self) -> Result<Rope> {
let rope = match self {
SourceMap::Decoded(r) => {
let mut bytes = vec![]; let mut bytes = vec![];
r.0.to_writer(&mut bytes)?; self.map.0.to_writer(&mut bytes)?;
Rope::from(bytes) Ok(Rope::from(bytes))
}
SourceMap::Sectioned(s) => {
let sections = s
.sections
.iter()
.map(async |s| Ok((s.offset, s.map.to_rope().await?)))
.try_join()
.await?;
Self::sections_to_rope(sections)?
}
};
Ok(rope)
} }
/// Traces a generated line/column into an mapping token representing either /// Traces a generated line/column into an mapping token representing either
/// synthetic code or user-authored original code. /// synthetic code or user-authored original code.
pub async fn lookup_token(&self, line: u32, column: u32) -> Result<Token> { pub fn lookup_token(&self, line: u32, column: u32) -> Token {
let (token, _) = self let (token, _) = self.lookup_token_and_source_internal(line, column, true);
.lookup_token_and_source_internal(line, column, true) token
.await?;
Ok(token)
} }
/// Traces a generated line/column into an mapping token representing either /// Traces a generated line/column into an mapping token representing either
/// synthetic code or user-authored original code. /// synthetic code or user-authored original code.
pub async fn lookup_token_and_source(&self, line: u32, column: u32) -> Result<TokenWithSource> { pub async fn lookup_token_and_source(&self, line: u32, column: u32) -> Result<TokenWithSource> {
let (token, content) = self let (token, content) = self.lookup_token_and_source_internal(line, column, true);
.lookup_token_and_source_internal(line, column, true)
.await?;
Ok(TokenWithSource { Ok(TokenWithSource {
token, token,
source_content: match content { source_content: match content {
...@@ -465,9 +405,10 @@ impl SourceMap { ...@@ -465,9 +405,10 @@ impl SourceMap {
let mut new_sources = Vec::with_capacity(count); let mut new_sources = Vec::with_capacity(count);
let mut new_source_contents = Vec::with_capacity(count); let mut new_source_contents = Vec::with_capacity(count);
for (source, source_content) in sources.into_iter().zip(source_contents.into_iter()) { for (source, source_content) in sources.into_iter().zip(source_contents.into_iter()) {
let (source, name) = resolve_source(source, source_content, origin.clone()).await?; let (source, source_content) =
resolve_source(source, source_content, origin.clone()).await?;
new_sources.push(source); new_sources.push(source);
new_source_contents.push(Some(name)); new_source_contents.push(Some(source_content));
} }
let mut map = let mut map =
RegularMap::new(file, tokens, names, new_sources, Some(new_source_contents)); RegularMap::new(file, tokens, names, new_sources, Some(new_source_contents));
...@@ -529,20 +470,9 @@ impl SourceMap { ...@@ -529,20 +470,9 @@ impl SourceMap {
} }
})) }))
} }
Ok(match self {
Self::Decoded(m) => { let map = Box::pin(decoded_map_with_resolved_sources(&self.map, origin)).await?;
let map = Box::pin(decoded_map_with_resolved_sources(&m.map, origin)).await?; Ok(Self::new_decoded(map.0))
Self::Decoded(InnerSourceMap::new(map.0))
}
Self::Sectioned(m) => {
let mut sections = Vec::with_capacity(m.sections.len());
for section in &m.sections {
let map = Box::pin(section.map.with_resolved_sources(origin.clone())).await?;
sections.push(SourceMapSection::new(section.offset, map));
}
SourceMap::new_sectioned(sections)
}
})
} }
} }
...@@ -559,16 +489,17 @@ async fn sourcemap_content_source(path: RcStr, content: RcStr) -> Result<Vc<Box< ...@@ -559,16 +489,17 @@ async fn sourcemap_content_source(path: RcStr, content: RcStr) -> Result<Vc<Box<
} }
impl SourceMap { impl SourceMap {
async fn lookup_token_and_source_internal( fn lookup_token_and_source_internal(
&self, &self,
line: u32, line: u32,
column: u32, column: u32,
need_source_content: bool, need_source_content: bool,
) -> Result<(Token, Option<Vc<Box<dyn Source>>>)> { ) -> (Token, Option<Vc<Box<dyn Source>>>) {
let mut content: Option<Vc<Box<dyn Source>>> = None; let mut content: Option<Vc<Box<dyn Source>>> = None;
let token: Token = match self { let token: Token = {
SourceMap::Decoded(map) => { let map = &self.map;
let tok = map.lookup_token(line, column); let tok = map.lookup_token(line, column);
let mut token = tok.map(Token::from).unwrap_or_else(|| { let mut token = tok.map(Token::from).unwrap_or_else(|| {
Token::Synthetic(SyntheticToken { Token::Synthetic(SyntheticToken {
...@@ -582,7 +513,7 @@ impl SourceMap { ...@@ -582,7 +513,7 @@ impl SourceMap {
guessed_original_file, guessed_original_file,
.. ..
}) = &mut token }) = &mut token
&& let DecodedMap::Regular(map) = &map.map.0 && let DecodedMap::Regular(map) = &map.0
&& map.get_source_count() == 1 && map.get_source_count() == 1
{ {
let source = map.sources().next().unwrap().clone(); let source = map.sources().next().unwrap().clone();
...@@ -591,7 +522,7 @@ impl SourceMap { ...@@ -591,7 +522,7 @@ impl SourceMap {
if need_source_content if need_source_content
&& content.is_none() && content.is_none()
&& let Some(map) = map.map.as_regular_source_map() && let Some(map) = map.as_regular_source_map()
{ {
content = tok.and_then(|tok| { content = tok.and_then(|tok| {
let src_id = tok.get_src_id(); let src_id = tok.get_src_id();
...@@ -608,94 +539,17 @@ impl SourceMap { ...@@ -608,94 +539,17 @@ impl SourceMap {
} }
token token
}
SourceMap::Sectioned(map) => {
let len = map.sections.len();
let mut low = 0;
let mut high = len;
let pos = SourcePos { line, column };
// A "greatest lower bound" binary search. We're looking for the closest section
// offset <= to our line/col.
while low < high {
let mid = (low + high) / 2;
if pos < map.sections[mid].offset {
high = mid;
} else {
low = mid + 1;
}
}
// Our GLB search will return the section immediately to the right of the
// section we actually want to recurse into, because the binary search does not
// early exit on an exact match (it'll `low = mid + 1`).
if low > 0 && low <= len {
let SourceMapSection { map, offset } = &map.sections[low - 1];
// We're looking for the position `l` lines into region covered by this
// sourcemap's section.
let l = line - offset.line;
// The source map starts offset by the section's column only on its first line.
// On the 2nd+ line, the source map covers starting at column 0.
let c = if line == offset.line {
column - offset.column
} else {
column
}; };
if need_source_content { (token, content)
let result = Box::pin(map.lookup_token_and_source(l, c)).await?;
return Ok((result.token, result.source_content.map(|v| *v)));
} else {
return Ok((Box::pin(map.lookup_token(l, c)).await?, None));
}
}
Token::Synthetic(SyntheticToken {
generated_line: line,
generated_column: column,
guessed_original_file: None,
})
}
};
Ok((token, content))
} }
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl GenerateSourceMap for SourceMap { impl GenerateSourceMap for SourceMap {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn generate_source_map(self: ResolvedVc<Self>) -> Result<Vc<OptionStringifiedSourceMap>> { fn generate_source_map(&self) -> Result<Vc<OptionStringifiedSourceMap>> {
Ok(Vc::cell(Some(self.await?.to_rope().await?))) Ok(Vc::cell(Some(self.to_rope()?)))
}
}
/// A regular source map covers an entire file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InnerSourceMap {
map: Arc<CrateMapWrapper>,
}
impl InnerSourceMap {
pub fn new(map: DecodedMap) -> Self {
InnerSourceMap {
map: Arc::new(CrateMapWrapper(map)),
}
}
}
impl Deref for InnerSourceMap {
type Target = Arc<CrateMapWrapper>;
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl Eq for InnerSourceMap {}
impl PartialEq for InnerSourceMap {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.map, &other.map)
} }
} }
...@@ -727,17 +581,6 @@ pub struct RegularMapWrapper(RegularMap); ...@@ -727,17 +581,6 @@ pub struct RegularMapWrapper(RegularMap);
unsafe impl Send for RegularMapWrapper {} unsafe impl Send for RegularMapWrapper {}
unsafe impl Sync for RegularMapWrapper {} unsafe impl Sync for RegularMapWrapper {}
#[derive(Debug)]
pub struct CrateIndexWrapper {
pub sections: Vec<CrateSectionWrapper>,
}
#[derive(Debug)]
pub struct CrateSectionWrapper {
pub offset: SourcePos,
pub map: Arc<CrateMapWrapper>,
}
impl CrateMapWrapper { impl CrateMapWrapper {
pub fn as_regular_source_map(&self) -> Option<Cow<'_, RegularMap>> { pub fn as_regular_source_map(&self) -> Option<Cow<'_, RegularMap>> {
match &self.0 { match &self.0 {
...@@ -773,46 +616,3 @@ impl<'de> Deserialize<'de> for CrateMapWrapper { ...@@ -773,46 +616,3 @@ impl<'de> Deserialize<'de> for CrateMapWrapper {
Ok(CrateMapWrapper(map)) Ok(CrateMapWrapper(map))
} }
} }
/// A sectioned source map contains many (possibly recursive) maps covering
/// different regions of the file.
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct SectionedSourceMap {
sections: Vec<SourceMapSection>,
}
impl SectionedSourceMap {
pub fn new(sections: Vec<SourceMapSection>) -> Self {
Self { sections }
}
pub async fn to_crate_wrapper(&self) -> Result<CrateIndexWrapper> {
let mut sections = Vec::with_capacity(self.sections.len());
for section in &self.sections {
sections.push(section.to_crate_wrapper().await?);
}
Ok(CrateIndexWrapper { sections })
}
}
/// A section of a larger sectioned source map, which applies at source
/// positions >= the offset (until the next section starts).
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct SourceMapSection {
offset: SourcePos,
map: SourceMap,
}
impl SourceMapSection {
pub fn new(offset: SourcePos, map: SourceMap) -> Self {
Self { offset, map }
}
pub async fn to_crate_wrapper(&self) -> Result<CrateSectionWrapper> {
let map = Box::pin(self.map.to_source_map()).await?;
Ok(CrateSectionWrapper {
offset: self.offset,
map,
})
}
}
...@@ -232,7 +232,7 @@ async fn resolve_source_mapping( ...@@ -232,7 +232,7 @@ async fn resolve_source_mapping(
let Some(sm) = &*SourceMap::new_from_rope_cached(sm).await? else { let Some(sm) = &*SourceMap::new_from_rope_cached(sm).await? else {
return Ok(ResolvedSourceMapping::NoSourceMap); return Ok(ResolvedSourceMapping::NoSourceMap);
}; };
let trace = trace_source_map(sm, line, column, name.map(|s| &**s)).await?; let trace = trace_source_map(sm, line, column, name.map(|s| &**s));
match trace { match trace {
TraceResult::Found(frame) => { TraceResult::Found(frame) => {
let lib_code = frame.file.contains("/node_modules/"); let lib_code = frame.file.contains("/node_modules/");
......
use std::{borrow::Cow, fmt::Display}; use std::{borrow::Cow, fmt::Display};
use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbopack_core::source_map::{SourceMap, Token}; use turbopack_core::source_map::{SourceMap, Token};
use turbopack_ecmascript::magic_identifier::unmangle_identifiers; use turbopack_ecmascript::magic_identifier::unmangle_identifiers;
...@@ -91,16 +90,14 @@ pub enum TraceResult { ...@@ -91,16 +90,14 @@ pub enum TraceResult {
/// memory hog, it'd be so much faster if we could just directly access /// memory hog, it'd be so much faster if we could just directly access
/// the individual sections of the JS file's map without the /// the individual sections of the JS file's map without the
/// serialization. /// serialization.
pub async fn trace_source_map( pub fn trace_source_map(
map: &SourceMap, map: &SourceMap,
line: u32, line: u32,
column: u32, column: u32,
name: Option<&str>, name: Option<&str>,
) -> Result<TraceResult> { ) -> TraceResult {
let token = map let token = map.lookup_token(line.saturating_sub(1), column.saturating_sub(1));
.lookup_token(line.saturating_sub(1), column.saturating_sub(1)) match token {
.await?;
let result = match token {
Token::Original(t) => TraceResult::Found(StackFrame { Token::Original(t) => TraceResult::Found(StackFrame {
file: Cow::Owned(t.original_file.into_owned()), file: Cow::Owned(t.original_file.into_owned()),
line: Some(t.original_line.saturating_add(1)), line: Some(t.original_line.saturating_add(1)),
...@@ -113,7 +110,5 @@ pub async fn trace_source_map( ...@@ -113,7 +110,5 @@ pub async fn trace_source_map(
.map(Cow::Owned), .map(Cow::Owned),
}), }),
_ => TraceResult::NotFound, _ => TraceResult::NotFound,
}; }
Ok(result)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment