Skip to content

Commit

Permalink
feat(#23): implement type-only defineEmits
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix-ru committed Jul 24, 2024
1 parent 0daf522 commit 270e261
Show file tree
Hide file tree
Showing 9 changed files with 1,640 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/fervid/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Spanned for CompileError {
fn span(&self) -> swc_core::common::Span {
match self {
CompileError::SfcParse(e) => e.span,
CompileError::TransformError(e) => e.span
CompileError::TransformError(e) => e.span()
}
}
}
1 change: 1 addition & 0 deletions crates/fervid_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ lazy_static = { workspace = true }
phf = { workspace = true }
swc_core = { workspace = true, features = ["common", "ecma_ast", "ecma_visit"] }
smallvec = { workspace = true }
itertools = "*"

[dev-dependencies]
swc_ecma_codegen = { workspace = true }
Expand Down
56 changes: 44 additions & 12 deletions crates/fervid_transform/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,71 @@ use fervid_css::CssError;
use swc_core::common::{Span, Spanned};

#[derive(Debug)]
pub struct TransformError {
pub span: Span,
pub kind: TransformErrorKind
pub enum TransformError {
CssError(CssError),
ScriptError(ScriptError)
}

#[derive(Debug)]
pub enum TransformErrorKind {
CssError(CssError),
ScriptError(ScriptError)
pub struct ScriptError {
pub span: Span,
pub kind: ScriptErrorKind
}

#[derive(Debug)]
pub enum ScriptError {
pub enum ScriptErrorKind {
/// A compiler macro was imported, but it didn't need to
CompilerMacroImport,
/// `defineEmits` called with 0 type arguments (e.g. `defineEmits<>()`)
DefineEmitsMalformed,
/// `defineEmits` was called with both runtime and type arguments
DefineEmitsTypeAndNonTypeArguments,
/// "defineEmits() type cannot mixed call signature and property syntax"
DefineEmitsMixedCallAndPropertySyntax,
/// Duplicate `defineEmits` call
DuplicateDefineEmits,
/// Different imports using the same local symbol,
/// e.g `import foo from './foo'` and `import { foo } from './bar'`.
DuplicateImport,
/// Could not resolve array element type
ResolveTypeElementType,
/// A type param was not provided,
/// e.g. `ExtractPropTypes<>`
ResolveTypeMissingTypeParam,
/// Type parameters were not provided,
/// e.g. `ExtractPropTypes`
ResolveTypeMissingTypeParams,
/// A type both not supported and not planned to be supported during type resolution
ResolveTypeUnresolvable,
/// "Failed to resolve index type into finite keys"
ResolveTypeUnresolvableIndexType,
/// An unsupported construction during type resolution
ResolveTypeUnsupported,
/// "Unsupported type when resolving index type"
ResolveTypeUnsupportedIndexType,
/// Unsupported computed key in type referenced by a macro
ResolveTypeUnsupportedComputedKey,
/// Disallow non-type exports inside `<script setup>`
SetupExport,
}

impl From<CssError> for TransformError {
fn from(value: CssError) -> Self {
TransformError {
span: value.span(),
kind: TransformErrorKind::CssError(value)
}
TransformError::CssError(value)
}
}

impl From<ScriptError> for TransformError {
fn from(value: ScriptError) -> Self {
TransformError::ScriptError(value)
}
}

impl Spanned for TransformError {
fn span(&self) -> Span {
self.span
match self {
TransformError::CssError(e) => e.span,
TransformError::ScriptError(e) => e.span,
}
}
}
1 change: 1 addition & 0 deletions crates/fervid_transform/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use self::{
pub mod common;
mod imports;
mod options_api;
mod resolve_type;
mod setup;
pub mod utils;

Expand Down
14 changes: 7 additions & 7 deletions crates/fervid_transform/src/script/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_core::ecma::{

use crate::{
atoms::{COMPUTED, DEFINE_EMITS, DEFINE_EXPOSE, DEFINE_PROPS, REACTIVE, REF, VUE},
error::{ScriptError, TransformError, TransformErrorKind},
error::{ScriptError, ScriptErrorKind, TransformError },
structs::VueResolvedImports,
BindingsHelper, ImportBinding, SetupBinding,
};
Expand Down Expand Up @@ -114,10 +114,10 @@ pub fn register_import(
|| *imported_word == *DEFINE_EMITS
|| *imported_word == *DEFINE_EXPOSE
{
errors.push(TransformError {
errors.push(TransformError::ScriptError(ScriptError {
span: named_spec.span,
kind: TransformErrorKind::ScriptError(ScriptError::CompilerMacroImport),
});
kind: ScriptErrorKind::CompilerMacroImport,
}));
return false;
}

Expand Down Expand Up @@ -145,10 +145,10 @@ pub fn register_import(
if let Some(existing) = bindings_helper.user_imports.get(&local) {
// Not exact duplicate means some local name has been used twice
if existing.source != *source || existing.imported != imported {
errors.push(TransformError {
errors.push(TransformError::ScriptError(ScriptError {
span,
kind: TransformErrorKind::ScriptError(ScriptError::DuplicateImport),
});
kind: ScriptErrorKind::DuplicateImport,
}));
}

return false;
Expand Down
Loading

0 comments on commit 270e261

Please sign in to comment.