Skip to content

AST Improvement: Store dotted name parts #7760

@konstin

Description

@konstin

Currently, the path of imports is not formatted, e.g.

import a . b

remains as-is. This is due to a bug in our AST:

/// A representation of an individual name imported via any import statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnyImport<'a> {
Import(Import<'a>),
ImportFrom(ImportFrom<'a>),
}
/// A representation of an individual name imported via an `import` statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Import<'a> {
pub name: Alias<'a>,
}
/// A representation of an individual name imported via a `from ... import` statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportFrom<'a> {
pub module: Option<&'a str>,
pub name: Alias<'a>,
pub level: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Alias<'a> {
pub name: &'a str,
pub as_name: Option<&'a str>,
}

The entire path is represented as a single string, even though it should be dot-separated identifier (the parser calls it DottedName, but then emits an identifier), especially since identifier can not contain dots.

  • Fix the AST so the import path is a Vec<Identifier> or something similar
  • Format import paths by removing whitespace (we can't insert parentheses like we do for dotted expressions)
  • Check that Identifier is only used for strings matching the rules

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserRelated to the parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions