Improve locationData of implicit object braces.

Set location of generated left brace to start of content, so the corresponding AST nodes don't span preceding spaces, blank lines or comments.
This commit is contained in:
Marc Häfner
2013-03-05 00:07:14 +01:00
parent ebff9fbc31
commit 74181c0ec0
2 changed files with 22 additions and 14 deletions

View File

@@ -331,22 +331,25 @@
Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
return this.scanTokens(function(token, i, tokens) {
var last_column, last_line, _ref, _ref1, _ref2;
var column, line, nextLocation, prevLocation, _ref, _ref1;
if (token[2]) {
return 1;
}
if (!(token.generated || token.explicit)) {
return 1;
}
_ref2 = (_ref = (_ref1 = tokens[i - 1]) != null ? _ref1[2] : void 0) != null ? _ref : {
last_line: 0,
last_column: 0
}, last_line = _ref2.last_line, last_column = _ref2.last_column;
if (token[0] === '{' && (nextLocation = (_ref = tokens[i + 1]) != null ? _ref[2] : void 0)) {
line = nextLocation.first_line, column = nextLocation.first_column;
} else if (prevLocation = (_ref1 = tokens[i - 1]) != null ? _ref1[2] : void 0) {
line = prevLocation.last_line, column = prevLocation.last_column;
} else {
line = column = 0;
}
token[2] = {
first_line: last_line,
first_column: last_column,
last_line: last_line,
last_column: last_column
first_line: line,
first_column: column,
last_line: line,
last_column: column
};
return 1;
});

View File

@@ -325,12 +325,17 @@ class exports.Rewriter
@scanTokens (token, i, tokens) ->
return 1 if token[2]
return 1 unless token.generated or token.explicit
{last_line, last_column} = tokens[i - 1]?[2] ? last_line: 0, last_column: 0
if token[0] is '{' and nextLocation=tokens[i + 1]?[2]
{first_line: line, first_column: column} = nextLocation
else if prevLocation = tokens[i - 1]?[2]
{last_line: line, last_column: column} = prevLocation
else
line = column = 0
token[2] =
first_line: last_line
first_column: last_column
last_line: last_line
last_column: last_column
first_line: line
first_column: column
last_line: line
last_column: column
1
# Because our grammar is LALR(1), it can't handle some single-line