Skip to content

Commit

Permalink
refactor(global): sort imports (#7883)
Browse files Browse the repository at this point in the history
Pure refactor. Re-order imports for clarity:

1. `std`
2. External crates
3. `oxc_*` crates
4. Current crate `use crate::...`
5. Super `use super::...`
6. Local modules

This order is from "furthest away" to "closest". This makes it clearer to see what is coming from where.

`cargo +nightly fmt` (#7877) did a lot of the work, but unfortunately `rustfmt` does not have an option to (a) put workspace crates in a separate block from external crates and (b) move `mod` statements to after `use` statements.
  • Loading branch information
overlookmotel committed Dec 14, 2024
1 parent 2c94236 commit 3858221
Show file tree
Hide file tree
Showing 123 changed files with 375 additions and 254 deletions.
23 changes: 14 additions & 9 deletions crates/oxc_ast/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,6 @@
//!
//! If you are seeing compile-time errors in `src/ast/macros.rs`, this will be the cause.
pub(crate) mod comment;
pub(crate) mod js;
pub(crate) mod jsx;
pub(crate) mod literal;
pub(crate) mod macros;
pub(crate) mod ts;

use macros::inherit_variants;
// Re-export AST types from other crates
pub use oxc_span::{Atom, Language, LanguageVariant, ModuleKind, SourceType, Span};
pub use oxc_syntax::{
Expand All @@ -190,4 +182,17 @@ pub use oxc_syntax::{
},
};

pub use self::{comment::*, js::*, jsx::*, literal::*, ts::*};
pub(crate) mod comment;
pub(crate) mod js;
pub(crate) mod jsx;
pub(crate) mod literal;
pub(crate) mod macros;
pub(crate) mod ts;

pub use comment::*;
pub use js::*;
pub use jsx::*;
pub use literal::*;
pub use ts::*;

use macros::inherit_variants;
7 changes: 4 additions & 3 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use cow_utils::CowUtils;
use num_bigint::BigInt;
use num_traits::Num;
use oxc_allocator::Box;
use oxc_span::{Atom, Span};
use oxc_syntax::number::BigintBase;
use serde::{
ser::{SerializeSeq, Serializer},
Serialize,
};

use oxc_allocator::Box;
use oxc_span::{Atom, Span};
use oxc_syntax::number::BigintBase;

use crate::ast::{
BigIntLiteral, BindingPatternKind, BooleanLiteral, Directive, Elision, FormalParameter,
FormalParameterKind, FormalParameters, JSXElementName, JSXIdentifier,
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hash::FxHashMap;

use oxc_ast::{Comment, CommentKind};
use oxc_syntax::identifier::is_line_terminator;
use rustc_hash::FxHashMap;

use crate::{Codegen, LegalComment};

Expand Down
13 changes: 8 additions & 5 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
mod is_literal_value;
mod value;
mod value_type;

use std::{borrow::Cow, cmp::Ordering};

use num_bigint::BigInt;
use num_traits::Zero;

use oxc_ast::ast::*;

pub use self::{is_literal_value::IsLiteralValue, value::ConstantValue, value_type::ValueType};
use crate::{side_effects::MayHaveSideEffects, ToBigInt, ToBoolean, ToInt32, ToJsString, ToNumber};

mod is_literal_value;
mod value;
mod value_type;
pub use is_literal_value::IsLiteralValue;
pub use value::ConstantValue;
pub use value_type::ValueType;

pub trait ConstantEvaluation<'a> {
fn is_global_reference(&self, ident: &IdentifierReference<'a>) -> bool {
matches!(ident.name.as_str(), "undefined" | "NaN" | "Infinity")
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ecmascript/src/to_big_int.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use num_bigint::BigInt;
use num_traits::{One, Zero};

use oxc_ast::ast::{BigIntLiteral, Expression};
use oxc_syntax::operator::UnaryOperator;

Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rustc_hash::FxHashMap;

use oxc_allocator::CloneIn;
use oxc_ast::ast::*;
use oxc_ecmascript::ToInt32;
Expand All @@ -6,7 +8,6 @@ use oxc_syntax::{
number::{NumberBase, ToJsString},
operator::{BinaryOperator, UnaryOperator},
};
use rustc_hash::FxHashMap;

use crate::{diagnostics::enum_member_initializers, IsolatedDeclarations};

Expand Down
21 changes: 11 additions & 10 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
//! * <https://www.typescriptlang.org/tsconfig#isolatedDeclarations>
//! * <https://github.com/microsoft/TypeScript/blob/v5.6.3/src/compiler/transformers/declarations.ts>
use std::{cell::RefCell, mem};

use rustc_hash::{FxHashMap, FxHashSet};

use oxc_allocator::{Allocator, CloneIn};
use oxc_ast::{ast::*, AstBuilder, Visit, NONE};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, GetSpan, SourceType, SPAN};

use crate::{diagnostics::function_with_assigning_properties, scope::ScopeTree};

mod class;
mod declaration;
mod diagnostics;
Expand All @@ -19,16 +30,6 @@ mod scope;
mod signatures;
mod types;

use std::{cell::RefCell, mem};

use oxc_allocator::{Allocator, CloneIn};
use oxc_ast::{ast::*, AstBuilder, Visit, NONE};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, GetSpan, SourceType, SPAN};
use rustc_hash::{FxHashMap, FxHashSet};

use crate::{diagnostics::function_with_assigning_properties, scope::ScopeTree};

#[derive(Debug, Default, Clone, Copy)]
pub struct IsolatedDeclarationsOptions {
/// Do not emit declarations for code that has an `@internal` annotation in its JSDoc comment.
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::cell::Cell;

use bitflags::bitflags;
use rustc_hash::FxHashMap;

use oxc_ast::{ast::*, visit::walk::*, Visit};
use oxc_span::Atom;
use oxc_syntax::scope::{ScopeFlags, ScopeId};
use rustc_hash::FxHashMap;

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_isolated_declarations/src/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rustc_hash::FxHashMap;

use oxc_allocator::{CloneIn, Vec};
use oxc_ast::ast::{TSMethodSignatureKind, TSSignature};
use oxc_span::GetSpan;
use rustc_hash::FxHashMap;

use crate::IsolatedDeclarations;

Expand Down
11 changes: 6 additions & 5 deletions crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use std::{

use cow_utils::CowUtils;
use log::debug;
use rustc_hash::FxHashSet;
use tower_lsp::lsp_types::{
self, CodeDescription, DiagnosticRelatedInformation, DiagnosticSeverity, NumberOrString,
Position, Range, Url,
};

use oxc_allocator::Allocator;
use oxc_data_structures::rope::{get_line_column, Rope};
use oxc_diagnostics::{Error, NamedSource, Severity};
Expand All @@ -18,11 +24,6 @@ use oxc_linter::{
use oxc_parser::{ParseOptions, Parser};
use oxc_semantic::SemanticBuilder;
use oxc_span::VALID_EXTENSIONS;
use rustc_hash::FxHashSet;
use tower_lsp::lsp_types::{
self, CodeDescription, DiagnosticRelatedInformation, DiagnosticSeverity, NumberOrString,
Position, Range, Url,
};

const LINT_DOC_LINK_PREFIX: &str = "https://oxc.rs/docs/guide/usage/linter/rules";
#[derive(Debug)]
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
mod linter;

use std::{fmt::Debug, path::PathBuf, str::FromStr};

use dashmap::DashMap;
use futures::future::join_all;
use globset::Glob;
use ignore::gitignore::Gitignore;
use log::{debug, error, info};
use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use rustc_hash::FxBuildHasher;
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, OnceCell, RwLock, SetError};
Expand All @@ -26,8 +23,12 @@ use tower_lsp::{
Client, LanguageServer, LspService, Server,
};

use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};

use crate::linter::{DiagnosticReport, ServerLinter};

mod linter;

type FxDashMap<K, V> = DashMap<K, V, FxBuildHasher>;

struct Backend {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/config/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::{
use dashmap::DashMap;
use rustc_hash::{FxBuildHasher, FxHashSet};

use crate::{rules::RULES, LintPlugins, RuleWithSeverity};

use super::{
overrides::{OverrideId, OxlintOverrides},
LintConfig,
};
use crate::{rules::RULES, LintPlugins, RuleWithSeverity};

type AppliedOverrideHash = u64;
type FxDashMap<K, V> = DashMap<K, V, FxBuildHasher>;
Expand Down
25 changes: 11 additions & 14 deletions crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

mod categories;
mod env;
mod flat;
Expand All @@ -7,20 +9,15 @@ mod oxlintrc;
mod plugins;
mod rules;
mod settings;

use std::path::PathBuf;

pub(crate) use self::flat::ResolvedLinterState;
pub use self::{
env::OxlintEnv,
flat::ConfigStore,
globals::OxlintGlobals,
overrides::OxlintOverrides,
oxlintrc::Oxlintrc,
plugins::LintPlugins,
rules::{ESLintRule, OxlintRules},
settings::{jsdoc::JSDocPluginSettings, OxlintSettings},
};
pub use env::OxlintEnv;
pub use flat::ConfigStore;
pub(crate) use flat::ResolvedLinterState;
pub use globals::OxlintGlobals;
pub use overrides::OxlintOverrides;
pub use oxlintrc::Oxlintrc;
pub use plugins::LintPlugins;
pub use rules::{ESLintRule, OxlintRules};
pub use settings::{jsdoc::JSDocPluginSettings, OxlintSettings};

#[derive(Debug, Default, Clone)]
pub(crate) struct LintConfig {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/config/overrides.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{borrow::Cow, ops::Deref, path::Path};

use nonmax::NonMaxU32;
use oxc_index::{Idx, IndexVec};
use schemars::{gen, schema::Schema, JsonSchema};
use serde::{de, ser, Deserialize, Serialize};

use oxc_index::{Idx, IndexVec};

use crate::{config::OxlintRules, LintPlugins};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/config/oxlintrc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::path::{Path, PathBuf};

use oxc_diagnostics::OxcDiagnostic;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use oxc_diagnostics::OxcDiagnostic;

use crate::utils::read_to_string;

use super::{
categories::OxlintCategories, env::OxlintEnv, globals::OxlintGlobals,
overrides::OxlintOverrides, plugins::LintPlugins, rules::OxlintRules, settings::OxlintSettings,
};
use crate::utils::read_to_string;

/// Oxlint Configuration File
///
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{borrow::Cow, fmt};

use oxc_diagnostics::{Error, OxcDiagnostic};
use rustc_hash::{FxHashMap, FxHashSet};
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{
Expand All @@ -9,6 +8,8 @@ use serde::{
Deserialize, Serialize,
};

use oxc_diagnostics::{Error, OxcDiagnostic};

use crate::{
rules::{RuleEnum, RULES},
utils::{is_eslint_rule_adapted_to_typescript, is_jest_rule_adapted_to_vitest},
Expand Down Expand Up @@ -344,12 +345,13 @@ mod test {
use serde::Deserialize;
use serde_json::{json, Value};

use super::{OxlintRules, RuleSet};
use crate::{
rules::{RuleEnum, RULES},
AllowWarnDeny, RuleWithSeverity,
};

use super::{OxlintRules, RuleSet};

#[test]
fn test_parse_rules() {
let rules = OxlintRules::deserialize(&json!({
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/context/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
use oxc_semantic::Semantic;
use oxc_span::SourceType;

use super::{plugin_name_to_prefix, LintContext};
use crate::{
config::{LintConfig, LintPlugins},
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
Expand All @@ -14,6 +13,8 @@ use crate::{
utils, FrameworkFlags, RuleWithSeverity,
};

use super::{plugin_name_to_prefix, LintContext};

/// Stores shared information about a file being linted.
///
/// When linting a file, there are a number of shared resources that are
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![allow(rustdoc::private_intra_doc_links)] // useful for intellisense
mod host;

use std::{ops::Deref, path::Path, rc::Rc};

pub(crate) use host::ContextHost;
use oxc_cfg::ControlFlowGraph;
use oxc_diagnostics::{OxcDiagnostic, Severity};
use oxc_semantic::Semantic;
Expand All @@ -18,6 +16,9 @@ use crate::{
AllowWarnDeny, FrameworkFlags, ModuleRecord, OxlintEnv, OxlintGlobals, OxlintSettings,
};

mod host;
pub(crate) use host::ContextHost;

#[derive(Clone)]
#[must_use]
/// Contains all of the state and context specific to this lint rule. Includes information
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/fixer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod fix;

use std::borrow::Cow;

pub use fix::{CompositeFix, Fix, FixKind, RuleFix};
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{GetSpan, Span};

use crate::LintContext;

mod fix;
pub use fix::{CompositeFix, Fix, FixKind, RuleFix};

/// Produces [`RuleFix`] instances. Inspired by ESLint's [`RuleFixer`].
///
/// [`RuleFixer`]: https://github.com/eslint/eslint/blob/v9.9.1/lib/linter/rule-fixer.js
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod partial_loader;
mod source;

use std::{error::Error, fmt, path::Path};

use oxc_span::SourceType;

mod partial_loader;
mod source;
pub use partial_loader::{PartialLoader, LINT_PARTIAL_LOADER_EXT};
pub use source::JavaScriptSource;

Expand Down
Loading

0 comments on commit 3858221

Please sign in to comment.