public virtual UsingStatementAst VisitUsingStatement(UsingStatementAst usingStatement)
 {
     return(new UsingStatementAst(
                usingStatement.Extent,
                usingStatement.UsingStatementKind,
                usingStatement.Name?.Rewrite(this, SyntaxKind.StringConstant)));
 }
            public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
            {
                // Look for 'using ...;' at the top of scripts

                if (!_targetVersions.Contains(s_v3) && !_targetVersions.Contains(s_v4))
                {
                    return(AstVisitAction.Continue);
                }

                string message = string.Format(
                    CultureInfo.CurrentCulture,
                    Strings.UseCompatibleSyntaxError,
                    "using statement",
                    "using ...;",
                    "3,4");

                _diagnosticAccumulator.Add(
                    new DiagnosticRecord(
                        message,
                        usingStatementAst.Extent,
                        _rule.GetName(),
                        _rule.Severity,
                        _analyzedFilePath
                        ));

                return(AstVisitAction.Continue);
            }
示例#3
0
        public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
        {
            if (usingStatementAst.UsingStatementKind == UsingStatementKind.Module)
            {
                Exception exception;
                bool      wildcardCharactersUsed;
                bool      isConstant;
                var       moduleInfo = GetModulesFromUsingModule(usingStatementAst, out exception, out wildcardCharactersUsed, out isConstant);
                if (!isConstant)
                {
                    _parser.ReportError(usingStatementAst.Extent,
                                        nameof(ParserStrings.RequiresArgumentMustBeConstant),
                                        ParserStrings.RequiresArgumentMustBeConstant);
                }
                else if (exception != null)
                {
                    // we re-using RequiresModuleInvalid string, semantic is very similar so it's fine to do that.
                    _parser.ReportError(usingStatementAst.Extent,
                                        nameof(ParserStrings.RequiresModuleInvalid),
                                        ParserStrings.RequiresModuleInvalid,
                                        exception.Message);
                }
                else if (wildcardCharactersUsed)
                {
                    _parser.ReportError(usingStatementAst.Extent,
                                        nameof(ParserStrings.WildCardModuleNameError),
                                        ParserStrings.WildCardModuleNameError);
                }
                else if (moduleInfo != null && moduleInfo.Count > 0)
                {
                    // it's ok, if we get more then one module. They are already sorted in the right order
                    // we just need to use the first one

                    // We must add the same objects (in sense of object refs) to usingStatementAst typeTable and to symbolTable.
                    // Later, this same TypeDefinitionAsts would be used in DefineTypes(), by the module, where it was imported from at compile time.
                    var exportedTypes = usingStatementAst.DefineImportedModule(moduleInfo[0]);
                    foreach (var typePairs in exportedTypes)
                    {
                        _symbolTable.AddTypeFromUsingModule(typePairs.Value, moduleInfo[0]);
                    }
                }
                else
                {
                    // if there is no exception, but we didn't find the module then it's not present
                    string moduleText = usingStatementAst.Name != null ? usingStatementAst.Name.Value : usingStatementAst.ModuleSpecification.Extent.Text;
                    _parser.ReportError(usingStatementAst.Extent,
                                        nameof(ParserStrings.ModuleNotFoundDuringParse),
                                        ParserStrings.ModuleNotFoundDuringParse,
                                        moduleText);
                }
            }

            return(AstVisitAction.Continue);
        }
示例#4
0
        public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
        {
            explanations.Add(new Explanation()
            {
                Description     = $"The using statement allows you to specify which namespaces are used in the session. Adding namespaces simplifies usage of .NET classes and member and allows you to import classes from script modules and assemblies. In this case a {usingStatementAst.UsingStatementKind} is loaded.",
                CommandName     = "using statement",
                HelpResult      = HelpTableQuery("about_using"),
                TextToHighlight = "using"
            }.AddDefaults(usingStatementAst, explanations));

            return(base.VisitUsingStatement(usingStatementAst));
        }
