use alloc::collections::BTreeSet;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::ast::Node;
use crate::dialect::Dialect;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarningType {
All,
VarUnset,
UseUnsetGlobal,
UseUnsetLocal,
UseEnv,
LocalSameAsGlobal,
ClassOverwrite,
Unreachable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarningMode {
MsgBox,
StdOut,
OutputDebug,
Off,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TheModes {
pub var_unset: WarningMode,
pub use_unset_global: WarningMode,
pub use_unset_local: WarningMode,
pub use_env: WarningMode,
pub local_same_as_global: WarningMode,
pub class_overwrite: WarningMode,
pub unreachable: WarningMode,
}
impl TheModes {
pub fn the_defaults_of_the_v2() -> TheModes {
TheModes {
var_unset: WarningMode::MsgBox,
use_unset_global: WarningMode::Off,
use_unset_local: WarningMode::Off,
use_env: WarningMode::Off,
local_same_as_global: WarningMode::Off,
class_overwrite: WarningMode::Off,
unreachable: WarningMode::MsgBox,
}
}
pub fn the_defaults_of_the_v1() -> TheModes {
TheModes {
var_unset: WarningMode::Off,
use_unset_global: WarningMode::Off,
use_unset_local: WarningMode::Off,
use_env: WarningMode::Off,
local_same_as_global: WarningMode::Off,
class_overwrite: WarningMode::Off,
unreachable: WarningMode::Off,
}
}
pub fn the_mode_of(&self, kind: WarningType) -> WarningMode {
match kind {
WarningType::All => WarningMode::Off,
WarningType::VarUnset => self.var_unset,
WarningType::UseUnsetGlobal => self.use_unset_global,
WarningType::UseUnsetLocal => self.use_unset_local,
WarningType::UseEnv => self.use_env,
WarningType::LocalSameAsGlobal => self.local_same_as_global,
WarningType::ClassOverwrite => self.class_overwrite,
WarningType::Unreachable => self.unreachable,
}
}
pub fn write(&mut self, kind: WarningType, mode: WarningMode) {
match kind {
WarningType::All => {
self.var_unset = mode;
self.use_unset_global = mode;
self.use_unset_local = mode;
self.use_env = mode;
self.local_same_as_global = mode;
self.class_overwrite = mode;
self.unreachable = mode;
}
WarningType::VarUnset => self.var_unset = mode,
WarningType::UseUnsetGlobal => self.use_unset_global = mode,
WarningType::UseUnsetLocal => self.use_unset_local = mode,
WarningType::UseEnv => self.use_env = mode,
WarningType::LocalSameAsGlobal => self.local_same_as_global = mode,
WarningType::ClassOverwrite => self.class_overwrite = mode,
WarningType::Unreachable => self.unreachable = mode,
}
}
}
pub fn the_types_that_are_carried() -> &'static [WarningType] {
&[
WarningType::VarUnset,
WarningType::UseUnsetGlobal,
WarningType::UseUnsetLocal,
WarningType::UseEnv,
WarningType::LocalSameAsGlobal,
WarningType::ClassOverwrite,
WarningType::Unreachable,
]
}
pub fn the_name_of_the_type(kind: WarningType) -> &'static str {
match kind {
WarningType::All => "All",
WarningType::VarUnset => "VarUnset",
WarningType::UseUnsetGlobal => "UseUnsetGlobal",
WarningType::UseUnsetLocal => "UseUnsetLocal",
WarningType::UseEnv => "UseEnv",
WarningType::LocalSameAsGlobal => "LocalSameAsGlobal",
WarningType::ClassOverwrite => "ClassOverwrite",
WarningType::Unreachable => "Unreachable",
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Warning {
pub line: u32,
pub message: String,
}
pub fn the_kind_and_the_mode(
argument: &str,
dialect: &dyn Dialect,
) -> (Option<WarningType>, Option<WarningMode>) {
let (kind, mode) = the_parts_of_the_argument(argument);
let kind = if kind.is_empty() {
None
} else {
dialect.the_type_of_the_warning(kind)
};
let mode = if mode.is_empty() {
None
} else {
dialect.the_mode_of_the_warning(mode)
};
(kind, mode)
}
pub fn the_parts_of_the_argument(argument: &str) -> (&str, &str) {
match argument.split_once(',') {
Some((kind, mode)) => (kind.trim(), mode.trim()),
None => (argument.trim(), ""),
}
}
pub fn the_modes_of_the_script(statements: &[Node], dialect: &dyn Dialect) -> TheModes {
let mut modes = dialect.the_default_warnings();
the_modes_of_the_level(statements, &mut modes, dialect);
modes
}
fn the_modes_of_the_level(statements: &[Node], modes: &mut TheModes, dialect: &dyn Dialect) {
for statement in statements {
if let Node::Directive { name, argument, .. } = statement {
if name.eq_ignore_ascii_case("warn") {
let (kind, mode) = the_kind_and_the_mode(argument, dialect);
if let Some(kind) = kind {
if let Some(mode) = mode {
modes.write(kind, mode);
}
}
}
}
for body in the_bodies_of(statement) {
the_modes_of_the_level(body, modes, dialect);
}
}
}
pub fn the_types_of_the_script(statements: &[Node], dialect: &dyn Dialect) -> Vec<WarningType> {
let mut types = Vec::new();
the_types_of_the_level(statements, &mut types, dialect);
types
}
fn the_types_of_the_level(
statements: &[Node],
types: &mut Vec<WarningType>,
dialect: &dyn Dialect,
) {
for statement in statements {
if let Node::Directive { name, argument, .. } = statement {
if name.eq_ignore_ascii_case("warn") {
let (kind, _) = the_kind_and_the_mode(argument, dialect);
if let Some(kind) = kind {
let kinds = if kind == WarningType::All {
dialect.the_types_of_the_warning().to_vec()
} else {
vec![kind]
};
for kind in kinds {
if !types.contains(&kind) {
types.push(kind);
}
}
}
}
}
for body in the_bodies_of(statement) {
the_types_of_the_level(body, types, dialect);
}
}
}
pub fn the_warnings_of_the_script(statements: &[Node]) -> Vec<Warning> {
let mut warnings = Vec::new();
the_warnings_of_the_level(statements, &mut warnings);
warnings
}
fn the_warnings_of_the_level(statements: &[Node], warnings: &mut Vec<Warning>) {
let mut after: Option<&'static str> = None;
for statement in statements {
if let Some(word) = after {
after = None;
if !matches!(statement, Node::Label { .. }) {
warnings.push(Warning {
line: statement.line(),
message: format!("This line will never execute, due to {word} preceding it."),
});
}
}
if !matches!(statement, Node::Label { .. }) {
after = the_word_that_leaves_the_level(statement);
}
for body in the_bodies_of(statement) {
the_warnings_of_the_level(body, warnings);
}
}
}
fn the_word_that_leaves_the_level(statement: &Node) -> Option<&'static str> {
match statement {
Node::Break { .. } => Some("Break"),
Node::Continue { .. } => Some("Continue"),
Node::Return { .. } => Some("Return"),
Node::Throw { .. } => Some("Throw"),
Node::Goto { .. } => Some("Goto"),
_ => None,
}
}
fn the_bodies_of(statement: &Node) -> Vec<&[Node]> {
match statement {
Node::Block { body, .. } => vec![body],
Node::If {
branches,
otherwise,
..
} => {
let mut bodies: Vec<&[Node]> = branches
.iter()
.map(|branch| branch.body.as_slice())
.collect();
bodies.push(otherwise);
bodies
}
Node::Switch { cases, .. } => cases.iter().map(|case| case.body.as_slice()).collect(),
Node::While { body, .. } => vec![body],
Node::Loop { body, .. } => vec![body],
Node::For { body, .. } => vec![body],
Node::FunctionDef { body, .. } => vec![body],
Node::Try {
body,
catches,
otherwise,
finally,
..
} => {
let mut bodies: Vec<&[Node]> = vec![body, otherwise, finally];
bodies.extend(catches.iter().map(|catch| catch.body.as_slice()));
bodies
}
Node::ClassDef {
methods, nested, ..
} => {
let mut bodies: Vec<&[Node]> = methods
.iter()
.map(|method| method.body.as_slice())
.collect();
for class in nested {
bodies.extend(the_bodies_of(class));
}
bodies
}
Node::LegacyIf {
body, otherwise, ..
} => vec![body, otherwise],
Node::HotkeyDefinition { body, .. } => vec![body],
_ => Vec::new(),
}
}
pub fn the_warnings_of_the_locals_that_are_globals(
statements: &[Node],
dialect: &dyn Dialect,
) -> Vec<Warning> {
let globals = the_globals_of_the_script(statements);
let mut warnings = Vec::new();
the_warnings_of_the_locals_of_the_level(statements, &globals, dialect, &mut warnings);
warnings
}
fn the_warnings_of_the_locals_of_the_level(
statements: &[Node],
globals: &BTreeSet<String>,
dialect: &dyn Dialect,
warnings: &mut Vec<Warning>,
) {
for statement in statements {
match statement {
Node::FunctionDef {
name,
parameters,
body,
declarations,
body_line,
..
} => {
let scope = TheScope {
name,
parameters,
body,
declarations,
body_line: *body_line,
};
the_warnings_of_the_locals_of_a_scope(scope, globals, dialect, warnings);
}
Node::ClassDef {
name,
methods,
accessors,
nested,
..
} => {
the_warnings_of_the_locals_of_a_class(
name, methods, accessors, nested, globals, dialect, warnings,
);
}
_ => {}
}
for body in the_bodies_of_the_level(statement) {
the_warnings_of_the_locals_of_the_level(body, globals, dialect, warnings);
}
}
}
struct TheScope<'a> {
name: &'a str,
parameters: &'a [crate::ast::Parameter],
body: &'a [Node],
declarations: &'a crate::ast::Declarations,
body_line: u32,
}
fn the_warnings_of_the_locals_of_a_scope(
scope: TheScope<'_>,
globals: &BTreeSet<String>,
dialect: &dyn Dialect,
warnings: &mut Vec<Warning>,
) {
let TheScope {
name,
parameters,
body,
declarations,
body_line,
} = scope;
let mut writes = Vec::new();
the_writes_of_a_level(body, &mut writes, false);
let mut locals = writes;
if dialect.the_scope_of_a_function_is_its_own() {
let mut reads = Vec::new();
the_reads_of_a_level(body, &mut reads);
locals.extend(reads);
locals.sort_by_key(|(_, line)| *line);
}
let mut seen = BTreeSet::new();
for (local, the_line) in locals {
if !seen.insert(local.to_lowercase()) {
continue;
}
if declarations
.globals
.iter()
.any(|name| name.to_lowercase() == local.to_lowercase())
{
continue;
}
if parameters
.iter()
.any(|parameter| parameter.name.to_lowercase() == local.to_lowercase())
{
continue;
}
if !globals.contains(&local.to_lowercase()) {
continue;
}
if crate::variables::is_built_in(&local) {
continue;
}
let line = if dialect.the_scope_of_a_function_is_its_own() {
body_line
} else {
the_line
};
warnings.push(Warning {
line,
message: format!(
"This local variable has the same name as a global variable.\n Specifically: {local} (in function {name})"
),
});
}
}
fn the_warnings_of_the_locals_of_a_class(
path: &str,
methods: &[crate::ast::MethodDef],
accessors: &[crate::ast::AccessDef],
nested: &[Node],
globals: &BTreeSet<String>,
dialect: &dyn Dialect,
warnings: &mut Vec<Warning>,
) {
for method in methods {
let name = dialect.the_name_of_a_member_of_a_class(path, &method.name, method.is_static);
let scope = TheScope {
name: &name,
parameters: &method.parameters,
body: &method.body,
declarations: &method.declarations,
body_line: method.body_line,
};
the_warnings_of_the_locals_of_a_scope(scope, globals, dialect, warnings);
}
for accessor in accessors {
for (kind, body) in [("get", &accessor.getter), ("set", &accessor.setter)] {
let Some(body) = body else { continue };
let name = dialect.the_name_of_a_member_of_a_class(
path,
&format!("{}.{kind}", accessor.name),
accessor.is_static,
);
let scope = TheScope {
name: &name,
parameters: &accessor.parameters,
body: &body.body,
declarations: &body.declarations,
body_line: body.line,
};
the_warnings_of_the_locals_of_a_scope(scope, globals, dialect, warnings);
}
}
for class in nested {
if let Node::ClassDef {
name,
methods,
accessors,
nested,
..
} = class
{
let path = format!("{path}.{name}");
the_warnings_of_the_locals_of_a_class(
&path, methods, accessors, nested, globals, dialect, warnings,
);
}
}
}
fn the_reads_of_a_level(statements: &[Node], reads: &mut Vec<(String, u32)>) {
for statement in statements {
the_reads_of_a_node(statement, reads);
for body in the_bodies_of_the_level(statement) {
the_reads_of_a_level(body, reads);
}
}
}
fn the_reads_of_a_node(node: &Node, reads: &mut Vec<(String, u32)>) {
match node {
Node::Literal { .. } => {}
Node::Var { name, line } => reads.push((name.clone(), *line)),
Node::Unary { operand, .. } => the_reads_of_a_node(operand, reads),
Node::Binary { left, right, .. } => {
the_reads_of_a_node(left, reads);
the_reads_of_a_node(right, reads);
}
Node::Unset { .. } => {}
Node::Ternary {
condition,
then,
otherwise,
..
} => {
the_reads_of_a_node(condition, reads);
the_reads_of_a_node(then, reads);
the_reads_of_a_node(otherwise, reads);
}
Node::DynamicName { pieces, .. } => {
for piece in pieces {
the_reads_of_a_piece(piece, reads);
}
}
Node::AssignDynamic { name, value, .. } => {
for piece in name {
the_reads_of_a_piece(piece, reads);
}
the_reads_of_a_node(value, reads);
}
Node::Assign { value, .. } => the_reads_of_a_node(value, reads),
Node::LegacyAssign { text, line, .. } => the_names_of_a_text(text, *line, reads),
Node::LegacyComparison {
left, text, line, ..
} => {
the_reads_of_a_node(left, reads);
the_names_of_a_text(text, *line, reads);
}
Node::LegacyMatch {
left, texts, line, ..
} => {
the_reads_of_a_node(left, reads);
for text in texts {
the_names_of_a_text(text, *line, reads);
}
}
Node::LegacyControl { condition, .. } => the_reads_of_a_node(condition, reads),
Node::Index { target, index, .. } => {
the_reads_of_a_node(target, reads);
the_reads_of_a_node(index, reads);
}
Node::Call { arguments, .. } => {
for argument in arguments {
the_reads_of_a_node(argument, reads);
}
}
Node::New { arguments, .. } => {
for argument in arguments {
the_reads_of_a_node(argument, reads);
}
}
Node::LegacyCall {
parameters, line, ..
} => {
for parameter in parameters {
match parameter {
crate::ast::LegacyParameter::Text(text) => {
the_names_of_a_text(text, *line, reads)
}
crate::ast::LegacyParameter::Expression(value) => {
the_reads_of_a_node(value, reads)
}
}
}
}
Node::LegacyIf {
parameters, line, ..
} => {
for text in parameters {
the_names_of_a_text(text, *line, reads);
}
}
Node::Expression { value, .. } => the_reads_of_a_node(value, reads),
Node::LegacyExpression { value, .. } => the_reads_of_a_node(value, reads),
Node::Block { .. } => {}
Node::If { branches, .. } => {
for branch in branches {
the_reads_of_a_node(&branch.condition, reads);
for item in &branch.tail {
the_reads_of_a_node(item, reads);
}
}
}
Node::Switch { subject, cases, .. } => {
the_reads_of_a_node(subject, reads);
for case in cases {
for value in &case.values {
the_reads_of_a_node(value, reads);
}
}
}
Node::While { condition, .. } => the_reads_of_a_node(condition, reads),
Node::Loop {
arguments, until, ..
} => {
for argument in arguments {
the_reads_of_a_node(argument, reads);
}
if let Some(until) = until {
the_reads_of_a_node(until, reads);
}
}
Node::Directive { .. } => {}
Node::Break { .. } => {}
Node::Continue { .. } => {}
Node::Return { value, .. } => {
if let Some(value) = value {
the_reads_of_a_node(value, reads);
}
}
Node::ExitApp { code, .. } => {
if let Some(code) = code {
the_reads_of_a_node(code, reads);
}
}
Node::FunctionDef { .. } => {}
Node::ClassDef { .. } => {}
Node::MethodCall {
target, arguments, ..
} => {
the_reads_of_a_node(target, reads);
for argument in arguments {
the_reads_of_a_node(argument, reads);
}
}
Node::Property { target, name, .. } => {
the_reads_of_a_node(target, reads);
the_reads_of_a_member_name(name, reads);
}
Node::AssignProperty {
target,
name,
value,
..
} => {
the_reads_of_a_node(target, reads);
the_reads_of_a_member_name(name, reads);
the_reads_of_a_node(value, reads);
}
Node::AssignIndex {
target,
index,
value,
..
} => {
the_reads_of_a_node(target, reads);
the_reads_of_a_node(index, reads);
the_reads_of_a_node(value, reads);
}
Node::ArrayLiteral { items, .. } => {
for item in items.iter().flatten() {
the_reads_of_a_node(item, reads);
}
}
Node::Group { items, .. } => {
for item in items {
the_reads_of_a_node(item, reads);
}
}
Node::Empty { .. } => {}
Node::Legacy { items, .. } => {
for item in items {
the_reads_of_a_node(item, reads);
}
}
Node::ObjectLiteral { items, .. } => {
for item in items {
match item {
crate::ast::ObjectItem::Property(name, value) => {
the_reads_of_a_member_name(name, reads);
the_reads_of_a_node(value, reads);
}
crate::ast::ObjectItem::LegacyKey(key, value) => {
the_reads_of_a_node(key, reads);
the_reads_of_a_node(value, reads);
}
crate::ast::ObjectItem::Value(value) => the_reads_of_a_node(value, reads),
}
}
}
Node::Lambda { body, .. } => the_reads_of_a_node(body, reads),
Node::Try { .. } => {}
Node::Throw { value, .. } => {
if let Some(value) = value {
the_reads_of_a_node(value, reads);
}
}
Node::Declare { .. } => {}
Node::Super { .. } => {}
Node::For { source, .. } => the_reads_of_a_node(source, reads),
Node::Label { .. } => {}
Node::Goto { .. } => {}
Node::HotkeyDefinition { .. } => {}
}
}
fn the_reads_of_a_piece(piece: &crate::ast::DynamicPiece, reads: &mut Vec<(String, u32)>) {
if let crate::ast::DynamicPiece::Value(value) = piece {
the_reads_of_a_node(value, reads);
}
}
fn the_reads_of_a_member_name(name: &crate::ast::MemberName, reads: &mut Vec<(String, u32)>) {
if let crate::ast::MemberName::Dynamic(pieces) = name {
for piece in pieces {
the_reads_of_a_piece(piece, reads);
}
}
}
fn the_names_of_a_text(text: &str, line: u32, reads: &mut Vec<(String, u32)>) {
let chars: Vec<char> = text.chars().collect();
let mut index = 0usize;
while index < chars.len() {
if chars[index] == '`' {
index += 2;
continue;
}
if chars[index] == '%' {
if let Some(close) = the_closing_percent(&chars, index + 1) {
let name: String = chars[index + 1..close].iter().collect();
reads.push((name.trim().to_string(), line));
index = close + 1;
continue;
}
}
index += 1;
}
}
fn the_closing_percent(chars: &[char], from: usize) -> Option<usize> {
let mut index = from;
while index < chars.len() {
match chars[index] {
'`' => index += 2,
'%' => return Some(index),
_ => index += 1,
}
}
None
}
type TheNames = BTreeSet<String>;
pub fn the_warnings_of_the_variables_that_are_never_written(statements: &[Node]) -> Vec<Warning> {
let mut warnings = Vec::new();
let mut globals = TheNames::new();
the_warnings_of_the_reads_of_the_script(statements, &mut globals, &mut warnings);
warnings
}
fn the_warnings_of_the_reads_of_the_script(
statements: &[Node],
globals: &mut TheNames,
warnings: &mut Vec<Warning>,
) {
let mut writes = Vec::new();
the_writes_of_a_level(statements, &mut writes, false);
for (name, _) in writes {
globals.insert(name.to_lowercase());
}
let empty = BTreeSet::new();
for statement in statements {
match statement {
Node::FunctionDef {
body,
declarations,
parameters,
..
} => {
let mut own = TheNames::new();
for parameter in parameters {
own.insert(parameter.name.to_lowercase());
}
let mut writes = Vec::new();
the_writes_of_a_level(body, &mut writes, false);
for (name, _) in writes {
own.insert(name.to_lowercase());
}
let mut reads = Vec::new();
the_reads_of_a_level(body, &mut reads);
the_warnings_of_the_reads_of_a_scope(
&reads,
&mut own,
Some(&mut *globals),
&declarations.globals,
false,
warnings,
);
}
Node::ClassDef {
properties,
methods,
accessors,
nested,
..
} => {
let mut own = TheNames::new();
own.insert("this".to_string());
let mut reads = Vec::new();
for property in properties {
if let Some(value) = &property.value {
the_reads_of_a_node(value, &mut reads);
}
}
the_warnings_of_the_reads_of_a_scope(
&reads,
&mut own,
Some(&mut *globals),
&empty,
false,
warnings,
);
for method in methods {
let mut own = TheNames::new();
for parameter in &method.parameters {
own.insert(parameter.name.to_lowercase());
}
own.insert("this".to_string());
let mut writes = Vec::new();
the_writes_of_a_level(&method.body, &mut writes, false);
for (name, _) in writes {
own.insert(name.to_lowercase());
}
let mut reads = Vec::new();
the_reads_of_a_level(&method.body, &mut reads);
the_warnings_of_the_reads_of_a_scope(
&reads,
&mut own,
Some(&mut *globals),
&method.declarations.globals,
false,
warnings,
);
}
for accessor in accessors {
for (body, is_the_setter) in [
(accessor.getter.as_ref(), false),
(accessor.setter.as_ref(), true),
] {
let Some(body) = body else { continue };
let mut own = TheNames::new();
for parameter in &accessor.parameters {
own.insert(parameter.name.to_lowercase());
}
own.insert("this".to_string());
if is_the_setter {
own.insert("value".to_string());
}
let mut writes = Vec::new();
the_writes_of_a_level(&body.body, &mut writes, false);
for (name, _) in writes {
own.insert(name.to_lowercase());
}
let mut reads = Vec::new();
the_reads_of_a_level(&body.body, &mut reads);
the_warnings_of_the_reads_of_a_scope(
&reads,
&mut own,
Some(&mut *globals),
&body.declarations.globals,
false,
warnings,
);
}
}
for class in nested {
if let Node::ClassDef { .. } = class {
the_warnings_of_the_reads_of_the_script(
core::slice::from_ref(class),
globals,
warnings,
);
}
}
}
_ => {
let mut reads = Vec::new();
the_reads_of_a_level(core::slice::from_ref(statement), &mut reads);
the_warnings_of_the_reads_of_a_scope(&reads, globals, None, &empty, true, warnings);
}
}
}
}
fn the_warnings_of_the_reads_of_a_scope(
reads: &[(String, u32)],
own: &mut TheNames,
mut globals: Option<&mut TheNames>,
declared_global: &BTreeSet<String>,
the_script: bool,
warnings: &mut Vec<Warning>,
) {
for (name, line) in reads {
let key = name.to_lowercase();
if own.contains(&key) {
continue;
}
if crate::variables::is_built_in(name) {
continue;
}
if let Some(globals) = globals.as_deref_mut() {
if globals.contains(&key) {
continue;
}
}
let is_global = the_script || declared_global.contains(&key);
let message = if is_global {
format!(
"This global variable appears to never be assigned a value.\n Specifically: {name}"
)
} else {
format!(
"This local variable appears to never be assigned a value.\n Specifically: {name}"
)
};
match globals.as_deref_mut() {
Some(globals) if is_global => globals.insert(key),
_ => own.insert(key),
};
warnings.push(Warning {
line: *line,
message,
});
}
}
pub fn the_warnings_of_the_classes_that_are_overwritten(statements: &[Node]) -> Vec<Warning> {
let mut classes = BTreeSet::new();
the_classes_of_the_script(statements, &mut classes);
let mut warnings = Vec::new();
let mut writes = Vec::new();
the_writes_of_a_level(statements, &mut writes, true);
for (name, line) in writes {
if !classes.contains(&name.to_lowercase()) {
continue;
}
warnings.push(Warning {
line,
message: format!("Class may be overwritten.\n Specifically: {name}"),
});
}
warnings
}
fn the_globals_of_the_script(statements: &[Node]) -> BTreeSet<String> {
let mut globals = BTreeSet::new();
let mut writes = Vec::new();
the_writes_of_a_level(statements, &mut writes, false);
for (name, _line) in writes {
globals.insert(name.to_lowercase());
}
let mut classes = BTreeSet::new();
the_classes_of_the_script(statements, &mut classes);
for name in classes {
globals.insert(name);
}
globals
}
fn the_classes_of_the_script(
statements: &[Node],
classes: &mut BTreeSet<String>,
) {
for statement in statements {
if let Node::ClassDef { name, .. } = statement {
classes.insert(name.to_lowercase());
}
for body in the_bodies_of_the_level(statement) {
the_classes_of_the_script(body, classes);
}
}
}
fn the_writes_of_a_level(statements: &[Node], writes: &mut Vec<(String, u32)>, deep: bool) {
for statement in statements {
match statement {
Node::Assign { name, line, .. } => writes.push((name.clone(), *line)),
Node::LegacyAssign { name, line, .. } => writes.push((name.clone(), *line)),
Node::For {
variables, line, ..
} => {
for name in variables {
writes.push((name.clone(), *line));
}
}
Node::LegacyCall {
name,
parameters,
line,
} if name.eq_ignore_ascii_case("VarSet") => {
if let Some(crate::ast::LegacyParameter::Text(text)) = parameters.first() {
if !text.contains('%') {
writes.push((text.clone(), *line));
}
}
}
_ => {}
}
let bodies = if deep {
the_bodies_of(statement)
} else {
the_bodies_of_the_level(statement)
};
for body in bodies {
the_writes_of_a_level(body, writes, deep);
}
}
}
fn the_bodies_of_the_level(statement: &Node) -> Vec<&[Node]> {
match statement {
Node::Block { body, .. } => vec![body],
Node::If {
branches,
otherwise,
..
} => {
let mut bodies: Vec<&[Node]> = branches
.iter()
.map(|branch| branch.body.as_slice())
.collect();
bodies.push(otherwise);
bodies
}
Node::Switch { cases, .. } => cases.iter().map(|case| case.body.as_slice()).collect(),
Node::While { body, .. } => vec![body],
Node::Loop { body, .. } => vec![body],
Node::For { body, .. } => vec![body],
Node::Try {
body,
catches,
otherwise,
finally,
..
} => {
let mut bodies: Vec<&[Node]> = vec![body, otherwise, finally];
bodies.extend(catches.iter().map(|catch| catch.body.as_slice()));
bodies
}
Node::LegacyIf {
body, otherwise, ..
} => vec![body, otherwise],
Node::HotkeyDefinition { body, .. } => vec![body],
_ => Vec::new(),
}
}