Skip to content

Commit

Permalink
Fix non idiomatic style lints (FuelLabs#384)
Browse files Browse the repository at this point in the history
* reimplement style diagnostics

* Use reimplemented stlye functions instead of inflector

* Add warning for non-SCREAMING_SNAKE_CASE const names
  • Loading branch information
canndrew authored Nov 16, 2021
1 parent 2972560 commit 141b2b9
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 23 deletions.
23 changes: 17 additions & 6 deletions core_lang/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::parser::Rule;
use crate::span::Span;
use crate::style::{to_screaming_snake_case, to_snake_case, to_upper_camel_case};
use crate::type_engine::{IntegerBits, TypeInfo};
use inflector::cases::classcase::to_class_case;
use inflector::cases::snakecase::to_snake_case;
use std::fmt;
use thiserror::Error;

Expand Down Expand Up @@ -192,6 +191,9 @@ pub enum Warning<'sc> {
NonSnakeCaseFunctionName {
name: &'sc str,
},
NonScreamingSnakeCaseConstName {
name: &'sc str,
},
LossOfPrecision {
initial_type: IntegerBits,
cast_to: IntegerBits,
Expand Down Expand Up @@ -233,23 +235,23 @@ impl<'sc> fmt::Display for Warning<'sc> {
"Struct name \"{}\" is not idiomatic. Structs should have a ClassCase name, like \
\"{}\".",
struct_name,
to_class_case(struct_name)
to_upper_camel_case(struct_name)
)
}
NonClassCaseTraitName { name } => {
write!(f,
"Trait name \"{}\" is not idiomatic. Traits should have a ClassCase name, like \
\"{}\".",
name,
to_class_case(name)
to_upper_camel_case(name)
)
}
NonClassCaseEnumName { enum_name } => write!(
f,
"Enum \"{}\"'s capitalization is not idiomatic. Enums should have a ClassCase \
name, like \"{}\".",
enum_name,
to_class_case(enum_name)
to_upper_camel_case(enum_name)
),
NonSnakeCaseStructFieldName { field_name } => write!(
f,
Expand All @@ -263,7 +265,7 @@ impl<'sc> fmt::Display for Warning<'sc> {
"Enum variant name \"{}\" is not idiomatic. Enum variant names should be \
ClassCase, like \"{}\".",
variant_name,
to_class_case(variant_name)
to_upper_camel_case(variant_name)
),
NonSnakeCaseFunctionName { name } => {
write!(f,
Expand All @@ -273,6 +275,15 @@ impl<'sc> fmt::Display for Warning<'sc> {
to_snake_case(name)
)
}
NonScreamingSnakeCaseConstName { name } => {
write!(
f,
"Constant name \"{}\" is not idiomatic. Constant names should be SCREAMING_SNAKE_CASE, like \
\"{}\".",
name,
to_screaming_snake_case(name),
)
},
LossOfPrecision {
initial_type,
cast_to,
Expand Down
1 change: 1 addition & 0 deletions core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod parse_tree;
mod parser;
pub mod semantic_analysis;
mod span;
mod style;
pub(crate) mod type_engine;

use crate::asm_generation::checks::check_invalid_opcodes;
Expand Down
29 changes: 22 additions & 7 deletions core_lang/src/parse_tree/declaration/constant_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::parse_tree::{Expression, Visibility};
use crate::{type_engine::TypeInfo, Ident};

use crate::build_config::BuildConfig;
use crate::error::{err, ok, CompileResult};
use crate::error::{err, ok, CompileResult, Warning};
use crate::parser::Rule;
use crate::span::Span;
use crate::style::is_screaming_snake_case;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand All @@ -19,6 +21,7 @@ impl<'sc> ConstantDeclaration<'sc> {
pair: Pair<'sc, Rule>,
config: Option<&BuildConfig>,
) -> CompileResult<'sc, ConstantDeclaration<'sc>> {
let path = config.map(|c| c.path());
let mut warnings = Vec::new();
let mut errors = Vec::new();
let mut const_decl_parts = pair.into_inner();
Expand Down Expand Up @@ -56,14 +59,26 @@ impl<'sc> ConstantDeclaration<'sc> {
warnings,
errors
);
let name = check!(
Ident::parse_from_pair(name_pair.clone(), config),
return err(warnings, errors),
warnings,
errors
);
assert_or_warn!(
is_screaming_snake_case(name.primary_name),
warnings,
Span {
span: name_pair.as_span(),
path,
},
Warning::NonScreamingSnakeCaseConstName {
name: name.primary_name,
}
);
ok(
ConstantDeclaration {
name: check!(
Ident::parse_from_pair(name_pair, config),
return err(warnings, errors),
warnings,
errors
),
name,
type_ascription,
value,
visibility,
Expand Down
6 changes: 3 additions & 3 deletions core_lang/src/parse_tree/declaration/enum_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{error::*, semantic_analysis::ast_node::TypedEnumDeclaration};
use crate::{
parse_tree::{declaration::TypeParameter, Visibility},
semantic_analysis::ast_node::TypedEnumVariant,
style::is_upper_camel_case,
};
use inflector::cases::classcase::is_class_case;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<'sc> EnumDeclaration<'sc> {
errors
);
assert_or_warn!(
is_class_case(name.primary_name),
is_upper_camel_case(name.primary_name),
warnings,
Span {
span: enum_name.as_span(),
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'sc> EnumVariant<'sc> {
errors
);
assert_or_warn!(
is_class_case(name.primary_name),
is_upper_camel_case(name.primary_name),
warnings,
name.span.clone(),
Warning::NonClassCaseEnumVariantName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::build_config::BuildConfig;
use crate::error::*;
use crate::parse_tree::{declaration::TypeParameter, Visibility};
use crate::span::Span;
use crate::style::is_snake_case;
use crate::type_engine::TypeInfo;
use crate::{CodeBlock, Ident, Rule};
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand Down
5 changes: 2 additions & 3 deletions core_lang/src/parse_tree/declaration/struct_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use crate::build_config::BuildConfig;
use crate::parse_tree::{declaration::TypeParameter, Visibility};
use crate::parser::Rule;
use crate::span::Span;
use crate::style::{is_snake_case, is_upper_camel_case};
use crate::type_engine::TypeInfo;
use crate::{error::*, Ident};
use inflector::cases::classcase::is_class_case;
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -89,7 +88,7 @@ impl<'sc> StructDeclaration<'sc> {
errors
);
assert_or_warn!(
is_class_case(name.primary_name),
is_upper_camel_case(name.primary_name),
warnings,
span,
Warning::NonClassCaseStructName {
Expand Down
5 changes: 2 additions & 3 deletions core_lang/src/parse_tree/declaration/trait_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::build_config::BuildConfig;
use crate::parse_tree::TypeParameter;
use crate::parser::Rule;
use crate::span::Span;
use crate::style::{is_snake_case, is_upper_camel_case};
use crate::type_engine::TypeInfo;
use crate::{error::*, Ident};
use inflector::cases::classcase::is_class_case;
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -45,7 +44,7 @@ impl<'sc> TraitDeclaration<'sc> {
);
let span = name.span.clone();
assert_or_warn!(
is_class_case(name_pair.as_str().trim()),
is_upper_camel_case(name_pair.as_str().trim()),
warnings,
span,
Warning::NonClassCaseTraitName {
Expand Down
Loading

0 comments on commit 141b2b9

Please sign in to comment.