-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathroblox_manual_fromscale_or_fromoffset.rs
156 lines (132 loc) · 4.67 KB
/
roblox_manual_fromscale_or_fromoffset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::*;
use crate::ast_util::range;
use std::convert::Infallible;
use full_moon::{
ast::{self, Ast},
visitors::Visitor,
};
pub struct ManualFromScaleOrFromOffsetLint;
fn create_diagnostic(args: &UDim2ComplexArgs) -> Diagnostic {
let code = "roblox_manual_fromscale_or_fromoffset";
let primary_label = Label::new(args.call_range);
match args.complexity_type {
UDim2ConstructorType::OffsetOnly => Diagnostic::new_complete(
code,
"this UDim2.new call only sets offset, and can be simplified using UDim2.fromOffset"
.to_owned(),
primary_label,
vec![format!(
"try: UDim2.fromOffset({}, {})",
args.arg_0, args.arg_1
)],
Vec::new(),
),
UDim2ConstructorType::ScaleOnly => Diagnostic::new_complete(
code,
"this UDim2.new call only sets scale, and can be simplified using UDim2.fromScale"
.to_owned(),
primary_label,
vec![format!(
"try: UDim2.fromScale({}, {})",
args.arg_0, args.arg_1
)],
Vec::new(),
),
}
}
impl Lint for ManualFromScaleOrFromOffsetLint {
type Config = ();
type Error = Infallible;
const SEVERITY: Severity = Severity::Warning;
const LINT_TYPE: LintType = LintType::Style;
fn new(_: Self::Config) -> Result<Self, Self::Error> {
Ok(ManualFromScaleOrFromOffsetLint)
}
fn pass(&self, ast: &Ast, context: &Context, _: &AstContext) -> Vec<Diagnostic> {
if !context.is_roblox() {
return Vec::new();
}
let mut visitor = UDim2NewVisitor::default();
visitor.visit_ast(ast);
visitor.args.iter().map(create_diagnostic).collect()
}
}
#[derive(Default)]
struct UDim2NewVisitor {
args: Vec<UDim2ComplexArgs>,
}
#[derive(PartialEq)]
enum UDim2ConstructorType {
ScaleOnly,
OffsetOnly,
}
struct UDim2ComplexArgs {
complexity_type: UDim2ConstructorType,
call_range: (usize, usize),
arg_0: String,
arg_1: String,
}
impl Visitor for UDim2NewVisitor {
fn visit_function_call(&mut self, call: &ast::FunctionCall) {
if_chain::if_chain! {
if let ast::Prefix::Name(token) = call.prefix();
if token.token().to_string() == "UDim2";
let mut suffixes = call.suffixes().collect::<Vec<_>>();
if suffixes.len() == 2; // .new and ()
let call_suffix = suffixes.pop().unwrap();
let index_suffix = suffixes.pop().unwrap();
if let ast::Suffix::Index(ast::Index::Dot { name, .. }) = index_suffix;
if name.token().to_string() == "new";
if let ast::Suffix::Call(ast::Call::AnonymousCall(
ast::FunctionArgs::Parentheses { arguments, .. }
)) = call_suffix;
then {
if arguments.len() != 4 {
return;
}
let mut iter = arguments.iter();
let x_scale = iter.next().unwrap().to_string();
let x_offset = iter.next().unwrap().to_string();
let y_scale = iter.next().unwrap().to_string();
let y_offset = iter.next().unwrap().to_string();
let only_offset = x_scale.parse::<f32>() == Ok(0.0) && y_scale.parse::<f32>() == Ok(0.0);
let only_scale = x_offset.parse::<f32>() == Ok(0.0) && y_offset.parse::<f32>() == Ok(0.0);
if only_offset && only_scale
{
// Skip linting if all are zero
return;
}
else if only_offset
{
self.args.push(UDim2ComplexArgs {
call_range: range(call),
arg_0: x_offset,
arg_1: y_offset,
complexity_type: UDim2ConstructorType::OffsetOnly,
});
}
else if only_scale
{
self.args.push(UDim2ComplexArgs {
call_range: range(call),
arg_0: x_scale,
arg_1: y_scale,
complexity_type: UDim2ConstructorType::ScaleOnly,
});
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::{super::test_util::test_lint, *};
#[test]
fn test_manual_fromscale_or_fromoffset() {
test_lint(
ManualFromScaleOrFromOffsetLint::new(()).unwrap(),
"roblox_manual_fromscale_or_fromoffset",
"roblox_manual_fromscale_or_fromoffset",
);
}
}