MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_alias

Method parse_alias

crates/ruff_python_parser/src/parser/statement.rs:776–824  ·  view source on GitHub ↗

Parses an `import` or `from` import name. See: - -

(&mut self, style: ImportStyle)

Source from the content-addressed store, hash-verified

774 /// - <https://docs.python.org/3/reference/simple_stmts.html#the-import-statement>
775 /// - <https://docs.python.org/3/library/ast.html#ast.alias>
776 fn parse_alias(&mut self, style: ImportStyle) -> ast::Alias {
777 let start = self.node_start();
778 if self.eat(TokenKind::Star) {
779 let range = self.node_range(start);
780 return ast::Alias {
781 name: ast::Identifier {
782 id: Name::new_static("*"),
783 range,
784 node_index: AtomicNodeIndex::NONE,
785 },
786 asname: None,
787 range,
788 node_index: AtomicNodeIndex::NONE,
789 };
790 }
791
792 let name = match style {
793 ImportStyle::Import => self.parse_dotted_name(),
794 ImportStyle::ImportFrom => self.parse_identifier(),
795 };
796
797 let asname = if self.eat(TokenKind::As) {
798 if self.at_name_or_soft_keyword() {
799 // test_ok import_as_name_soft_keyword
800 // import foo as match
801 // import bar as case
802 // import baz as type
803 // import qux as lazy
804 Some(self.parse_identifier())
805 } else {
806 // test_err import_alias_missing_asname
807 // import x as
808 self.add_error(
809 ParseErrorType::OtherError("Expected symbol after `as`".to_string()),
810 self.current_token_range(),
811 );
812 None
813 }
814 } else {
815 None
816 };
817
818 ast::Alias {
819 range: self.node_range(start),
820 name,
821 asname,
822 node_index: AtomicNodeIndex::NONE,
823 }
824 }
825
826 /// Parses a dotted name.
827 ///

Callers 2

Calls 9

node_startMethod · 0.80
node_rangeMethod · 0.80
parse_dotted_nameMethod · 0.80
parse_identifierMethod · 0.80
current_token_rangeMethod · 0.80
eatMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected