• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vcfxb / wright-lang / 14120649496

28 Mar 2025 02:25AM UTC coverage: 74.175% (-0.9%) from 75.076%
14120649496

push

github

vcfxb
nit: remove unused import

1011 of 1363 relevant lines covered (74.17%)

29.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/wright/src/parser/decl/import.rs
1
//! Parser implementation for `use path::to::thing;` declaration.
2

3
use crate::{
4
    ast::{decl::import::ImportDecl, identifier::Identifier, path::Path},
5
    lexer::token::{Token, TokenTy},
6
    parser::{
7
        Parser,
8
        error::{ParserError, ParserErrorKind},
9
        whitespace,
10
    },
11
    source_tracking::fragment::Fragment,
12
};
13

14
impl ImportDecl {
15
    /// Parse an import declaration.
16
    ///
17
    /// This will advance the parser if `use` is seen -- if a valid formed import does not follow,
18
    /// the parser may be left in the middle of a malformed declaration.
19
    pub fn parse(parser: &mut Parser) -> Result<Self, ParserError> {
4✔
20
        let use_kw: Token = parser.next_if_is(TokenTy::KwUse).ok_or(
4✔
21
            ParserErrorKind::ExpectedImportDeclaration.at(parser.peek_fragment_or_rest_cloned()),
4✔
22
        )?;
4✔
23

24
        // Require a whitespace after the keyword.
25
        whitespace::require_whitespace(parser)?;
4✔
26
        // Parse the path.
27
        let path: Path = Path::parse(parser)?;
4✔
28

29
        // Whitespace and then "as ...;" or optional whitespace and semi ";".
30

31
        // The "as ...;" requires a whitespace.
32
        let imported_as = if parser.matches(&[TokenTy::Whitespace, TokenTy::KwAs]) {
4✔
33
            parser.advance(2);
2✔
34

2✔
35
            whitespace::require_whitespace(parser)
2✔
36
                .map_err(|e| e.with_help("whitespace needed between \"as\" and binding."))?;
2✔
37

38
            let imported_as = Identifier::parse(parser)
2✔
39
                .map_err(|e| e.with_help("expected binding in \"use ... as\" declaration."))?;
2✔
40

41
            Some(imported_as)
2✔
42
        } else {
43
            None
2✔
44
        };
45

46
        whitespace::optional_whitespace(parser);
4✔
47

48
        if let Some(semi) = parser.next_if_is(TokenTy::Semi) {
4✔
49
            Ok(ImportDecl {
4✔
50
                matching_source: Fragment::cover(use_kw.fragment, semi.fragment),
4✔
51
                imported_item: path,
4✔
52
                imported_as,
4✔
53
            })
4✔
54
        } else {
55
            Err(ParserErrorKind::ImportMustEndWithSemicolon
×
56
                .at(parser.peek_fragment_or_rest_cloned()))
×
57
        }
58
    }
4✔
59
}
60

61
#[cfg(test)]
62
mod tests {
63
    use crate::{ast::decl::import::ImportDecl, lexer::Lexer, parser::Parser};
64

65
    #[test]
66
    fn test_import() {
1✔
67
        let mut parser = Parser::new(Lexer::new_test("use wright::util;"));
1✔
68
        let import_decl = ImportDecl::parse(&mut parser).unwrap();
1✔
69
        assert!(parser.lexer.remaining.is_empty());
1✔
70
        assert_eq!(import_decl.imported_item.head.fragment.as_str(), "wright");
1✔
71
        assert_eq!(import_decl.imported_item.tail[0].fragment.as_str(), "util");
1✔
72
    }
1✔
73

74
    #[test]
75
    fn test_import_with_whitespace() {
1✔
76
        let mut parser = Parser::new(Lexer::new_test("use wright :: util ;"));
1✔
77
        let import_decl = ImportDecl::parse(&mut parser).unwrap();
1✔
78
        assert!(parser.lexer.remaining.is_empty());
1✔
79
        assert_eq!(import_decl.imported_item.head.fragment.as_str(), "wright");
1✔
80
        assert_eq!(import_decl.imported_item.tail[0].fragment.as_str(), "util");
1✔
81
    }
1✔
82

83
    #[test]
84
    fn test_import_as() {
1✔
85
        let mut parser = Parser::new(Lexer::new_test("use wright::util as u;"));
1✔
86
        let import_decl = ImportDecl::parse(&mut parser).unwrap();
1✔
87
        assert!(parser.lexer.remaining.is_empty());
1✔
88
        assert_eq!(import_decl.imported_item.head.fragment.as_str(), "wright");
1✔
89
        assert_eq!(import_decl.imported_item.tail[0].fragment.as_str(), "util");
1✔
90
        assert_eq!(import_decl.imported_as.unwrap().fragment.as_str(), "u");
1✔
91
    }
1✔
92

93
    #[test]
94
    fn test_import_as_with_comment() {
1✔
95
        let mut parser = Parser::new(Lexer::new_test("use wright::util as /* old_name */ u;"));
1✔
96
        let import_decl = ImportDecl::parse(&mut parser).unwrap();
1✔
97
        assert!(parser.lexer.remaining.is_empty());
1✔
98
        assert_eq!(import_decl.imported_item.head.fragment.as_str(), "wright");
1✔
99
        assert_eq!(import_decl.imported_item.tail[0].fragment.as_str(), "util");
1✔
100
        assert_eq!(import_decl.imported_as.unwrap().fragment.as_str(), "u");
1✔
101
    }
1✔
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc