Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 875defc

Browse files
author
Hendrik van Antwerpen
committed
Replace lazy_static with once_cell
1 parent 593ebe3 commit 875defc

File tree

7 files changed

+51
-53
lines changed

7 files changed

+51
-53
lines changed

tree-sitter-stack-graphs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ dialoguer = { version = "0.10", optional = true }
4545
env_logger = { version = "0.9", optional = true }
4646
indoc = { version = "1.0", optional = true }
4747
itertools = "0.10"
48-
lazy_static = "1.4"
4948
log = "0.4"
5049
lsp-positions = { version="0.3", path="../lsp-positions" }
50+
once_cell = "1"
5151
pathdiff = { version = "0.2.1", optional = true }
5252
regex = "1"
5353
rust-ini = "0.18"

tree-sitter-stack-graphs/src/cli/init.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use dialoguer::Select;
1414
use dialoguer::Validator;
1515
use indoc::printdoc;
1616
use indoc::writedoc;
17-
use lazy_static::lazy_static;
17+
use once_cell::sync::Lazy;
1818
use regex::Regex;
1919
use std::fs;
2020
use std::fs::File;
@@ -31,12 +31,12 @@ mod license;
3131

3232
const TSSG_VERSION: &str = env!("CARGO_PKG_VERSION");
3333

34-
lazy_static! {
35-
static ref VALID_CRATE_NAME: Regex = Regex::new(r"^[a-zA-Z_-][a-zA-Z0-9_-]*$").unwrap();
36-
static ref VALID_CRATE_VERSION: Regex = Regex::new(r"^[0-9]+\.[0-9]+\.[0-9]+$").unwrap();
37-
static ref VALID_DEPENDENCY_VERSION: Regex =
38-
Regex::new(r"^[~^]?[0-9]+(\.[0-9]+(\.[0-9]+)?)?$").unwrap();
39-
}
34+
static VALID_CRATE_NAME: Lazy<Regex> =
35+
Lazy::new(|| Regex::new(r"^[a-zA-Z_-][a-zA-Z0-9_-]*$").unwrap());
36+
static VALID_CRATE_VERSION: Lazy<Regex> =
37+
Lazy::new(|| Regex::new(r"^[0-9]+\.[0-9]+\.[0-9]+$").unwrap());
38+
static VALID_DEPENDENCY_VERSION: Lazy<Regex> =
39+
Lazy::new(|| Regex::new(r"^[~^]?[0-9]+(\.[0-9]+(\.[0-9]+)?)?$").unwrap());
4040

4141
/// Initialize project
4242
#[derive(Args)]

tree-sitter-stack-graphs/src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@
309309
//! ```
310310
311311
use controlled_option::ControlledOption;
312-
use lazy_static::lazy_static;
313312
use lsp_positions::SpanCalculator;
313+
use once_cell::sync::Lazy;
314314
use stack_graphs::arena::Handle;
315315
use stack_graphs::graph::File;
316316
use stack_graphs::graph::Node;
@@ -368,19 +368,16 @@ static SYMBOL_ATTR: &'static str = "symbol";
368368
static TYPE_ATTR: &'static str = "type";
369369

370370
// Expected attributes per node type
371-
lazy_static! {
372-
static ref DROP_SCOPES_ATTRS: HashSet<&'static str> = HashSet::from([TYPE_ATTR]);
373-
static ref POP_SCOPED_SYMBOL_ATTRS: HashSet<&'static str> =
374-
HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]);
375-
static ref POP_SYMBOL_ATTRS: HashSet<&'static str> =
376-
HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]);
377-
static ref PUSH_SCOPED_SYMBOL_ATTRS: HashSet<&'static str> =
378-
HashSet::from([TYPE_ATTR, SYMBOL_ATTR, SCOPE_ATTR, IS_REFERENCE_ATTR]);
379-
static ref PUSH_SYMBOL_ATTRS: HashSet<&'static str> =
380-
HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_REFERENCE_ATTR]);
381-
static ref SCOPE_ATTRS: HashSet<&'static str> =
382-
HashSet::from([TYPE_ATTR, IS_EXPORTED_ATTR, IS_ENDPOINT_ATTR]);
383-
}
371+
static POP_SCOPED_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
372+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]));
373+
static POP_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
374+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_DEFINITION_ATTR]));
375+
static PUSH_SCOPED_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
376+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, SCOPE_ATTR, IS_REFERENCE_ATTR]));
377+
static PUSH_SYMBOL_ATTRS: Lazy<HashSet<&'static str>> =
378+
Lazy::new(|| HashSet::from([TYPE_ATTR, SYMBOL_ATTR, IS_REFERENCE_ATTR]));
379+
static SCOPE_ATTRS: Lazy<HashSet<&'static str>> =
380+
Lazy::new(|| HashSet::from([TYPE_ATTR, IS_EXPORTED_ATTR, IS_ENDPOINT_ATTR]));
384381

385382
// Edge attribute names
386383
static PRECEDENCE_ATTR: &'static str = "precedence";

tree-sitter-stack-graphs/src/loader.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use ini::Ini;
1111
use itertools::Itertools;
12-
use lazy_static::lazy_static;
12+
use once_cell::sync::Lazy;
1313
use regex::Regex;
1414
use stack_graphs::graph::StackGraph;
1515
use std::collections::HashMap;
@@ -29,12 +29,10 @@ use crate::CancellationFlag;
2929
use crate::FileAnalyzer;
3030
use crate::StackGraphLanguage;
3131

