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: provide full import statement range #533

Merged
merged 3 commits into from
Sep 26, 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
58 changes: 57 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub struct StaticDependencyDescriptor {
pub specifier: String,
/// The range of the specifier.
pub specifier_range: PositionRange,
/// The range of the import statement.
pub range: PositionRange,
/// Import attributes for this dependency.
#[serde(skip_serializing_if = "ImportAttributes::is_none", default)]
pub import_attributes: ImportAttributes,
Expand Down Expand Up @@ -172,6 +174,7 @@ pub struct DynamicDependencyDescriptor {
pub argument: DynamicArgument,
/// The range of the argument.
pub argument_range: PositionRange,
pub range: PositionRange,
Copy link
Member

@dsherret dsherret Oct 8, 2024

Choose a reason for hiding this comment

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

I just realized this breaks the deserialization of information we store on JSR. We'll need to think of a solution for this.

/// Import attributes for this dependency.
#[serde(skip_serializing_if = "ImportAttributes::is_none", default)]
pub import_attributes: ImportAttributes,
Expand Down Expand Up @@ -375,6 +378,16 @@ mod test {
character: 4,
},
},
range: PositionRange {
start: Position {
line: 1,
character: 0,
},
end: Position {
line: 3,
character: 5,
},
},
import_attributes: ImportAttributes::None,
}
.into(),
Expand All @@ -385,6 +398,10 @@ mod test {
start: Position::zeroed(),
end: Position::zeroed(),
},
range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
import_attributes: ImportAttributes::Known(HashMap::from([
("key".to_string(), ImportAttribute::Unknown),
(
Expand Down Expand Up @@ -414,10 +431,12 @@ mod test {
},
"specifier": "./test",
"specifierRange": [[1, 2], [3, 4]],
"range": [[1, 0], [3, 5]],
}, {
"type": "dynamic",
"argument": "./test2",
"argumentRange": [[0, 0], [0, 0]],
"range": [[0, 0], [0, 0]],
"importAttributes": {
"known": {
"key": null,
Expand Down Expand Up @@ -596,6 +615,10 @@ mod test {
start: Position::zeroed(),
end: Position::zeroed(),
},
range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
import_attributes: ImportAttributes::Unknown,
});
run_serialization_test(
Expand All @@ -609,6 +632,7 @@ mod test {
},
"specifier": "./test",
"specifierRange": [[0, 0], [0, 0]],
"range": [[0, 0], [0, 0]],
"importAttributes": "unknown",
}),
);
Expand All @@ -630,6 +654,10 @@ mod test {
start: Position::zeroed(),
end: Position::zeroed(),
},
range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
import_attributes: ImportAttributes::Unknown,
}),
json!({
Expand All @@ -639,6 +667,7 @@ mod test {
"range": [[0, 0], [0, 0]],
},
"argumentRange": [[0, 0], [0, 0]],
"range": [[0, 0], [0, 0]],
"importAttributes": "unknown",
}),
);
Expand All @@ -651,12 +680,17 @@ mod test {
start: Position::zeroed(),
end: Position::zeroed(),
},
range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
import_attributes: ImportAttributes::Unknown,
}),
json!({
"type": "dynamic",
"argument": "test",
"argumentRange": [[0, 0], [0, 0]],
"range": [[0, 0], [0, 0]],
"importAttributes": "unknown",
}),
);
Expand Down Expand Up @@ -728,6 +762,16 @@ mod test {
character: 4,
},
},
range: PositionRange {
start: Position {
line: 1,
character: 0,
},
end: Position {
line: 3,
character: 5,
},
},
types_specifier: Some(SpecifierWithRange {
text: "./a.d.ts".to_string(),
range: PositionRange {
Expand Down Expand Up @@ -756,6 +800,7 @@ mod test {
"kind": "import",
"specifier": "./a.js",
"specifierRange": [[1, 2], [3, 4]],
"range": [[1, 0], [3, 5]],
"leadingComments": [{
"text": " @deno-types=\"./a.d.ts\"",
"range": [[0, 0], [0, 25]],
Expand All @@ -782,6 +827,16 @@ mod test {
character: 4,
},
},
range: PositionRange {
start: Position {
line: 1,
character: 0,
},
end: Position {
line: 3,
character: 5,
},
},
types_specifier: None,
import_attributes: ImportAttributes::None,
},
Expand All @@ -797,7 +852,8 @@ mod test {
"type": "static",
"kind": "import",
"specifier": "./a.js",
"specifierRange": [[1, 2], [3, 4]]
"specifierRange": [[1, 2], [3, 4]],
"range": [[1, 0], [3, 5]],
}]
});
run_v1_deserialization_test(json, &expected);
Expand Down
2 changes: 2 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ fn analyze_dependencies(
d.specifier_range,
text_info,
),
range: PositionRange::from_source_range(d.range, text_info),
import_attributes: d.import_attributes,
})
}
Expand Down Expand Up @@ -498,6 +499,7 @@ fn analyze_dependencies(
d.argument_range,
text_info,
),
range: PositionRange::from_source_range(d.range, text_info),
import_attributes: d.import_attributes,
})
}
Expand Down
Loading