Skip to content

Commit

Permalink
Convert MDX errors into issues (vercel/turborepo#8879)
Browse files Browse the repository at this point in the history
Trying vercel/turborepo#8031 again

~~Depends on vercel/turborepo#8766

<img width="979" alt="Bildschirmfoto 2024-07-30 um 15 35 48"
src="https://github.com/user-attachments/assets/32d88ffc-c816-41fb-928b-79d27e89b56d">

---------

Co-authored-by: 강동윤 (Donny) <[email protected]>
  • Loading branch information
mischnic and kdy1 authored Jul 31, 2024
1 parent 58cd69e commit 92498e0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 9 deletions.
1 change: 1 addition & 0 deletions crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ pub struct IssueSource {
range: Option<Vc<SourceRange>>,
}

/// The end position is the first character after the range
#[turbo_tasks::value]
#[derive(Clone, Debug)]
enum SourceRange {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-mdx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
anyhow = { workspace = true }
serde = { workspace = true }

markdown = { workspace = true }
mdxjs = { workspace = true }

turbo-tasks = { workspace = true }
Expand Down
105 changes: 96 additions & 9 deletions crates/turbopack-mdx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#![feature(min_specialization)]
#![feature(arbitrary_self_types)]

use anyhow::{anyhow, Result};
use anyhow::Result;
use mdxjs::{compile, MdxParseOptions, Options};
use turbo_tasks::{RcStr, ValueDefault, Vc};
use turbo_tasks_fs::{rope::Rope, File, FileContent};
use turbo_tasks_fs::{rope::Rope, File, FileContent, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
ident::AssetIdent,
issue::IssueDescriptionExt,
issue::{
Issue, IssueDescriptionExt, IssueExt, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
source::Source,
source_pos::SourcePos,
source_transform::SourceTransform,
};

Expand Down Expand Up @@ -175,14 +179,56 @@ impl MdxTransformedAsset {
..Default::default()
};

// TODO: upstream mdx currently bubbles error as string
let mdx_jsx_component =
compile(&file.content().to_str()?, &options).map_err(|e| anyhow!("{}", e))?;
let result = compile(&file.content().to_str()?, &options);

Ok(MdxTransformResult {
content: AssetContent::file(File::from(Rope::from(mdx_jsx_component)).into()),
match result {
Ok(mdx_jsx_component) => Ok(MdxTransformResult {
content: AssetContent::file(File::from(Rope::from(mdx_jsx_component)).into()),
}
.cell()),
Err(err) => {
let loc = Vc::cell(err.place.map(|p| {
let (start, end) = match *p {
// markdown's positions are 1-indexed, SourcePos is 0-indexed.
// Both end positions point to the first character after the range
markdown::message::Place::Position(p) => (
SourcePos {
line: p.start.line - 1,
column: p.start.column - 1,
},
SourcePos {
line: p.end.line - 1,
column: p.end.column - 1,
},
),
markdown::message::Place::Point(p) => {
let p = SourcePos {
line: p.line - 1,
column: p.column - 1,
};
(p, p)
}
};

IssueSource::from_line_col(this.source, start, end)
}));

MdxIssue {
path: this.source.ident().path(),
loc,
reason: err.reason,
mdx_rule_id: *err.rule_id,
mdx_source: *err.source,
}
.cell()
.emit();

Ok(MdxTransformResult {
content: AssetContent::File(FileContent::NotFound.cell()).cell(),
}
.cell())
}
}
.cell())
}
}

Expand All @@ -191,6 +237,47 @@ struct MdxTransformResult {
content: Vc<AssetContent>,
}

#[turbo_tasks::value]
struct MdxIssue {
/// Place of message.
path: Vc<FileSystemPath>,
loc: Vc<OptionIssueSource>,
/// Reason for message (should use markdown).
reason: String,
/// Category of message.
mdx_rule_id: String,
/// Namespace of message.
mdx_source: String,
}

#[turbo_tasks::value_impl]
impl Issue for MdxIssue {
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.path
}

#[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> {
self.loc
}

#[turbo_tasks::function]
fn stage(self: Vc<Self>) -> Vc<IssueStage> {
IssueStage::Parse.cell()
}

#[turbo_tasks::function]
fn title(self: Vc<Self>) -> Vc<StyledString> {
StyledString::Text("MDX Parse Error".into()).cell()
}

#[turbo_tasks::function]
fn description(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(StyledString::Text(self.reason.clone().into()).cell()))
}
}

pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
Expand Down

0 comments on commit 92498e0

Please sign in to comment.