32-
lazy_static! {
33-
pub static ref DEFAULT_TSG_PATHS: Vec<LoadPath> =
34-
vec![LoadPath::Grammar("queries/stack-graphs".into())];
35-
pub static ref DEFAULT_BUILTINS_PATHS: Vec<LoadPath> =
36-
vec![LoadPath::Grammar("queries/builtins".into())];
37-
}
32+
pub static DEFAULT_TSG_PATHS: Lazy<Vec<LoadPath>> =
33+
Lazy::new(|| vec![LoadPath::Grammar("queries/stack-graphs".into())]);
34+
pub static DEFAULT_BUILTINS_PATHS: Lazy<Vec<LoadPath>> =
35+
Lazy::new(|| vec![LoadPath::Grammar("queries/builtins".into())]);
3836

3937
/// Data type that holds all information to recognize and analyze files for a language
4038
pub struct LanguageConfiguration {

tree-sitter-stack-graphs/src/test.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
//! Any content before the first fragment header of the file is ignored, and will not be part of the test.
5959
6060
use itertools::Itertools;
61-
use lazy_static::lazy_static;
6261
use lsp_positions::Position;
6362
use lsp_positions::PositionedSubstring;
6463
use lsp_positions::SpanCalculator;
64+
use once_cell::sync::Lazy;
6565
use regex::Regex;
6666
use stack_graphs::arena::Handle;
6767
use stack_graphs::assert::Assertion;
@@ -85,15 +85,14 @@ const DEFINED: &'static str = "defined";
8585
const DEFINES: &'static str = "defines";
8686
const REFERS: &'static str = "refers";
8787

88-
lazy_static! {
89-
static ref PATH_REGEX: Regex = Regex::new(r#"---\s*path:\s*([^\s]+)\s*---"#).unwrap();
90-
static ref GLOBAL_REGEX: Regex =
91-
Regex::new(r#"---\s*global:\s*([^\s]+)=([^\s]+)\s*---"#).unwrap();
92-
static ref ASSERTION_REGEX: Regex =
93-
Regex::new(r#"(\^)\s*(\w+):\s*([^\s,]+(?:\s*,\s*[^\s,]+)*)?"#).unwrap();
94-
static ref LINE_NUMBER_REGEX: Regex = Regex::new(r#"\d+"#).unwrap();
95-
static ref NAME_REGEX: Regex = Regex::new(r#"[^\s,]+"#).unwrap();
96-
}
88+
static PATH_REGEX: Lazy<Regex> =
89+
Lazy::new(|| Regex::new(r#"---\s*path:\s*([^\s]+)\s*---"#).unwrap());
90+
static GLOBAL_REGEX: Lazy<Regex> =
91+
Lazy::new(|| Regex::new(r#"---\s*global:\s*([^\s]+)=([^\s]+)\s*---"#).unwrap());
92+
static ASSERTION_REGEX: Lazy<Regex> =
93+
Lazy::new(|| Regex::new(r#"(\^)\s*(\w+):\s*([^\s,]+(?:\s*,\s*[^\s,]+)*)?"#).unwrap());
94+
static LINE_NUMBER_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\d+"#).unwrap());
95+
static NAME_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"[^\s,]+"#).unwrap());
9796

9897
/// An error that can occur while parsing tests
9998
#[derive(Debug, Error)]

tree-sitter-stack-graphs/tests/it/loader.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8-
use lazy_static::lazy_static;
8+
use once_cell::sync::Lazy;
99
use pretty_assertions::assert_eq;
1010
use stack_graphs::graph::StackGraph;
1111
use std::path::PathBuf;
@@ -15,13 +15,13 @@ use tree_sitter_stack_graphs::loader::Loader;
1515
use tree_sitter_stack_graphs::NoCancellation;
1616
use tree_sitter_stack_graphs::StackGraphLanguage;
1717

18-
lazy_static! {
19-
static ref PATH: PathBuf = PathBuf::from("test.py");
20-
static ref TSG: String = r#"
18+
static PATH: Lazy<PathBuf> = Lazy::new(|| PathBuf::from("test.py"));
19+
static TSG: Lazy<String> = Lazy::new(|| {
20+
r#"
2121
(module) {}
2222
"#
23-
.to_string();
24-
}
23+
.to_string()
24+
});
2525

2626
#[test]
2727
fn can_load_from_provided_language_configuration() {

tree-sitter-stack-graphs/tests/it/test.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8-
use lazy_static::lazy_static;
8+
use once_cell::sync::Lazy;
99
use pretty_assertions::assert_eq;
1010
use stack_graphs::arena::Handle;
1111
use stack_graphs::graph::File;
@@ -18,9 +18,9 @@ use tree_sitter_stack_graphs::LoadError;
1818
use tree_sitter_stack_graphs::NoCancellation;
1919
use tree_sitter_stack_graphs::StackGraphLanguage;
2020

21-
lazy_static! {
22-
static ref PATH: PathBuf = PathBuf::from("test.py");
23-
static ref TSG: String = r#"
21+
static PATH: Lazy<PathBuf> = Lazy::new(|| PathBuf::from("test.py"));
22+
static TSG: Lazy<String> = Lazy::new(|| {
23+
r#"
2424
global ROOT_NODE
2525
(module) @mod {
2626
node @mod.lexical_in
@@ -50,11 +50,15 @@ lazy_static! {
5050
attr (@name.ref) type = "push_symbol", symbol = (source-text @name), source_node = @name, is_reference
5151
edge @name.ref -> @stmt.lexical_in
5252
}
53-
"#.to_string();
54-
static ref TSG_WITH_PKG: String = r#"
53+
"#.to_string()
54+
});
55+
static TSG_WITH_PKG: Lazy<String> = Lazy::new(|| {
56+
r#"
5557
global PKG
56-
"#.to_string() + &TSG;
57-
}
58+
"#
59+
.to_string()
60+
+ &TSG
61+
});
5862

5963
fn build_stack_graph_into(
6064
graph: &mut StackGraph,

0 commit comments

Comments
 (0)