Skip to content

Commit

Permalink
refactor: remove lazy_static (#3611)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Aug 7, 2024
1 parent e835ce5 commit 4880f84
Show file tree
Hide file tree
Showing 35 changed files with 155 additions and 177 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ getrandom = "0.2.15"
ignore = "0.4.22"
indexmap = { version = "2.3.0", features = ["serde"] }
insta = "1.39.0"
lazy_static = "1.5.0"
natord = "1.0.9"
oxc_resolver = "1.10.2"
proc-macro2 = "1.0.86"
Expand Down
1 change: 0 additions & 1 deletion crates/biome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ crossbeam = { workspace = true }
dashmap = { workspace = true }
hdrhistogram = { version = "7.5.4", default-features = false }
indexmap = { workspace = true }
lazy_static = { workspace = true }
path-absolutize = { version = "3.1.1", optional = false, features = ["use_unix_paths_on_wasm"] }
quick-junit = "0.4.0"
rayon = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ pub enum BiomeCommand {
stdin_file_path: Option<String>,

/// When set to true, only the files that have been staged (the ones prepared to be committed)
/// will be linted.
/// will be linted. This option should be used when working locally.
#[bpaf(long("staged"), switch)]
staged: bool,

/// When set to true, only the files that have been changed compared to your `defaultBranch`
/// configuration will be linted.
/// configuration will be linted. This option should be used in CI environments.
#[bpaf(long("changed"), switch)]
changed: bool,

/// Use this to specify the base branch to compare against when you're using the --changed
/// flag and the `defaultBranch` is not set in your biome.json
/// flag and the `defaultBranch` is not set in your `biome.json`
#[bpaf(long("since"), argument("REF"))]
since: Option<String>,

Expand Down
8 changes: 3 additions & 5 deletions crates/biome_cli/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use hdrhistogram::Histogram;
use rustc_hash::FxHashMap;
use std::sync::{Mutex, RwLock};
use std::sync::{LazyLock, Mutex, RwLock};
use tracing::{span, subscriber::Interest, Level, Metadata, Subscriber};
use tracing_subscriber::{
layer::Context,
Expand All @@ -20,10 +20,8 @@ use tracing_subscriber::{
/// Implementation of a tracing [Layer] that collects timing information for spans into [Histogram]s
struct MetricsLayer;

lazy_static::lazy_static! {
/// Global storage for metrics data
static ref METRICS: RwLock<FxHashMap<CallsiteKey, Mutex<CallsiteEntry>>> = RwLock::default();
}
static METRICS: LazyLock<RwLock<FxHashMap<CallsiteKey, Mutex<CallsiteEntry>>>> =
LazyLock::new(RwLock::default);

/// Static pointer to the metadata of a callsite, used as a unique identifier
/// for collecting spans created from there in the global metrics map
Expand Down
9 changes: 4 additions & 5 deletions crates/biome_cli/tests/snap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ use biome_fs::{ConfigName, FileSystemExt, MemoryFileSystem};
use biome_json_formatter::context::JsonFormatOptions;
use biome_json_formatter::format_node;
use biome_json_parser::{parse_json, JsonParserOptions};
use lazy_static::lazy_static;
use regex::Regex;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::env::{current_exe, temp_dir};
use std::fmt::Write as _;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::sync::LazyLock;

lazy_static! {
static ref TIME_REGEX: Regex = Regex::new("\\s[0-9]+[mµn]?s\\.").unwrap();
static ref TIME_JUNIT_REGEX: Regex = Regex::new("time=\\\"[.0-9]+\\\"").unwrap();
}
static TIME_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("\\s[0-9]+[mµn]?s\\.").unwrap());
static TIME_JUNIT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("time=\\\"[.0-9]+\\\"").unwrap());

#[derive(Default)]
struct InMessages {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ Available options:
code.
Example: `echo 'let a;' | biome check --stdin-file-path=file.js`
--staged When set to true, only the files that have been staged (the ones
prepared to be committed) will be linted.
prepared to be committed) will be linted. This option should be used
when working locally.
--changed When set to true, only the files that have been changed compared to
your `defaultBranch` configuration will be linted.
your `defaultBranch` configuration will be linted. This option should
be used in CI environments.
--since=REF Use this to specify the base branch to compare against when you're
using the --changed flag and the `defaultBranch` is not set in your
biome.json
`biome.json`
-h, --help Prints help information
```
1 change: 0 additions & 1 deletion crates/biome_css_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }
lazy_static = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
Expand Down
21 changes: 8 additions & 13 deletions crates/biome_css_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ use biome_analyze::{
use biome_css_syntax::CssLanguage;
use biome_diagnostics::{category, Error};
use biome_suppression::{parse_suppression_comment, SuppressionDiagnostic};
use std::ops::Deref;
use std::sync::LazyLock;

/// Return the static [MetadataRegistry] for the JSON analyzer rules
pub fn metadata() -> &'static MetadataRegistry {
lazy_static::lazy_static! {
static ref METADATA: MetadataRegistry = {
let mut metadata = MetadataRegistry::default();
visit_registry(&mut metadata);
metadata
};
}

&METADATA
}
pub static METADATA: LazyLock<MetadataRegistry> = LazyLock::new(|| {
let mut metadata = MetadataRegistry::default();
visit_registry(&mut metadata);
metadata
});

/// Run the analyzer on the provided `root`: this process will use the given `filter`
/// to selectively restrict analysis to specific rules / a specific source range,
Expand Down Expand Up @@ -111,7 +106,7 @@ where
}

let mut analyzer = biome_analyze::Analyzer::new(
metadata(),
METADATA.deref(),
biome_analyze::InspectMatcher::new(registry, inspect_matcher),
parse_linter_suppression_comment,
Box::new(CssSuppressionAction),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use biome_console::markup;
use biome_css_syntax::{CssFunction, CssParameter};
use biome_rowan::AstNode;
use biome_rowan::AstSeparatedList;
use lazy_static::lazy_static;
use regex::Regex;
use std::sync::LazyLock;

use crate::utils::vendor_prefixed;

Expand Down Expand Up @@ -50,23 +50,21 @@ declare_lint_rule! {
}
}

lazy_static! {
// It is necessary to find case-insensitive string.
// Also Check if 'in' is a word boundary.
// For examples,`to top in srgb` is valid but `to top insrgb` is not valid.
pub static ref IN_KEYWORD: Regex = Regex::new(r"(?i)\bin\b").unwrap();
// It is necessary to find case-insensitive string.
// Also Check if 'in' is a word boundary.
// For examples,`to top in srgb` is valid but `to top insrgb` is not valid.
pub static IN_KEYWORD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\bin\b").unwrap());

// This regex checks if a string consists of a number immediately followed by a unit, with no space between them.
// For examples, `45deg`, `45grad` is valid but `45 deg`, `45de` is not valid.
pub static ref ANGLE: Regex = Regex::new(r"^[\d.]+(?:deg|grad|rad|turn)$").unwrap();
// This regex checks if a string consists of a number immediately followed by a unit, with no space between them.
// For examples, `45deg`, `45grad` is valid but `45 deg`, `45de` is not valid.
pub static ANGLE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[\d.]+(?:deg|grad|rad|turn)$").unwrap());

// This need for capture `side-or-corner` keyword from linear-gradient function.
// Ensure starts `side-or-corner` keyword `to` and ends with the keyword `side-or-corner`.
pub static ref DIRECTION_WITHOUT_TO: Regex = Regex::new(&format!(
r"(?i)^({0})(?: ({0}))?$",
"top|left|bottom|right"
)).unwrap();
}
// This need for capture `side-or-corner` keyword from linear-gradient function.
// Ensure starts `side-or-corner` keyword `to` and ends with the keyword `side-or-corner`.
pub static DIRECTION_WITHOUT_TO: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(r"(?i)^({0})(?: ({0}))?$", "top|left|bottom|right")).unwrap()
});

impl Rule for NoInvalidDirectionInLinearGradient {
type Query = Ast<CssFunction>;
Expand Down
4 changes: 3 additions & 1 deletion crates/biome_css_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_test_utils::{
has_bogus_nodes_or_empty_slots, parse_test_path, register_leak_checker, scripts_from_json,
write_analyzer_snapshot, CheckActionType,
};
use std::ops::Deref;
use std::{ffi::OsStr, fs::read_to_string, path::Path, slice};

tests_macros::gen_tests! {"tests/specs/**/*.{css,json,jsonc}", crate::run_test, "module"}
Expand All @@ -27,7 +28,8 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
if group == "specs" || group == "suppression" {
panic!("the test file must be placed in the {group}/{rule}/<rule-name>/ directory");
}
if biome_css_analyze::metadata()
if biome_css_analyze::METADATA
.deref()
.find_rule(group, rule)
.is_none()
{
Expand Down
10 changes: 8 additions & 2 deletions crates/biome_flags/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//! A simple implementation of feature flags.

use std::ops::Deref;
use std::sync::LazyLock;

/// Returns `true` if this is an unstable build of Biome
pub const fn is_unstable() -> bool {
option_env!("BIOME_VERSION").is_none()
pub fn is_unstable() -> bool {
BIOME_VERSION.deref().is_none()
}

/// The internal version of Biome. This is usually supplied during the CI build
pub static BIOME_VERSION: LazyLock<Option<&str>> = LazyLock::new(|| option_env!("BIOME_VERSION"));
1 change: 0 additions & 1 deletion crates/biome_graphql_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ biome_diagnostics = { workspace = true }
biome_graphql_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_suppression = { workspace = true }
lazy_static = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

Expand Down
21 changes: 8 additions & 13 deletions crates/biome_graphql_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ use biome_analyze::{
use biome_diagnostics::{category, Error};
use biome_graphql_syntax::GraphqlLanguage;
use biome_suppression::{parse_suppression_comment, SuppressionDiagnostic};
use std::ops::Deref;
use std::sync::LazyLock;

/// Return the static [MetadataRegistry] for the JSON analyzer rules
pub fn metadata() -> &'static MetadataRegistry {
lazy_static::lazy_static! {
static ref METADATA: MetadataRegistry = {
let mut metadata = MetadataRegistry::default();
visit_registry(&mut metadata);
metadata
};
}

&METADATA
}
pub static METADATA: LazyLock<MetadataRegistry> = LazyLock::new(|| {
let mut metadata = MetadataRegistry::default();
visit_registry(&mut metadata);
metadata
});

/// Run the analyzer on the provided `root`: this process will use the given `filter`
/// to selectively restrict analysis to specific rules / a specific source range,
Expand Down Expand Up @@ -109,7 +104,7 @@ where
}

let mut analyzer = biome_analyze::Analyzer::new(
metadata(),
METADATA.deref(),
biome_analyze::InspectMatcher::new(registry, inspect_matcher),
parse_linter_suppression_comment,
Box::new(GraphqlSuppressionAction),
Expand Down
4 changes: 3 additions & 1 deletion crates/biome_graphql_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use biome_test_utils::{
has_bogus_nodes_or_empty_slots, parse_test_path, register_leak_checker, scripts_from_json,
write_analyzer_snapshot, CheckActionType,
};
use std::ops::Deref;
use std::{ffi::OsStr, fs::read_to_string, path::Path, slice};

tests_macros::gen_tests! {"tests/specs/**/*.{graphql,json,jsonc}", crate::run_test, "module"}
Expand All @@ -27,7 +28,8 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
if group == "specs" || group == "suppression" {
panic!("the test file must be placed in the {group}/{rule}/<rule-name>/ directory");
}
if biome_graphql_analyze::metadata()
if biome_graphql_analyze::METADATA
.deref()
.find_rule(group, rule)
.is_none()
{
Expand Down
1 change: 0 additions & 1 deletion crates/biome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ biome_unicode_table = { workspace = true }
bitflags = { workspace = true }
bitvec = "1.0.1"
enumflags2 = { workspace = true }
lazy_static = { workspace = true }
natord = { workspace = true }
regex = { workspace = true }
roaring = "0.10.6"
Expand Down
Loading

0 comments on commit 4880f84

Please sign in to comment.