Skip to content
Open
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
32 changes: 8 additions & 24 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,10 @@ use nodejs_semver::{Range, Version};

use crate::error::OrchestrionError;
use crate::function_query::FunctionQuery;
use crate::{get_arr, get_str};

use yaml_rust2::{Yaml, YamlLoader};

macro_rules! get_str {
($property:expr, $name:expr) => {
$property[$name]
.as_str()
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
};
}

macro_rules! get_arr {
($property:expr, $name:expr) => {
$property[$name]
.as_vec()
.ok_or(format!("Invalid config: '{}' must be a array", $name))?
};
}

#[derive(Clone, Debug)]
pub enum InstrumentationOperator {
Callback,
Expand Down Expand Up @@ -64,28 +49,27 @@ pub struct InstrumentationConfig {

pub struct Config {
pub instrumentations: Vec<InstrumentationConfig>,
pub dc_module: String,
pub diagnostic_channel_module: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as dc_module, to keep it consistent with the YAML, which we'd like to be clear, but also concise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I like the descriptive name in both places over the "you know what the abbreviation means so you know what the name means" version.

}

impl Config {
pub fn from_yaml_data(yaml_str: &str) -> Result<Config, OrchestrionError> {
let docs = YamlLoader::load_from_str(yaml_str)?;
let doc = &docs[0];

let version = doc["version"]
.as_i64()
.ok_or("Invalid config: 'version' must be a number")?;
if version != 1 {
return Err("Invalid config version".into());
}
match doc["version"].as_i64() {
Some(1) => 1,
Some(_) => return Err("Invalid config version".into()),
None => return Err("Invalid config: 'version' must be a number".into()),
};

let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel");

let configs = InstrumentationConfig::from_yaml(doc)?;

Ok(Config {
instrumentations: configs,
dc_module: dc_module.to_string(),
diagnostic_channel_module: dc_module.to_string(),
})
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/function_query.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use crate::error::OrchestrionError;
use crate::get_str;
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
use yaml_rust2::Yaml;

macro_rules! get_str {
($property:expr, $name:expr) => {
$property[$name]
.as_str()
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
};
}

#[derive(Debug)]
pub enum FunctionType {
FunctionDeclaration,
Expand Down
11 changes: 3 additions & 8 deletions src/instrumentation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::InstrumentationConfig;
use crate::ident;
use std::path::PathBuf;
use swc_core::common::{Span, SyntaxContext};
use swc_core::ecma::{
Expand All @@ -11,12 +12,6 @@ use swc_core::ecma::{
};
use swc_core::quote;

macro_rules! ident {
($name:expr) => {
Ident::new($name.into(), Span::default(), SyntaxContext::empty())
};
}

/// An [`Instrumentation`] instance represents a single instrumentation configuration, and implements
/// SWC's [`VisitMut`] trait to insert tracing code into matching functions. You can use this
/// wherever you would use a [`VisitMut`] instance, such as within an SWC plugin, for example.
Expand Down Expand Up @@ -121,7 +116,7 @@ impl Instrumentation {
if ($ch.hasSubscribers) {
$ch.start.publish($ctx);
}
} catch (tr_ch_err) {
} catch (tr_ch_err) {
if ($ch.hasSubscribers) {
$ctx.error = tr_ch_err;
try {
Expand Down Expand Up @@ -226,7 +221,7 @@ impl Instrumentation {
.function_query
.class
.as_ref()
.map_or(true, |class| node.ident.sym.as_ref() == class);
.is_none_or(|class| node.ident.sym.as_ref() == class);
true
}

Expand Down
15 changes: 4 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
//!
//! [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html

#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::style)]
#![deny(clippy::perf)]
#![deny(clippy::complexity)]
#![deny(clippy::correctness)]
#![deny(clippy::unwrap_used)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be left in because they make clippy more strict.


mod macros;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -44,7 +37,7 @@ mod function_query;
/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
pub struct Instrumentor {
instrumentations: Vec<Instrumentation>,
dc_module: String,
diagnostic_channel_module: String,
}

impl Instrumentor {
Expand All @@ -55,7 +48,7 @@ impl Instrumentor {
.into_iter()
.map(Instrumentation::new)
.collect(),
dc_module: config.dc_module,
diagnostic_channel_module: config.diagnostic_channel_module,
}
}

Expand All @@ -71,7 +64,7 @@ impl Instrumentor {
.instrumentations
.iter_mut()
.filter(|instr| instr.matches(module_name, version, file_path));
InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref())
InstrumentationVisitor::new(instrumentations, self.diagnostic_channel_module.as_ref())
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[macro_export]
macro_rules! get_str {
($property:expr, $name:expr) => {
$property[$name]
.as_str()
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
};
}

#[macro_export]
macro_rules! get_arr {
($property:expr, $name:expr) => {
$property[$name]
.as_vec()
.ok_or(format!("Invalid config: '{}' must be a array", $name))?
};
}

#[macro_export]
macro_rules! ident {
($name:expr) => {
Ident::new($name.into(), Span::default(), SyntaxContext::empty())
};
}