<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[172722] trunk/Source/WebInspectorUI</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/172722">172722</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2014-08-18 13:54:55 -0700 (Mon, 18 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: Introduce an inspector Abstract Syntax Tree.
https://bugs.webkit.org/show_bug.cgi?id=135763

Patch by Saam Barati &lt;sbarati@apple.com&gt; on 2014-08-18
Reviewed by Joseph Pecoraro.

This patch adds an Abstract Syntax Tree to the Web inspector.
This syntax tree is modeled off the Esprima.js syntax tree
which complies with the Mozilla Parser API:
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
Script is the owner of the Web Inspector syntax tree, and has an API
for accessing its syntax tree property.

* UserInterface/External/Esprima/esprima.js:
* UserInterface/Main.html:
* UserInterface/Models/Script.js:
(WebInspector.Script):
(WebInspector.Script.prototype.requestScriptSyntaxTree.makeSyntaxTreeAndCallCallback):
(WebInspector.Script.prototype._resolveResource):
(WebInspector.Script.prototype._makeSyntaxTree):
* UserInterface/Models/ScriptSyntaxTree.js: Added.
(WebInspector.ScriptSyntaxTree):
(WebInspector.ScriptSyntaxTree.prototype.get parsedSuccessfully):
(WebInspector.ScriptSyntaxTree.prototype.forEachNode):
(WebInspector.ScriptSyntaxTree.prototype.):
(WebInspector.ScriptSyntaxTree.prototype.filter):
(WebInspector.ScriptSyntaxTree.prototype.filterForNodesInRange):
(WebInspector.ScriptSyntaxTree.prototype.filterByRange):
(WebInspector.ScriptSyntaxTree.prototype.containsNonEmptyReturnStatement.removeFunctionsFilter):
(WebInspector.ScriptSyntaxTree.prototype.containsNonEmptyReturnStatement):
(WebInspector.ScriptSyntaxTree.prototype.updateTypes):
(WebInspector.ScriptSyntaxTree.prototype._defaultParserState):
(WebInspector.ScriptSyntaxTree.prototype._recurse):
(WebInspector.ScriptSyntaxTree.prototype._recurseArray):
(WebInspector.ScriptSyntaxTree.prototype._createInternalSyntaxTree):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceExternalEsprimaesprimajs">trunk/Source/WebInspectorUI/UserInterface/External/Esprima/esprima.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceMainhtml">trunk/Source/WebInspectorUI/UserInterface/Main.html</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceModelsScriptjs">trunk/Source/WebInspectorUI/UserInterface/Models/Script.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceModelsScriptSyntaxTreejs">trunk/Source/WebInspectorUI/UserInterface/Models/ScriptSyntaxTree.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (172721 => 172722)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog        2014-08-18 20:52:04 UTC (rev 172721)
+++ trunk/Source/WebInspectorUI/ChangeLog        2014-08-18 20:54:55 UTC (rev 172722)
</span><span class="lines">@@ -1,3 +1,40 @@
</span><ins>+2014-08-18  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        Web Inspector: Introduce an inspector Abstract Syntax Tree.
+        https://bugs.webkit.org/show_bug.cgi?id=135763
+
+        Reviewed by Joseph Pecoraro.
+
+        This patch adds an Abstract Syntax Tree to the Web inspector. 
+        This syntax tree is modeled off the Esprima.js syntax tree
+        which complies with the Mozilla Parser API:
+        https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
+        Script is the owner of the Web Inspector syntax tree, and has an API
+        for accessing its syntax tree property.
+
+        * UserInterface/External/Esprima/esprima.js:
+        * UserInterface/Main.html:
+        * UserInterface/Models/Script.js:
+        (WebInspector.Script):
+        (WebInspector.Script.prototype.requestScriptSyntaxTree.makeSyntaxTreeAndCallCallback):
+        (WebInspector.Script.prototype._resolveResource):
+        (WebInspector.Script.prototype._makeSyntaxTree):
+        * UserInterface/Models/ScriptSyntaxTree.js: Added.
+        (WebInspector.ScriptSyntaxTree):
+        (WebInspector.ScriptSyntaxTree.prototype.get parsedSuccessfully):
+        (WebInspector.ScriptSyntaxTree.prototype.forEachNode):
+        (WebInspector.ScriptSyntaxTree.prototype.):
+        (WebInspector.ScriptSyntaxTree.prototype.filter):
+        (WebInspector.ScriptSyntaxTree.prototype.filterForNodesInRange):
+        (WebInspector.ScriptSyntaxTree.prototype.filterByRange):
+        (WebInspector.ScriptSyntaxTree.prototype.containsNonEmptyReturnStatement.removeFunctionsFilter):
+        (WebInspector.ScriptSyntaxTree.prototype.containsNonEmptyReturnStatement):
+        (WebInspector.ScriptSyntaxTree.prototype.updateTypes):
+        (WebInspector.ScriptSyntaxTree.prototype._defaultParserState):
+        (WebInspector.ScriptSyntaxTree.prototype._recurse):
+        (WebInspector.ScriptSyntaxTree.prototype._recurseArray):
+        (WebInspector.ScriptSyntaxTree.prototype._createInternalSyntaxTree):
+
</ins><span class="cx"> 2014-08-16  Joseph Pecoraro  &lt;pecoraro@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: Improve performance of selection range changes viewing Scripts timeline
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceExternalEsprimaesprimajs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/External/Esprima/esprima.js (172721 => 172722)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/External/Esprima/esprima.js        2014-08-18 20:52:04 UTC (rev 172721)
+++ trunk/Source/WebInspectorUI/UserInterface/External/Esprima/esprima.js        2014-08-18 20:54:55 UTC (rev 172722)
</span><span class="lines">@@ -32,9 +32,9 @@
</span><span class="cx"> */
</span><span class="cx"> 
</span><span class="cx"> // WebKit Modifications:
</span><del>-// - The only edit made to this file is the use 'WebInspector._esprima' as the 'exports' object for esprima. 
-//   The code that determines the context esprima is loaded in (node.js, the browser, etc.) has been replaced with a reference to 'WebInspector._esprima'.
-WebInspector._esprima = {};
</del><ins>+// - The only edit made to this file is the use 'WebInspector.Esprima' as the 'exports' object for esprima. 
+//   The code that determines the context esprima is loaded in (node.js, the browser, etc.) has been replaced with a reference to 'WebInspector.Esprima'.
+WebInspector.Esprima = {};
</ins><span class="cx"> 
</span><span class="cx"> (function (exports) {
</span><span class="cx">     'use strict';
</span><span class="lines">@@ -3732,4 +3732,4 @@
</span><span class="cx">         return types;
</span><span class="cx">     }());
</span><span class="cx"> 
</span><del>-})(WebInspector._esprima);
</del><ins>+})(WebInspector.Esprima);
</ins></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceMainhtml"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Main.html (172721 => 172722)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Main.html        2014-08-18 20:52:04 UTC (rev 172721)
+++ trunk/Source/WebInspectorUI/UserInterface/Main.html        2014-08-18 20:54:55 UTC (rev 172722)
</span><span class="lines">@@ -260,6 +260,7 @@
</span><span class="cx">     &lt;script src=&quot;Models/Revision.js&quot;&gt;&lt;/script&gt;
</span><span class="cx">     &lt;script src=&quot;Models/ScopeChainNode.js&quot;&gt;&lt;/script&gt;
</span><span class="cx">     &lt;script src=&quot;Models/Script.js&quot;&gt;&lt;/script&gt;
</span><ins>+    &lt;script src=&quot;Models/ScriptSyntaxTree.js&quot;&gt;&lt;/script&gt;
</ins><span class="cx">     &lt;script src=&quot;Models/ScriptTimelineRecord.js&quot;&gt;&lt;/script&gt;
</span><span class="cx">     &lt;script src=&quot;Models/Setting.js&quot;&gt;&lt;/script&gt;
</span><span class="cx">     &lt;script src=&quot;Models/SourceCodePosition.js&quot;&gt;&lt;/script&gt;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceModelsScriptjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Models/Script.js (172721 => 172722)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Models/Script.js        2014-08-18 20:52:04 UTC (rev 172721)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/Script.js        2014-08-18 20:54:55 UTC (rev 172722)
</span><span class="lines">@@ -41,6 +41,8 @@
</span><span class="cx"> 
</span><span class="cx">     if (sourceMapURL)
</span><span class="cx">         WebInspector.sourceMapManager.downloadSourceMap(sourceMapURL, this._url, this);
</span><ins>+
+    this._scriptSyntaxTree = null;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> WebInspector.Script.TypeIdentifier = &quot;script&quot;;
</span><span class="lines">@@ -127,6 +129,32 @@
</span><span class="cx">         cookie[WebInspector.Script.DisplayNameCookieKey] = this.displayName;
</span><span class="cx">     },
</span><span class="cx"> 
</span><ins>+    requestScriptSyntaxTree: function(callback)
+    {
+        if (this._scriptSyntaxTree) {
+            setTimeout(function() { callback(this._scriptSyntaxTree); }.bind(this), 0);
+            return;
+        }
+
+        var makeSyntaxTreeAndCallCallback = function(content)
+        {
+            this._makeSyntaxTree(content);
+            callback(this._scriptSyntaxTree);
+        }.bind(this);
+
+        var content = this.content;
+        if (!content &amp;&amp; this._resource &amp;&amp; this._resource.type === WebInspector.Resource.Type.Script &amp;&amp; this._resource.finished)
+            content = this._resource.content;
+        if (content) {
+            setTimeout(makeSyntaxTreeAndCallCallback, 0, content);
+            return;
+        }
+
+        this.requestContent(function(error, sourceText) {
+            makeSyntaxTreeAndCallCallback(error ? null : sourceText);
+        });
+    },
+
</ins><span class="cx">     // Private
</span><span class="cx"> 
</span><span class="cx">     _resolveResource: function()
</span><span class="lines">@@ -174,6 +202,14 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return null;
</span><ins>+    },
+
+    _makeSyntaxTree: function(sourceText) 
+    {
+        if (this._scriptSyntaxTree || !sourceText)
+            return;
+
+        this._scriptSyntaxTree = new WebInspector.ScriptSyntaxTree(sourceText, this);
</ins><span class="cx">     }
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceModelsScriptSyntaxTreejs"></a>
<div class="addfile"><h4>Added: trunk/Source/WebInspectorUI/UserInterface/Models/ScriptSyntaxTree.js (0 => 172722)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Models/ScriptSyntaxTree.js                                (rev 0)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/ScriptSyntaxTree.js        2014-08-18 20:54:55 UTC (rev 172722)
</span><span class="lines">@@ -0,0 +1,782 @@
</span><ins>+/*
+ * Copyright (C) 2014 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+WebInspector.ScriptSyntaxTree = function(sourceText, script)
+{
+    console.assert(script &amp;&amp; script instanceof WebInspector.Script, script);
+
+    WebInspector.Object.call(this);
+    this._script = script;
+
+    try {
+        var esprimaSyntaxTree = WebInspector.Esprima.parse(sourceText, {range: true});
+        this._syntaxTree = this._createInternalSyntaxTree(esprimaSyntaxTree);
+        this._parsedSuccessfully = true;
+    } catch (error) {
+        this._parsedSuccessfully = false;
+        this._syntaxTree = null;
+        console.error(&quot;Couldn't parse JavaScript File: &quot; + script.url, error);
+    }
+};
+
+// This should be kept in sync with an enum in JavaSciptCore/runtime/HighFidelityTypeProfiler.h
+WebInspector.ScriptSyntaxTree.TypeProfilerSearchDescriptor = {
+    NormalExpression: 1,
+    FunctionReturn: 2
+};
+
+WebInspector.ScriptSyntaxTree.NodeType = {
+    AssignmentExpression: &quot;assignment-expression&quot;,
+    ArrayExpression: &quot;expression&quot;,
+    BlockStatement: &quot;block-statement&quot;,
+    BinaryExpression: &quot;binary-expression&quot;,
+    BreakStatement: &quot;break-statement&quot;,
+    CallExpression: &quot;call-expression&quot;,
+    CatchClause: &quot;catch-clause&quot;,
+    ConditionalExpression: &quot;conditional-expression&quot;,
+    ContinueStatement: &quot;continue-statement&quot;,
+    DoWhileStatement: &quot;do-while-statement&quot;,
+    DebuggerStatement: &quot;debugger-statement&quot;,
+    EmptyStatement: &quot;empty-statement&quot;,
+    ExpressionStatement: &quot;expression-statement&quot;,
+    ForStatement: &quot;for-statement&quot;,
+    ForInStatement: &quot;for-in-statement&quot;,
+    FunctionDeclaration: &quot;function-declaration&quot;,
+    FunctionExpression: &quot;function-expression&quot;,
+    Identifier: &quot;identifier&quot;,
+    IfStatement: &quot;if-statement&quot;,
+    Literal: &quot;literal&quot;,
+    LabeledStatement: &quot;labeled-statement&quot;,
+    LogicalExpression: &quot;logical-expression&quot;,
+    MemberExpression: &quot;member-expression&quot;,
+    NewExpression: &quot;new-expression&quot;,
+    ObjectExpression: &quot;objectExpression&quot;,
+    Program: &quot;program&quot;,
+    Property: &quot;property&quot;,
+    ReturnStatement: &quot;return-statement&quot;,
+    SequenceExpression: &quot;sequence-expression&quot;,
+    SwitchStatement: &quot;switch-statement&quot;,
+    SwitchCase: &quot;switch-case&quot;,
+    ThisExpression: &quot;this-expression&quot;,
+    ThrowStatement: &quot;throw-statement&quot;,
+    TryStatement: &quot;try-statement&quot;,
+    UnaryExpression: &quot;unary-expression&quot;,
+    UpdateExpression: &quot;update-expression&quot;,
+    VariableDeclaration: &quot;variable-declaration&quot;,
+    VariableDeclarator: &quot;variable-declarator&quot;,
+    WhileStatement: &quot;while-statement&quot;,
+    WithStatement: &quot;with-statement&quot;
+};
+
+WebInspector.ScriptSyntaxTree.prototype = {
+    constructor: WebInspector.ScriptSyntaxTree,
+    __proto__: WebInspector.Object.prototype,
+
+    // Public
+    
+    get parsedSuccessfully()
+    {
+        return this._parsedSuccessfully;
+    },
+
+    forEachNode: function(callback)
+    {
+        console.assert(this._parsedSuccessfully);
+        if (!this._parsedSuccessfully)
+            return;
+
+        this._recurse(this._syntaxTree, callback, this._defaultParserState());
+    },
+
+    filter: function(predicate, startNode) 
+    {
+        console.assert(startNode &amp;&amp; this._parsedSuccessfully);
+        if (!this._parsedSuccessfully)
+            return [];
+
+        var nodes = [];
+        function filter(node, state)
+        {
+            if (predicate(node))
+                nodes.push(node);
+            else
+                state.skipChildNodes = true; 
+        }
+
+        this._recurse(startNode, filter, this._defaultParserState());
+
+        return nodes;
+    },
+
+    filterByRange: function(startOffset, endOffset)
+    {
+        console.assert(this._parsedSuccessfully);
+        if (!this._parsedSuccessfully)
+            return [];
+        
+        var allNodes = [];
+        const start = 0;
+        const end = 1;
+        function filterForNodesInRange(node, state)
+        {
+            // program start        range            program end
+            // [                 [         ]               ]
+            //            [ ]  [   [        ] ]  [ ]
+
+            // If a node's range ends before the range we're interested in starts, we don't need to search any of its
+            // enclosing ranges, because, by definition, those enclosing ranges are contained within this node's range.
+            if (node.range[end] &lt; startOffset)
+                state.skipChildNodes = true;
+
+            // We are only interested in nodes whose start position is within our range.
+            if (startOffset &lt;= node.range[start] &amp;&amp; node.range[start] &lt;= endOffset)
+                allNodes.push(node);
+
+            // Once we see nodes that start beyond our range, we can quit traversing the AST. We can do this safely
+            // because we know the AST is traversed using depth first search, so it will traverse into enclosing ranges
+            // before it traverses into adjacent ranges.
+            if (node.range[start] &gt; endOffset)
+                state.shouldStopEarly = true;
+        }
+
+        this.forEachNode(filterForNodesInRange);
+
+        return allNodes;
+    },
+
+    containsNonEmptyReturnStatement: function(startNode)
+    {
+        console.assert(startNode &amp;&amp; this._parsedSuccessfully);
+        if (!this._parsedSuccessfully)
+            return false;
+
+        if (startNode._attachments._hasNonEmptyReturnStatement !== undefined)
+            return startNode._attachments._hasNonEmptyReturnStatement;
+
+        function removeFunctionsFilter(node)
+        {
+            return node.type !== WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression 
+                &amp;&amp; node.type !== WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration;
+        }
+
+        var nodes = this.filter(removeFunctionsFilter, startNode);
+        var hasNonEmptyReturnStatement = false;
+        var returnStatementType = WebInspector.ScriptSyntaxTree.NodeType.ReturnStatement;
+        for (var node of nodes) {
+            if (node.type === returnStatementType &amp;&amp; node.argument) {
+                hasNonEmptyReturnStatement = true;
+                break;
+            }
+        }
+         
+        startNode._attachments._hasNonEmptyReturnStatement = hasNonEmptyReturnStatement;
+
+        return hasNonEmptyReturnStatement;
+    },
+
+    updateTypes: function(sourceID, nodesToUpdate, callback) 
+    {
+        console.assert(Array.isArray(nodesToUpdate) &amp;&amp; this._parsedSuccessfully);
+        if (!this._parsedSuccessfully)
+            return;
+
+        var allRequests = [];
+        var allRequestNodes = [];
+
+        for (var node of nodesToUpdate) {
+            switch (node.type) {
+            case WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration:
+            case WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression:
+                for (var param of node.params) {
+                    allRequests.push({
+                        typeInformationDescriptor: WebInspector.ScriptSyntaxTree.TypeProfilerSearchDescriptor.NormalExpression,
+                        sourceID: sourceID,
+                        divot: param.range[0]
+                    });
+                    allRequestNodes.push(param);
+                }
+
+                allRequests.push({
+                    typeInformationDescriptor: WebInspector.ScriptSyntaxTree.TypeProfilerSearchDescriptor.FunctionReturn,
+                    sourceID: sourceID,
+                    divot: node.body.range[0]
+                });
+                allRequestNodes.push(node);
+                break;
+            case WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator:
+                allRequests.push({
+                    typeInformationDescriptor: WebInspector.ScriptSyntaxTree.TypeProfilerSearchDescriptor.NormalExpression,
+                    sourceID: sourceID,
+                    divot: node.id.range[0]
+                });
+                allRequestNodes.push(node);
+                break;
+            }
+        }
+
+        console.assert(allRequests.length === allRequestNodes.length);
+
+        function handleTypes(error, typeInformationArray)
+        {
+            if (error)
+                return;
+
+            console.assert(typeInformationArray.length === allRequests.length);
+
+            for (var i = 0; i &lt; typeInformationArray.length; i++) {
+                var node = allRequestNodes[i];
+                var typeInformation = typeInformationArray[i];
+                if (allRequests[i].typeInformationDescriptor === WebInspector.ScriptSyntaxTree.TypeProfilerSearchDescriptor.FunctionReturn)
+                    node._attachments._returnTypes = typeInformation;
+                else
+                    node._attachments._types = typeInformation;
+            }
+
+            callback();
+        }
+
+        RuntimeAgent.getRuntimeTypesForVariablesAtOffsets(allRequests, handleTypes);
+    },
+
+    // Private
+
+    _defaultParserState: function() 
+    {
+        return {
+            shouldStopEarly: false,
+            skipChildNodes: false
+        };
+    },
+
+    _recurse: function(node, callback, state) 
+    {
+        if (!node)
+            return;
+
+        if (state.shouldStopEarly || state.skipChildNodes)
+            return;
+
+        switch (node.type) {
+        case WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression:
+            callback(node, state);
+            this._recurse(node.left, callback, state);
+            this._recurse(node.right, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ArrayExpression:
+            callback(node, state);
+            this._recurseArray(node.elements, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.BlockStatement:
+            callback(node, state);
+            this._recurseArray(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.BinaryExpression:
+            callback(node, state);
+            this._recurse(node.left, callback, state);
+            this._recurse(node.right, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.BreakStatement:
+            callback(node, state);
+            this._recurse(node.label, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.CatchClause:
+            callback(node, state);
+            this._recurse(node.param, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.CallExpression:
+            callback(node, state);
+            this._recurse(node.callee, callback, state);
+            this._recurseArray(node.arguments, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ContinueStatement:
+            callback(node, state);
+            this._recurse(node.label, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.DoWhileStatement:
+            callback(node, state);
+            this._recurse(node.body, callback, state);
+            this._recurse(node.test, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.DebuggerStatement:
+            callback(node, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement:
+            callback(node, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement:
+            callback(node, state);
+            this._recurse(node.expression, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ForStatement:
+            callback(node, state);
+            this._recurse(node.init, callback, state);
+            this._recurse(node.test, callback, state);
+            this._recurse(node.update, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ForInStatement:
+            callback(node, state);
+            this._recurse(node.left, callback, state);
+            this._recurse(node.right, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration:
+            callback(node, state);
+            this._recurse(node.id, callback, state);
+            this._recurseArray(node.params, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression:
+            callback(node, state);
+            this._recurse(node.id, callback, state);
+            this._recurseArray(node.params, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.Identifier:
+            callback(node, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.IfStatement:
+            callback(node, state);
+            this._recurse(node.test, callback, state);
+            this._recurse(node.consequent, callback, state);
+            this._recurse(node.alternate, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.Literal:
+            callback(node, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.LabeledStatement:
+            callback(node, state);
+            this._recurse(node.label, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.LogicalExpression:
+            callback(node, state);
+            this._recurse(node.left, callback, state);
+            this._recurse(node.right, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.MemberExpression:
+            callback(node, state);
+            this._recurse(node.object, callback, state);
+            this._recurse(node.property, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.NewExpression:
+            callback(node, state);
+            this._recurse(node.callee, callback, state);
+            this._recurseArray(node.arguments, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression:
+            callback(node, state);
+            this._recurseArray(node.properties, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.Program:
+            callback(node, state);
+            this._recurseArray(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.Property:
+            callback(node, state);
+            this._recurse(node.key, callback, state);
+            this._recurse(node.value, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ReturnStatement:
+            callback(node, state);
+            this._recurse(node.argument, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.SequenceExpression:
+            callback(node, state);
+            this._recurseArray(node.expressions, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.SwitchStatement:
+            callback(node, state);
+            this._recurse(node.discriminant, callback, state);
+            this._recurseArray(node.cases, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.SwitchCase:
+            callback(node, state);
+            this._recurse(node.test, callback, state);
+            this._recurseArray(node.consequent, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ConditionalExpression:
+            callback(node, state);
+            this._recurse(node.test, callback, state);
+            this._recurse(node.consequent, callback, state);
+            this._recurse(node.alternate, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ThisExpression:
+            callback(node, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ThrowStatement:
+            callback(node, state);
+            this._recurse(node.argument, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.TryStatement:
+            callback(node, state);
+            this._recurse(node.block, callback, state);
+            this._recurseArray(node.guardedHandlers, callback, state);
+            this._recurseArray(node.handlers, callback, state);
+            this._recurse(node.finalizer, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.UnaryExpression:
+            callback(node, state);
+            this._recurse(node.argument, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.UpdateExpression:
+            callback(node, state);
+            this._recurse(node.argument, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration:
+            callback(node, state);
+            this._recurseArray(node.declarations, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator:
+            callback(node, state);
+            this._recurse(node.id, callback, state);
+            this._recurse(node.init, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.WhileStatement:
+            callback(node, state);
+            this._recurse(node.test, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.WithStatement:
+            callback(node, state);
+            this._recurse(node.object, callback, state);
+            this._recurse(node.body, callback, state);
+            break;
+        }
+
+        state.skipChildNodes = false;
+    },
+
+    _recurseArray: function(array, callback, state) 
+    {
+        for (var node of array)
+            this._recurse(node, callback, state);
+    },
+    
+    // This function translates from esprima's Abstract Syntax Tree to ours. 
+    // Mostly, this is just the identity function. We've added an extra isGetterOrSetter property for functions.
+    // Our AST complies with the Mozilla parser API:
+    // https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
+    _createInternalSyntaxTree: function(node) 
+    {
+        if (!node)
+            return null;
+
+        var result = null;
+        switch (node.type) {
+        case &quot;AssignmentExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression,
+                operator: node.operator,
+                left: this._createInternalSyntaxTree(node.left),
+                right: this._createInternalSyntaxTree(node.right)
+            };
+            break;
+        case &quot;ArrayExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ArrayExpression,
+                elements: node.elements.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;BlockStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.BlockStatement,
+                body: node.body.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;BinaryExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.BinaryExpression,
+                operator: node.operator,
+                left: this._createInternalSyntaxTree(node.left),
+                right: this._createInternalSyntaxTree(node.right)
+            };
+            break;
+        case &quot;BreakStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.BreakStatement,
+                label: this._createInternalSyntaxTree(node.label)
+            };
+            break;
+        case &quot;CallExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.CallExpression,
+                callee: this._createInternalSyntaxTree(node.callee),
+                arguments: node.arguments.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;CatchClause&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.CatchClause,
+                param: this._createInternalSyntaxTree(node.param),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        case &quot;ConditionalExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ConditionalExpression,
+                test: this._createInternalSyntaxTree(node.test),
+                consequent: this._createInternalSyntaxTree(node.consequent),
+                alternate: this._createInternalSyntaxTree(node.alternate)
+            };
+            break;
+        case &quot;ContinueStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ContinueStatement,
+                label: this._createInternalSyntaxTree(node.label)
+            };
+            break;
+        case &quot;DoWhileStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.DoWhileStatement,
+                body: this._createInternalSyntaxTree(node.body),
+                test: this._createInternalSyntaxTree(node.test)
+            };
+            break;
+        case &quot;DebuggerStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.DebuggerStatement
+            };
+            break;
+        case &quot;EmptyStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement
+            };
+            break;
+        case &quot;ExpressionStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement,
+                expression: this._createInternalSyntaxTree(node.expression)
+            };
+            break;
+        case &quot;ForStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ForStatement,
+                init: this._createInternalSyntaxTree(node.init),
+                test: this._createInternalSyntaxTree(node.test),
+                update: this._createInternalSyntaxTree(node.update),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        case &quot;ForInStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ForInStatement,
+                left: this._createInternalSyntaxTree(node.left),
+                right: this._createInternalSyntaxTree(node.right),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        case &quot;FunctionDeclaration&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration,
+                id: this._createInternalSyntaxTree(node.id),
+                params: node.params.map(this._createInternalSyntaxTree.bind(this)),
+                body: this._createInternalSyntaxTree(node.body),
+                isGetterOrSetter: false // This is obvious, but is convenient none the less b/c Declarations and Expressions are often intertwined.
+            };
+            break;
+        case &quot;FunctionExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression,
+                id: this._createInternalSyntaxTree(node.id),
+                params: node.params.map(this._createInternalSyntaxTree.bind(this)),
+                body: this._createInternalSyntaxTree(node.body),
+                isGetterOrSetter: false // If true, it is set in the Property AST node.
+            };
+            break;
+        case &quot;Identifier&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.Identifier,
+                name: node.name
+            };
+            break;
+        case &quot;IfStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.IfStatement,
+                test: this._createInternalSyntaxTree(node.test),
+                consequent: this._createInternalSyntaxTree(node.consequent),
+                alternate: this._createInternalSyntaxTree(node.alternate)
+            };
+            break;
+        case &quot;Literal&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.Literal,
+                value: node.value,
+                raw: node.raw
+            };
+            break;
+        case &quot;LabeledStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.LabeledStatement,
+                label: this._createInternalSyntaxTree(node.label),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        case &quot;LogicalExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.LogicalExpression,
+                left: this._createInternalSyntaxTree(node.left),
+                right: this._createInternalSyntaxTree(node.right)
+            };
+            break;
+        case &quot;MemberExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.MemberExpression,
+                object: this._createInternalSyntaxTree(node.object),
+                property: this._createInternalSyntaxTree(node.property)
+            };
+            break;
+        case &quot;NewExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.NewExpression,
+                callee: this._createInternalSyntaxTree(node.callee),
+                arguments: node.arguments.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;ObjectExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression,
+                properties: node.properties.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;Program&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.Program,
+                body: node.body.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;Property&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.Property,
+                key: this._createInternalSyntaxTree(node.key),
+                value: this._createInternalSyntaxTree(node.value),
+                kind: node.kind
+            };
+            if (result.kind === &quot;get&quot; || result.kind === &quot;set&quot;) {
+                result.value.isGetterOrSetter = true;
+                result.value.getterOrSetterRange = result.key.range;
+            }
+            break;
+        case &quot;ReturnStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ReturnStatement,
+                argument: this._createInternalSyntaxTree(node.argument)
+            };
+            break;
+        case &quot;SequenceExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.SequenceExpression,
+                expressions: node.expressions.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;SwitchStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.SwitchStatement,
+                discriminant: this._createInternalSyntaxTree(node.discriminant),
+                cases: node.cases.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;SwitchCase&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.SwitchCase,
+                test: this._createInternalSyntaxTree(node.test),
+                consequent: node.consequent.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;ThisExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ThisExpression
+            };
+            break;
+        case &quot;ThrowStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ThrowStatement,
+                argument: this._createInternalSyntaxTree(node.argument)
+            };
+            break;
+        case &quot;TryStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.TryStatement,
+                block: this._createInternalSyntaxTree(node.block),
+                // FIXME: What are guarded handlers?
+                guardedHandlers: node.guardedHandlers.map(this._createInternalSyntaxTree.bind(this)),
+                handlers: node.handlers.map(this._createInternalSyntaxTree.bind(this)),
+                finalizer: this._createInternalSyntaxTree(node.finalizer)
+            };
+            break;
+        case &quot;UnaryExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.UnaryExpression,
+                operator: node.operator,
+                argument: this._createInternalSyntaxTree(node.argument)
+            };
+            break;
+        case &quot;UpdateExpression&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.UpdateExpression,
+                operator: node.operator,
+                prefix: node.prefix,
+                argument: this._createInternalSyntaxTree(node.argument)
+            };
+            break;
+        case &quot;VariableDeclaration&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration,
+                declarations: node.declarations.map(this._createInternalSyntaxTree.bind(this))
+            };
+            break;
+        case &quot;VariableDeclarator&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator,
+                id: this._createInternalSyntaxTree(node.id),
+                init: this._createInternalSyntaxTree(node.init)
+            };
+            break;
+        case &quot;WhileStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.WhileStatement,
+                test: this._createInternalSyntaxTree(node.test),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        case &quot;WithStatement&quot;:
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.WithStatement,
+                object: this._createInternalSyntaxTree(node.object),
+                body: this._createInternalSyntaxTree(node.body)
+            };
+            break;
+        default:
+            console.error(&quot;Unsupported Syntax Tree Node: &quot; + node.type, node);
+        }
+        
+        result.range = node.range;
+        // This is an object for which you can add fields to an AST node without worrying about polluting the syntax-related fields of the node.
+        result._attachments = {}; 
+
+        return result;
+    }
+};
</ins></span></pre>
</div>
</div>

</body>
</html>