Skip to content

Commit

Permalink
Add option for type-keyword imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lol768 committed Nov 6, 2023
1 parent bceb542 commit 668bb46
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ internal class TypeScriptImportDefaultFromPathStatement : TypeScriptImportStatem
{
public override string GenerateCode(ICodeGenerationContext context)
{
return $"import {TypeName} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';";
var typeKeyword = UseTypeKeyword ? "type " : "";
return $"import {typeKeyword}{TypeName} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';";
}

public string TypeName { get; set; }
public TypeScriptUnit CurrentUnit { get; set; }
public string PathToUnit { get; set; }
public bool UseTypeKeyword { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ internal class TypeScriptImportFromPathStatement : TypeScriptImportStatement
{
public override string GenerateCode(ICodeGenerationContext context)
{
return $"import {{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';";
var typeKeyword = UseTypeKeyword ? "type " : "";
return $"import {typeKeyword}{{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';";
}

public string TypeName { get; set; }
public TypeScriptUnit CurrentUnit { get; set; }
public string PathToUnit { get; set; }
public bool UseTypeKeyword { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ internal class TypeScriptImportFromUnitStatement : TypeScriptImportStatement
{
public override string GenerateCode(ICodeGenerationContext context)
{
return $"import {{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, TargetUnit.Path)}';";
var typeKeyword = UseTypeKeyword ? "type " : "";
return $"import {typeKeyword}{{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, TargetUnit.Path)}';";
}

public string TypeName { get; set; }
public TypeScriptUnit TargetUnit { get; set; }
public TypeScriptUnit CurrentUnit { get; set; }
public bool UseTypeKeyword { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public RedirectToTypeBuildingContext(string typeName, string path, ITypeInfo typ

protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
{
return targetUnit.AddTypeImport(type, new TypeScriptInterfaceDeclaration {Name = typeName}, new TypeScriptUnit {Path = path});
return targetUnit.AddTypeImport(type, new TypeScriptInterfaceDeclaration {Name = typeName}, new TypeScriptUnit {Path = path}, typeGenerator.Options.UseTypeImports);
}

private readonly string typeName;
private readonly string path;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void Initialize(ITypeGenerator typeGenerator)

protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
{
return targetUnit.AddTypeImport(type, Declaration, Unit);
return targetUnit.AddTypeImport(type, Declaration, Unit, typeGenerator.Options.UseTypeImports);
}
}
}
}
4 changes: 2 additions & 2 deletions TypeScript.ContractGenerator/TypeScriptGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class TypeScriptGenerationOptions
public bool EnableOptionalProperties { get; set; } = true;
public bool UseGlobalNullable { get; set; }
public string? CustomContentMarker { get; set; }

public bool UseTypeImports { get; set; } = false;
public static TypeScriptGenerationOptions Default => new TypeScriptGenerationOptions();
}
}
}
9 changes: 6 additions & 3 deletions TypeScript.ContractGenerator/TypeScriptUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TypeScriptUnit

public List<TypeScriptStatement> Body { get; } = new List<TypeScriptStatement>();

public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTypeDeclaration typeDeclaration, TypeScriptUnit sourceUnit)
public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTypeDeclaration typeDeclaration, TypeScriptUnit sourceUnit, bool useTypeKeyword=false)
{
if (sourceUnit != this && !imports.ContainsKey(sourceType))
{
Expand All @@ -24,6 +24,7 @@ public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTyp
TypeName = typeDeclaration.Name,
CurrentUnit = this,
TargetUnit = sourceUnit,
UseTypeKeyword = useTypeKeyword
});
}
return new TypeScriptTypeReference(typeDeclaration.Name);
Expand All @@ -39,12 +40,13 @@ public TypeScriptVariableReference AddSymbolImport(string symbolName, string pat
TypeName = symbolName,
CurrentUnit = this,
PathToUnit = path,
UseTypeKeyword = false,
});
}
return new TypeScriptVariableReference(symbolName);
}

public TypeScriptVariableReference AddDefaultSymbolImport(string localName, string path)
public TypeScriptVariableReference AddDefaultSymbolImport(string localName, string path, bool useTypeKeyword=false)
{
var importedSymbol = new ImportedSymbol("default", localName, path);
if (!symbolImports.ContainsKey(importedSymbol))
Expand All @@ -54,6 +56,7 @@ public TypeScriptVariableReference AddDefaultSymbolImport(string localName, stri
TypeName = localName,
CurrentUnit = this,
PathToUnit = path,
UseTypeKeyword = useTypeKeyword,
});
}
return new TypeScriptVariableReference(localName);
Expand Down Expand Up @@ -84,4 +87,4 @@ public string GenerateCode(DefaultCodeGenerationContext context)
private readonly Dictionary<ITypeInfo, TypeScriptImportStatement> imports = new Dictionary<ITypeInfo, TypeScriptImportStatement>();
private readonly Dictionary<ImportedSymbol, TypeScriptImportStatement> symbolImports = new Dictionary<ImportedSymbol, TypeScriptImportStatement>();
}
}
}

0 comments on commit 668bb46

Please sign in to comment.