Skip to content

Commit

Permalink
vfmt,parser: keep the original import name in ast.Import, and use it …
Browse files Browse the repository at this point in the history
…without modifications for paths unders ~/.vmodules
  • Loading branch information
spytheman committed Jan 29, 2024
1 parent 2d68230 commit 804a7bd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/tools/vast/vast.v
Expand Up @@ -448,6 +448,7 @@ fn (t Tree) stmt(node ast.Stmt) &Node {
fn (t Tree) import_module(node ast.Import) &Node {
mut obj := new_object()
obj.add_terse('ast_type', t.string_node('Import'))
obj.add_terse('source_name', t.string_node(node.source_name))
obj.add_terse('mod', t.string_node(node.mod))
obj.add_terse('alias', t.string_node(node.alias))
obj.add_terse('syms', t.array_node_import_symbol(node.syms))
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/ast/ast.v
Expand Up @@ -498,6 +498,8 @@ pub enum StructInitKind {
// import statement
pub struct Import {
pub:
source_name string // The original name in the source, `import abc.def` -> 'abc.def', *no matter* how the module is resolved
//
mod string // the module name of the import
alias string // the `x` in `import xxx as x`
pos token.Pos
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/fmt/fmt.v
Expand Up @@ -3,6 +3,7 @@
// that can be found in the LICENSE file.
module fmt

import os
import strings
import v.ast
import v.util
Expand Down Expand Up @@ -53,6 +54,7 @@ pub mut:
wsinfix_depth int
format_state FormatState
source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.
inside_vmodules bool
}

@[params]
Expand All @@ -69,6 +71,13 @@ pub fn fmt(file ast.File, table &ast.Table, pref_ &pref.Preferences, is_debug bo
out: strings.new_builder(1000)
out_imports: strings.new_builder(200)
}
for vpath in os.vmodules_paths() {
if file.path.starts_with(vpath) {
f.inside_vmodules = true
break
}
}

f.source_text = options.source_text
f.process_file_imports(file)
f.set_current_module_name('main')
Expand Down Expand Up @@ -365,6 +374,9 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
}

pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
if f.inside_vmodules {
return imp.source_name
}
mod := if imp.mod.len == 0 { imp.alias } else { imp.mod }
normalized_mod := mod.all_after('src.') // Ignore the 'src.' folder prefix since src/ folder is root of code
is_diff := imp.alias != normalized_mod && !normalized_mod.ends_with('.' + imp.alias)
Expand Down Expand Up @@ -1945,6 +1957,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64']
&& !node.left.scope.known_var(node.left.name) {
f.file.imports << ast.Import{
source_name: node.left.name
mod: node.left.name
alias: node.left.name
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/module.v
Expand Up @@ -41,6 +41,7 @@ fn (mut p Parser) register_auto_import(alias string) {
p.imports[alias] = alias
p.table.imports << alias
node := ast.Import{
source_name: alias
pos: p.tok.pos()
mod: alias
alias: alias
Expand Down
11 changes: 9 additions & 2 deletions vlib/v/parser/parser.v
Expand Up @@ -3684,14 +3684,16 @@ fn (mut p Parser) import_stmt() ast.Import {
p.error_with_pos('`import()` has been deprecated, use `import x` instead', pos)
return import_node
}
mut source_name := p.check_name()
mut mod_name_arr := []string{}
mod_name_arr << p.check_name()
mod_name_arr << source_name
if import_pos.line_nr != pos.line_nr {
p.error_with_pos('`import` statements must be a single line', pos)
return import_node
}
mut mod_alias := mod_name_arr[0]
import_node = ast.Import{
source_name: source_name
pos: import_pos.extend(pos)
mod_pos: pos
alias_pos: pos
Expand All @@ -3711,16 +3713,19 @@ fn (mut p Parser) import_stmt() ast.Import {
mod_name_arr << submod_name
mod_alias = submod_name
pos = pos.extend(submod_pos)
source_name = mod_name_arr.join('.')
import_node = ast.Import{
source_name: source_name
pos: import_pos.extend(pos)
mod_pos: pos
alias_pos: submod_pos
mod: util.qualify_import(p.pref, mod_name_arr.join('.'), p.file_name)
mod: util.qualify_import(p.pref, source_name, p.file_name)
alias: mod_alias
}
}
if mod_name_arr.len == 1 {
import_node = ast.Import{
source_name: source_name
pos: import_node.pos
mod_pos: import_node.mod_pos
alias_pos: import_node.alias_pos
Expand All @@ -3739,6 +3744,7 @@ fn (mut p Parser) import_stmt() ast.Import {
return import_node
}
import_node = ast.Import{
source_name: source_name
pos: import_node.pos.extend(alias_pos)
mod_pos: import_node.mod_pos
alias_pos: alias_pos
Expand All @@ -3752,6 +3758,7 @@ fn (mut p Parser) import_stmt() ast.Import {
initial_syms_pos = initial_syms_pos.extend(p.tok.pos())
import_node = ast.Import{
...import_node
source_name: source_name
syms_pos: initial_syms_pos
pos: import_node.pos.extend(initial_syms_pos)
}
Expand Down

0 comments on commit 804a7bd

Please sign in to comment.