Skip to content

Commit

Permalink
port turbopack-swc-utils to ResolvedVc
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 22, 2024
1 parent 3ba49c7 commit 7abcea0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
29 changes: 18 additions & 11 deletions turbopack/crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl CapturedIssues {
#[turbo_tasks::value]
#[derive(Clone, Debug)]
pub struct IssueSource {
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
range: Option<ResolvedVc<SourceRange>>,
}

Expand All @@ -447,7 +447,7 @@ impl IssueSource {
// Sometimes we only have the source file that causes an issue, not the
// exact location, such as as in some generated code.
#[turbo_tasks::function]
pub fn from_source_only(source: Vc<Box<dyn Source>>) -> Vc<Self> {
pub fn from_source_only(source: ResolvedVc<Box<dyn Source>>) -> Vc<Self> {
Self::cell(IssueSource {
source,
range: None,
Expand All @@ -456,7 +456,7 @@ impl IssueSource {

#[turbo_tasks::function]
pub fn from_line_col(
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
start: SourcePos,
end: SourcePos,
) -> Vc<Self> {
Expand Down Expand Up @@ -511,7 +511,11 @@ impl IssueSource {
/// * `start`: The start index of the span. Must use **1-based** indexing.
/// * `end`: The end index of the span. Must use **1-based** indexing.
#[turbo_tasks::function]
pub fn from_swc_offsets(source: Vc<Box<dyn Source>>, start: usize, end: usize) -> Vc<Self> {
pub fn from_swc_offsets(
source: ResolvedVc<Box<dyn Source>>,
start: usize,
end: usize,
) -> Vc<Self> {
Self::cell(IssueSource {
source,
range: match (start == 0, end == 0) {
Expand All @@ -536,7 +540,7 @@ impl IssueSource {
/// * `start`: Byte offset into the source that the text begins. 0-based index and inclusive.
/// * `end`: Byte offset into the source that the text ends. 0-based index and exclusive.
pub async fn from_byte_offset(
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
start: usize,
end: usize,
) -> Result<Vc<Self>> {
Expand Down Expand Up @@ -581,12 +585,12 @@ impl IssueSource {
}

async fn source_pos(
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
origin: Vc<FileSystemPath>,
start: SourcePos,
end: SourcePos,
) -> Result<Option<(Vc<Box<dyn Source>>, SourcePos, SourcePos)>> {
let Some(generator) = Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(source).await?
) -> Result<Option<(ResolvedVc<Box<dyn Source>>, SourcePos, SourcePos)>> {
let Some(generator) = ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(source).await?
else {
return Ok(None);
};
Expand Down Expand Up @@ -628,7 +632,10 @@ async fn source_pos(
return Ok(None);
};

let (content_1, content_2) = (content_1.resolve().await?, content_2.resolve().await?);
let (content_1, content_2) = (
content_1.to_resolved().await?,
content_2.to_resolved().await?,
);

if content_1 != content_2 {
return Ok(None);
Expand Down Expand Up @@ -775,7 +782,7 @@ impl IssueSource {
#[turbo_tasks::function]
pub async fn into_plain(&self) -> Result<Vc<PlainIssueSource>> {
Ok(PlainIssueSource {
asset: PlainSource::from_source(self.source).await?,
asset: PlainSource::from_source(*self.source).await?,
range: match self.range {
Some(range) => match &*range.await? {
SourceRange::LineColumn(start, end) => Some((*start, *end)),
Expand Down Expand Up @@ -809,7 +816,7 @@ pub struct PlainSource {
#[turbo_tasks::value_impl]
impl PlainSource {
#[turbo_tasks::function]
pub async fn from_source(asset: Vc<Box<dyn Source>>) -> Result<Vc<PlainSource>> {
pub async fn from_source(asset: ResolvedVc<Box<dyn Source>>) -> Result<Vc<PlainSource>> {
let asset_content = asset.content().await?;
let content = match *asset_content {
AssetContent::File(file_content) => file_content.await?,
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-ecmascript/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ async fn parse_file_content(
true,
false,
Box::new(IssueEmitter::new(
*source,
source,
source_map.clone(),
Some("Ecmascript file had an error".into()),
)),
);

let emitter = Box::new(IssueEmitter::new(
*source,
source,
source_map.clone(),
Some("Parsing ecmascript source code failed".into()),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
let handler = Handler::with_emitter(
true,
false,
Box::new(IssueEmitter::new(*source, source_map.clone(), None)),
Box::new(IssueEmitter::new(source, source_map.clone(), None)),
);

let mut var_graph =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use crate::{

#[turbo_tasks::function]
pub async fn module_references(
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
runtime: Vc<WebpackRuntime>,
transforms: ResolvedVc<EcmascriptInputTransforms>,
) -> Result<Vc<ModuleReferences>> {
let parsed = parse(
source,
*source,
Value::new(EcmascriptModuleAssetType::Ecmascript),
*transforms,
)
Expand Down
12 changes: 6 additions & 6 deletions turbopack/crates/turbopack-swc-utils/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use swc_core::common::{
SourceMap,
};
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, IssueSource, StyledString},
source::Source,
};

#[derive(Clone)]
pub struct IssueEmitter {
pub source: Vc<Box<dyn Source>>,
pub source: ResolvedVc<Box<dyn Source>>,
pub source_map: Arc<SourceMap>,
pub title: Option<RcStr>,
pub emitted_issues: Vec<Vc<AnalyzeIssue>>,
pub emitted_issues: Vec<ResolvedVc<AnalyzeIssue>>,
}

impl IssueEmitter {
pub fn new(
source: Vc<Box<dyn Source>>,
source: ResolvedVc<Box<dyn Source>>,
source_map: Arc<SourceMap>,
title: Option<RcStr>,
) -> Self {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Emitter for IssueEmitter {
}

let source = db.span.primary_span().map(|span| {
IssueSource::from_swc_offsets(self.source, span.lo.to_usize(), span.hi.to_usize())
IssueSource::from_swc_offsets(*self.source, span.lo.to_usize(), span.hi.to_usize())
});
// TODO add other primary and secondary spans with labels as sub_issues

Expand All @@ -91,7 +91,7 @@ impl Emitter for IssueEmitter {
code,
source,
}
.cell();
.resolved_cell();

self.emitted_issues.push(issue);

Expand Down

0 comments on commit 7abcea0

Please sign in to comment.