Skip to content

Commit

Permalink
feat: capture all regexes using tokio channel
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Mar 5, 2024
1 parent 59433a2 commit 258372b
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 141 deletions.
115 changes: 89 additions & 26 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions crates/redos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ owo-colors = "4.0"
redos = { path = "../redos" }
reqwest = { version = "0.11.20", features = ["blocking"] }
swc_common = { version = "0.33", features = ["tty-emitter"] }
swc_ecma_ast = "0.111"
swc_ecma_parser = { version = "0.142.0", features = ["typescript"] }
swc_ecma_visit = "0.97"
swc_ecma_ast = "0.112"
swc_ecma_parser = { version = "0.143", features = ["typescript"] }
swc_ecma_visit = "0.98"
tar = "0.4.40"
tempdir = "0.3.7"
tokio = { version = "1.36.0", features = ["full"] }
async-trait = "0.1.77"
101 changes: 101 additions & 0 deletions crates/redos-cli/src/languages/javascript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::path::Path;

use swc_common::sync::Lrc;
use swc_common::{
errors::{ColorConfig, Handler},
SourceMap,
};
use swc_ecma_ast::{EsVersion, Regex};
use swc_ecma_parser::TsConfig;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
use swc_ecma_visit::{fold_module_item, Fold};

use anyhow::{anyhow, Result};

use async_trait::async_trait;

use super::language::{Language, Location};

/// List of scanned extensions
const EXTENSIONS: [&str; 8] = ["js", "jsx", "ts", "tsx", "mjs", "cjs", "mts", "cts"];

pub struct JavaScript;

#[async_trait(?Send)]
impl Language for JavaScript {
async fn check_file(path: &Path) -> Result<Option<Vec<(String, Location)>>> {
let ext = path.extension().unwrap_or_default();

if !EXTENSIONS.contains(&ext.to_str().unwrap()) {
return Ok(None);
}

let cm: Lrc<SourceMap> = Default::default();
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

let fm = cm.load_file(path)?;

let lexer = Lexer::new(
Syntax::Typescript(TsConfig {
tsx: true,
decorators: true,
dts: false,
no_early_errors: true,
disallow_ambiguous_jsx_like: false,
}),
EsVersion::latest(),
StringInput::from(&*fm),
None,
);

let mut parser = Parser::new_from(lexer);

let module = parser
.parse_module()
.map_err(|e| {
// Unrecoverable fatal error occurred
e.into_diagnostic(&handler).emit();
})
.map_err(|_| anyhow!("Failed to parse file"))?;

let (tx, mut rx) = tokio::sync::mpsc::channel(1);

for token in module.body {
let tx = tx.clone();
fold_module_item(
&mut Visitor {
callback: Box::new(move |regex| {
// regex_list.push(regex.to_string());
tx.blocking_send(regex).unwrap();
}),
},
token,
);
}

let mut regex_list = vec![];

while let Some(regex) = rx.recv().await {
regex_list.push(regex);
}

Ok(Some(regex_list))
}
}

struct Visitor {
callback: Box<dyn Fn((String, Location))>,
}

impl Fold for Visitor {
fn fold_regex(&mut self, regex: Regex) -> Regex {
(self.callback)((
regex.exp.as_ref().to_string(),
Location {
line: regex.span.lo.0 as usize,
column: regex.span.hi.0 as usize,
},
));
regex
}
}
25 changes: 25 additions & 0 deletions crates/redos-cli/src/languages/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Result;
use async_trait::async_trait;
use std::fmt::Display;
use std::path::Path;

#[derive(Debug)]
pub struct Location {
pub line: usize,
pub column: usize,
}

impl Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}

#[async_trait(?Send)]
pub trait Language {
/// Scans a file for every known regex.
/// Returns None if the file is not supported.
///
/// Else, returns a list of regexes and their location in the file.
async fn check_file(path: &Path) -> Result<Option<Vec<(String, Location)>>>;
}
2 changes: 2 additions & 0 deletions crates/redos-cli/src/languages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod javascript;
pub mod language;
Loading

0 comments on commit 258372b

Please sign in to comment.