RefactorMCP catalog

Inline Type Alias

Writes the type an alias names in place of every use of the alias and removes the alias's using directive. The reverse of Introduce Type Alias.

Precondition

  • The file declares a using alias with the given name.
  • The alias names a type, not a namespace.

Transformation

  • Every use of the alias, in types and in expressions such as Stamp.UtcNow, becomes the type, written as briefly as the file's using directives allow.
  • When the file does not import a namespace the type needs, a using directive for it is added, so Stamp becomes DateTimeOffset with using System; rather than a qualified name.
  • The alias's directive is removed. A header comment above it stays at the top of the file.
  • A global using alias is inlined in every file of its project. A file that held nothing but the directive is deleted.

Preserved

  • What every use of the alias refers to.
  • A nullable annotation on a use: Lookup? becomes Dictionary<string, string>?.
  • Comments and layout around the uses.

Limitations

  • A namespace alias is refused rather than replaced by the namespace's full name.
  • An imported namespace that would make another name in the file ambiguous is refused because the result would not compile, rather than falling back to a qualified name.

Error codes

CodeMeaning
alias-not-foundthe file declares no alias with that name
not-a-type-aliasthe alias names a namespace

Cases

·

Diffs show each file from before to after. /*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.

#alias-in-file

Replaces every use of an alias with the type it names and removes the alias

success
TargetInventory.cs
Arguments
alias"StockIndex"
Inventory.csmodified
11 using System.Collections.Generic;
2−using StockIndex = System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<int>>;
32
43 namespace Shop
54 {
65 public class Inventory
76 {
8− private readonly StockIndex _stock = new StockIndex();
7+ private readonly Dictionary<string, List<int>> _stock = new Dictionary<string, List<int>>();
98
10− public StockIndex Snapshot() => new StockIndex(_stock);
9+ public Dictionary<string, List<int>> Snapshot() => new Dictionary<string, List<int>>(_stock);
1110
1211 public List<int> Levels(string sku) => _stock[sku];
1312 }
1413 }

#alias-not-found-refused

Refuses when the file declares no alias with the name

refusal
TargetOrder.cs
Arguments
alias"Money"
Refusesalias-not-found; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 public decimal Total { get; set; }
6 }
7 }

#global-alias

Inlines a global alias in every file of the project and deletes the file its directive leaves empty

success
TargetUsings.cs
Arguments
alias"Money"
Order.csmodified
11 namespace Shop
22 {
33 public class Order
44 {
5− public Money Total { get; set; }
5+ public decimal Total { get; set; }
66 }
77 }
Refund.csmodified
11 namespace Shop
22 {
33 public class Refund
44 {
5− public Money Amount(Order order) => order.Total / 2;
5+ public decimal Amount(Order order) => order.Total / 2;
66 }
77 }
Usings.csdeleted
1−global using Money = System.Decimal;

#imports-namespace

Imports the namespace of the aliased type when the file did not, keeping the alias's neighbours and comments

success
TargetClock.cs
Arguments
alias"Stamp"
Clock.csmodified
11 // Clock helpers.
2+using System;
23 using System.Collections.Generic;
3−using Stamp = System.DateTimeOffset;
44
55 namespace Shop
66 {
77 public class Clock
88 {
99 // The current time.
10− public Stamp Now() => Stamp.UtcNow;
10+ public DateTimeOffset Now() => DateTimeOffset.UtcNow;
1111
12− public List<Stamp> History { get; } = new List<Stamp>();
12+ public List<DateTimeOffset> History { get; } = new List<DateTimeOffset>();
1313 }
1414 }

#namespace-alias-refused

Refuses an alias that names a namespace rather than a type

refusal
TargetFiles.cs
Arguments
alias"IO"
Refusesnot-a-type-alias; every file is left unchanged
Files.csinput
1 using IO = System.IO;
2
3 namespace Shop
4 {
5 public class Files
6 {
7 public bool Exists(string path) => IO.File.Exists(path);
8 }
9 }

#nullable-use

Keeps the nullable annotation on a use of the alias

success
TargetCache.cs
Arguments
alias"Lookup"
Projectnullable enable
Cache.csmodified
11 using System.Collections.Generic;
2−using Lookup = System.Collections.Generic.Dictionary<string, string>;
32
43 namespace Shop
54 {
65 public class Cache
76 {
8− private Lookup? _entries;
7+ private Dictionary<string, string>? _entries;
98
10− public Lookup Entries => _entries ??= new Lookup();
9+ public Dictionary<string, string> Entries => _entries ??= new Dictionary<string, string>();
1110 }
1211 }