Skip to content

Commit

Permalink
test: FileScopedNamespaceDeclaration -> NamespaceDeclaration transfor…
Browse files Browse the repository at this point in the history
…mation
  • Loading branch information
R-unic committed Jan 9, 2025
1 parent 8eeb217 commit 18aa107
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion RobloxCS.Luau/AstUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public static string FixIdentifierNameText(SyntaxNode node, string name, bool re
name += '_';

name += useCount;
return name;
return name.Replace("@", "");
}

public static TypeRef? CreateTypeRef(string? typePath)
Expand Down
1 change: 0 additions & 1 deletion RobloxCS.Tests/GenerationTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.CodeAnalysis.CSharp;
using RobloxCS.Luau;

namespace RobloxCS.Tests;
Expand Down
20 changes: 15 additions & 5 deletions RobloxCS.Tests/TransformerTests/MainTransformerTest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RobloxCS.Tests.TransformerTests;

public class MainTransformerTest
{
[Fact]
public void Transforms_FileScopedNamespaces()
{
var compilationUnit = Transform("namespace Abc;");
Assert.Single(compilationUnit.Members);
Assert.IsType<NamespaceDeclarationSyntax>(compilationUnit.Members.First());

var @namespace = (NamespaceDeclarationSyntax)compilationUnit.Members.First();
Assert.Equal("Abc", @namespace.Name.ToString());
}

[Fact]
public void AddsRobloxImports()
{
var transformedTree = Transform("");
var compilationUnit = transformedTree.GetCompilationUnitRoot();
var compilationUnit = Transform("");
Assert.Equal(2, compilationUnit.Usings.Count);

var usingRoblox = compilationUnit.Usings.First();
Expand All @@ -21,11 +31,11 @@ public void AddsRobloxImports()
Assert.Equal("Roblox.Globals", usingRobloxGlobals.Name?.ToString());
}

private static SyntaxTree Transform(string source)
private static CompilationUnitSyntax Transform(string source)
{
var cleanTree = SyntaxFactory.ParseSyntaxTree(source);
var transform = BuiltInTransformers.Main();
var transformedTree = transform(cleanTree, new ConfigData());
return transformedTree;
return transformedTree.GetCompilationUnitRoot();
}
}
11 changes: 8 additions & 3 deletions RobloxCS/Transformers/BaseTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ protected static string GetName(SyntaxNode node) =>
protected static bool HasSyntax(SyntaxTokenList tokens, SyntaxKind syntax) =>
tokens.Any(token => token.IsKind(syntax));

protected static SyntaxToken CreateIdentifierToken(string text, string? valueText = null, SyntaxTriviaList? trivia = null)
protected static SyntaxToken CreateIdentifierToken(string text, string? valueText = null,
SyntaxTriviaList? leadingTrivia = null, SyntaxTriviaList? trailingTrivia = null)
{
var triviaList = trivia ?? SyntaxFactory.TriviaList();
return SyntaxFactory.VerbatimIdentifier(triviaList, text, valueText ?? text, triviaList);
return SyntaxFactory.VerbatimIdentifier(
leadingTrivia ?? SyntaxFactory.TriviaList(),
text,
valueText ?? text,
trailingTrivia ?? SyntaxFactory.TriviaList()
);
}
}
12 changes: 0 additions & 12 deletions RobloxCS/Transformers/MainTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ public sealed class MainTransformer(SyntaxTree tree, ConfigData config) : BaseTr
return SyntaxFactory.IsPatternExpression(node.Left, pattern);
}

// Transform `@` out of identifier names
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
var identifierText = node.Identifier.Text;
if (!identifierText.Contains('@') || identifierText == "var")
return base.VisitIdentifierName(node);

var fixedIdentifierText = identifierText.Replace("@", "");
var newToken = CreateIdentifierToken(fixedIdentifierText);
return base.VisitIdentifierName(node.WithIdentifier(newToken));
}

// Fix conditional accesses so that they return the AST you expect them to
public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
{
Expand Down

0 comments on commit 18aa107

Please sign in to comment.