@@ -21,7 +21,7 @@ type ReturnStrategy =
21
21
type Import =
22
22
{
23
23
Module: string
24
- LocalIdent: Identifier option
24
+ LocalIdent: Identifier
25
25
Name: string option
26
26
}
27
27
@@ -4249,17 +4249,17 @@ module Util =
4249
4249
match im.Name with
4250
4250
| Some " *"
4251
4251
| Some " default" ->
4252
- let ( Identifier local ) = im.LocalIdent.Value
4252
+ let ( Identifier local ) = im.LocalIdent
4253
4253
4254
4254
if moduleName <> local then
4255
- Some moduleName, Alias.alias im.LocalIdent.Value
4255
+ Some moduleName, Alias.alias im.LocalIdent
4256
4256
else
4257
- None, Alias.alias im.LocalIdent.Value
4257
+ None, Alias.alias im.LocalIdent
4258
4258
| Some name ->
4259
4259
let name = Naming.toSnakeCase name
4260
4260
4261
- Some moduleName, Alias.alias ( Identifier( Helpers.clean name), ? asname = im.LocalIdent)
4262
- | None -> None, Alias.alias ( Identifier( moduleName), ? asname = im.LocalIdent)
4261
+ Some moduleName, Alias.alias ( Identifier( Helpers.clean name), asname = im.LocalIdent)
4262
+ | None -> None, Alias.alias ( Identifier( moduleName), asname = im.LocalIdent)
4263
4263
)
4264
4264
|> List.groupBy fst
4265
4265
|> List.map ( fun ( a , b ) -> a, List.map snd b)
@@ -4290,16 +4290,9 @@ module Util =
4290
4290
let getIdentForImport ( ctx : Context ) ( moduleName : string ) ( name : string option ) =
4291
4291
// printfn "getIdentForImport: %A" (moduleName, name)
4292
4292
match name with
4293
- | None -> Path.GetFileNameWithoutExtension( moduleName) |> Identifier |> Some
4294
- | Some name ->
4295
- match name with
4296
- | " default"
4297
- | " *" -> Path.GetFileNameWithoutExtension( moduleName)
4298
- | _ -> name
4299
- |> Naming.toSnakeCase
4300
- |> getUniqueNameInRootScope ctx
4301
- |> Identifier
4302
- |> Some
4293
+ | None -> Path.GetFileNameWithoutExtension( moduleName)
4294
+ | Some name -> name |> Naming.toSnakeCase |> getUniqueNameInRootScope ctx
4295
+ |> Identifier
4303
4296
4304
4297
module Compiler =
4305
4298
open Util
@@ -4317,47 +4310,59 @@ module Compiler =
4317
4310
4318
4311
member _.GetImportExpr ( ctx , moduleName , ? name , ? r ) =
4319
4312
// printfn "GetImportExpr: %A" (moduleName, name)
4320
- let cachedName = moduleName + " ::" + defaultArg name " module"
4313
+ let name =
4314
+ match name with
4315
+ | None
4316
+ | Some null -> " "
4317
+ | Some name -> name.Trim()
4318
+
4319
+ let isQualifiedPythonImport =
4320
+ match name with
4321
+ | " "
4322
+ | " default"
4323
+ | " *" -> false
4324
+ | _ -> true
4325
+
4326
+ let cachedName =
4327
+ moduleName
4328
+ + " ::"
4329
+ + if isQualifiedPythonImport then
4330
+ name
4331
+ else
4332
+ " "
4321
4333
4322
4334
match imports.TryGetValue( cachedName) with
4323
- | true , i ->
4324
- match i.LocalIdent with
4325
- | Some localIdent -> Expression.identifier localIdent
4326
- | None -> Expression.none
4335
+ | true , i -> i.LocalIdent |> Expression.identifier
4327
4336
| false , _ ->
4328
- let local_id = getIdentForImport ctx moduleName name
4329
-
4330
- match name with
4331
- | Some " *"
4332
- | None ->
4333
- let i =
4334
- {
4335
- Name = None
4336
- Module = moduleName
4337
- LocalIdent = local_ id
4338
- }
4339
-
4340
- imports.Add( cachedName, i)
4341
- | Some name ->
4342
- let i =
4343
- {
4344
- Name =
4345
- if name = Naming.placeholder then
4346
- " `importMember` must be assigned to a variable" |> addError com [] r
4347
-
4348
- name
4349
- else
4350
- name
4351
- |> Some
4352
- Module = moduleName
4353
- LocalIdent = local_ id
4354
- }
4355
-
4356
- imports.Add( cachedName, i)
4357
-
4358
- match local_ id with
4359
- | Some localId -> Expression.identifier localId
4360
- | None -> Expression.none
4337
+ let local_id =
4338
+ if isQualifiedPythonImport then
4339
+ Some name
4340
+ else
4341
+ None
4342
+ |> getIdentForImport ctx moduleName
4343
+
4344
+ let importName =
4345
+ match name with
4346
+ | _ when not isQualifiedPythonImport -> None
4347
+ | Naming.placeholder ->
4348
+ " `importMember` must be assigned to a variable" |> addError com [] r
4349
+ Some name
4350
+ | _ -> Some name
4351
+
4352
+ let i =
4353
+ {
4354
+ Name = importName
4355
+ Module = moduleName
4356
+ LocalIdent = local_ id
4357
+ }
4358
+
4359
+ imports.Add( cachedName, i)
4360
+
4361
+ // If the import member is empty we understand this is done for side-effects only
4362
+ if name = " " then
4363
+ Expression.none
4364
+ else
4365
+ Expression.identifier local_ id
4361
4366
4362
4367
member _.GetAllImports () =
4363
4368
imports.Values :> Import seq |> List.ofSeq
0 commit comments