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

Fix error reporting #16

Merged
merged 1 commit into from
Sep 5, 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
492 changes: 270 additions & 222 deletions Cargo.lock

Large diffs are not rendered by default.

190 changes: 102 additions & 88 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions packages/fusion-js-test/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const run = async () => {
},
},
})
).code;
result = await format(result, { parser: "babel" });
);
result = await format(result.code, { parser: "babel" });

if (result != output) {
console.log(`❌ Fixture ${fixtureName} failed.`);
Expand Down Expand Up @@ -111,4 +111,6 @@ const run = async () => {
process.exit(0);
};

run();
run().catch(e => {
console.log(e);
});
2 changes: 1 addition & 1 deletion packages/fusion-js-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Vojtech Miksu <vojtech@uber.com>",
"license": "Apache-2.0",
"dependencies": {
"@swc/core": "^1.3.58",
"@swc/core": "^1.3.82",
"colors": "^1.4.0",
"diff": "^5.1.0",
"prettier": "^2.8.7"
Expand Down
4 changes: 2 additions & 2 deletions packages/fusion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
serde = "1"
serde_json = "1.0.79"
swc_common = { version = "0.31.9", features = ["concurrent"] }
swc_core = { version = "0.76.6", features = [
swc_common = { version = "0.32.1", features = ["concurrent"] }
swc_core = { version = "0.83.0", features = [
"ecma_plugin_transform",
"ecma_utils",
"ecma_visit",
Expand Down
7 changes: 5 additions & 2 deletions packages/fusion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
use std::path::PathBuf;

use fusion::Config;
use swc_common::{plugin::metadata::TransformPluginMetadataContextKind, FileName};
use swc_common::FileName;
use swc_core::{
ecma::{ast::Program, visit::VisitMutWith},
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
plugin::{
metadata::{TransformPluginMetadataContextKind, TransformPluginProgramMetadata},
plugin_transform,
},
};

#[plugin_transform]
Expand Down
4 changes: 2 additions & 2 deletions packages/fusion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ swc_core = { features = [
"ecma_utils",
"ecma_visit",
"trace_macro",
], version = "0.76.6" }
], version = "0.83.0" }
tracing = { version = "0.1.37" }
url = "2.2.2"

Expand All @@ -36,5 +36,5 @@ swc_core = { features = [
"testing_transform",
"ecma_parser",
"ecma_transforms_react",
], version = "0.76.6" }
], version = "0.83.0" }
testing = "0.33.10"
30 changes: 19 additions & 11 deletions packages/fusion/transform/src/i18n/analyze_imports.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{cell::RefCell, rc::Rc};

use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitWith,
},
},
plugin::errors::HANDLER,
};
use tracing::debug;

Expand Down Expand Up @@ -61,18 +61,24 @@ pub fn find_id_attribute(opening_element: &JSXOpeningElement) -> Option<String>
return Some(lit_str.value.as_ref().to_string());
}
_ => HANDLER.with(|handler| {
handler.err(&format!(
"The translate component must have props.id being \
a string literal."
));
handler
.struct_span_err(
attr.span,
"The translate component id prop must be a a \
string literal.",
)
.emit();
}),
}
}
_ => HANDLER.with(|handler| {
handler.err(&format!(
"The translate component must have props.id being a \
string literal."
));
handler
.struct_span_err(
attr.span,
"The translate component id prop must be a a string \
literal.",
)
.emit();
}),
}
}
Expand Down Expand Up @@ -184,7 +190,7 @@ impl Visit for Analyzer<'_> {

fn visit_call_expr(&mut self, call_expr: &CallExpr) {
call_expr.visit_children_with(self);
let error_msg = "The withTranslations hoc must be called with an array of string literal \
let error_msg = "The withTranslations HoC must be called with an array of string literal \
translation keys.";
match &call_expr.callee {
Callee::Expr(boxed_expr) => match &**boxed_expr {
Expand All @@ -206,7 +212,9 @@ impl Visit for Analyzer<'_> {
);
} else {
HANDLER.with(|handler| {
handler.err(error_msg);
handler
.struct_span_err(call_expr.span, error_msg)
.emit();
});
}
}
Expand Down
28 changes: 17 additions & 11 deletions packages/fusion/transform/src/i18n/analyze_use_translation.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{cell::RefCell, collections::BTreeSet, rc::Rc};

use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitWith,
},
},
plugin::errors::HANDLER,
};

