diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-05-20 16:40:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 16:40:55 -0400 |
commit | 1fcecb6789c3f111bc1554766ba9347afcfd02dc (patch) | |
tree | 19cd1b121412b992994b2fe4bea0463793d3986e /cli/lsp/testing | |
parent | e7c894e8f54ebd2d9fd61c97a265906ac54e2068 (diff) |
refactor: upgrade to deno_ast 0.15 (#14680)
Diffstat (limited to 'cli/lsp/testing')
-rw-r--r-- | cli/lsp/testing/collectors.rs | 67 | ||||
-rw-r--r-- | cli/lsp/testing/definitions.rs | 33 | ||||
-rw-r--r-- | cli/lsp/testing/execution.rs | 6 | ||||
-rw-r--r-- | cli/lsp/testing/server.rs | 2 |
4 files changed, 43 insertions, 65 deletions
diff --git a/cli/lsp/testing/collectors.rs b/cli/lsp/testing/collectors.rs index 33ad3a7ab..537dd5806 100644 --- a/cli/lsp/testing/collectors.rs +++ b/cli/lsp/testing/collectors.rs @@ -3,9 +3,10 @@ use super::definitions::TestDefinition; use deno_ast::swc::ast; -use deno_ast::swc::common::Span; use deno_ast::swc::visit::Visit; use deno_ast::swc::visit::VisitWith; +use deno_ast::SourceRange; +use deno_ast::SourceRangedForSpanned; use deno_core::ModuleSpecifier; use std::collections::HashSet; @@ -254,12 +255,12 @@ impl TestStepCollector { fn add_step<N: AsRef<str>>( &mut self, name: N, - span: &Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) { let step = TestDefinition::new_step( name.as_ref().to_string(), - *span, + range, self.parent.clone(), self.level, steps, @@ -267,11 +268,11 @@ impl TestStepCollector { self.steps.push(step); } - fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { + fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) { if let Some((name, steps)) = check_call_expr(&self.parent, node, self.level + 1) { - self.add_step(name, span, steps); + self.add_step(name, range, steps); } } @@ -288,7 +289,7 @@ impl Visit for TestStepCollector { // Identify calls to identified variables ast::Expr::Ident(ident) => { if self.vars.contains(&ident.sym.to_string()) { - self.check_call_expr(node, &ident.span); + self.check_call_expr(node, ident.range()); } } // Identify calls to `test.step()` @@ -298,7 +299,7 @@ impl Visit for TestStepCollector { if ns_prop_ident.sym.eq("step") { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if ident.sym == *test_context { - self.check_call_expr(node, &ns_prop_ident.span); + self.check_call_expr(node, ns_prop_ident.range()); } } } @@ -386,23 +387,23 @@ impl TestCollector { fn add_definition<N: AsRef<str>>( &mut self, name: N, - span: &Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) { let definition = TestDefinition::new( &self.specifier, name.as_ref().to_string(), - *span, + range, steps, ); self.definitions.push(definition); } - fn check_call_expr(&mut self, node: &ast::CallExpr, span: &Span) { + fn check_call_expr(&mut self, node: &ast::CallExpr, range: SourceRange) { if let Some((name, steps)) = check_call_expr(self.specifier.as_str(), node, 1) { - self.add_definition(name, span, steps); + self.add_definition(name, range, steps); } } @@ -418,7 +419,7 @@ impl Visit for TestCollector { match callee_expr.as_ref() { ast::Expr::Ident(ident) => { if self.vars.contains(&ident.sym.to_string()) { - self.check_call_expr(node, &ident.span); + self.check_call_expr(node, ident.range()); } } ast::Expr::Member(member_expr) => { @@ -426,7 +427,7 @@ impl Visit for TestCollector { if ns_prop_ident.sym.to_string() == "test" { if let ast::Expr::Ident(ident) = member_expr.obj.as_ref() { if ident.sym.to_string() == "Deno" { - self.check_call_expr(node, &ns_prop_ident.span); + self.check_call_expr(node, ns_prop_ident.range()); } } } @@ -494,24 +495,20 @@ impl Visit for TestCollector { #[cfg(test)] pub mod tests { use super::*; - use deno_ast::swc::common::BytePos; - use deno_ast::swc::common::SyntaxContext; + use deno_ast::StartSourcePos; use deno_core::resolve_url; - use std::sync::Arc; - pub fn new_span(lo: u32, hi: u32, ctxt: u32) -> Span { - Span { - lo: BytePos(lo), - hi: BytePos(hi), - ctxt: SyntaxContext::from_u32(ctxt), - } + pub fn new_range(start: usize, end: usize) -> SourceRange { + SourceRange::new( + StartSourcePos::START_SOURCE_POS + start, + StartSourcePos::START_SOURCE_POS + end, + ) } #[test] fn test_test_collector() { let specifier = resolve_url("file:///a/example.ts").unwrap(); - let source = Arc::new( - r#" + let source = r#" Deno.test({ name: "test a", async fn(t) { @@ -535,13 +532,11 @@ pub mod tests { const t = Deno.test; t("test d", () => {}); - "# - .to_string(), - ); + "#; let parsed_module = deno_ast::parse_module(deno_ast::ParseParams { specifier: specifier.to_string(), - source: deno_ast::SourceTextInfo::new(source), + text_info: deno_ast::SourceTextInfo::new(source.into()), media_type: deno_ast::MediaType::TypeScript, capture_tokens: true, scope_analysis: true, @@ -557,19 +552,19 @@ pub mod tests { id: "cf31850c831233526df427cdfd25b6b84b2af0d6ce5f8ee1d22c465234b46348".to_string(), level: 0, name: "test a".to_string(), - span: new_span(12, 16, 0), + range: new_range(12, 16), steps: Some(vec![ TestDefinition { id: "4c7333a1e47721631224408c467f32751fe34b876cab5ec1f6ac71980ff15ad3".to_string(), level: 1, name: "a step".to_string(), - span: new_span(83, 87, 0), + range: new_range(83, 87), steps: Some(vec![ TestDefinition { id: "abf356f59139b77574089615f896a6f501c010985d95b8a93abeb0069ccb2201".to_string(), level: 2, name: "sub step".to_string(), - span: new_span(132, 136, 3), + range: new_range(132, 136), steps: None, } ]) @@ -580,13 +575,13 @@ pub mod tests { id: "86b4c821900e38fc89f24bceb0e45193608ab3f9d2a6019c7b6a5aceff5d7df2".to_string(), level: 0, name: "useFnName".to_string(), - span: new_span(254, 258, 0), + range: new_range(254, 258), steps: Some(vec![ TestDefinition { id: "67a390d0084ae5fb88f3510c470a72a553581f1d0d5ba5fa89aee7a754f3953a".to_string(), level: 1, name: "step c".to_string(), - span: new_span(313, 314, 4), + range: new_range(313, 314), steps: None, } ]) @@ -595,21 +590,21 @@ pub mod tests { id: "580eda89d7f5e619774c20e13b7d07a8e77c39cba101d60565144d48faa837cb".to_string(), level: 0, name: "test b".to_string(), - span: new_span(358, 362, 0), + range: new_range(358, 362), steps: None, }, TestDefinition { id: "0b7c6bf3cd617018d33a1bf982a08fe088c5bb54fcd5eb9e802e7c137ec1af94".to_string(), level: 0, name: "test c".to_string(), - span: new_span(420, 424, 1), + range: new_range(420, 424), steps: None, }, TestDefinition { id: "69d9fe87f64f5b66cb8b631d4fd2064e8224b8715a049be54276c42189ff8f9f".to_string(), level: 0, name: "test d".to_string(), - span: new_span(480, 481, 1), + range: new_range(480, 481), steps: None, } ] diff --git a/cli/lsp/testing/definitions.rs b/cli/lsp/testing/definitions.rs index aad667959..c810b6a25 100644 --- a/cli/lsp/testing/definitions.rs +++ b/cli/lsp/testing/definitions.rs @@ -3,38 +3,21 @@ use super::lsp_custom; use crate::checksum; +use crate::lsp::analysis::source_range_to_lsp_range; use crate::lsp::client::TestingNotification; -use deno_ast::swc::common::Span; +use deno_ast::SourceRange; use deno_ast::SourceTextInfo; use deno_core::ModuleSpecifier; use std::collections::HashMap; use tower_lsp::lsp_types as lsp; -fn span_to_range( - span: &Span, - source_text_info: &SourceTextInfo, -) -> Option<lsp::Range> { - let start = source_text_info.line_and_column_index(span.lo); - let end = source_text_info.line_and_column_index(span.hi); - Some(lsp::Range { - start: lsp::Position { - line: start.line_index as u32, - character: start.column_index as u32, - }, - end: lsp::Position { - line: end.line_index as u32, - character: end.column_index as u32, - }, - }) -} - #[derive(Debug, Clone, PartialEq)] pub struct TestDefinition { pub id: String, pub level: usize, pub name: String, - pub span: Span, + pub range: SourceRange, pub steps: Option<Vec<TestDefinition>>, } @@ -42,7 +25,7 @@ impl TestDefinition { pub fn new( specifier: &ModuleSpecifier, name: String, - span: Span, + range: SourceRange, steps: Option<Vec<TestDefinition>>, ) -> Self { let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]); @@ -50,14 +33,14 @@ impl TestDefinition { id, level: 0, name, - span, + range, steps, } } pub fn new_step( name: String, - span: Span, + range: SourceRange, parent: String, level: usize, steps: Option<Vec<TestDefinition>>, @@ -71,7 +54,7 @@ impl TestDefinition { id, level, name, - span, + range, steps, } } @@ -89,7 +72,7 @@ impl TestDefinition { .map(|step| step.as_test_data(source_text_info)) .collect() }), - range: span_to_range(&self.span, source_text_info), + range: Some(source_range_to_lsp_range(&self.range, source_text_info)), } } diff --git a/cli/lsp/testing/execution.rs b/cli/lsp/testing/execution.rs index 358f9357b..4f4b9bf1f 100644 --- a/cli/lsp/testing/execution.rs +++ b/cli/lsp/testing/execution.rs @@ -917,7 +917,7 @@ impl test::TestReporter for LspTestReporter { #[cfg(test)] mod tests { use super::*; - use crate::lsp::testing::collectors::tests::new_span; + use crate::lsp::testing::collectors::tests::new_range; #[test] fn test_as_queue_and_filters() { @@ -949,7 +949,7 @@ mod tests { .to_string(), level: 0, name: "test a".to_string(), - span: new_span(420, 424, 1), + range: new_range(420, 424), steps: None, }; let test_def_b = TestDefinition { @@ -957,7 +957,7 @@ mod tests { .to_string(), level: 0, name: "test b".to_string(), - span: new_span(480, 481, 1), + range: new_range(480, 481), steps: None, }; let test_definitions = TestDefinitions { diff --git a/cli/lsp/testing/server.rs b/cli/lsp/testing/server.rs index ab8f99c0e..fbc835cc2 100644 --- a/cli/lsp/testing/server.rs +++ b/cli/lsp/testing/server.rs @@ -117,7 +117,7 @@ impl TestServer { test_definitions.as_notification( specifier, mru.as_ref(), - parsed_source.source(), + parsed_source.text_info(), ), ); } |