Search in sources :

Example 11 with OriginalMapping

use of com.google.debugging.sourcemap.proto.Mapping.OriginalMapping in project closure-compiler by google.

the class SourceMapTestCase method check.

protected void check(Map<String, String> originalInputs, String generatedSource, String sourceMapFileContent, SourceMapSupplier supplier) {
    // Find all instances of the __XXX__ pattern in the original
    // source code.
    Map<String, Token> originalTokens = findTokens(originalInputs);
    // Find all instances of the __XXX__ pattern in the generated
    // source code.
    Map<String, Token> resultTokens = findTokens(generatedSource);
    // Ensure that the generated instances match via the source map
    // to the original source code.
    // Ensure the token counts match.
    assertThat(resultTokens).hasSize(originalTokens.size());
    SourceMapping reader;
    try {
        reader = SourceMapConsumerFactory.parse(sourceMapFileContent, supplier);
    } catch (SourceMapParseException e) {
        throw new RuntimeException("unexpected exception", e);
    }
    // input source and ensure that the map is correct.
    for (Token token : resultTokens.values()) {
        OriginalMapping mapping = reader.getMappingForLine(token.position.getLine() + 1, token.position.getColumn() + 1);
        assertThat(mapping).isNotNull();
        // Find the associated token in the input source.
        Token inputToken = originalTokens.get(token.tokenName);
        assertThat(inputToken).isNotNull();
        assertThat(inputToken.inputName).isEqualTo(mapping.getOriginalFile());
        // Ensure that the map correctly points to the token (we add 1
        // to normalize versus the Rhino line number indexing scheme).
        assertWithMessage("Checking line for token %s ", token.tokenName).that(inputToken.position.getLine() + 1).isEqualTo(mapping.getLineNumber());
        int start = inputToken.position.getColumn() + 1;
        if (inputToken.tokenName.startsWith("STR")) {
            // include the preceding quote.
            start--;
        }
        if (validateColumns) {
            assertWithMessage("Checking column for token %s ", token.tokenName).that(mapping.getColumnPosition()).isEqualTo(start);
        }
        // string) it has an original name.
        if (!inputToken.tokenName.startsWith("STR")) {
            assertWithMessage("missing name for %s", inputToken.tokenName).that(mapping.getIdentifier()).isNotEmpty();
        }
        // Ensure that if the mapping has a name, it matches the token.
        if (!mapping.getIdentifier().isEmpty()) {
            assertThat("__" + inputToken.tokenName + "__").isEqualTo(mapping.getIdentifier());
        }
    }
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) TypeToken(com.google.common.reflect.TypeToken)

Example 12 with OriginalMapping

use of com.google.debugging.sourcemap.proto.Mapping.OriginalMapping in project closure-compiler by google.

the class CoverageInstrumenterTest method testCompilerSupplier_instruments.

// Tests for CompilerSupplier
@Test
public void testCompilerSupplier_instruments() throws Exception {
    CoverageInstrumenter.CompileResult result = compiler.compile(SOURCE_JS, "var x = 42;");
    String[] expected = new String[] { "if(!self.window){self.window=self;self.window.top=self}", "var __jscov=window.top[\"__jscov\"]||", "(window.top[\"__jscov\"]={\"fileNames\":[],\"instrumentedLines\":[],\"executedLines\":[]});", "var JSCompiler_lcov_data_source_js=[];", "__jscov[\"executedLines\"].push(JSCompiler_lcov_data_source_js);", "__jscov[\"instrumentedLines\"].push(\"01\");", "__jscov[\"fileNames\"].push(\"source.js\");", "JSCompiler_lcov_data_source_js[0]=true;var x=42;" };
    assertThat(result.source).isEqualTo(Joiner.on("").join(expected));
    assertThat(result.errors).isEmpty();
    assertThat(result.transformed).isTrue();
    // Ensure that source map for "x" is correct.
    SourceMapConsumerV3 sourceMap = new SourceMapConsumerV3();
    sourceMap.parse(result.sourceMap);
    OriginalMapping mappingFoX = sourceMap.getMappingForLine(1, result.source.indexOf("x=42") + 1);
    assertThat(mappingFoX).isEqualTo(OriginalMapping.newBuilder().setOriginalFile(SOURCE_JS.toString()).setLineNumber(1).setColumnPosition(5).setIdentifier("x").setPrecision(Precision.EXACT).build());
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) Test(org.junit.Test)

Example 13 with OriginalMapping

use of com.google.debugging.sourcemap.proto.Mapping.OriginalMapping in project closure-compiler by google.

the class CompilerTest method testApplyInputSourceMaps.

@Test
public void testApplyInputSourceMaps() throws Exception {
    FilePosition originalSourcePosition = new FilePosition(17, 25);
    ImmutableMap<String, SourceMapInput> inputSourceMaps = ImmutableMap.of("input.js", sourcemap("input.js.map", "input.ts", originalSourcePosition));
    CompilerOptions options = new CompilerOptions();
    options.setLanguageIn(LanguageMode.ECMASCRIPT3);
    options.sourceMapOutputPath = "fake/source_map_path.js.map";
    options.inputSourceMaps = inputSourceMaps;
    options.applyInputSourceMaps = true;
    Compiler compiler = new Compiler();
    compiler.compile(EMPTY_EXTERNS.get(0), SourceFile.fromCode("input.js", "// Unmapped line\nvar x = 1;\nalert(x);"), options);
    assertThat(compiler.toSource()).isEqualTo("var x=1;alert(x);");
    SourceMap sourceMap = compiler.getSourceMap();
    StringWriter out = new StringWriter();
    sourceMap.appendTo(out, "source.js.map");
    SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
    consumer.parse(out.toString());
    // Column 5 contains the first actually mapped code ('x').
    OriginalMapping mapping = consumer.getMappingForLine(1, 5);
    assertThat(mapping.getOriginalFile()).isEqualTo("input.ts");
    // FilePosition above is 0-based, whereas OriginalMapping is 1-based, thus 18 & 26.
    assertThat(mapping.getLineNumber()).isEqualTo(18);
    assertThat(mapping.getColumnPosition()).isEqualTo(26);
    assertThat(mapping.getIdentifier()).isEqualTo("testSymbolName");
    assertThat(consumer.getOriginalSources()).containsExactly("input.js", "input.ts");
    assertThat(consumer.getOriginalSourcesContent()).isNull();
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) StringWriter(java.io.StringWriter) SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) FilePosition(com.google.debugging.sourcemap.FilePosition) Test(org.junit.Test)

Aggregations

OriginalMapping (com.google.debugging.sourcemap.proto.Mapping.OriginalMapping)13 Test (org.junit.Test)4 SourceMapConsumerV3 (com.google.debugging.sourcemap.SourceMapConsumerV3)3 Node (com.google.javascript.rhino.Node)3 GwtIncompatible (com.google.common.annotations.GwtIncompatible)2 FilePosition (com.google.debugging.sourcemap.FilePosition)2 JsonWriter (com.google.gson.stream.JsonWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 TypeToken (com.google.common.reflect.TypeToken)1 Builder (com.google.debugging.sourcemap.proto.Mapping.OriginalMapping.Builder)1 Builder (com.google.javascript.jscomp.JsMessage.Builder)1 ErrorWithLevel (com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel)1 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Nullable (javax.annotation.Nullable)1