Bug 964706 - Support getter and setter literal syntax in the debugger's pretty printer; r=benvie

This commit is contained in:
Nick Fitzgerald 2014-02-15 12:41:01 -08:00
Родитель 59d91c4a63
Коммит c28afba469
2 изменённых файлов: 50 добавлений и 4 удалений

Просмотреть файл

@ -194,7 +194,7 @@
* tokens.
*
* @param Object token
* The token we want to determine if it is an array literal.
* The current token.
* @param Object lastToken
* The last token we added to the pretty printed results.
*
@ -217,6 +217,30 @@
return true;
}
/**
* Determine if we have encountered a getter or setter.
*
* @param Object token
* The current token. If this is a getter or setter, it would be the
* property name.
* @param Object lastToken
* The last token we added to the pretty printed results. If this is a
* getter or setter, it would be the `get` or `set` keyword
* respectively.
* @param Array stack
* The stack of open parens/curlies/brackets/etc.
*
* @returns Boolean
* True if this is a getter or setter.
*/
function isGetterOrSetter(token, lastToken, stack) {
return stack[stack.length - 1] == "{"
&& lastToken
&& lastToken.type.type == "name"
&& (lastToken.value == "get" || lastToken.value == "set")
&& token.type.type == "name";
}
/**
* Determine if we should add a newline after the given token.
*
@ -359,12 +383,13 @@
var ttk = token.type.keyword;
var ttt = token.type.type;
var newlineAdded = addedNewline;
var ltt = lastToken ? lastToken.type.type : null;
// Handle whitespace and newlines after "}" here instead of in
// `isLineDelimiter` because it is only a line delimiter some of the
// time. For example, we don't want to put "else if" on a new line after
// the first if's block.
if (lastToken && lastToken.type.type == "}") {
if (lastToken && ltt == "}") {
if (ttk == "while" && stack[stack.length - 1] == "do") {
write(" ",
lastToken.startLoc.line,
@ -387,13 +412,19 @@
}
}
if (isGetterOrSetter(token, lastToken, stack)) {
write(" ",
lastToken.startLoc.line,
lastToken.startLoc.column);
}
if (ttt == ":" && stack[stack.length - 1] == "?") {
write(" ",
lastToken.startLoc.line,
lastToken.startLoc.column);
}
if (lastToken && lastToken.type.type != "}" && ttk == "else") {
if (lastToken && ltt != "}" && ttk == "else") {
write(" ",
lastToken.startLoc.line,
lastToken.startLoc.column);

Просмотреть файл

@ -6,6 +6,7 @@
var prettyFast = this.prettyFast || require("./pretty-fast");
var testCases = [
{
name: "Simple function",
input: "function foo() { bar(); }",
@ -431,6 +432,19 @@ var testCases = [
output: "new F()\n"
},
{
name: "Getter and setter literals",
input: "var obj={get foo(){return this._foo},set foo(v){this._foo=v}}",
output: "var obj = {\n" +
" get foo() {\n" +
" return this._foo\n" +
" },\n" +
" set foo(v) {\n" +
" this._foo = v\n" +
" }\n" +
"}\n"
},
];
var sourceMap = this.sourceMap || require("source-map");
@ -468,6 +482,7 @@ function run_test() {
// Only run the tests if this is node and we are running this file
// directly. (Firefox's test runner will import this test file, and then call
// run_test itself.)
if (typeof exports == "object") {
if (typeof require == "function" && typeof module == "object"
&& require.main === module) {
run_test();
}