Skip to content

Commit cd2743b

Browse files
committed
small fixes
1 parent 06853de commit cd2743b

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/TypeScriptDefinitionGenerator.Core/GeneratorService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public static void CreateDtsFile(string sourceFilePath, IGeneratorOptions? optio
3535

3636
if (string.IsNullOrEmpty(dts))
3737
{
38-
log?.Invoke($"No types to generate in {sourceFilePath}");
38+
var content = File.ReadAllText(sourceFilePath);
39+
var reason = RoslynParser.GetEmptyReason(sourceFilePath, content, options);
40+
log?.Invoke(reason != null
41+
? $"{reason} ({sourceFilePath})"
42+
: $"No types to generate in {sourceFilePath}");
3943
return;
4044
}
4145

src/TypeScriptDefinitionGenerator.Core/RoslynParser.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ public static IEnumerable<IntellisenseObject> ProcessFile(string filePath, strin
7474
return new HashSet<IntellisenseObject>(list);
7575
}
7676

77+
/// <summary>
78+
/// Returns a user-friendly reason why no types were generated, or null if the reason is unknown.
79+
/// </summary>
80+
public static string? GetEmptyReason(string filePath, string fileContent, IGeneratorOptions options)
81+
{
82+
var syntaxTree = CSharpSyntaxTree.ParseText(fileContent, path: filePath);
83+
var refs = GetDefaultReferences();
84+
var compilation = CSharpCompilation.Create(
85+
"TempAssembly",
86+
new[] { syntaxTree },
87+
refs,
88+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
89+
var semanticModel = compilation.GetSemanticModel(syntaxTree);
90+
91+
var typeDecls = syntaxTree.GetRoot().DescendantNodes().OfType<BaseTypeDeclarationSyntax>().ToList();
92+
if (typeDecls.Count == 0)
93+
return "File has no class or enum declarations.";
94+
95+
var nonPublicCount = 0;
96+
var symbolCount = 0;
97+
foreach (var typeDecl in typeDecls)
98+
{
99+
var symbol = semanticModel.GetDeclaredSymbol(typeDecl, default) as INamedTypeSymbol;
100+
if (symbol == null)
101+
continue;
102+
symbolCount++;
103+
if (symbol.DeclaredAccessibility != Accessibility.Public)
104+
nonPublicCount++;
105+
}
106+
107+
if (symbolCount > 0 && nonPublicCount == symbolCount)
108+
return "File contains only internal or private types. Only public classes and enums are generated.";
109+
110+
return null;
111+
}
112+
77113
private static bool ShouldProcessClass(INamedTypeSymbol symbol)
78114
{
79115
return symbol.DeclaredAccessibility == Accessibility.Public && !symbol.IsStatic;

0 commit comments

Comments
 (0)