示例#1
0
    public void ShouldCorrectlyGatherSourceInfo(BobrilSourceInfoTestData testData)
    {
        var outFiles = BobrilSourceInfoTestCore(testData);

        foreach (var(name, genContent) in outFiles)
        {
            var expected = testData.InputContent.TryGetValue("out/" + name, out var content) ? content : "";
            Assert.Equal(expected, genContent);
        }
    }
示例#2
0
    public static Dictionary <string, string> BobrilSourceInfoTestCore(BobrilSourceInfoTestData testData)
    {
        var output = new Dictionary <string, string>();

        var source   = SourceMap.RemoveLinkToSourceMap(testData.InputContent["index.js"]);
        var toplevel = Parser.Parse(source);

        toplevel.FigureOutScope();
        var files      = new InMemoryImportResolver();
        var ctx        = new ResolvingConstEvalCtx("index.js", files);
        var sourceInfo = GatherBobrilSourceInfo.Gather(toplevel, ctx,
                                                       (myctx, text) =>
        {
            if (text.StartsWith('.'))
            {
                return(PathUtils.Join(PathUtils.Parent(myctx.SourceName), text));
            }
            return(text);
        });

        var builder = new SourceMapBuilder();
        var adder   = builder.CreateSourceAdder(source,
                                                testData.InputContent.ContainsKey("index.js.map")
                ? SourceMap.Parse(testData.InputContent["index.js.map"], ".")
                : null);
        var sourceReplacer = new SourceReplacer();

        ProcessReplacements(sourceReplacer, sourceInfo);
        sourceReplacer.Apply(adder);
        builder.AddText("//# sourceMappingURL=index.js.map");
        output["index.sourceinfo.json"] = JsonSerializer
                                          .Serialize(sourceInfo, new JsonSerializerOptions {
            WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
        })
                                          .Replace("\r\n", "\n");
        output["index.js"]     = builder.Content();
        output["index.js.map"] = builder.Build(".", "..").ToString();

        if (testData.InputContent.ContainsKey("index.js.map"))
        {
            SourceMap.Parse(testData.InputContent["index.js.map"], ".").ResolveInAst(toplevel);
        }

        var coverageInstrumentation = new CoverageInstrumentation();

        toplevel = coverageInstrumentation.Instrument(toplevel);
        coverageInstrumentation.AddCountingHelpers(toplevel);

        coverageInstrumentation.CleanUp(new TestUtf8Reader(testData.InputContent));

        builder = new SourceMapBuilder();
        toplevel.PrintToBuilder(builder, new OutputOptions {
            Beautify = true
        });
        builder.AddText("//# sourceMappingURL=cov.js.map");
        output["cov.info.json"] = JsonSerializer
                                  .Serialize(coverageInstrumentation.InstrumentedFiles,
                                             new JsonSerializerOptions {
            WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
        })
                                  .Replace("\r\n", "\n");
        output["cov.js"]     = builder.Content();
        output["cov.js.map"] = builder.Build(".", "..").ToString();

        return(output);
    }