Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add pretty errors #12

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
swc_core = { version = "0.86.*", features = ["ecma_plugin_transform"] }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-tag-swc-plugin",
"version": "0.1.6",
"version": "0.1.7-0",
"description": "SWC plugin to expand gql tags at build time",
"author": "rishabh3112 <[email protected]>",
"license": "ISC",
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// libs
use serde::Deserialize;
use swc_core::plugin::{
metadata::TransformPluginMetadataContextKind,
plugin_transform,
proxies::{PluginCommentsProxy, TransformPluginProgramMetadata},
};
Expand All @@ -21,6 +22,11 @@ pub struct Config {

#[plugin_transform]
pub fn process_transform(program: Program, data: TransformPluginProgramMetadata) -> Program {
let mut file_path: String = String::new();
if let Some(name) = data.get_context(&TransformPluginMetadataContextKind::Filename) {
file_path = name;
}

let mut program = program;
let mut unique_visitor = UniqueIdentifierVisitor::new();

Expand All @@ -36,6 +42,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: false,
file_path: String::new(),
unique_fn_name: unique_fn_name.clone(),
unique_fn_used: false,
};
Expand All @@ -52,6 +59,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
.gql_tag_identifiers
.unwrap_or(default_config.gql_tag_identifiers),
strip: config.strip.unwrap_or(false),
file_path,
unique_fn_name,
unique_fn_used: false,
},
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn graphql_tag_fixture(input: PathBuf) {
strip: false,
unique_fn_name: "unique".into(),
unique_fn_used: false,
file_path: input.to_str().unwrap().into(),
},
_tr.comments.clone(),
))
Expand All @@ -58,6 +59,7 @@ fn graphql_tag_fixture(input: PathBuf) {
strip: true,
unique_fn_name: "unique".into(),
unique_fn_used: false,
file_path: input.to_str().unwrap().into(),
},
_tr.comments.clone(),
))
Expand Down
52 changes: 48 additions & 4 deletions transforms/graphql_tag/Cargo.lock

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

2 changes: 2 additions & 0 deletions transforms/graphql_tag/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ swc_core = { version = "0.86.55", features = ["ecma_plugin_transform"] }
regex = "1"
serde = "1.0.193"
serde_json = "1.0.108"
miette = { version = "3.2.0", features = ["fancy"] }
thiserror = "1.0.30"
29 changes: 21 additions & 8 deletions transforms/graphql_tag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::collections::HashMap;

// libs
use miette::NamedSource;
use swc_common::comments::Comments;
use swc_ecma_ast::*;
use swc_ecma_visit::{VisitMut, VisitMutWith};
Expand All @@ -15,14 +16,23 @@ mod utils;
use parser::utils::strip_ignored_characters;

// structs
use structs::{GraphQLTagConfig, TransformVisitor};
use structs::{GraphQLTagConfig, PrettyError, TransformVisitor};
use utils::add_unique_fn_to_program;

impl<C> TransformVisitor<C>
where
C: Comments,
{
pub fn new(config: GraphQLTagConfig, comments: C) -> Self {
let _ = miette::set_hook(Box::new(|_| {
Box::new(
miette::MietteHandlerOpts::new()
.force_graphical(true)
.color(true)
.build(),
)
}));

Self {
unique_fn_used: false,
active_gql_tag_identifiers: vec![],
Expand Down Expand Up @@ -129,7 +139,7 @@ where

let unique_fn_name = self.config.unique_fn_name.clone();
let gql_swc_ast_result = parser::parse_graphql_tag(
gql_text,
gql_text.clone(),
tag_tpl.span,
expressions,
&mut self.expr_def_map,
Expand All @@ -142,12 +152,15 @@ where
Ok(swc_ast) => *node = swc_ast,
Err(gql_ast) => {
for error in gql_ast.errors() {
println!(
"GraphQL Error: At index {}, {} got \"{}\" instead\n",
error.index(),
error.message(),
error.data()
)
let miette_error = miette::Report::new(PrettyError {
ty: format!("{}, got {} instead", error.message(), error.data()),
src: NamedSource::new(
self.config.file_path.clone(),
gql_text.clone(),
),
span: (error.index(), error.data().len()).into(),
});
println!("\n{:?}", miette_error);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions transforms/graphql_tag/src/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use std::collections::HashMap;

use swc_common::comments::Comments;
// libs
use miette::{Diagnostic, NamedSource, SourceSpan};
use swc_ecma_ast::Expr;
use thiserror::Error;

pub struct GraphQLTagConfig {
pub import_sources: Vec<String>,
pub gql_tag_identifiers: Vec<String>,
pub strip: bool,
pub file_path: String,
pub unique_fn_name: String,
pub unique_fn_used: bool,
}
Expand All @@ -23,3 +26,14 @@ where
pub comments: C,
pub unique_fn_used: bool,
}

#[derive(Error, Debug, Diagnostic)]
#[error("{}", self.ty)]
#[diagnostic(code("GraphQL Error"))]
pub struct PrettyError {
pub ty: String,
#[source_code]
pub src: NamedSource,
#[label("{}", self.ty)]
pub span: SourceSpan,
}