/// <summary> /// Does not perform any operations yet on the AST. /// </summary> private void RetrieveOverridenNamespaceNames() { new MultiPurposeASTWalker(this.node, delegate (SyntaxNode astNode) { var typeDeclarationNode = astNode as BaseTypeDeclarationSyntax; // Recognizing only classes, enums and interfaces var classNode = astNode as ClassDeclarationSyntax; var enumNode = astNode as EnumDeclarationSyntax; var interfaceNode = astNode as InterfaceDeclarationSyntax; if (classNode == null && enumNode == null && interfaceNode == null) { return false; } var helper = new AttributeLists(typeDeclarationNode); return RetrieveScriptNamespaceAttribute(helper) != null; }, delegate (SyntaxNode astNode) { var typeDeclarationNode = astNode as BaseTypeDeclarationSyntax; var scriptNamespaceAttributeHelper = RetrieveScriptNamespaceAttribute(new AttributeLists(typeDeclarationNode)); AttributeListSyntax scriptNamespaceAttributeListSyntax = scriptNamespaceAttributeHelper.AttributeDecoration.AttributeList; // The list where ScriptNamespace belongs to AttributeSyntax scriptNamespaceAttributeSyntax = scriptNamespaceAttributeHelper.AttributeDecoration.AttributeNode; // The ScriptNamespace attribute // We create a new class node with the attribute removed SeparatedSyntaxList<AttributeSyntax> newAttributeListSyntaxAttributes = scriptNamespaceAttributeListSyntax.Attributes.Remove(scriptNamespaceAttributeSyntax); AttributeListSyntax newAttributeListSyntax = SyntaxFactory.AttributeList(newAttributeListSyntaxAttributes); SyntaxList<AttributeListSyntax> newAttributeLists = typeDeclarationNode.AttributeLists.Remove(scriptNamespaceAttributeListSyntax); newAttributeLists = newAttributeLists.Add(newAttributeListSyntax); BaseTypeDeclarationSyntax newTypeDeclarationSyntax = typeDeclarationNode.RemoveNode(scriptNamespaceAttributeListSyntax, SyntaxRemoveOptions.KeepNoTrivia); if (newTypeDeclarationSyntax as ClassDeclarationSyntax != null) { (newTypeDeclarationSyntax as ClassDeclarationSyntax).WithAttributeLists(newAttributeLists); } else if (newTypeDeclarationSyntax as InterfaceDeclarationSyntax != null) { (newTypeDeclarationSyntax as InterfaceDeclarationSyntax).WithAttributeLists(newAttributeLists); } else if (newTypeDeclarationSyntax as EnumDeclarationSyntax != null) { (newTypeDeclarationSyntax as EnumDeclarationSyntax).WithAttributeLists(newAttributeLists); } else { throw new InvalidOperationException("Not recognized type at rearrangement phase. Expecting classes and interfaces only!"); } // Asserting that the overriden namespace has a proper value if (string.IsNullOrEmpty(scriptNamespaceAttributeHelper.OverridenNamespace) || string.IsNullOrWhiteSpace(scriptNamespaceAttributeHelper.OverridenNamespace)) { throw new InvalidOperationException("The ScriptNamespace attribute contains an overriden namespace value which cannot be accepted!"); } var info = new TransformationInfo() { OriginalNode = typeDeclarationNode, TransformedNode = newTypeDeclarationSyntax, OriginalNamespace = this.compilation != null ? this.compilation.GetSemanticModel(this.tree).GetDeclaredSymbol(typeDeclarationNode).ContainingNamespace.ToString() : null, OverridenNamespace = scriptNamespaceAttributeHelper.OverridenNamespace }; this.transformationInfos.Add(info); }) .Start(); }
private static ScriptNamespaceAttributeDecoration RetrieveScriptNamespaceAttribute(AttributeLists helper) { foreach (var attribute in helper.Attributes) { if (ScriptNamespaceAttributeDecoration.IsScriptNamespaceAttributeDecoration(attribute)) { return new ScriptNamespaceAttributeDecoration(attribute); } } return null; }