From adec5560f345158724f8697a6757c603e44c162a Mon Sep 17 00:00:00 2001 From: maca88 Date: Sat, 4 Nov 2017 23:50:00 +0100 Subject: [PATCH] Replace EOL when is not CRLF with LF --- .../Extensions/Internal/SyntaxNodeExtensions.cs | 13 +++++++------ .../Internal/ProjectTransformer.Namespace.cs | 8 +++++--- .../Internal/ProjectTransformer.Type.cs | 14 +++++++++----- appveyor.yml | 2 -- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Source/AsyncGenerator/Extensions/Internal/SyntaxNodeExtensions.cs b/Source/AsyncGenerator/Extensions/Internal/SyntaxNodeExtensions.cs index 11df544b..aaa52558 100644 --- a/Source/AsyncGenerator/Extensions/Internal/SyntaxNodeExtensions.cs +++ b/Source/AsyncGenerator/Extensions/Internal/SyntaxNodeExtensions.cs @@ -564,7 +564,7 @@ internal static NamespaceDeclarationSyntax ReplaceWithMembers(this NamespaceDecl // TODO: take the original directive whitespace internal static TypeDeclarationSyntax RemoveMembersKeepDirectives(this TypeDeclarationSyntax node, - Predicate predicate, SyntaxTrivia directiveLeadingWhitespace) + Predicate predicate, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol) { var annotations = new List(); foreach (var memberSpan in node.Members.Where(o => predicate(o)).Select(o => o.Span)) @@ -576,14 +576,14 @@ internal static TypeDeclarationSyntax RemoveMembersKeepDirectives(this TypeDecla } foreach (var annotation in annotations) { - node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace); + node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace, eol); } return node; } // TODO: take the original directive whitespace internal static NamespaceDeclarationSyntax RemoveMembersKeepDirectives(this NamespaceDeclarationSyntax node, - Predicate predicate, SyntaxTrivia directiveLeadingWhitespace) + Predicate predicate, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol) { var annotations = new List(); foreach (var memberSpan in node.Members.Where(o => predicate(o)).Select(o => o.Span)) @@ -595,13 +595,13 @@ internal static NamespaceDeclarationSyntax RemoveMembersKeepDirectives(this Name } foreach (var annotation in annotations) { - node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace); + node = RemoveNodeKeepDirectives(node, annotation, directiveLeadingWhitespace, eol); } return node; } // TODO: take the original directive whitespace - internal static T RemoveNodeKeepDirectives(this T node, string annotation, SyntaxTrivia directiveLeadingWhitespace) + internal static T RemoveNodeKeepDirectives(this T node, string annotation, SyntaxTrivia directiveLeadingWhitespace, SyntaxTrivia eol) where T : SyntaxNode { var toRemoveNode = node.GetAnnotatedNodes(annotation).First(); @@ -621,7 +621,8 @@ internal static T RemoveNodeKeepDirectives(this T node, string annotation, Sy { return null; // TODO: we need to preserve or remove directives! } - if (!EnvironmentHelper.IsWindows) + // Workaround: When EOL is not CRLF we need to replace all CRLF that may be added by RemoveNode with LF + if (eol.ToFullString() != CarriageReturnLineFeed.ToFullString()) { node = (T)new UnixEndOfLineTriviaRewriter().Visit(node); } diff --git a/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Namespace.cs b/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Namespace.cs index d82092a6..9c9298a2 100644 --- a/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Namespace.cs +++ b/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Namespace.cs @@ -86,7 +86,8 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati { if (transformResult.AnalyzationResult.Conversion == NamespaceConversion.Ignore) { - rootNode = rootNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia); + rootNode = rootNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia, + transformResult.EndOfLineTrivia); continue; } @@ -99,7 +100,7 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati if (transformType.AnalyzationResult.Conversion == TypeConversion.Ignore) { // We need to add a whitespace trivia to kept directives as they will not have any leading whitespace - newNode = newNode.RemoveNodeKeepDirectives(transformType.Annotation, memberWhitespace); + newNode = newNode.RemoveNodeKeepDirectives(transformType.Annotation, memberWhitespace, transformResult.EndOfLineTrivia); continue; } @@ -112,7 +113,8 @@ private RootNamespaceTransformationResult TransformNamespace(INamespaceAnalyzati } // We need to remove all other members that are not namespaces or types - newNode = newNode.RemoveMembersKeepDirectives(o => !(o is NamespaceDeclarationSyntax || o is TypeDeclarationSyntax), memberWhitespace); + newNode = newNode.RemoveMembersKeepDirectives(o => !(o is NamespaceDeclarationSyntax || o is TypeDeclarationSyntax), memberWhitespace, + transformResult.EndOfLineTrivia); // TODO: adding namespaces can introduce name conflicts, we should avoid it if (!transformResult.TaskConflict && !rootResult.Node.HasUsing("System.Threading.Tasks")) diff --git a/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Type.cs b/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Type.cs index a46b0098..f35a9cb1 100644 --- a/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Type.cs +++ b/Source/AsyncGenerator/Transformation/Internal/ProjectTransformer.Type.cs @@ -222,7 +222,8 @@ private RootTypeTransformationResult TransformType(ITypeAnalyzationResult rootTy !typeResult.GetSelfAndDescendantsTypes().SelectMany(o => o.MethodsAndAccessors).Any(o => o.Missing) )) { - rootTypeNode = rootTypeNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia); + rootTypeNode = rootTypeNode.RemoveNodeKeepDirectives(transformResult.Annotation, transformResult.LeadingWhitespaceTrivia, + transformResult.EndOfLineTrivia); continue; } @@ -237,7 +238,8 @@ private RootTypeTransformationResult TransformType(ITypeAnalyzationResult rootTy // We need to remove all other members that are not methods, properties or types newTypeNode = newTypeNode.RemoveMembersKeepDirectives(o => - !(o is BaseMethodDeclarationSyntax || o is TypeDeclarationSyntax || o is PropertyDeclarationSyntax), memberWhitespace); + !(o is BaseMethodDeclarationSyntax || o is TypeDeclarationSyntax || o is PropertyDeclarationSyntax), memberWhitespace, + transformResult.EndOfLineTrivia); newTypeNode = TransformMethodsAndProperties(newTypeNode, transformResult, namespaceMetadata, memberWhitespace, onlyMissingMembers); newTypeNode = RunTypeTransformers(newTypeNode, transformResult, namespaceMetadata, onlyMissingMembers); transformResult.Transformed = newTypeNode; @@ -316,7 +318,8 @@ private TypeDeclarationSyntax TransformSpecialMethod(FunctionTransformationResul if (methodTransform.AnalyzationResult.Conversion == MethodConversion.Ignore || onlyMissingMembers) { // We need to add a whitespace trivia to keep directives as they will not have any leading whitespace - newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace); + newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace, + transformResult.EndOfLineTrivia); return newTypeNode; } var methodNode = newTypeNode.GetAnnotatedNodes(methodTransform.Annotation) @@ -339,7 +342,7 @@ private TypeDeclarationSyntax TransformMethod(MethodTransformationResult methodT if (methodTransform.AnalyzationResult.Conversion == MethodConversion.Ignore || (onlyMissingMembers && !methodTransform.AnalyzationResult.Missing)) { // We need to add a whitespace trivia to keep directives as they will not have any leading whitespace - newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace); + newTypeNode = newTypeNode.RemoveNodeKeepDirectives(methodTransform.Annotation, memberWhitespace, transformResult.EndOfLineTrivia); return newTypeNode; } var methodNode = newTypeNode.GetAnnotatedNodes(methodTransform.Annotation) @@ -431,7 +434,8 @@ private TypeDeclarationSyntax TransformProperty(PropertyTransformationResult pro if (onlyMissingMembers || propertyTransform.AnalyzationResult.Conversion == PropertyConversion.Ignore) { // We need to add a whitespace trivia to keep directives as they will not have any leading whitespace - newTypeNode = newTypeNode.RemoveNodeKeepDirectives(propertyTransform.Annotation, memberWhitespace); + newTypeNode = newTypeNode.RemoveNodeKeepDirectives(propertyTransform.Annotation, memberWhitespace, + transformResult.EndOfLineTrivia); } return newTypeNode; } diff --git a/appveyor.yml b/appveyor.yml index a00154b9..febb3187 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,5 @@ version: 1.0.{build} image: Visual Studio 2017 -init: -- git config --global core.autocrlf false build_script: - cmd: PowerShell -Version 2.0 .\build.ps1 test: off