DevTools: improve javascript formatter to support "return" properties.

The uglifier tokenizer, which is used under the hood in javascript
formatter, is not clever enough to figure out that the "return" word
in "a.return" is not a "return" statement, but identifier.
This patch works around this issue via verifying the token value after
regex to be a valid identifier.

BUG=218470
R=vsevik, apavlov

Review URL: https://codereview.chromium.org/479033002

git-svn-id: svn://svn.chromium.org/blink/trunk@180467 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 4d583b29
......@@ -12,6 +12,7 @@ function f() {
</script><script>
function g() {
**var a;
**window.return = 10;
**if (a)
****return;
}
......
......@@ -88,7 +88,7 @@ var test = function()
next();
};
var content = "<html><body><script>function f(){}<" + "/script><script>function g(){var a;if (a) return;}<" + "/script></body></html>";
var content = "<html><body><script>function f(){}<" + "/script><script>function g(){var a;window.return = 10;if (a) return;}<" + "/script></body></html>";
worker.postMessage({ method: "format", params: { mimeType: "text/html", content: content, indentString: "**" } });
}
]);
......
......@@ -41,6 +41,8 @@ FormatterWorker.JavaScriptFormatter = function(tokenizer, builder)
this._nextToken = this._tokenizer.next();
}
FormatterWorker.JavaScriptFormatter._identifierRegex = /^[$A-Z_][0-9A-Z_$]*$/i;
FormatterWorker.JavaScriptFormatter.prototype = {
format: function()
{
......@@ -91,6 +93,13 @@ FormatterWorker.JavaScriptFormatter.prototype = {
throw "Unexpected token: expected " + token + ", actual " + next;
},
_expectGeneralIdentifier: function()
{
var next = this._next();
if (next !== FormatterWorker.JavaScriptTokens.IDENTIFIER && !FormatterWorker.JavaScriptFormatter._identifierRegex.test(this._token.value))
throw "Unexpected token: expected javascript identifier, actual " + this._token.value;
},
_expectSemicolon: function()
{
if (this._peek() === FormatterWorker.JavaScriptTokens.SEMICOLON)
......@@ -522,7 +531,7 @@ FormatterWorker.JavaScriptFormatter.prototype = {
case FormatterWorker.JavaScriptTokens.PERIOD:
this._consume(FormatterWorker.JavaScriptTokens.PERIOD);
this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER);
this._expectGeneralIdentifier();
break;
default:
......@@ -563,7 +572,7 @@ FormatterWorker.JavaScriptFormatter.prototype = {
case FormatterWorker.JavaScriptTokens.PERIOD:
this._consume(FormatterWorker.JavaScriptTokens.PERIOD);
this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER);
this._expectGeneralIdentifier();
break;
case FormatterWorker.JavaScriptTokens.LPAREN:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment