Add `--add-nullable-enable` option.
This commit is contained in:
Родитель
5d9dfaebb4
Коммит
2c7e2d335f
|
@ -24,7 +24,6 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ICSharpCode.NullabilityInference;
|
||||
|
@ -67,6 +66,9 @@ Remarks:
|
|||
[Option("--all-nullable", "Don't run inference, just mark all reference types as nullable.", CommandOptionType.NoValue)]
|
||||
public bool AllNullable { get; }
|
||||
|
||||
[Option("-e|--add-nullable-enable", "Add '#nullable enable' to the top of each source file.", CommandOptionType.NoValue)]
|
||||
public bool AddNullableEnable { get; }
|
||||
|
||||
#if DEBUG
|
||||
[Option("-g|--show-graph", "Show type graph. Requires GraphViz dot.exe in PATH.", CommandOptionType.NoValue)]
|
||||
public bool ShowGraph { get; }
|
||||
|
@ -146,10 +148,23 @@ Remarks:
|
|||
return 0;
|
||||
}
|
||||
|
||||
private static void WriteTree(SyntaxTree tree, CancellationToken cancellationToken)
|
||||
private void WriteTree(SyntaxTree tree, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tree.FilePath))
|
||||
return;
|
||||
if (AddNullableEnable) {
|
||||
var root = tree.GetRoot(cancellationToken);
|
||||
if (!root.GetLeadingTrivia().Any(trivia => trivia.IsKind(SyntaxKind.NullableDirectiveTrivia))) {
|
||||
var newDirective = SyntaxFactory.Trivia(
|
||||
SyntaxFactory.NullableDirectiveTrivia(
|
||||
settingToken: SyntaxFactory.Token(SyntaxKind.EnableKeyword).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")),
|
||||
isActive: true
|
||||
).WithTrailingTrivia(SyntaxFactory.EndOfLine(Environment.NewLine))
|
||||
);
|
||||
root = root.WithLeadingTrivia(new[] { newDirective }.Concat(root.GetLeadingTrivia()));
|
||||
tree = tree.WithRootAndOptions(root, tree.Options);
|
||||
}
|
||||
}
|
||||
using var stream = new FileStream(tree.FilePath, FileMode.Create, FileAccess.Write);
|
||||
using var writer = new StreamWriter(stream, tree.Encoding);
|
||||
writer.Write(tree.GetText(cancellationToken));
|
||||
|
|
|
@ -25,6 +25,14 @@ Note: this is a work in progress. Many C# constructs will trigger a NotImplement
|
|||
* If using the tool on multiple projects; apply the tool in the build order.
|
||||
* For the .NET base class library, use .NET Core 3 (or later), or use [ReferenceAssemblyAnnotator](https://github.com/tunnelvisionlabs/ReferenceAssemblyAnnotator).
|
||||
* For third-party libraries, consider upgrading to a newer version of the library if that adds nullability annotations.
|
||||
* You can use `#nullable enable` to mark code that you have finished manually reviewing.
|
||||
* The tool will never touch any code after `#nullable enable` or `#nullable disable`. It only modifies code prior to those directives and code after `#nullable restore`.
|
||||
* You can use this to add nullability annotations to your project file-by-file:
|
||||
* Don't use `<Nullable>enable</Nullable>` on the project level
|
||||
* Use the `InferNull --add-nullable-enable` command-line option to let the inference tool add the directive to all files
|
||||
* Use git to revert all changes made by the tool except those to a subset of the files.
|
||||
* Make code changes to that subset of files to fix the remaining warnings.
|
||||
* Commit, then later re-run `InferNull --add-nullable-enable` to work on the next batch of files.
|
||||
|
||||
## The algorithm
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче