Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): useNamingConvention #4534

Merged
merged 9 commits into from
Jun 18, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tests_macros = { path = "./crates/tests_macros" }
rome_formatter_test = { path = "./crates/rome_formatter_test" }
rome_js_analyze = { path = "./crates/rome_js_analyze" }
schemars = { version = "0.8.10" }
smallvec = { version = "1.8.0", features = ["union", "const_new"] }


[profile.dev.package.rome_wasm]
Expand Down
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ define_categories! {
"lint/nursery/useLiteralKeys": "https://docs.rome.tools/lint/rules/useLiteralKeys",
"lint/nursery/useSimpleNumberKeys": "https://docs.rome.tools/lint/rules/useSimpleNumberKeys",
"lint/nursery/noStaticOnlyClass": "https://docs.rome.tools/lint/rules/noStaticOnlyClass",
"lint/nursery/useNamingConvention": "https://docs.rome.tools/lint/rules/useNamingConvention",
// Insert new nursery rule here


Expand Down
1 change: 1 addition & 0 deletions crates/rome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lazy_static = { workspace = true }
natord = "1.0.9"
bpaf.workspace = true
schemars = { workspace = true, optional = true }
smallvec = { workspace = true }

[dev-dependencies]
tests_macros = { workspace = true }
Expand Down
34 changes: 5 additions & 29 deletions crates/rome_js_analyze/src/analyzers/style/no_inferrable_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Ast, Rule
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_syntax::{
AnyJsExpression, AnyTsPropertyAnnotation, AnyTsType, AnyTsVariableAnnotation,
JsFormalParameter, JsInitializerClause, JsPropertyClassMember, JsSyntaxKind,
JsVariableDeclaration, JsVariableDeclarator, JsVariableDeclaratorList, TsPropertyParameter,
TsReadonlyModifier, TsTypeAnnotation,
AnyJsExpression, AnyTsPropertyAnnotation, AnyTsVariableAnnotation, JsFormalParameter,
JsInitializerClause, JsPropertyClassMember, JsSyntaxKind, JsVariableDeclaration,
JsVariableDeclarator, JsVariableDeclaratorList, TsPropertyParameter, TsReadonlyModifier,
TsTypeAnnotation,
};
use rome_rowan::chain_trivia_pieces;
use rome_rowan::AstNode;
Expand Down Expand Up @@ -149,9 +149,7 @@ impl Rule for NoInferrableTypes {
//
// In non-const contexts, wide type annotation are rejected.
// e.g. `let x: number = <literal>`
if (is_const && is_literal_type(&ty).is_some())
|| (!is_const && is_literal_wide_type(&ty).is_some())
{
if (is_const && ty.is_literal_type()) || (!is_const && ty.is_primitive_type()) {
return Some(type_annotation);
}
}
Expand Down Expand Up @@ -211,25 +209,3 @@ fn has_trivially_inferrable_type(expr: &AnyJsExpression) -> Option<()> {
_ => None,
}
}

fn is_literal_type(ty: &AnyTsType) -> Option<()> {
match ty {
AnyTsType::TsBooleanLiteralType(_)
| AnyTsType::TsBigintLiteralType(_)
| AnyTsType::TsNullLiteralType(_)
| AnyTsType::TsNumberLiteralType(_)
| AnyTsType::TsStringLiteralType(_)
| AnyTsType::TsUndefinedType(_) => Some(()),
_ => None,
}
}

fn is_literal_wide_type(ty: &AnyTsType) -> Option<()> {
match ty {
AnyTsType::TsBooleanType(_)
| AnyTsType::TsBigintType(_)
| AnyTsType::TsNumberType(_)
| AnyTsType::TsStringType(_) => Some(()),
_ => None,
}
}
41 changes: 30 additions & 11 deletions crates/rome_js_analyze/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use crate::semantic_analyzers::nursery::use_exhaustive_dependencies::{
hooks_options, HooksOptions,
};
use crate::semantic_analyzers::nursery::use_naming_convention::{
naming_convention_options, NamingConventionOptions,
};
use bpaf::Bpaf;
use rome_analyze::options::RuleOptions;
use rome_analyze::RuleKey;
Expand All @@ -21,6 +24,8 @@ use std::str::FromStr;
pub enum PossibleOptions {
/// Options for `useExhaustiveDependencies` and `useHookAtTopLevel` rule
Hooks(#[bpaf(external(hooks_options), hide)] HooksOptions),
/// Options for `useNamingConvention` rule
NamingConvention(#[bpaf(external(naming_convention_options), hide)] NamingConventionOptions),
/// No options available
#[default]
NoOptions,
Expand All @@ -35,18 +40,24 @@ impl FromStr for PossibleOptions {
}

impl PossibleOptions {
const KNOWN_KEYS: &'static [&'static str] = &["hooks"];
const KNOWN_KEYS: &'static [&'static str] = &["hooks", "strictCase", "enumMemberCase"];

pub fn extract_option(&self, rule_key: &RuleKey) -> RuleOptions {
match rule_key.rule_name() {
"useExhaustiveDependencies" | "useHookAtTopLevel" => {
let options = match self {
PossibleOptions::Hooks(hooks) => hooks.clone(),
PossibleOptions::Hooks(options) => options.clone(),
_ => HooksOptions::default(),
};
RuleOptions::new(options)
}

"useNamingConvention" => {
let options = match self {
PossibleOptions::NamingConvention(options) => *options,
_ => NamingConventionOptions::default(),
};
RuleOptions::new(options)
}
// TODO: review error
_ => panic!("This rule {:?} doesn't have options", rule_key),
}
Expand All @@ -69,15 +80,23 @@ impl VisitNode<JsonLanguage> for PossibleOptions {
value: &SyntaxNode<JsonLanguage>,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<()> {
let (name, value) = self.get_key_and_value(key, value, diagnostics)?;
let name_text = name.text();

if name_text == "hooks" {
let mut options = HooksOptions::default();
self.map_to_array(&value, &name, &mut options, diagnostics);
*self = PossibleOptions::Hooks(options);
let (name, val) = self.get_key_and_value(key, value, diagnostics)?;
match name.text() {
"hooks" => {
let mut options = HooksOptions::default();
self.map_to_array(&val, &name, &mut options, diagnostics)?;
*self = PossibleOptions::Hooks(options);
}
"strictCase" | "enumMemberCase" => {
let mut options = match self {
PossibleOptions::NamingConvention(options) => *options,
_ => NamingConventionOptions::default(),
};
options.visit_map(key, value, diagnostics)?;
*self = PossibleOptions::NamingConvention(options);
}
_ => (),
}

Some(())
}
}
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/semantic_analyzers/nursery.rs

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

Loading