use super::State;
Expand Down Expand Up @@ -73,12 +73,15 @@ impl Visit for Analyzer<'_> {
== ""
{
HANDLER.with(|handler| {
handler.err(&format!(
"useTranslations template literal must be \
hinted, e.g. \
useTranslations(`hint.${{foo}}`) vs \
useTranslatiosn(`${{foo}}`)."
));
handler
.struct_span_err(
call_expr.span,
"useTranslations template literal must be \
hinted, e.g. \
useTranslations(`hint.${{foo}}`) vs \
useTranslations(`${{foo}}`)",
)
.emit();
});
}
let mut tpl_parts: BTreeSet<String> = BTreeSet::new();
Expand All @@ -89,10 +92,13 @@ impl Visit for Analyzer<'_> {
self.state.add_translation_id_tpl(tpl_parts);
}
_ => HANDLER.with(|handler| {
handler.err(&format!(
"useTranslations result function must be passed \
string literal or hinted template literal"
));
handler
.struct_span_err(
call_expr.span,
"useTranslations result function must be passed a \
string literal or hinted template literal",
)
.emit();
}),
},
},
Expand Down
36 changes: 11 additions & 25 deletions packages/fusion/transform/src/visitors/asseturl.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
use std::collections::BTreeSet;
use std::{
cell::RefCell,
// path::Path,
rc::Rc,
};
use std::{cell::RefCell, collections::BTreeSet, rc::Rc};

use swc_core::{
common::{errors::HANDLER, DUMMY_SP},
common::DUMMY_SP,
ecma::{
ast::*,
// atoms::JsWord,
utils::prepend_stmt,
visit::{
as_folder,
// noop_fold_type,
noop_visit_mut_type,
Fold,
// FoldWith,
VisitMut,
VisitMutWith,
},
visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith},
},
plugin::errors::HANDLER,
};
use tracing::{
debug,
span,
// trace,
Level,
};
use tracing::{debug, span, Level};

use crate::{asseturl_utils::State, shared::converters::JsVarConverter};

Expand Down Expand Up @@ -70,7 +52,11 @@ impl AsseturlVisitor {
}
_ => HANDLER.with(|handler| {
handler
.err(&format!("asseturl() argument must be a string literal"));
.struct_span_err(
call_expr.span,
"asseturl() argument must be a string literal",
)
.emit();
}),
}
}
Expand Down Expand Up @@ -108,7 +94,7 @@ impl VisitMut for AsseturlVisitor {
raw: None,
}),
type_only: Default::default(),
asserts: Default::default(),
with: Default::default(),
})),
);
}
Expand Down
12 changes: 9 additions & 3 deletions packages/fusion/transform/src/visitors/gql.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{cell::RefCell, collections::BTreeSet, rc::Rc};

use swc_core::{
common::{errors::HANDLER, DUMMY_SP},
common::DUMMY_SP,
ecma::{
ast::*,
utils::prepend_stmt,
visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith},
},
plugin::errors::HANDLER,
};
use tracing::{debug, span, Level};

Expand Down Expand Up @@ -50,7 +51,12 @@ impl GqlVisitor {
self.to_prepend.insert(Thing { file_path: src_str });
}
_ => HANDLER.with(|handler| {
handler.err(&format!("gql() argument must be a string literal"));
handler
.struct_span_err(
call_expr.span,
"gql() argument must be a string literal",
)
.emit();
}),
}
}
Expand Down Expand Up @@ -85,7 +91,7 @@ impl VisitMut for GqlVisitor {
raw: None,
}),
type_only: Default::default(),
asserts: Default::default(),
with: Default::default(),
})),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fusion/transform/src/visitors/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl VisitMut for I18nVisitor {
raw: None,
}),
type_only: Default::default(),
asserts: Default::default(),
with: Default::default(),
})),
);

Expand Down
21 changes: 14 additions & 7 deletions packages/fusion/transform/src/visitors/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::rc::Rc;

use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use swc_core::{
common::{errors::HANDLER, FileName, DUMMY_SP},
common::{FileName, DUMMY_SP},
ecma::{
ast::*,
visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith},
},
plugin::errors::HANDLER,
};
use tracing::debug;

Expand Down Expand Up @@ -192,15 +193,21 @@ impl VisitMut for SplitVisitor {
};
}
_ => HANDLER.with(|handler| {
handler.err(&format!(
"Only string literal is supported in dynamic import"
));
handler
.struct_span_err(
call_expr.span,
"Only string literal is supported in dynamic import",
)
.emit();
}),
},
_ => HANDLER.with(|handler| {
handler.err(&format!(
"Only string literal is supported in dynamic import"
));
handler
.struct_span_err(
call_expr.span,
"Only string literal is supported in dynamic import",
)
.emit();
}),
},
},
Expand Down
10 changes: 1 addition & 9 deletions scripts/build-all.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#!/usr/bin/env bash
set -eu


CRATES="$(cargo metadata --format-version 1 \
| jq -r '.packages[] | select(.source == null) | .name')"


for CRATE in $CRATES
do
cargo build --release -p $CRATE --target wasm32-wasi
done
cargo build --release --target wasm32-wasi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $SCRIPT_DIR
Expand Down
Loading