mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer): unify diagnostics
This commit is contained in:
parent
6e90f67c47
commit
d351f2d93a
2 changed files with 26 additions and 34 deletions
|
|
@ -1,30 +1,27 @@
|
|||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::Error,
|
||||
};
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
use oxc_span::Span;
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("pragma and pragmaFrag cannot be set when runtime is automatic.")]
|
||||
#[diagnostic(severity(warning), help("Remove `pragma` and `pragmaFrag` options."))]
|
||||
pub struct PragmaAndPragmaFragCannotBeSet;
|
||||
pub fn pragma_and_pragma_frag_cannot_be_set() -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("pragma and pragmaFrag cannot be set when runtime is automatic.")
|
||||
.with_help("Remove `pragma` and `pragmaFrag` options.")
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("importSource cannot be set when runtime is classic.")]
|
||||
#[diagnostic(severity(warning), help("Remove `importSource` option."))]
|
||||
pub struct ImportSourceCannotBeSet;
|
||||
pub fn import_source_cannot_be_set() -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("importSource cannot be set when runtime is classic.")
|
||||
.with_help("Remove `importSource` option.")
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.")]
|
||||
#[diagnostic(severity(warning))]
|
||||
pub struct NamespaceDoesNotSupport(#[label] pub Span);
|
||||
pub fn namespace_does_not_support(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can set `throwIfNamespace: false` to bypass this warning.")
|
||||
.with_labels([span0.into()])
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("Please provide an explicit key value. Using \"key\" as a shorthand for \"key={{true}}\" is not allowed.")]
|
||||
#[diagnostic(severity(warning))]
|
||||
pub struct ValuelessKey(#[label] pub Span);
|
||||
pub fn valueless_key(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("Please provide an explicit key value. Using \"key\" as a shorthand for \"key={{true}}\" is not allowed.")
|
||||
.with_labels([span0.into()])
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("Spread children are not supported in React.")]
|
||||
#[diagnostic(severity(warning))]
|
||||
pub struct SpreadChildrenAreNotSupported(#[label] pub Span);
|
||||
pub fn spread_children_are_not_supported(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("Spread children are not supported in React.")
|
||||
.with_labels([span0.into()])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,11 +19,6 @@ pub use super::{
|
|||
options::{ReactJsxRuntime, ReactOptions},
|
||||
};
|
||||
|
||||
use self::diagnostics::{
|
||||
ImportSourceCannotBeSet, NamespaceDoesNotSupport, PragmaAndPragmaFragCannotBeSet,
|
||||
SpreadChildrenAreNotSupported, ValuelessKey,
|
||||
};
|
||||
|
||||
/// [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
|
||||
///
|
||||
/// This plugin generates production-ready JS code.
|
||||
|
|
@ -109,7 +104,7 @@ impl<'a> ReactJsx<'a> {
|
|||
pub fn add_runtime_imports(&mut self, program: &mut Program<'a>) {
|
||||
if self.options.runtime.is_classic() {
|
||||
if self.options.import_source != "react" {
|
||||
self.ctx.error(ImportSourceCannotBeSet);
|
||||
self.ctx.error(diagnostics::import_source_cannot_be_set());
|
||||
}
|
||||
|
||||
if self.options.is_jsx_source_plugin_enabled() {
|
||||
|
|
@ -122,7 +117,7 @@ impl<'a> ReactJsx<'a> {
|
|||
if self.options.pragma != "React.createElement"
|
||||
|| self.options.pragma_frag != "React.Fragment"
|
||||
{
|
||||
self.ctx.error(PragmaAndPragmaFragCannotBeSet);
|
||||
self.ctx.error(diagnostics::pragma_and_pragma_frag_cannot_be_set());
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -363,7 +358,7 @@ impl<'a> ReactJsx<'a> {
|
|||
|
||||
if attr.is_key() {
|
||||
if attr.value.is_none() {
|
||||
self.ctx.error(ValuelessKey(attr.name.span()));
|
||||
self.ctx.error(diagnostics::valueless_key(attr.name.span()));
|
||||
}
|
||||
// In automatic mode, extract the key before spread prop,
|
||||
// and add it to the third argument later.
|
||||
|
|
@ -521,7 +516,7 @@ impl<'a> ReactJsx<'a> {
|
|||
}
|
||||
JSXElementName::NamespacedName(name) => {
|
||||
if self.options.throw_if_namespace {
|
||||
self.ctx.error(NamespaceDoesNotSupport(name.span));
|
||||
self.ctx.error(diagnostics::namespace_does_not_support(name.span));
|
||||
}
|
||||
let name = self.ast().new_atom(&name.to_string());
|
||||
let string_literal = StringLiteral::new(SPAN, name);
|
||||
|
|
@ -718,7 +713,7 @@ impl<'a> ReactJsx<'a> {
|
|||
JSXChild::Element(e) => Some(self.transform_jsx(&JSXElementOrFragment::Element(e))),
|
||||
JSXChild::Fragment(e) => Some(self.transform_jsx(&JSXElementOrFragment::Fragment(e))),
|
||||
JSXChild::Spread(e) => {
|
||||
self.ctx.error(SpreadChildrenAreNotSupported(e.span));
|
||||
self.ctx.error(diagnostics::spread_children_are_not_supported(e.span));
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue