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

feat: analyze commonjs files #540

Merged
merged 13 commits into from
Oct 28, 2024
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
8 changes: 4 additions & 4 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 @@ -41,7 +41,7 @@ harness = false
anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.42.0", features = ["dep_analysis", "emit"] }
deno_ast = { version = "0.43.0", features = ["dep_analysis", "emit"] }
deno_unsync.workspace = true
deno_semver = "0.5.13"
encoding_rs = "0.8.33"
Expand Down
56 changes: 50 additions & 6 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use std::sync::Arc;

use deno_ast::dep::DependencyKind;
use deno_ast::dep::DynamicDependencyKind;
use deno_ast::dep::ImportAttributes;
use deno_ast::dep::StaticDependencyKind;
use deno_ast::MediaType;
use deno_ast::ModuleSpecifier;
use deno_ast::ParseDiagnostic;
Expand Down Expand Up @@ -114,7 +115,7 @@ impl DependencyDescriptor {
#[serde(rename_all = "camelCase")]
pub struct StaticDependencyDescriptor {
/// The kind of dependency.
pub kind: DependencyKind,
pub kind: StaticDependencyKind,
/// An optional specifier overriding the types associated with the
/// import/export statement, if any.
#[serde(skip_serializing_if = "Option::is_none", default)]
Expand Down Expand Up @@ -163,6 +164,8 @@ pub enum DynamicTemplatePart {
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DynamicDependencyDescriptor {
#[serde(skip_serializing_if = "is_dynamic_esm", default)]
pub kind: DynamicDependencyKind,
/// An optional specifier overriding the types associated with the
/// import/export statement, if any.
#[serde(skip_serializing_if = "Option::is_none", default)]
Expand All @@ -177,6 +180,10 @@ pub struct DynamicDependencyDescriptor {
pub import_attributes: ImportAttributes,
}

fn is_dynamic_esm(kind: &DynamicDependencyKind) -> bool {
*kind == DynamicDependencyKind::Import
}

impl From<DynamicDependencyDescriptor> for DependencyDescriptor {
fn from(descriptor: DynamicDependencyDescriptor) -> Self {
DependencyDescriptor::Dynamic(descriptor)
Expand All @@ -202,6 +209,10 @@ pub enum TypeScriptReference {
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModuleInfo {
/// If the module has nothing that makes it for sure an ES module
/// (no TLA, imports, exports, import.meta).
#[serde(skip_serializing_if = "is_false", default, rename = "script")]
pub is_script: bool,
/// Dependencies of the module.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub dependencies: Vec<DependencyDescriptor>,
Expand Down Expand Up @@ -325,6 +336,10 @@ impl<'a> Default for &'a dyn ModuleAnalyzer {
}
}

fn is_false(v: &bool) -> bool {
!v
}

#[cfg(test)]
mod test {
use std::collections::HashMap;
Expand All @@ -340,6 +355,7 @@ mod test {
fn module_info_serialization_empty() {
// empty
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::new(),
self_types_specifier: None,
Expand All @@ -354,9 +370,10 @@ mod test {
fn module_info_serialization_deps() {
// with dependencies
let module_info = ModuleInfo {
is_script: true,
dependencies: Vec::from([
StaticDependencyDescriptor {
kind: DependencyKind::ImportEquals,
kind: StaticDependencyKind::ImportEquals,
types_specifier: Some(SpecifierWithRange {
text: "a".to_string(),
range: PositionRange {
Expand All @@ -379,6 +396,7 @@ mod test {
}
.into(),
DynamicDependencyDescriptor {
kind: DynamicDependencyKind::Import,
types_specifier: None,
argument: DynamicArgument::String("./test2".to_string()),
argument_range: PositionRange {
Expand All @@ -395,6 +413,17 @@ mod test {
])),
}
.into(),
DynamicDependencyDescriptor {
kind: DynamicDependencyKind::Require,
types_specifier: None,
argument: DynamicArgument::String("./test3".to_string()),
argument_range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
import_attributes: ImportAttributes::None,
}
.into(),
]),
ts_references: Vec::new(),
self_types_specifier: None,
Expand All @@ -407,6 +436,7 @@ mod test {
// WARNING: Deserialization MUST be backwards compatible in order
// to load data from JSR.
json!({
"script": true,
"dependencies": [{
"type": "static",
"kind": "importEquals",
Expand All @@ -427,6 +457,11 @@ mod test {
"key2": "value",
}
}
}, {
"type": "dynamic",
"kind": "require",
"argument": "./test3",
"argumentRange": [[0, 0], [0, 0]]
}]
}),
);
Expand All @@ -435,6 +470,7 @@ mod test {
#[test]
fn module_info_serialization_ts_references() {
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::from([
TypeScriptReference::Path(SpecifierWithRange {
Expand Down Expand Up @@ -478,6 +514,7 @@ mod test {
#[test]
fn module_info_serialization_self_types_specifier() {
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::new(),
self_types_specifier: Some(SpecifierWithRange {
Expand Down Expand Up @@ -507,6 +544,7 @@ mod test {
#[test]
fn module_info_serialization_jsx_import_source() {
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::new(),
self_types_specifier: None,
Expand Down Expand Up @@ -536,6 +574,7 @@ mod test {
#[test]
fn module_info_serialization_jsx_import_source_types() {
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::new(),
self_types_specifier: None,
Expand Down Expand Up @@ -565,6 +604,7 @@ mod test {
#[test]
fn module_info_jsdoc_imports() {
let module_info = ModuleInfo {
is_script: false,
dependencies: Vec::new(),
ts_references: Vec::new(),
self_types_specifier: None,
Expand Down Expand Up @@ -595,7 +635,7 @@ mod test {
fn static_dependency_descriptor_serialization() {
// with dependencies
let descriptor = DependencyDescriptor::Static(StaticDependencyDescriptor {
kind: DependencyKind::ExportEquals,
kind: StaticDependencyKind::ExportEquals,
types_specifier: Some(SpecifierWithRange {
text: "a".to_string(),
range: PositionRange {
Expand Down Expand Up @@ -632,6 +672,7 @@ mod test {
fn dynamic_dependency_descriptor_serialization() {
run_serialization_test(
&DependencyDescriptor::Dynamic(DynamicDependencyDescriptor {
kind: DynamicDependencyKind::Import,
types_specifier: Some(SpecifierWithRange {
text: "a".to_string(),
range: PositionRange {
Expand Down Expand Up @@ -661,6 +702,7 @@ mod test {

run_serialization_test(
&DependencyDescriptor::Dynamic(DynamicDependencyDescriptor {
kind: DynamicDependencyKind::Import,
types_specifier: None,
argument: DynamicArgument::String("test".to_string()),
argument_range: PositionRange {
Expand Down Expand Up @@ -734,9 +776,10 @@ mod test {
#[test]
fn test_v1_to_v2_deserialization_with_leading_comment() {
let expected = ModuleInfo {
is_script: false,
dependencies: vec![DependencyDescriptor::Static(
StaticDependencyDescriptor {
kind: DependencyKind::Import,
kind: StaticDependencyKind::Import,
specifier: "./a.js".to_string(),
specifier_range: PositionRange {
start: Position {
Expand Down Expand Up @@ -788,9 +831,10 @@ mod test {
#[test]
fn test_v1_to_v2_deserialization_no_leading_comment() {
let expected = ModuleInfo {
is_script: false,
dependencies: vec![DependencyDescriptor::Static(
StaticDependencyDescriptor {
kind: DependencyKind::Import,
kind: StaticDependencyKind::Import,
specifier: "./a.js".to_string(),
specifier_range: PositionRange {
start: Position {
Expand Down
Loading