Code with Misuse: |
class SourceMap2 {
/**
* Adds a mapping for the given node. Mappings must be added in order.
*
* @param node The node that the new mapping represents.
* @param startPosition The position on the starting line
* @param endPosition The position on the ending line.
*/
public void addMapping(Node node, Position startPosition, Position endPosition) {
String sourceFile = (String)node.getProp(Node.SOURCENAME_PROP);
// If the node does not have an associated source file or
// its line number is -1, then the node does not have sufficient
// information for a mapping to be useful.
if (sourceFile == null || node.getLineno() < 0) {
return;
}
if (sourceFile != lastSourceFile) {
lastSourceFile = sourceFile;
Integer index = source_file_map.get(sourceFile);
if (index != null) {
lastSourceFileIndex = index;
} else {
lastSourceFileIndex = source_file_map.size();
source_file_map.put(sourceFile, lastSourceFileIndex);
}
}
// Create the new mapping.
Mapping mapping = new Mapping();
mapping.sourceFile = lastSourceFileIndex;
mapping.originalPosition = new Position(node.getLineno(), node.getCharno());
String originalName = (String)node.getProp(Node.ORIGINALNAME_PROP);
if (originalName != null) {
mapping.originalName = originalName;
}
if (offsetPosition.getLineNumber() == 0
&& offsetPosition.getCharacterIndex() == 0) {
mapping.startPosition = startPosition;
mapping.endPosition = endPosition;
} else {
// If the mapping is found on the first line, we need to offset
// its character position by the number of characters found on
// the *last* line of the source file to which the code is
// being generated.
int offsetLine = offsetPosition.getLineNumber();
int startOffsetPosition = offsetPosition.getCharacterIndex();
int endOffsetPosition = offsetPosition.getCharacterIndex();
if (startPosition.getLineNumber() > 0) {
startOffsetPosition = 0;
}
if (endPosition.getLineNumber() > 0) {
endOffsetPosition = 0;
}
mapping.startPosition =
new Position(startPosition.getLineNumber() + offsetLine,
startPosition.getCharacterIndex() + startOffsetPosition);
mapping.endPosition =
new Position(endPosition.getLineNumber() + offsetLine,
endPosition.getCharacterIndex() + endOffsetPosition);
}
// Validate the mappings are in a proper order.
if (lastMapping != null) {
int lastLine = lastMapping.startPosition.getLineNumber();
int lastColumn = lastMapping.startPosition.getCharacterIndex();
int nextLine = mapping.startPosition.getLineNumber();
int nextColumn = mapping.startPosition.getCharacterIndex();
Preconditions.checkState(nextLine > lastLine
|| (nextLine == lastLine && nextColumn >= lastColumn),
"Incorrect source mappings order, previous : (%s,%s)\n"
+ "new : (%s,%s)\nnode : %s",
lastLine, lastColumn, nextLine, nextColumn, node);
}
lastMapping = mapping;
mappings.add(mapping);
}
}
|