Skip to content

Commit

Permalink
Merge pull request #503 from DataDog/amaan.qureshi/minified
Browse files Browse the repository at this point in the history
[STAL-2472] feat: avoid processing minified JavaScript files
  • Loading branch information
amaanq authored Aug 30, 2024
2 parents 9dbd0ef + dbf7d86 commit 3dbedfe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 4 additions & 2 deletions crates/static-analysis-kernel/src/analysis/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::analysis::ddsa_lib::common::DDSAJsRuntimeError;
use crate::analysis::ddsa_lib::runtime::ExecutionResult;
use crate::analysis::ddsa_lib::JsRuntime;
use crate::analysis::generated_content::is_generated_file;
use crate::analysis::generated_content::{is_generated_file, is_minified_file};
use crate::analysis::tree_sitter::get_tree;
use crate::model::analysis::{
FileIgnoreBehavior, LinesToIgnore, ERROR_RULE_EXECUTION, ERROR_RULE_TIMEOUT,
Expand Down Expand Up @@ -183,7 +183,9 @@ where
I::Item: Borrow<RuleInternal>,
{
// check if we should ignore the file before doing any more expensive work.
if analysis_option.ignore_generated_files && is_generated_file(code, language) {
if analysis_option.ignore_generated_files
&& (is_generated_file(code, language) || is_minified_file(code, language))
{
if analysis_option.use_debug {
eprintln!("Skipping generated file {}", filename);
}
Expand Down
26 changes: 25 additions & 1 deletion crates/static-analysis-kernel/src/analysis/generated_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ pub fn is_generated_file(full_content: &str, language: &Language) -> bool {
}
}

/// Returns if a file is minified or not.
/// The heuristic for detecting minified files is based on the average line length being greater
/// than 110.
pub fn is_minified_file(content: &str, language: &Language) -> bool {
if language == &Language::JavaScript {
content
.len()
.checked_div(content.lines().count())
.is_some_and(|avg| avg > 110)
} else {
false
}
}

/// Glob patterns that are, by default, excluded from analysis. These patterns are for paths
/// that often contain either vendored 3rd party dependencies or generated files.
pub const DEFAULT_IGNORED_GLOBS: &[&str] = &[
Expand All @@ -70,7 +84,9 @@ pub const DEFAULT_IGNORED_GLOBS: &[&str] = &[

#[cfg(test)]
mod tests {
use crate::analysis::generated_content::{is_generated_file, PROTOBUF_HEADER, THRIFT_HEADER};
use crate::analysis::generated_content::{
is_generated_file, is_minified_file, PROTOBUF_HEADER, THRIFT_HEADER,
};
use crate::model::common::Language;

#[test]
Expand Down Expand Up @@ -178,4 +194,12 @@ mod tests {
&Language::TypeScript
));
}

#[test]
fn test_is_minified_file_javascript() {
assert!(is_minified_file(
&"var x = 2;".repeat(100),
&Language::JavaScript
));
}
}

0 comments on commit 3dbedfe

Please sign in to comment.