Skip to content

Commit 804a7bd

Browse files
committed
vfmt,parser: keep the original import name in ast.Import, and use it without modifications for paths unders ~/.vmodules
1 parent 2d68230 commit 804a7bd

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

cmd/tools/vast/vast.v

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ fn (t Tree) stmt(node ast.Stmt) &Node {
448448
fn (t Tree) import_module(node ast.Import) &Node {
449449
mut obj := new_object()
450450
obj.add_terse('ast_type', t.string_node('Import'))
451+
obj.add_terse('source_name', t.string_node(node.source_name))
451452
obj.add_terse('mod', t.string_node(node.mod))
452453
obj.add_terse('alias', t.string_node(node.alias))
453454
obj.add_terse('syms', t.array_node_import_symbol(node.syms))

vlib/v/ast/ast.v

+2
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ pub enum StructInitKind {
498498
// import statement
499499
pub struct Import {
500500
pub:
501+
source_name string // The original name in the source, `import abc.def` -> 'abc.def', *no matter* how the module is resolved
502+
//
501503
mod string // the module name of the import
502504
alias string // the `x` in `import xxx as x`
503505
pos token.Pos

vlib/v/fmt/fmt.v

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// that can be found in the LICENSE file.
44
module fmt
55

6+
import os
67
import strings
78
import v.ast
89
import v.util
@@ -53,6 +54,7 @@ pub mut:
5354
wsinfix_depth int
5455
format_state FormatState
5556
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.
57+
inside_vmodules bool
5658
}
5759

5860
@[params]
@@ -69,6 +71,13 @@ pub fn fmt(file ast.File, table &ast.Table, pref_ &pref.Preferences, is_debug bo
6971
out: strings.new_builder(1000)
7072
out_imports: strings.new_builder(200)
7173
}
74+
for vpath in os.vmodules_paths() {
75+
if file.path.starts_with(vpath) {
76+
f.inside_vmodules = true
77+
break
78+
}
79+
}
80+
7281
f.source_text = options.source_text
7382
f.process_file_imports(file)
7483
f.set_current_module_name('main')
@@ -365,6 +374,9 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
365374
}
366375

367376
pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
377+
if f.inside_vmodules {
378+
return imp.source_name
379+
}
368380
mod := if imp.mod.len == 0 { imp.alias } else { imp.mod }
369381
normalized_mod := mod.all_after('src.') // Ignore the 'src.' folder prefix since src/ folder is root of code
370382
is_diff := imp.alias != normalized_mod && !normalized_mod.ends_with('.' + imp.alias)
@@ -1945,6 +1957,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
19451957
if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64']
19461958
&& !node.left.scope.known_var(node.left.name) {
19471959
f.file.imports << ast.Import{
1960+
source_name: node.left.name
19481961
mod: node.left.name
19491962
alias: node.left.name
19501963
}

vlib/v/parser/module.v

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fn (mut p Parser) register_auto_import(alias string) {
4141
p.imports[alias] = alias
4242
p.table.imports << alias
4343
node := ast.Import{
44+
source_name: alias
4445
pos: p.tok.pos()
4546
mod: alias
4647
alias: alias

vlib/v/parser/parser.v

+9-2
Original file line numberDiff line numberDiff line change
@@ -3684,14 +3684,16 @@ fn (mut p Parser) import_stmt() ast.Import {
36843684
p.error_with_pos('`import()` has been deprecated, use `import x` instead', pos)
36853685
return import_node
36863686
}
3687+
mut source_name := p.check_name()
36873688
mut mod_name_arr := []string{}
3688-
mod_name_arr << p.check_name()
3689+
mod_name_arr << source_name
36893690
if import_pos.line_nr != pos.line_nr {
36903691
p.error_with_pos('`import` statements must be a single line', pos)
36913692
return import_node
36923693
}
36933694
mut mod_alias := mod_name_arr[0]
36943695
import_node = ast.Import{
3696+
source_name: source_name
36953697
pos: import_pos.extend(pos)
36963698
mod_pos: pos
36973699
alias_pos: pos
@@ -3711,16 +3713,19 @@ fn (mut p Parser) import_stmt() ast.Import {
37113713
mod_name_arr << submod_name
37123714
mod_alias = submod_name
37133715
pos = pos.extend(submod_pos)
3716+
source_name = mod_name_arr.join('.')
37143717
import_node = ast.Import{
3718+
source_name: source_name
37153719
pos: import_pos.extend(pos)
37163720
mod_pos: pos
37173721
alias_pos: submod_pos
3718-
mod: util.qualify_import(p.pref, mod_name_arr.join('.'), p.file_name)
3722+
mod: util.qualify_import(p.pref, source_name, p.file_name)
37193723
alias: mod_alias
37203724
}
37213725
}
37223726
if mod_name_arr.len == 1 {
37233727
import_node = ast.Import{
3728+
source_name: source_name
37243729
pos: import_node.pos
37253730
mod_pos: import_node.mod_pos
37263731
alias_pos: import_node.alias_pos
@@ -3739,6 +3744,7 @@ fn (mut p Parser) import_stmt() ast.Import {
37393744
return import_node
37403745
}
37413746
import_node = ast.Import{
3747+
source_name: source_name
37423748
pos: import_node.pos.extend(alias_pos)
37433749
mod_pos: import_node.mod_pos
37443750
alias_pos: alias_pos
@@ -3752,6 +3758,7 @@ fn (mut p Parser) import_stmt() ast.Import {
37523758
initial_syms_pos = initial_syms_pos.extend(p.tok.pos())
37533759
import_node = ast.Import{
37543760
...import_node
3761+
source_name: source_name
37553762
syms_pos: initial_syms_pos
37563763
pos: import_node.pos.extend(initial_syms_pos)
37573764
}

0 commit comments

Comments
 (0)