Enabling `import_to_using` breaks `..` import in submodule by converting to `using`
rben01 opened this issue · comments
JuliaFormatter version: v1.0.16
Julia version: 1.8.2
Consider the following code:
julia> x = 1;
julia> module M
import ..x
y = x
end;
julia> M.x
1
This uses the ..
syntax to import a bare item (not scoped by module) from the parent module. import ..x
can be read as “from this module (the first dot), from its parent module (the second dot), import x
”.
When using JuliaFormatter to format the module's code, import_to_using
breaks the import when converting to using
.
julia> fmtd = JuliaFormatter.format_text("""
module M
import ..x
y = x
end""";
import_to_using=true);
julia> println(fmtd)
module M
using ..x: x
y = x
end
julia> eval(Meta.parse(fmtd))
WARNING: replacing module M.
ERROR: invalid using path: "x" does not name a module
Stacktrace:
[1] eval
@ ./boot.jl:368 [inlined]
AFAIK there is no valid way to rewrite this import
as a using
without naming the parent module, so even with import_to_using
enabled, JuliaFormatter should leave import ..x
unchanged.
As for how to achieve this: I do not know. I don't know how JuliaFormatter could differentiate between when x
is a module and when x
is some other kind of object without a deeper understanding of all of the relevant source code. Perhaps import
statements whose target begins with dots should be left alone, and only those whose target begins with a word character should be converted to a using
statement.
(As for why I don't simply use import_to_using=false
: I'm using JuliaFormatter in my IDE and generally do want it to convert import
to using
, and only in this somewhat oddball case where I import ..x
in a module do I want it to leave the import
alone.)
i think we can detect the dots fairly easily
Ok, so looking at this more this is not something that's going to be changed since there's no way without more information than just what's available from a CST to know if x is a module or not and the dot syntax is used for modules for removing that is not an option.