Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Sep 12, 2024
1 parent 5ebc1ec commit 941ddbd
Show file tree
Hide file tree
Showing 28 changed files with 546 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ proc-macro2 = { version = "1.0.79" }
quote = { version = "1.0.35" }
rayon = { version = "1.10.0" }
regex = { version = "1.10.4" }
ropey = "1.6.1"
rspack_sources = { version = "=0.3.2" }
rustc-hash = { version = "1.1.0" }
serde = { version = "1.0.197" }
Expand Down
2 changes: 2 additions & 0 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ default = []
plugin = ["rspack_binding_options/plugin"]

[dependencies]
ropey = { workspace = true }
rspack_allocator = { version = "0.1.0", path = "../rspack_allocator" }
rspack_binding_options = { version = "0.1.0", path = "../rspack_binding_options" }
rspack_binding_values = { version = "0.1.0", path = "../rspack_binding_values" }
Expand All @@ -27,6 +28,7 @@ rspack_napi = { version = "0.1.0", path = "../rspack_napi" }
rspack_paths = { version = "0.1.0", path = "../rspack_paths" }
rspack_plugin_html = { version = "0.1.0", path = "../rspack_plugin_html" }
rspack_plugin_javascript = { version = "0.1.0", path = "../rspack_plugin_javascript" }
rspack_util = { version = "0.1.0", path = "../rspack_util" }

rspack_tracing = { version = "0.1.0", path = "../rspack_tracing" }
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }
Expand Down
31 changes: 27 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ export class JsCompilation {
get chunkGroups(): Array<JsChunkGroup>
get hash(): string | null
dependencies(): DependenciesDto
pushDiagnostic(diagnostic: JsDiagnostic): void
spliceDiagnostic(start: number, end: number, replaceWith: Array<JsDiagnostic>): void
pushDiagnostic(diagnostic: JsRspackDiagnostic): void
spliceDiagnostic(start: number, end: number, replaceWith: Array<JsRspackDiagnostic>): void
pushNativeDiagnostic(diagnostic: ExternalObject<'Diagnostic'>): void
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): void
getErrors(): Array<JsRspackError>
getWarnings(): Array<JsRspackError>
Expand Down Expand Up @@ -281,6 +282,8 @@ export interface ContextInfo {
issuer: string
}

export function formatDiagnostic(diagnostic: JsDiagnostic): ExternalObject<Diagnostic>