示例#5
0
            public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
            {
                // Look for 'using ...;' at the top of scripts

                if (!_targetVersions.Contains(s_v3) && !_targetVersions.Contains(s_v4))
                {
                    return(AstVisitAction.Continue);
                }

                AddDiagnostic(
                    usingStatementAst,
                    "using statement",
                    "using ...;",
                    "3,4");

                return(AstVisitAction.Continue);
            }
示例#6
0
        public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
        {
            if (usingStatementAst.UsingStatementKind == UsingStatementKind.Module)
            {
                Exception exception;
                bool wildcardCharactersUsed;
                bool isConstant;
                var moduleInfo = GetModulesFromUsingModule(usingStatementAst, out exception, out wildcardCharactersUsed, out isConstant);
                if (!isConstant)
                {
                    _parser.ReportError(usingStatementAst.Extent, () => ParserStrings.RequiresArgumentMustBeConstant);
                }
                else if (exception != null)
                {
                    // we re-using RequiresModuleInvalid string, semantic is very similar so it's fine to do that.
                    _parser.ReportError(usingStatementAst.Extent, () => ParserStrings.RequiresModuleInvalid, exception.Message);
                }
                else if (wildcardCharactersUsed)
                {
                    _parser.ReportError(usingStatementAst.Extent, () => ParserStrings.WildCardModuleNameError);
                }
                else if (moduleInfo != null && moduleInfo.Count > 0)
                {
                    // it's ok, if we get more then one module. They are already sorted in the right order
                    // we just need to use the first one

                    // We must add the same objects (in sense of object refs) to usingStatementAst typeTable and to symbolTable.
                    // Later, this same TypeDefinitionAsts would be used in DefineTypes(), by the module, where it was imported from at compile time.
                    var exportedTypes = usingStatementAst.DefineImportedModule(moduleInfo[0]);
                    foreach (var typePairs in exportedTypes)
                    {
                        _symbolTable.AddTypeFromUsingModule(typePairs.Value, moduleInfo[0]);
                    }
                }
                else
                {
                    // if there is no exception, but we didn't find the module then it's not present
                    string moduleText = usingStatementAst.Name != null ? usingStatementAst.Name.Value : usingStatementAst.ModuleSpecification.Extent.Text;
                    _parser.ReportError(usingStatementAst.Extent, () => ParserStrings.ModuleNotFoundDuringParse, moduleText);
                }
            }

            return AstVisitAction.Continue;
        }
 public object VisitUsingStatement(UsingStatementAst usingStatement)
 {
     throw new System.NotImplementedException();
 }
