Пример #1
0
        public void BadRequire()
        {
            // foo.js
            //      require('./rec1')
            // rec1\
            //      package.json
            //          { "main": "../rec2" }
            // rec2\
            //      package.json
            //          { "main": "../rec1" }

            var analyzer = new JsAnalyzer();
            var mod      = @"var x = require('./rec1')";

            analyzer.AddPackageJson("rec1\\package.json", "../rec2");
            analyzer.AddPackageJson("rec2\\package.json", "../rec1");

            var sourceUnit = Analysis.GetSourceUnit(mod);
            var entry      = analyzer.AddModule("one.js", null);

            Analysis.Prepare(entry, sourceUnit);

            entry.Analyze(CancellationToken.None);

            Assert.AreEqual(
                0,
                entry.Analysis.GetTypeIdsByIndex("x", 0).Count()
                );
        }
Пример #2
0
        public static JsAnalyzer Analyze(AnalysisLimits limits, Action parseCallback, params AnalysisFile[] files)
        {
            Dictionary <string, IJsProjectEntry> entries = new Dictionary <string, IJsProjectEntry>();
            var analyzer = new JsAnalyzer(limits);

            foreach (var file in files)
            {
                if (Path.GetFileName(file.Filename).Equals("package.json", StringComparison.OrdinalIgnoreCase))
                {
                    analyzer.AddPackageJson(file.Filename, file.Content);
                }
                else
                {
                    var projEntry = analyzer.AddModule(file.Filename);
                    entries[file.Filename] = projEntry;
                }
            }

            foreach (var file in files)
            {
                if (!Path.GetFileName(file.Filename).Equals("package.json", StringComparison.OrdinalIgnoreCase))
                {
                    var source = Analysis.GetSourceUnit(file.Content);
                    Analysis.Prepare(
                        entries[file.Filename],
                        source
                        );
                }
            }

            if (parseCallback != null)
            {
                parseCallback();
            }

            foreach (var file in files)
            {
                if (!Path.GetFileName(file.Filename).Equals("package.json", StringComparison.OrdinalIgnoreCase))
                {
                    ((IGroupableAnalysisProjectEntry)entries[file.Filename]).Analyze(CancellationToken.None, true);
                }
            }

            foreach (var file in files)
            {
                IJsProjectEntry projEntry;
                if (entries.TryGetValue(file.Filename, out projEntry))
                {
                    projEntry.AnalysisGroup.AnalyzeQueuedEntries(CancellationToken.None);
                    break;
                }
            }

            return(analyzer);
        }
Пример #3
0
        public void RequireNodeModules()
        {
            var mod1 = @"exports.foo = 42;";
            var mod2 = @"x = require('one.js').foo";

            var sourceUnit1 = Analysis.GetSourceUnit(mod1);
            var sourceUnit2 = Analysis.GetSourceUnit(mod2);
            var state       = new JsAnalyzer();
            var entry1      = state.AddModule("node_modules\\one.js", null);
            var entry2      = state.AddModule("two.js", null);

            Analysis.Prepare(entry1, sourceUnit1);
            Analysis.Prepare(entry2, sourceUnit2);

            entry1.Analyze(CancellationToken.None);
            entry2.Analyze(CancellationToken.None);

            AssertUtil.ContainsExactly(
                entry2.Analysis.GetTypeIdsByIndex("x", mod2.Length),
                BuiltinTypeId.Number
                );
        }
Пример #4
0
        public void RequireNonRelativePackageJson()
        {
            var analyzer = new JsAnalyzer();
            var mod      = @"var x = require('./rec1')";
            var myindex  = @"exports.abc = 100;";

            analyzer.AddPackageJson("rec1\\package.json", "lib/myindex.js");

            var sourceUnit        = Analysis.GetSourceUnit(mod);
            var myindexSourceUnit = Analysis.GetSourceUnit(myindex);
            var entry             = analyzer.AddModule("one.js", null);
            var myindexEntry      = analyzer.AddModule("rec1\\lib\\myindex.js", null);

            Analysis.Prepare(entry, sourceUnit);
            Analysis.Prepare(myindexEntry, myindexSourceUnit);

            entry.Analyze(CancellationToken.None);
            myindexEntry.Analyze(CancellationToken.None);

            AssertUtil.ContainsExactly(
                entry.Analysis.GetTypeIdsByIndex("x.abc", 0),
                BuiltinTypeId.Number
                );
        }