export interface JsAdditionalTreeRuntimeRequirementsArg {
chunk: JsChunk
runtimeRequirements: JsRuntimeGlobals
Expand Down Expand Up @@ -484,8 +487,23 @@ export interface JsCreateData {
}

export interface JsDiagnostic {
severity: JsRspackSeverity
error: JsRspackError
message: string
help?: string
sourceCode?: string
location?: JsDiagnosticLocation
file?: string
severity: "error" | "warning"
moduleIdentifier?: string
}

export interface JsDiagnosticLocation {
text?: string
/** 1-based */
line: number
/** 0-based in bytes */
column: number
/** Length in bytes */
length: number
}

export interface JsEntryData {
Expand Down Expand Up @@ -689,6 +707,11 @@ export interface JsResourceData {
fragment?: string
}

export interface JsRspackDiagnostic {
severity: JsRspackSeverity
error: JsRspackError
}

export interface JsRspackError {
name: string
message: string
Expand Down
83 changes: 83 additions & 0 deletions crates/node_binding/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use napi::bindgen_prelude::External;
use rspack_error::{
miette::{self, LabeledSpan, MietteDiagnostic, Severity},
Diagnostic,
};
use rspack_util::location::{
try_line_column_length_to_location, try_line_column_length_to_offset_length,
};

#[napi(object)]
pub struct JsDiagnosticLocation {
pub text: Option<String>,
/// 1-based
pub line: u32,
/// 0-based in bytes
pub column: u32,
/// Length in bytes
pub length: u32,
}

#[napi(object)]
pub struct JsDiagnostic {
pub message: String,
pub help: Option<String>,
pub source_code: Option<String>,
pub location: Option<JsDiagnosticLocation>,
pub file: Option<String>,

#[napi(ts_type = "\"error\" | \"warning\"")]
pub severity: String,
pub module_identifier: Option<String>,
}

#[napi]
pub fn format_diagnostic(diagnostic: JsDiagnostic) -> External<Diagnostic> {
let JsDiagnostic {
message,
help,
source_code,
location,
severity,
module_identifier,
file,
} = diagnostic;
let mut d = MietteDiagnostic::new(message).with_severity(match severity.as_str() {
"warning" => Severity::Warning,
_ => Severity::Error,
});
if let Some(help) = help {
d = d.with_help(help);
}
let mut loc = None;
if let Some(ref source_code) = source_code {
let rope = ropey::Rope::from_str(source_code);
if let Some(location) = location {
loc = try_line_column_length_to_location(
&rope,
location.line as usize,
location.column as usize,
location.length as usize,
);
if let Some((offset, length)) = try_line_column_length_to_offset_length(
&rope,
location.line as usize,
location.column as usize,
location.length as usize,
) {
d = d.with_label(LabeledSpan::new(location.text, offset, length));
}
}
}

let mut error = miette::Error::new(d);
if let Some(source_code) = source_code {
error = error.with_source_code(source_code);
}
External::new(
Diagnostic::from(error)
.with_file(file.map(Into::into))
.with_loc(loc.map(|l| l.to_string()))
.with_module_identifier(module_identifier.map(Into::into)),
)
}
2 changes: 2 additions & 0 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ use rspack_error::Diagnostic;
use rspack_fs_node::{AsyncNodeWritableFileSystem, ThreadsafeNodeFS};

mod compiler;
mod diagnostic;
mod panic;
mod plugins;
mod resolver_factory;

pub use diagnostic::*;
use plugins::*;
use resolver_factory::*;
use rspack_binding_options::*;
Expand Down
21 changes: 21 additions & 0 deletions crates/node_binding/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { formatDiagnostic } = require("./binding")

const diagnostic = {
name: "ModuleError",
message: "failed to link",
severity: "error",
sourceCode: `abc;
def;
ghi;`,
help: "Try to fix it",
location: {
text: "abc",
line: 1,
column: 1,
length: 1,
},
module_identifier: "test",
file: "test",
}

formatDiagnostic(diagnostic)
11 changes: 8 additions & 3 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
chunk::JsChunk, CompatSource, JsAsset, JsAssetInfo, JsChunkGroup, JsCompatSource, JsPathData,
JsStats, ToJsCompatSource,
};
use crate::{JsDiagnostic, JsRspackError};
use crate::{JsRspackDiagnostic, JsRspackError};

#[napi]
pub struct JsCompilation(pub(crate) &'static mut rspack_core::Compilation);
Expand Down Expand Up @@ -309,7 +309,7 @@ impl JsCompilation {
}

#[napi]
pub fn push_diagnostic(&mut self, diagnostic: JsDiagnostic) {
pub fn push_diagnostic(&mut self, diagnostic: JsRspackDiagnostic) {
self.0.push_diagnostic(diagnostic.into());
}

Expand All @@ -318,14 +318,19 @@ impl JsCompilation {
&mut self,
start: u32,
end: u32,
replace_with: Vec<crate::JsDiagnostic>,
replace_with: Vec<crate::JsRspackDiagnostic>,
) {
let diagnostics = replace_with.into_iter().map(Into::into).collect();
self
.0
.splice_diagnostic(start as usize, end as usize, diagnostics);
}

#[napi(ts_args_type = r#"diagnostic: ExternalObject<'Diagnostic'>"#)]
pub fn push_native_diagnostic(&mut self, diagnostic: External<Diagnostic>) {
self.0.push_diagnostic(diagnostic.clone());
}

#[napi(ts_args_type = r#"diagnostics: ExternalObject<'Diagnostic[]'>"#)]
pub fn push_native_diagnostics(&mut self, mut diagnostics: External<Vec<Diagnostic>>) {
while let Some(diagnostic) = diagnostics.pop() {
Expand Down
17 changes: 13 additions & 4 deletions crates/rspack_binding_values/src/rspack_error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use napi_derive::napi;
use rspack_error::{Diagnostic, Result, RspackSeverity};
use rspack_error::{miette, Diagnostic, Result, RspackSeverity};

#[napi(object)]
pub struct JsDiagnostic {
pub struct JsRspackDiagnostic {
pub severity: JsRspackSeverity,
pub error: JsRspackError,
}

impl From<JsDiagnostic> for Diagnostic {
fn from(value: JsDiagnostic) -> Self {
impl From<JsRspackDiagnostic> for Diagnostic {
fn from(value: JsRspackDiagnostic) -> Self {
value.error.into_diagnostic(value.severity.into())
}
}
Expand All @@ -28,6 +28,15 @@ impl From<JsRspackSeverity> for RspackSeverity {
}
}

impl From<JsRspackSeverity> for miette::Severity {
fn from(value: JsRspackSeverity) -> Self {
match value {
JsRspackSeverity::Error => miette::Severity::Error,
JsRspackSeverity::Warn => miette::Severity::Warning,
}
}
}

#[napi(object)]
#[derive(Debug)]
pub struct JsRspackError {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ num-bigint = { version = "0.4.4" }
once_cell = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
ropey = "1.6.1"
ropey = { workspace = true }
rspack_ast = { version = "0.1.0", path = "../rspack_ast" }
rspack_collections = { version = "0.1.0", path = "../rspack_collections" }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/src/webpack_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn byte_offset_to_location(rope: &ropey::Rope, start: usize, end: usize) -> Loca

/// Convert match item to error span within the source
///
/// # Panic
/// # Panics
///
/// Panics if `comment_span` is out-of-bound of `source`.
/// Panics if either `match_start` or `match_end` is out-of-bound of `comment_text`.
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dashmap = { workspace = true }
indexmap = { workspace = true }
itoa = { version = "1.0.11" }
regex = { workspace = true }
ropey = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod fx_hash;
pub mod identifier;
pub mod infallible;
pub mod itoa;
pub mod location;
pub mod number_hash;
pub mod path;
pub mod queue;
Expand Down
Loading

0 comments on commit 941ddbd

Please sign in to comment.