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

Method parse_alias

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

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

(&mut self, style: ImportStyle)

Source from the content-addressed store, hash-verified

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

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