refactor(transformer): clean up more diagnostics

This commit is contained in:
Boshen 2024-05-12 00:38:40 +08:00
parent 736232ee26
commit 7067f9c646
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1
8 changed files with 26 additions and 51 deletions

View file

@ -17,7 +17,7 @@ pub fn namespace_does_not_support(span0: Span) -> OxcDiagnostic {
} }
pub fn valueless_key(span0: Span) -> OxcDiagnostic { 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.") OxcDiagnostic::warning("Please provide an explicit key value. Using \"key\" as a shorthand for \"key={true}\" is not allowed.")
.with_labels([span0.into()]) .with_labels([span0.into()])
} }

View file

@ -1,10 +0,0 @@
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_span::Span;
#[derive(Debug, Error, Diagnostic)]
#[error("Duplicate __self prop found.")]
#[diagnostic(severity(warning))]
pub struct DuplicateSelfProp(#[label] pub Span);

View file

@ -1,14 +1,11 @@
mod diagnostics;
use std::rc::Rc; use std::rc::Rc;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Span, SPAN}; use oxc_span::{Span, SPAN};
use crate::context::Ctx; use crate::context::Ctx;
use self::diagnostics::DuplicateSelfProp;
const SELF: &str = "__self"; const SELF: &str = "__self";
/// [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self) /// [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
@ -46,7 +43,8 @@ impl<'a> ReactJsxSelf<'a> {
} }
pub fn report_error(&self, span: Span) { pub fn report_error(&self, span: Span) {
self.ctx.error(DuplicateSelfProp(span)); let error = OxcDiagnostic::warning("Duplicate __self prop found.").with_label(span);
self.ctx.error(error);
} }
} }

View file

@ -1,10 +0,0 @@
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_span::Span;
#[derive(Debug, Error, Diagnostic)]
#[error("Duplicate __source prop found.")]
#[diagnostic(severity(warning))]
pub struct DuplicateSourceProp(#[label] pub Span);

View file

@ -1,15 +1,12 @@
mod diagnostics;
use std::rc::Rc; use std::rc::Rc;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Span, SPAN}; use oxc_span::{Span, SPAN};
use oxc_syntax::number::NumberBase; use oxc_syntax::number::NumberBase;
use crate::context::Ctx; use crate::context::Ctx;
use self::diagnostics::DuplicateSourceProp;
use super::utils::get_line_column; use super::utils::get_line_column;
const SOURCE: &str = "__source"; const SOURCE: &str = "__source";
@ -52,7 +49,8 @@ impl<'a> ReactJsxSource<'a> {
} }
pub fn report_error(&self, span: Span) { pub fn report_error(&self, span: Span) {
self.ctx.error(DuplicateSourceProp(span)); let error = OxcDiagnostic::warning("Duplicate __source prop found.").with_label(span);
self.ctx.error(error);
} }
} }

View file

@ -1,15 +1,12 @@
use oxc_diagnostics::{ use oxc_diagnostics::OxcDiagnostic;
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_span::Span; use oxc_span::Span;
#[derive(Debug, Error, Diagnostic)] pub fn import_equals_require_unsupported(span0: Span) -> OxcDiagnostic {
#[error("`import lib = require(...);` is only supported when compiling modules to CommonJS.\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.")] OxcDiagnostic::warning("`import lib = require(...);` is only supported when compiling modules to CommonJS.\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.")
#[diagnostic(severity(warning))] .with_labels([span0.into()])
pub struct ImportEqualsRequireUnsupported(#[label] pub Span); }
#[derive(Debug, Error, Diagnostic)] pub fn export_assignment_unsupported(span0: Span) -> OxcDiagnostic {
#[error("`export = <value>;` is only supported when compiling modules to CommonJS.\nPlease consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.")] OxcDiagnostic::warning("`export = <value>;` is only supported when compiling modules to CommonJS.\nPlease consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.")
#[diagnostic(severity(warning))] .with_labels([span0.into()])
pub struct ExportAssignmentUnsupported(#[label] pub Span); }

View file

@ -2,10 +2,7 @@ use oxc_allocator::Box;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_span::SPAN; use oxc_span::SPAN;
use super::{ use super::TypeScript;
diagnostics::{ExportAssignmentUnsupported, ImportEqualsRequireUnsupported},
TypeScript,
};
impl<'a> TypeScript<'a> { impl<'a> TypeScript<'a> {
fn transform_ts_type_name(&self, type_name: &mut TSTypeName<'a>) -> Expression<'a> { fn transform_ts_type_name(&self, type_name: &mut TSTypeName<'a>) -> Expression<'a> {
@ -50,7 +47,9 @@ impl<'a> TypeScript<'a> {
} }
TSModuleReference::ExternalModuleReference(reference) => { TSModuleReference::ExternalModuleReference(reference) => {
if self.ctx.source_type.is_module() { if self.ctx.source_type.is_module() {
self.ctx.error(ImportEqualsRequireUnsupported(decl_span)); self.ctx.error(super::diagnostics::import_equals_require_unsupported(
decl_span,
));
} }
let callee = self.ctx.ast.identifier_reference_expression( let callee = self.ctx.ast.identifier_reference_expression(
@ -81,7 +80,8 @@ impl<'a> TypeScript<'a> {
export_assignment: &mut TSExportAssignment<'a>, export_assignment: &mut TSExportAssignment<'a>,
) { ) {
if self.ctx.source_type.is_module() { if self.ctx.source_type.is_module() {
self.ctx.error(ExportAssignmentUnsupported(export_assignment.span)); self.ctx
.error(super::diagnostics::export_assignment_unsupported(export_assignment.span));
} }
} }
} }

View file

@ -1,4 +1,4 @@
Passed: 304/362 Passed: 302/362
# All Passed: # All Passed:
* babel-preset-react * babel-preset-react
@ -63,8 +63,10 @@ Passed: 304/362
* optimize-const-enums/merged-exported/input.ts * optimize-const-enums/merged-exported/input.ts
* regression/15768/input.ts * regression/15768/input.ts
# babel-plugin-transform-react-jsx (141/143) # babel-plugin-transform-react-jsx (139/143)
* autoImport/complicated-scope-module/input.js * autoImport/complicated-scope-module/input.js
* react/should-disallow-valueless-key/input.js
* react-automatic/should-disallow-valueless-key/input.js
* react-automatic/should-throw-when-filter-is-specified/input.js * react-automatic/should-throw-when-filter-is-specified/input.js
# babel-plugin-transform-react-jsx-self (2/3) # babel-plugin-transform-react-jsx-self (2/3)