示例#8
0
        /// <summary>
        /// Resolves using module to a collection of PSModuleInfos. Doesn't throw.
        /// PSModuleInfo objects are returned in the right order: i.e. if multiply versions of the module
        /// is presented on the system and user didn't specify version, we will return all of them, but newer one would go first.
        /// </summary>
        /// <param name="usingStatementAst">using statement</param>
        /// <param name="exception">If exception happens, return exception object.</param>
        /// <param name="wildcardCharactersUsed">
        /// True if in the module name uses wildcardCharacter. 
        /// We don't want to resolve any wild-cards in using module.
        /// </param>
        /// <param name="isConstant">True if module hashtable contains constant value (it's our requirement).</param>
        /// <returns>Modules, if can resolve it. null if any problems happens.</returns>
        private Collection<PSModuleInfo> GetModulesFromUsingModule(UsingStatementAst usingStatementAst, out Exception exception, out bool wildcardCharactersUsed, out bool isConstant)
        {
            exception = null;
            wildcardCharactersUsed = false;
            isConstant = true;

            // fullyQualifiedName can be string or hashtable
            object fullyQualifiedName;
            if (usingStatementAst.ModuleSpecification != null)
            {
                object resultObject;
                if (!IsConstantValueVisitor.IsConstant(usingStatementAst.ModuleSpecification, out resultObject, forAttribute: false, forRequires: true))
                {
                    isConstant = false;
                    return null;
                }

                var hashtable = resultObject as System.Collections.Hashtable;
                var ms = new ModuleSpecification();
                exception = ModuleSpecification.ModuleSpecificationInitHelper(ms, hashtable);
                if (exception != null)
                {
                    return null;
                }

                if (WildcardPattern.ContainsWildcardCharacters(ms.Name))
                {
                    wildcardCharactersUsed = true;
                    return null;
                }

                fullyQualifiedName = ms;
            }
            else
            {
                string fullyQualifiedNameStr = usingStatementAst.Name.Value;

                if (WildcardPattern.ContainsWildcardCharacters(fullyQualifiedNameStr))
                {
                    wildcardCharactersUsed = true;
                    return null;
                }

                // case 1: relative path. Relative for file in the same folder should include .\
                bool isPath = fullyQualifiedNameStr.Contains(@"\");
                if (isPath && !LocationGlobber.IsAbsolutePath(fullyQualifiedNameStr))
                {
                    string rootPath = Path.GetDirectoryName(_parser._fileName);
                    if (rootPath != null)
                    {
                        fullyQualifiedNameStr = Path.Combine(rootPath, fullyQualifiedNameStr);
                    }
                }

                // case 2: Module by name
                // case 3: Absolute Path
                // We don't need to do anything for these cases, FullyQualifiedName already handle it.

                fullyQualifiedName = fullyQualifiedNameStr;
            }

            var commandInfo = new CmdletInfo("Get-Module", typeof(GetModuleCommand));
            // TODO(sevoroby): we should consider an async call with cancellation here.
            UsingStatementResolvePowerShell.Commands.Clear();
            try
            {
                return UsingStatementResolvePowerShell.AddCommand(commandInfo)
                    .AddParameter("FullyQualifiedName", fullyQualifiedName)
                    .AddParameter("ListAvailable", true)
                    .Invoke<PSModuleInfo>();
            }
            catch (Exception e)
            {
                exception = e;
                return null;
            }
        }
示例#9
0
 /// <summary/>
 public virtual object VisitUsingStatement(UsingStatementAst usingStatement) { return null; }
示例#10
0
 /// <summary/>
 public virtual AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst) => DefaultVisit(usingStatementAst);
示例#11
0
 public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst) => VisitAst(usingStatementAst);
示例#12
0
 public object VisitUsingStatement(UsingStatementAst usingStatement)
 {
     return(false);
 }
示例#13
0
 public override AstVisitAction VisitUsingStatement(UsingStatementAst ast)
 {
     return(Check(ast));
 }
示例#14
0
 public object VisitUsingStatement(UsingStatementAst usingStatement)
 {
     throw PSTraceSource.NewArgumentException(nameof(usingStatement));
 }
 public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
 {
     return(Visit(usingStatementAst));
 }
示例#16
0
        public override AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
        {
            bool usingKindSupported = usingStatementAst.UsingStatementKind == UsingStatementKind.Namespace ||
                                      usingStatementAst.UsingStatementKind == UsingStatementKind.Assembly ||
                                      usingStatementAst.UsingStatementKind == UsingStatementKind.Module;
            if (!usingKindSupported ||
                usingStatementAst.Alias != null)
            {
                _parser.ReportError(usingStatementAst.Extent, () => ParserStrings.UsingStatementNotSupported);
            }

            return AstVisitAction.Continue;
        }
 object ICustomAstVisitor2.VisitUsingStatement(UsingStatementAst usingStatement)
 => ProcessRewriter(VisitUsingStatement, usingStatement);
示例#18
0
 /// <summary/>
 public virtual object VisitUsingStatement(UsingStatementAst usingStatement)
 {
     return(null);
 }
示例#19
0
 /// <summary/>
 public virtual AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst) { return AstVisitAction.Continue; }
