leo_passes/name_validation/
visitor.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use leo_ast::*;
18use leo_errors::{Handler, NameValidationError};
19use snarkvm::prelude::{Program, TestnetV0};
20
21pub struct NameValidationVisitor<'a> {
22    pub handler: &'a mut Handler,
23}
24
25impl NameValidationVisitor<'_> {
26    pub fn does_not_contain_aleo(&self, name: Identifier, item_type: &str) {
27        if name.to_string().contains("aleo") {
28            self.handler.emit_err(NameValidationError::illegal_name_content(name, item_type, "aleo", name.span));
29        }
30    }
31
32    pub fn is_not_keyword(&self, name: Identifier, item_type: &str, whitelist: &[&str]) {
33        // Flatten RESTRICTED_KEYWORDS by ignoring ConsensusVersion
34        let restricted = Program::<TestnetV0>::RESTRICTED_KEYWORDS.iter().flat_map(|(_, kws)| kws.iter().copied());
35        let keywords = Program::<TestnetV0>::KEYWORDS.iter().copied();
36        let aleo = std::iter::once("aleo");
37
38        let it = keywords.chain(restricted).chain(aleo).filter(|w| !whitelist.contains(w));
39
40        for word in it {
41            if name.to_string() == word {
42                self.handler.emit_err(NameValidationError::illegal_name(name, item_type, word, name.span));
43                break;
44            }
45        }
46    }
47}