| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 75 |
|
| 76 |
public static enum DetailLevel implements Predicate<Node> { |
| 77 |
|
| 78 |
|
| 79 |
ALL { |
| 80 |
@Override public boolean apply(Node node) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
}, |
| 84 |
|
| 85 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 152 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 205 |
|
| 206 |
public void setPrefixMappings(List<LocationMapping> sourceMapLocationMappings) { |
| 207 |
this.prefixMappings = sourceMapLocationMappings; |
| 208 |
} |
| 209 |
} |