protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var interestingDeclarations = State.AllDeclarations.Where(item => item.AsTypeDeclaration != null && ClassModuleDeclaration.HasDefaultMember(item.AsTypeDeclaration)); var interestingReferences = interestingDeclarations .SelectMany(declaration => declaration.References) .Where(reference => { var letStmtContext = reference.Context.GetAncestor <VBAParser.LetStmtContext>(); return(reference.IsAssignment && letStmtContext != null && letStmtContext.LET() == null && !reference.IsIgnoringInspectionResultFor(AnnotationName)); }); return(interestingReferences.Select(reference => new IdentifierReferenceInspectionResult(this, string.Format(InspectionResults.ImplicitDefaultMemberAssignmentInspection, reference.Declaration.IdentifierName, reference.Declaration.AsTypeDeclaration.IdentifierName), State, reference))); }
private static Declaration GetNewClassDeclaration(Declaration project, string name, IEnumerable <IAnnotation> annotations) { var declaration = new ClassModuleDeclaration(new QualifiedMemberName(project.QualifiedModuleName, name), project, name, true, annotations, new Attributes()); return(declaration); }
public void Refactor(QualifiedSelection selection) { _targetInterface = _state.DeclarationFinder.FindInterface(selection); _targetClass = _declarations.SingleOrDefault(d => ImplementingModuleTypes.Contains(d.DeclarationType) && d.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName)) as ClassModuleDeclaration; if (_targetClass == null || _targetInterface == null) { _messageBox.NotifyWarn(RubberduckUI.ImplementInterface_InvalidSelectionMessage, RubberduckUI.ImplementInterface_Caption); return; } var oldSelection = _vbe.GetActiveSelection(); ImplementMissingMembers(_state.GetRewriter(_targetClass)); if (oldSelection.HasValue) { using (var module = _state.ProjectsProvider.Component(oldSelection.Value.QualifiedName).CodeModule) { using (var pane = module.CodePane) { pane.Selection = oldSelection.Value.Selection; } } } _state.OnParseRequested(this); }
public void GetSupertypesReturnsAnEmptyEnumerableForDeclarationsWithDeclarationTypeClassModuleWhichAreNoClassModuleDeclarations() { var projectDeclaration = GetTestProject("testProject"); var fakeClassModule = GetTestFakeClassModule(projectDeclaration, "testFakeClass"); Assert.IsFalse(ClassModuleDeclaration.GetSupertypes(fakeClassModule).Any()); }
public void GetSupertypesReturnsAnEmptyEnumerableForProceduralModules() { var projectDeclaration = GetTestProject("testProject"); var proceduralModule = GetTestProceduralModule(projectDeclaration, "testModule"); Assert.IsFalse(ClassModuleDeclaration.GetSupertypes(proceduralModule).Any()); }
private static bool MayRequireAssignmentUsingSet(Declaration declaration) { if (declaration.DeclarationType == DeclarationType.PropertyLet) { return(false); } if (declaration.AsTypeName == Tokens.Variant) { return(true); } if (declaration.IsArray) { return(false); } if (declaration.AsTypeDeclaration != null) { if ((ClassModuleDeclaration.HasDefaultMember(declaration.AsTypeDeclaration) || declaration.AsTypeDeclaration.DeclarationType == DeclarationType.Enumeration)) { return(false); } } if (SymbolList.ValueTypes.Contains(declaration.AsTypeName)) { return(false); } return(true); }
private List <Declaration> GetExcelRangeDeclarations() { var excelDeclaration = new ProjectDeclaration(new QualifiedMemberName(new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "Excel"), "Excel"), "Excel", true); var globalDeclaration = new ClassModuleDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "_Global"), "_Global"), excelDeclaration, "_Global", true, null, null); var globalCoClassDeclarationAttributes = new Attributes(); globalCoClassDeclarationAttributes.AddPredeclaredIdTypeAttribute(); globalCoClassDeclarationAttributes.AddGlobalClassAttribute(); var globalCoClassDeclaration = new ClassModuleDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "Global"), "Global"), excelDeclaration, "Global", true, null, globalCoClassDeclarationAttributes); globalDeclaration.AddSubtype(globalCoClassDeclaration); globalCoClassDeclaration.AddSupertype(globalDeclaration); globalCoClassDeclaration.AddSupertype("_Global"); var rangeClassModuleDeclaration = new ClassModuleDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "Range"), "Range"), excelDeclaration, "Range", true, new List <IAnnotation>(), new Attributes()); var rangeDeclaration = new PropertyGetDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "_Global"), "Range"), globalDeclaration, globalDeclaration, "Range", null, null, Accessibility.Global, null, Selection.Home, false, true, new List <IAnnotation>(), new Attributes()); var firstParamDeclaration = new ParameterDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "_Global"), "Cell1"), rangeDeclaration, "Variant", null, null, false, false); var secondParamDeclaration = new ParameterDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "_Global"), "Cell2"), rangeDeclaration, "Variant", null, null, true, false); rangeDeclaration.AddParameter(firstParamDeclaration); rangeDeclaration.AddParameter(secondParamDeclaration); return(new List <Declaration> { excelDeclaration, globalDeclaration, globalCoClassDeclaration, rangeClassModuleDeclaration, rangeDeclaration, firstParamDeclaration, secondParamDeclaration, }); }
public void ReportsRange() { const string inputCode = @"Sub foo() Dim arr1() As Variant arr1 = Range(""A1:B2"") End Sub "; //Arrange var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none) .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode) .AddReference("Excel", string.Empty, true) // Apparently, the COM loader can't find it when it isn't actually loaded... //.AddReference("VBA", "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA7.1\\VBE7.DLL", true) .Build(); var vbe = builder.AddProject(project).Build(); var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState()); var excelDeclaration = new ProjectDeclaration(new QualifiedMemberName(new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "Excel"), "Excel"), "Excel", true); var listColumnDeclaration = new ClassModuleDeclaration(new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "ListColumn"), "ListColumn"), excelDeclaration, "ListColumn", true, null, null); var rangeDeclaration = new Declaration( new QualifiedMemberName( new QualifiedModuleName("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", "ListColumn"), "Range"), listColumnDeclaration, "EXCEL.EXE;Excel.ListColumn", "Range", null, false, false, Accessibility.Global, (DeclarationType)3712, false, null, true, null, new Attributes()); parser.State.AddDeclaration(excelDeclaration); parser.State.AddDeclaration(listColumnDeclaration); parser.State.AddDeclaration(rangeDeclaration); parser.Parse(); if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } var inspection = new ImplicitActiveSheetReferenceInspection(vbe.Object, parser.State); var inspectionResults = inspection.GetInspectionResults(); Assert.AreEqual(1, inspectionResults.Count()); }
public void GetSupertypesReturnsTheSupertypesOfAClassModule() { var projectDeclaration = GetTestProject("testProject"); var supertype = GetTestClassModule(projectDeclaration, "testSupertype", true, null); var classModule = GetTestClassModule(projectDeclaration, "testClass", true, null); classModule.AddSupertype(supertype); Assert.AreEqual(supertype, ClassModuleDeclaration.GetSupertypes(classModule).First()); }
private static SubroutineDeclaration DebugPrintDeclaration(ClassModuleDeclaration debugClass) { return(new SubroutineDeclaration( new QualifiedMemberName(debugClass.QualifiedName.QualifiedModuleName, "Print"), debugClass, debugClass, null, Accessibility.Global, null, Selection.Home, true, null, new Attributes())); }
private static SubroutineDeclaration DebugAssertDeclaration(ClassModuleDeclaration debugClass) { return(new SubroutineDeclaration( new QualifiedMemberName(debugClass.QualifiedName.QualifiedModuleName, "Assert"), debugClass, debugClass, null, Accessibility.Global, null, Selection.Home, false, new List <IAnnotation>(), new Attributes())); }
public ExtractInterfaceModel(IDeclarationFinderProvider declarationFinderProvider, ClassModuleDeclaration target, ICodeBuilder codeBuilder) { TargetDeclaration = target; DeclarationFinderProvider = declarationFinderProvider; if (TargetDeclaration == null) { return; } InterfaceName = $"I{TargetDeclaration.IdentifierName}"; InterfaceInstancing = ImplementingClassInstancing; LoadMembers(codeBuilder); }
private static bool IsEnclosingModule(Declaration callingModule, Declaration calleeMember) { if (callingModule.Equals(calleeMember.ParentScopeDeclaration)) { return(true); } foreach (var supertype in ClassModuleDeclaration.GetSupertypes(callingModule)) { if (IsEnclosingModule(supertype, calleeMember)) { return(true); } } return(false); }
private static ClassModuleDeclaration GetHostWorkbookDeclaration(DeclarationFinder finder) { var documentModuleQMNs = finder.AllModules.Where(m => m.ComponentType == ComponentType.Document); ClassModuleDeclaration result = null; foreach (var qmn in documentModuleQMNs) { var declaration = finder.ModuleDeclaration(qmn) as ClassModuleDeclaration; if (declaration.Supertypes.Any(t => t.IdentifierName.Equals("Workbook") && t.ProjectName == "Excel" && !t.IsUserDefined)) { result = declaration; break; } } return(result ?? throw new System.InvalidOperationException("Failed to find the host Workbook declaration.")); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var interestingDeclarations = State.AllDeclarations.Where(item => item.AsTypeDeclaration != null && ClassModuleDeclaration.HasDefaultMember(item.AsTypeDeclaration)); var interestingReferences = interestingDeclarations .SelectMany(declaration => declaration.References) .Where(reference => { var letStmtContext = ParserRuleContextHelper.GetParent <VBAParser.LetStmtContext>(reference.Context); return(reference.IsAssignment && letStmtContext != null && letStmtContext.LET() == null); }); return(interestingReferences.Select(reference => new ImplicitDefaultMemberAssignmentInspectionResult(this, reference))); }
public void Refactor(QualifiedSelection selection) { _targetInterface = _declarationFinderProvider.DeclarationFinder.FindInterface(selection); _targetClass = _declarations.SingleOrDefault(d => ImplementingModuleTypes.Contains(d.DeclarationType) && d.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName)) as ClassModuleDeclaration; if (_targetClass == null || _targetInterface == null) { _messageBox.NotifyWarn(RubberduckUI.ImplementInterface_InvalidSelectionMessage, RubberduckUI.ImplementInterface_Caption); return; } var rewriteSession = _rewritingManager.CheckOutCodePaneSession(); var rewriter = rewriteSession.CheckOutModuleRewriter(_targetClass.QualifiedModuleName); ImplementMissingMembers(rewriter); rewriteSession.TryRewrite(); }
private static bool HasPotentiallyNonObjectParameterlessDefaultMember(ClassModuleDeclaration classModule) { var defaultMember = classModule?.DefaultMember; if (defaultMember == null) { return(false); } var parameters = (defaultMember as IParameterizedDeclaration)?.Parameters; // assign declaration is an object without a default parameterless (or with all parameters optional) member - LHS needs a 'Set' keyword. if (parameters != null && parameters.Any(p => !p.IsOptional)) { return(false); } var defaultMemberType = defaultMember.AsTypeDeclaration as ClassModuleDeclaration; return(defaultMemberType == null || HasPotentiallyNonObjectParameterlessDefaultMember(defaultMemberType)); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var interestingDeclarations = State.AllUserDeclarations.Where(item => !item.IsSelfAssigned && !item.IsArray && !SymbolList.ValueTypes.Contains(item.AsTypeName) && (item.AsTypeDeclaration == null || (!ClassModuleDeclaration.HasDefaultMember(item.AsTypeDeclaration) && item.AsTypeDeclaration.DeclarationType != DeclarationType.Enumeration && item.AsTypeDeclaration.DeclarationType != DeclarationType.UserDefinedType)) && (item.DeclarationType == DeclarationType.Variable || item.DeclarationType == DeclarationType.Parameter)); var interestingMembers = State.AllUserDeclarations.Where(item => (item.DeclarationType == DeclarationType.Function || item.DeclarationType == DeclarationType.PropertyGet) && !item.IsArray && item.IsTypeSpecified && !SymbolList.ValueTypes.Contains(item.AsTypeName) && (item.AsTypeDeclaration == null || // null if unresolved (e.g. in unit tests) (item.AsTypeDeclaration.DeclarationType != DeclarationType.Enumeration && item.AsTypeDeclaration.DeclarationType != DeclarationType.UserDefinedType && item.AsTypeDeclaration != null && !ClassModuleDeclaration.HasDefaultMember(item.AsTypeDeclaration)))); var interestingReferences = interestingDeclarations .Union(interestingMembers.SelectMany(item => item.References.Where(reference => reference.ParentScoping.Equals(item) && reference.IsAssignment) .Select(reference => reference.Declaration))) .SelectMany(declaration => declaration.References.Where(reference => { var letStmtContext = ParserRuleContextHelper.GetParent <VBAParser.LetStmtContext>(reference.Context); return(reference.IsAssignment && letStmtContext != null && letStmtContext.LET() == null); }) ); return(interestingReferences.Select(reference => new ObjectVariableNotSetInspectionResult(this, reference))); }
public ImplementInterfaceModel(ClassModuleDeclaration targetInterface, ClassModuleDeclaration targetClass) { TargetInterface = targetInterface; TargetClass = targetClass; }
private static ImplementInterfaceModel Model(ClassModuleDeclaration targetInterface, ClassModuleDeclaration targetClass) { return(new ImplementInterfaceModel(targetInterface, targetClass)); }
private IBoundExpression ResolveDefaultMember(IBoundExpression lExpression, string asTypeName, Declaration asTypeDeclaration) { if (lExpression.ReferencedDeclaration.IsArray) { return(null); } /* * The declared type of <l-expression> is Object or Variant, and <argument-list> contains no * named arguments. In this case, the index expression is classified as an unbound member with * a declared type of Variant, referencing <l-expression> with no member name. */ if ( asTypeName != null && (asTypeName.ToUpperInvariant() == "VARIANT" || asTypeName.ToUpperInvariant() == "OBJECT") && !_argumentList.HasNamedArguments) { return(new IndexExpression(null, ExpressionClassification.Unbound, _expression, lExpression, _argumentList)); } /* * The declared type of <l-expression> is a specific class, which has a public default Property * Get, Property Let, function or subroutine, and one of the following is true: */ bool hasDefaultMember = asTypeDeclaration != null && asTypeDeclaration.DeclarationType == DeclarationType.ClassModule && ((ClassModuleDeclaration)asTypeDeclaration).DefaultMember != null; if (hasDefaultMember) { ClassModuleDeclaration classModule = (ClassModuleDeclaration)asTypeDeclaration; Declaration defaultMember = classModule.DefaultMember; bool isPropertyGetLetFunctionProcedure = defaultMember.DeclarationType == DeclarationType.PropertyGet || defaultMember.DeclarationType == DeclarationType.PropertyLet || defaultMember.DeclarationType == DeclarationType.Function || defaultMember.DeclarationType == DeclarationType.Procedure; bool isPublic = defaultMember.Accessibility == Accessibility.Global || defaultMember.Accessibility == Accessibility.Implicit || defaultMember.Accessibility == Accessibility.Public; if (isPropertyGetLetFunctionProcedure && isPublic) { /* * This default member’s parameter list is compatible with <argument-list>. In this case, the * index expression references this default member and takes on its classification and * declared type. * * TODO: Primitive argument compatibility checking for now. */ if (((IParameterizedDeclaration)defaultMember).Parameters.Count() == _argumentList.Arguments.Count) { return(new IndexExpression(defaultMember, lExpression.Classification, _expression, lExpression, _argumentList)); } /** * This default member cannot accept any parameters. In this case, the static analysis restarts * recursively, as if this default member was specified instead for <l-expression> with the * same <argument-list>. */ if (((IParameterizedDeclaration)defaultMember).Parameters.Count() == 0) { // Recursion limit reached, abort. if (DEFAULT_MEMBER_RECURSION_LIMIT == _defaultMemberRecursionLimitCounter) { return(null); } _defaultMemberRecursionLimitCounter++; ExpressionClassification classification; if (defaultMember.DeclarationType.HasFlag(DeclarationType.Property)) { classification = ExpressionClassification.Property; } else if (defaultMember.DeclarationType == DeclarationType.Procedure) { classification = ExpressionClassification.Subroutine; } else { classification = ExpressionClassification.Function; } var defaultMemberAsLExpression = new SimpleNameExpression(defaultMember, classification, _expression); return(Resolve(defaultMemberAsLExpression)); } } } return(null); }
private static bool IsInstanceMemberOfModuleOrOneOfItsSupertypes(Declaration module, Declaration member) { return(IsInstanceMemberOfModule(module, member) || ClassModuleDeclaration.GetSupertypes(module).Any(supertype => IsInstanceMemberOfModuleOrOneOfItsSupertypes(supertype, member))); //ClassModuleDeclaration.GetSuperTypes never returns null. }