Debian Codegraph @

src/com/google/javascript/jscomp/SourceMap.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Predicate;
20 import com.google.common.collect.Maps;
21 import com.google.debugging.sourcemap.FilePosition;
22 import com.google.debugging.sourcemap.SourceMapFormat;
23 import com.google.debugging.sourcemap.SourceMapGenerator;
24 import com.google.debugging.sourcemap.SourceMapGeneratorFactory;
25 import com.google.debugging.sourcemap.SourceMapGeneratorV1;
26 import com.google.debugging.sourcemap.SourceMapGeneratorV2;
27 import com.google.javascript.rhino.Node;
28
29 import java.io.IOException;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33
34 /**
35 * Collects information mapping the generated (compiled) source back to
36 * its original source for debugging purposes.
37 *
38 * @see CodeConsumer
39 * @see CodeGenerator
40 * @see CodePrinter
41 *
42 */
43 public class SourceMap {
44
45 public static enum Format {
46 V1 {
47 @Override SourceMap getInstance() {
48 return new SourceMap(
49 SourceMapGeneratorFactory.getInstance(SourceMapFormat.V1));
50 }
51 },
52 DEFAULT {
53 @Override SourceMap getInstance() {
54 return new SourceMap(
55 SourceMapGeneratorFactory.getInstance(SourceMapFormat.DEFAULT));
56 }
57 },
58 V2 {
59 @Override SourceMap getInstance() {
60 return new SourceMap(
61 SourceMapGeneratorFactory.getInstance(SourceMapFormat.V2));
62 }
63 },
64 V3 {
65 @Override SourceMap getInstance() {
66 return new SourceMap(
67 SourceMapGeneratorFactory.getInstance(SourceMapFormat.V3));
68 }
69 };
70 abstract SourceMap getInstance();
71 }
72
73 /**
74 * Source maps can be very large different levels of detail can be specified.
75 */
76 public static enum DetailLevel implements Predicate<Node> {
77 // ALL is best when the fullest details are needed for debugging or for
78 // code-origin analysis.
79 ALL {
80 @Override public boolean apply(Node node) {
81 return true;
82 }
83 },
84 // SYMBOLS is intended to be used for stack trace deobfuscation when full
85 // detail is not needed.
86 SYMBOLS {
87 @Override public boolean apply(Node node) {
88 return node.isCall()
89 || node.isNew()
90 || node.isFunction()
91 || node.isName()
92 || NodeUtil.isGet(node)
93 || NodeUtil.isObjectLitKey(node)
94 || (node.isString() && NodeUtil.isGet(node.getParent()));
95 }
96 };
97 }
98
99 public static class LocationMapping {
100 final String prefix;
101 final String replacement;
102 public LocationMapping(String prefix, String replacement) {
103 this.prefix = prefix;
104 this.replacement = replacement;
105 }
106 }
107
108 private final SourceMapGenerator generator;
109 private List<LocationMapping> prefixMappings = Collections.emptyList();
110 private final Map<String, String> sourceLocationFixupCache =
111 Maps.newHashMap();
112
113 private SourceMap(SourceMapGenerator generator) {
114 this.generator = generator;
115 }
116
117 public void addMapping(
118 Node node,
119 FilePosition outputStartPosition,
120 FilePosition outputEndPosition) {
121 String sourceFile = node.getSourceFileName();
122
123 // If the node does not have an associated source file or
124 // its line number is -1, then the node does not have sufficient
125 // information for a mapping to be useful.
126 if (sourceFile == null || node.getLineno() < 0) {
127 return;
128 }
129
130 sourceFile = fixupSourceLocation(sourceFile);
131
132 String originalName = (String) node.getProp(Node.ORIGINALNAME_PROP);
133
134 // Strangely, Rhino source lines are one based but columns are
135 // zero based.
136 // We don't change this for the v1 or v2 source maps but for
137 // v3 we make them both 0 based.
138 int lineBaseOffset = 1;
139 if (generator instanceof SourceMapGeneratorV1
140 || generator instanceof SourceMapGeneratorV2) {
141 lineBaseOffset = 0;
142 }
143
144 generator.addMapping(
145 sourceFile, originalName,
146 new FilePosition(node.getLineno() - lineBaseOffset, node.getCharno()),
147 outputStartPosition, outputEndPosition);
148 }
149
150 /**
151 * @param sourceFile The source file location to fixup.
152 * @return a remapped source file.
153 */
154 private String fixupSourceLocation(String sourceFile) {
155 if (prefixMappings.isEmpty()) {
156 return sourceFile;
157 }
158
159 String fixed = sourceLocationFixupCache.get(sourceFile);
160 if (fixed != null) {
161 return fixed;
162 }
163
164 // Replace the first prefix found with its replacement
165 for (LocationMapping mapping : prefixMappings) {
166 if (sourceFile.startsWith(mapping.prefix)) {
167 fixed = mapping.replacement + sourceFile.substring(
168 mapping.prefix.length());
169 break;
170 }
171 }
172
173 // If none of the mappings match then use the original file path.
174 if (fixed == null) {
175 fixed = sourceFile;
176 }
177
178 sourceLocationFixupCache.put(sourceFile, fixed);
179 return fixed;
180 }
181
182 public void appendTo(Appendable out, String name) throws IOException {
183 generator.appendTo(out, name);
184 }
185
186 public void reset() {
187 generator.reset();
188 sourceLocationFixupCache.clear();
189 }
190
191 public void setStartingPosition(int offsetLine, int offsetIndex) {
192 generator.setStartingPosition(offsetLine, offsetIndex);
193 }
194
195 public void setWrapperPrefix(String prefix) {
196 generator.setWrapperPrefix(prefix);
197 }
198
199 public void validate(boolean validate) {
200 generator.validate(validate);
201 }
202
203 /**
204 * @param sourceMapLocationMappings
205 */
206 public void setPrefixMappings(List<LocationMapping> sourceMapLocationMappings) {
207 this.prefixMappings = sourceMapLocationMappings;
208 }
209 }