示例#20
0
        /// <summary>
        /// Resolves using module to a collection of PSModuleInfos. Doesn't throw.
        /// PSModuleInfo objects are returned in the right order: i.e. if multiply versions of the module
        /// is presented on the system and user didn't specify version, we will return all of them, but newer one would go first.
        /// </summary>
        /// <param name="usingStatementAst">Using statement.</param>
        /// <param name="exception">If exception happens, return exception object.</param>
        /// <param name="wildcardCharactersUsed">
        /// True if in the module name uses wildcardCharacter.
        /// We don't want to resolve any wild-cards in using module.
        /// </param>
        /// <param name="isConstant">True if module hashtable contains constant value (it's our requirement).</param>
        /// <returns>Modules, if can resolve it. null if any problems happens.</returns>
        private Collection <PSModuleInfo> GetModulesFromUsingModule(UsingStatementAst usingStatementAst, out Exception exception, out bool wildcardCharactersUsed, out bool isConstant)
        {
            exception = null;
            wildcardCharactersUsed = false;
            isConstant             = true;

            // fullyQualifiedName can be string or hashtable
            object fullyQualifiedName;

            if (usingStatementAst.ModuleSpecification != null)
            {
                object resultObject;
                if (!IsConstantValueVisitor.IsConstant(usingStatementAst.ModuleSpecification, out resultObject, forAttribute: false, forRequires: true))
                {
                    isConstant = false;
                    return(null);
                }

                var hashtable = resultObject as System.Collections.Hashtable;
                var ms        = new ModuleSpecification();
                exception = ModuleSpecification.ModuleSpecificationInitHelper(ms, hashtable);
                if (exception != null)
                {
                    return(null);
                }

                if (WildcardPattern.ContainsWildcardCharacters(ms.Name))
                {
                    wildcardCharactersUsed = true;
                    return(null);
                }

                fullyQualifiedName = ms;
            }
            else
            {
                string fullyQualifiedNameStr = usingStatementAst.Name.Value;

                if (WildcardPattern.ContainsWildcardCharacters(fullyQualifiedNameStr))
                {
                    wildcardCharactersUsed = true;
                    return(null);
                }

                // case 1: relative path. Relative for file in the same folder should include .\
                bool isPath = fullyQualifiedNameStr.Contains(@"\");
                if (isPath && !LocationGlobber.IsAbsolutePath(fullyQualifiedNameStr))
                {
                    string rootPath = Path.GetDirectoryName(_parser._fileName);
                    if (rootPath != null)
                    {
                        fullyQualifiedNameStr = Path.Combine(rootPath, fullyQualifiedNameStr);
                    }
                }

                // case 2: Module by name
                // case 3: Absolute Path
                // We don't need to do anything for these cases, FullyQualifiedName already handle it.

                fullyQualifiedName = fullyQualifiedNameStr;
            }

            var commandInfo = new CmdletInfo("Get-Module", typeof(GetModuleCommand));

            // TODO(sevoroby): we should consider an async call with cancellation here.
            UsingStatementResolvePowerShell.Commands.Clear();
            try
            {
                return(UsingStatementResolvePowerShell.AddCommand(commandInfo)
                       .AddParameter("FullyQualifiedName", fullyQualifiedName)
                       .AddParameter("ListAvailable", true)
                       .Invoke <PSModuleInfo>());
            }
            catch (Exception e)
            {
                exception = e;
                return(null);
            }
        }
示例#21
0
 public object VisitUsingStatement(UsingStatementAst usingStatementAst)
 {
     return ExpressionCache.Empty;
 }
示例#22
0
 /// <summary/>
 public virtual AstVisitAction VisitUsingStatement(UsingStatementAst usingStatementAst)
 {
     return(AstVisitAction.Continue);
 }
示例#23
0
 public override UsingStatementAst VisitUsingStatement(UsingStatementAst usingStatement)
 => VisitOther(base.VisitUsingStatement(usingStatement));
示例#24
0
 public object VisitUsingStatement(UsingStatementAst usingStatement)
 {
     return(AutomationNull.Value);
 }
示例#25
0
 public override AstVisitAction VisitUsingStatement(UsingStatementAst ast) { return Check(ast); }