IdentitySourceMapGenerator { public String generateSourceMap() throws IOException { final String sourceMethod = "generateSourceMap"; final boolean isTraceLogging = log.isLoggable(Level.FINER); if (isTraceLogging) { log.entering(sourceClass, sourceMethod, new Object[]{name}); } String result = null; lineLengths = getLineLengths(); Compiler compiler = new Compiler(); sgen = new SourceMapGeneratorV3(); compiler.initOptions(compiler_options); currentLine = currentChar = 0; Node node = compiler.parse(JSSourceFile.fromCode(name, source)); if (compiler.hasErrors()) { if (log.isLoggable(Level.WARNING)) { JSError[] errors = compiler.getErrors(); for (JSError error : errors) { log.logp(Level.WARNING, sourceClass, sourceMethod, error.toString()); } } } if (node != null) { processNode(node); StringWriter writer = new StringWriter(); sgen.appendTo(writer, name); result = writer.toString(); } sgen = null; lineLengths = null; if (isTraceLogging) { log.exiting(sourceClass, sourceMethod, result); } return result; } IdentitySourceMapGenerator(String name, String source, CompilerOptions compiler_options); String generateSourceMap();  }
@Test public void test() throws Exception { CompilerOptions options = CompilerUtil.getDefaultOptions(); IdentitySourceMapGenerator gen = new IdentitySourceMapGenerator("test", source, options); String sm = gen.generateSourceMap(); System.out.println(sm); SourceMapConsumerV3 smcon = new SourceMapConsumerV3(); smcon.parse(sm); Collection<String> sources = smcon.getOriginalSources(); Assert.assertEquals(Arrays.asList("test"), sources); int[][] expectedMappings = new int[][]{ new int[]{4,1}, new int[]{4,8}, new int[]{5,3}, new int[]{6,3}, new int[]{7,4}, new int[]{7,13}, new int[]{7,18}, new int[]{7,23}, new int[]{8,3}, new int[]{8,7}, new int[]{8,20}, new int[]{10,3}, new int[]{10,11}, new int[]{10,15}, new int[]{11,3}, new int[]{11,7}, new int[]{12,15}, new int[]{12,21}, new int[]{12,48} }; for (int[] expected : expectedMappings) { OriginalMapping mapping = smcon.getMappingForLine(expected[0], expected[1]); Assert.assertEquals(expected[0], mapping.getLineNumber()); Assert.assertEquals(expected[1], mapping.getColumnPosition()); } }