Example #1
0
        /// <summary>
        /// Populates current scope with a given <paramref name="symbolTable"/>.
        /// </summary>
        internal void PopulateFromSymbolTable([CanBeNull] ISymbolTable symbolTable)
        {
            // The binder in some cases optimizes the local table block and does not create a symbol table at all
            if (symbolTable == null)
            {
                return;
            }

            foreach (var kvp in symbolTable)
            {
                var symbol      = kvp.Value;
                var declaration = symbol.DeclarationList.First();
                if (declaration.Kind != TypeScript.Net.Types.SyntaxKind.VariableDeclaration && declaration.Kind != TypeScript.Net.Types.SyntaxKind.Parameter)
                {
                    // Need to register only variable declarations!
                    continue;
                }

                // TODO: next statment will fail even for valid names.
                // For instance, $ is not a valid character for SymbolAtom but it is a valid identifier.
                // This logic needs to be revisit.
                var atom = SymbolAtom.Create(m_stringTable, declaration.Name.Text);

                var location = declaration.Location(m_sourceFile, m_sourceFilePath, m_pathTable);

                bool isConstant = NodeUtilities.IsConst(declaration);

                var index = AddVariable(atom, location, isConstant);
                Contract.Assume(
                    index != null,
                    I($"Found duplicate variable '{declaration.Name.Text}' in a source file. This should never happen because binder should fail on it!"));
            }
        }
Example #2
0
        public void VarBindingIsNotConstant()
        {
            string code =
                @"var x = 42";

            var node = ParsingHelper.ParseFirstStatementFrom <IVariableStatement>(code);

            Assert.False(NodeUtilities.IsConst(node));
        }
Example #3
0
        public void ConstBindingIsConstant()
        {
            string code =
                @"const x = 42";

            var node = ParsingHelper.ParseFirstStatementFrom <IVariableStatement>(code);

            Assert.True(NodeUtilities.IsConst(node));
        }
        private static void AnalyzeVariableStatement(INode node, DiagnosticContext context)
        {
            var statement = node.Cast <IVariableStatement>();

            if (statement.IsTopLevelOrNamespaceLevelDeclaration() && !NodeUtilities.IsConst(statement))
            {
                context.Logger.ReportOnlyConstBindingOnNamespaceLevel(
                    context.LoggingContext,
                    statement.LocationForLogging(context.SourceFile));
            }
        }
Example #5
0
 /// <summary>
 /// Returns true if <paramref name="node"/> is a const enum declaration.
 /// </summary>
 public static bool IsConstEnumDeclaration(this INode node)
 {
     return(node.Kind == SyntaxKind.EnumDeclaration && NodeUtilities.IsConst(node));
 }