Web Inspector: Add ability to distinguish if a Script was parsed as a module
authorjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Dec 2016 00:04:13 +0000 (00:04 +0000)
committerjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Dec 2016 00:04:13 +0000 (00:04 +0000)
https://bugs.webkit.org/show_bug.cgi?id=164900
<rdar://problem/29323817>

Reviewed by Timothy Hatcher.

Source/JavaScriptCore:

* inspector/agents/InspectorDebuggerAgent.cpp:
(Inspector::InspectorDebuggerAgent::didParseSource):
* inspector/protocol/Debugger.json:
Add an optional event parameter to distinguish if a script was a module or not.

Source/WebInspectorUI:

* UserInterface/Models/Script.js:
(WebInspector.Script.prototype.get sourceType):
New property of Scripts. SourceType is either Program or Module.

* UserInterface/Controllers/DebuggerManager.js:
(WebInspector.DebuggerManager.prototype.scriptDidParse):
* UserInterface/Protocol/DebuggerObserver.js:
(WebInspector.DebuggerObserver.prototype.scriptParsed):
Convert incoming module boolean into SourceType when creating new Scripts.

* UserInterface/Models/ScriptSyntaxTree.js:
(WebInspector.ScriptSyntaxTree):
(WebInspector.ScriptSyntaxTree.prototype._recurse):
(WebInspector.ScriptSyntaxTree.prototype._createInternalSyntaxTree):
Update the generic AST for new module specific Esprima types.

* UserInterface/Views/SourceCodeTextEditor.js:
(WebInspector.SourceCodeTextEditor.prototype.textEditorScriptSourceType):
* UserInterface/Views/TextEditor.js:
(WebInspector.TextEditor.prototype._startWorkerPrettyPrint):
For pretty printing correctly state if this is a module or not for Esprima.

LayoutTests:

* inspector/model/parse-script-syntax-tree-expected.txt:
* inspector/model/parse-script-syntax-tree.html:
* inspector/model/resources/module.js: Added.
(import.string_appeared_here.myModule):
* inspector/model/resources/other-module.js: Added.
(myOtherModule):
* inspector/model/resources/program.js: Added.
(myProgram):
* inspector/model/script-sourceType-expected.txt: Added.
* inspector/model/script-sourceType.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@209492 268f45cc-cd09-0410-ab3c-d52691b4dbfc

18 files changed:
LayoutTests/ChangeLog
LayoutTests/inspector/model/parse-script-syntax-tree-expected.txt
LayoutTests/inspector/model/parse-script-syntax-tree.html
LayoutTests/inspector/model/resources/module.js [new file with mode: 0644]
LayoutTests/inspector/model/resources/other-module.js [new file with mode: 0644]
LayoutTests/inspector/model/resources/program.js [new file with mode: 0644]
LayoutTests/inspector/model/script-sourceType-expected.txt [new file with mode: 0644]
LayoutTests/inspector/model/script-sourceType.html [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp
Source/JavaScriptCore/inspector/protocol/Debugger.json
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Controllers/DebuggerManager.js
Source/WebInspectorUI/UserInterface/Models/Script.js
Source/WebInspectorUI/UserInterface/Models/ScriptSyntaxTree.js
Source/WebInspectorUI/UserInterface/Protocol/DebuggerObserver.js
Source/WebInspectorUI/UserInterface/Views/SourceCodeTextEditor.js
Source/WebInspectorUI/UserInterface/Views/TextEditor.js

index 588de08..a490da3 100644 (file)
@@ -1,5 +1,24 @@
 2016-12-07  Joseph Pecoraro  <pecoraro@apple.com>
 
+        Web Inspector: Add ability to distinguish if a Script was parsed as a module
+        https://bugs.webkit.org/show_bug.cgi?id=164900
+        <rdar://problem/29323817>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/model/parse-script-syntax-tree-expected.txt:
+        * inspector/model/parse-script-syntax-tree.html:
+        * inspector/model/resources/module.js: Added.
+        (import.string_appeared_here.myModule):
+        * inspector/model/resources/other-module.js: Added.
+        (myOtherModule):
+        * inspector/model/resources/program.js: Added.
+        (myProgram):
+        * inspector/model/script-sourceType-expected.txt: Added.
+        * inspector/model/script-sourceType.html: Added.
+
+2016-12-07  Joseph Pecoraro  <pecoraro@apple.com>
+
         Web Inspector: Update Esprima to support new features / syntax (**, async/await, trailing comma)
         https://bugs.webkit.org/show_bug.cgi?id=164830
         <rdar://problem/29293814>
index 3175e2b..e2e4425 100644 (file)
@@ -45,6 +45,12 @@ passed AssignmentPattern
 passed ArrowFunctionExpression
 passed Async Functions
 passed AwaitExpression
+passed ImportDeclaration, ImportSpecifier
+passed ImportDefaultSpecifier
+passed ImportNamespaceSpecifier
+passed ExportNamedDeclaration, ExportSpecifier
+passed ExportDefaultDeclaration
+passed ExportAllDeclaration
 passed computed method on object literal
 passed method on object literal
 passed computed method property on object literal
index 801c0d6..76d112d 100644 (file)
@@ -5,11 +5,13 @@
 <script>
 function test()
 {
-    function makeNode(text, isExpression)
+    function makeNode(text, isExpression, isModule)
     {
-        var script = new WebInspector.Script(1, new WebInspector.TextRange(0, text.length));
-        var scriptSyntaxTree = new WebInspector.ScriptSyntaxTree(text, script);
-        var syntaxTree = scriptSyntaxTree._syntaxTree;
+        const target = null, url = null;
+        let sourceType = isModule ? WebInspector.Script.SourceType.Module : WebInspector.Script.SourceType.Program;
+        let script = new WebInspector.Script(target, 1, new WebInspector.TextRange(0, text.length), url, sourceType);
+        let scriptSyntaxTree = new WebInspector.ScriptSyntaxTree(text, script);
+        let syntaxTree = scriptSyntaxTree._syntaxTree;
 
         InspectorTest.assert(scriptSyntaxTree.parsedSuccessfully, "ScriptSyntaxTree should be able to parse: \"" + text + "\"");
         InspectorTest.assert(syntaxTree.type === WebInspector.ScriptSyntaxTree.NodeType.Program, "Should be program.");
@@ -17,11 +19,21 @@ function test()
         if (isExpression) {
             InspectorTest.assert(syntaxTree.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
             return syntaxTree.body[0].expression;
-        } else
+        } else if (isModule) {
+            InspectorTest.assert(syntaxTree.sourceType === "module");
             return syntaxTree.body[0];
+        } else {
+            InspectorTest.assert(syntaxTree.sourceType === "script");
+            return syntaxTree.body[0];
+        }
+    }
+
+    function makeNodeForModule(text)
+    {
+        return makeNode(text, false, true);
     }
 
-    var node = null;
+    let node = null;
 
     node = makeNode("x = 20;", true);
     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression);
@@ -52,7 +64,7 @@ function test()
     InspectorTest.assert(node.elements[0].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
     InspectorTest.assert(node.elements[1].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
     InspectorTest.log("passed ArrayExpression");
-    
+
     node = makeNode("{foo();}", false);
     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
     InspectorTest.assert(node.body);
@@ -112,7 +124,7 @@ function test()
     InspectorTest.assert(node.consequent.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
     InspectorTest.assert(node.alternate.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
     InspectorTest.log("passed ConditionalExpression");
-    
+
     node = makeNode("label:while(true) {continue label;}", false);
     InspectorTest.assert(node.body.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ContinueStatement);
     InspectorTest.assert(node.body.body.body[0].label.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
@@ -321,7 +333,7 @@ function test()
     InspectorTest.assert(node.right.properties[0].key.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
     InspectorTest.log("passed Property");
 
-    node = makeNode("function foo(...things) { return things; }", false);    
+    node = makeNode("function foo(...things) { return things; }", false);
     InspectorTest.assert(node.params.length === 1);
     InspectorTest.assert(node.params[0].type === WebInspector.ScriptSyntaxTree.NodeType.RestElement);
     InspectorTest.assert(node.params[0].argument.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
@@ -550,6 +562,66 @@ function test()
     InspectorTest.assert(node.body.body[0].expression.argument.value === 1);
     InspectorTest.log("passed AwaitExpression");
 
+
+    // Modules
+
+    node = makeNodeForModule(`import {a,b as x,c} from "module.js"`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ImportDeclaration);
+    InspectorTest.assert(node.source.value === "module.js");
+    InspectorTest.assert(node.specifiers.length === 3);
+    InspectorTest.assert(node.specifiers[0].type === WebInspector.ScriptSyntaxTree.NodeType.ImportSpecifier);
+    InspectorTest.assert(node.specifiers[0].local.name === "a");
+    InspectorTest.assert(node.specifiers[0].imported.name === "a");
+    InspectorTest.assert(node.specifiers[1].type === WebInspector.ScriptSyntaxTree.NodeType.ImportSpecifier);
+    InspectorTest.assert(node.specifiers[1].local.name === "x");
+    InspectorTest.assert(node.specifiers[1].imported.name === "b");
+    InspectorTest.assert(node.specifiers[2].type === WebInspector.ScriptSyntaxTree.NodeType.ImportSpecifier);
+    InspectorTest.assert(node.specifiers[2].local.name === "c");
+    InspectorTest.assert(node.specifiers[2].imported.name === "c");
+    InspectorTest.log("passed ImportDeclaration, ImportSpecifier");
+
+    node = makeNodeForModule(`import x from "module.js"`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ImportDeclaration);
+    InspectorTest.assert(node.source.value === "module.js");
+    InspectorTest.assert(node.specifiers.length === 1);
+    InspectorTest.assert(node.specifiers[0].type === WebInspector.ScriptSyntaxTree.NodeType.ImportDefaultSpecifier);
+    InspectorTest.log("passed ImportDefaultSpecifier");
+
+    node = makeNodeForModule(`import * as A from "module.js"`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ImportDeclaration);
+    InspectorTest.assert(node.source.value === "module.js");
+    InspectorTest.assert(node.specifiers.length === 1);
+    InspectorTest.assert(node.specifiers[0].type === WebInspector.ScriptSyntaxTree.NodeType.ImportNamespaceSpecifier);
+    InspectorTest.assert(node.specifiers[0].local.name === "A");
+    InspectorTest.log("passed ImportNamespaceSpecifier");
+
+    node = makeNodeForModule(`export {a, b as x, c}`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ExportNamedDeclaration);
+    InspectorTest.assert(node.specifiers.length === 3);
+    InspectorTest.assert(node.specifiers[0].type === WebInspector.ScriptSyntaxTree.NodeType.ExportSpecifier);
+    InspectorTest.assert(node.specifiers[0].local.name === "a");
+    InspectorTest.assert(node.specifiers[0].exported.name === "a");
+    InspectorTest.assert(node.specifiers[1].type === WebInspector.ScriptSyntaxTree.NodeType.ExportSpecifier);
+    InspectorTest.assert(node.specifiers[1].local.name === "b");
+    InspectorTest.assert(node.specifiers[1].exported.name === "x");
+    InspectorTest.assert(node.specifiers[2].type === WebInspector.ScriptSyntaxTree.NodeType.ExportSpecifier);
+    InspectorTest.assert(node.specifiers[2].local.name === "c");
+    InspectorTest.assert(node.specifiers[2].exported.name === "c");
+    InspectorTest.log("passed ExportNamedDeclaration, ExportSpecifier");
+
+    node = makeNodeForModule(`export default x`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ExportDefaultDeclaration);
+    InspectorTest.assert(node.declaration.name === "x");
+    InspectorTest.log("passed ExportDefaultDeclaration");
+
+    node = makeNodeForModule(`export * from "module.js"`);
+    InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ExportAllDeclaration);
+    InspectorTest.assert(node.source.value === "module.js");
+    InspectorTest.log("passed ExportAllDeclaration");
+
+
+    // Divots
+
     node = makeNode("var o = {['c']() { }};", false);
     //                        ^
     //                 type profiling return divot.
diff --git a/LayoutTests/inspector/model/resources/module.js b/LayoutTests/inspector/model/resources/module.js
new file mode 100644 (file)
index 0000000..1f2493d
--- /dev/null
@@ -0,0 +1,3 @@
+import "./other-module.js"
+
+function myModule() {}
diff --git a/LayoutTests/inspector/model/resources/other-module.js b/LayoutTests/inspector/model/resources/other-module.js
new file mode 100644 (file)
index 0000000..caa0d36
--- /dev/null
@@ -0,0 +1 @@
+function myOtherModule() {}
diff --git a/LayoutTests/inspector/model/resources/program.js b/LayoutTests/inspector/model/resources/program.js
new file mode 100644 (file)
index 0000000..3246dcc
--- /dev/null
@@ -0,0 +1 @@
+function myProgram() {}
diff --git a/LayoutTests/inspector/model/script-sourceType-expected.txt b/LayoutTests/inspector/model/script-sourceType-expected.txt
new file mode 100644 (file)
index 0000000..5aec25f
--- /dev/null
@@ -0,0 +1,11 @@
+Tests for WebInspector.Script.SourceType.
+
+
+== Running test suite: WebInspector.Script.SourceType
+-- Running test case: WebInspector.Script.SourceType.Program
+PASS: <script> should be SourceType.Program.
+
+-- Running test case: WebInspector.Script.SourceType.Module
+PASS: imported module should be SourceType.Module.
+PASS: <script type="module"> should be SourceType.Module.
+
diff --git a/LayoutTests/inspector/model/script-sourceType.html b/LayoutTests/inspector/model/script-sourceType.html
new file mode 100644 (file)
index 0000000..a083f25
--- /dev/null
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
+<script>
+function triggerProgramScript() {
+    let script = document.createElement("script");
+    script.src = "resources/program.js";
+    document.body.appendChild(script);
+}
+
+function triggerModuleScript() {
+    let script = document.createElement("script");
+    script.type = "module";
+    script.src = "resources/module.js";
+    document.body.appendChild(script);
+}
+
+function test()
+{
+    let suite = InspectorTest.createAsyncSuite("WebInspector.Script.SourceType");
+
+    suite.addTestCase({
+        name: "WebInspector.Script.SourceType.Program",
+        description: "Normal <script src> should be Program.",
+        test(resolve, reject) {
+            InspectorTest.evaluateInPage("triggerProgramScript()");
+            let listener = WebInspector.debuggerManager.addEventListener(WebInspector.DebuggerManager.Event.ScriptAdded, (event) => {
+                let script = event.data.script;
+                if (!/program.js$/.test(script.url))
+                    return;
+
+                WebInspector.debuggerManager.removeEventListener(WebInspector.DebuggerManager.Event.ScriptAdded, listener);
+                InspectorTest.expectEqual(script.sourceType, WebInspector.Script.SourceType.Program, `<script> should be SourceType.Program.`);
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: "WebInspector.Script.SourceType.Module",
+        description: "Module <script type=module> should be Module.",
+        test(resolve, reject) {
+            InspectorTest.evaluateInPage("triggerModuleScript()");
+            let listener = WebInspector.debuggerManager.addEventListener(WebInspector.DebuggerManager.Event.ScriptAdded, (event) => {
+                let script = event.data.script;
+                if (!/module.js$/.test(script.url))
+                    return;
+
+                if (/other-module.js$/.test(script.url)) {
+                    InspectorTest.expectEqual(script.sourceType, WebInspector.Script.SourceType.Module, `imported module should be SourceType.Module.`);
+                    return;
+                }
+
+                WebInspector.debuggerManager.removeEventListener(WebInspector.DebuggerManager.Event.ScriptAdded, listener);
+                InspectorTest.expectEqual(script.sourceType, WebInspector.Script.SourceType.Module, `<script type="module"> should be SourceType.Module.`);
+                resolve();
+            });
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>Tests for WebInspector.Script.SourceType.</p>
+</body>
+</html>
index 922bfa6..63c637b 100644 (file)
@@ -1,3 +1,16 @@
+2016-12-07  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: Add ability to distinguish if a Script was parsed as a module
+        https://bugs.webkit.org/show_bug.cgi?id=164900
+        <rdar://problem/29323817>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/agents/InspectorDebuggerAgent.cpp:
+        (Inspector::InspectorDebuggerAgent::didParseSource):
+        * inspector/protocol/Debugger.json:
+        Add an optional event parameter to distinguish if a script was a module or not.
+
 2016-12-07  Simon Fraser  <simon.fraser@apple.com>
 
         Add system trace points for JavaScript VM entry/exit
index 0e60b83..acd9965 100644 (file)
@@ -887,11 +887,12 @@ void InspectorDebuggerAgent::didParseSource(JSC::SourceID sourceID, const Script
     String sourceURL = script.sourceURL;
     String sourceMappingURL = sourceMapURLForScript(script);
 
+    const bool isModule = script.sourceProvider->sourceType() == JSC::SourceProviderSourceType::Module;
     const bool* isContentScript = script.isContentScript ? &script.isContentScript : nullptr;
     String* sourceURLParam = hasSourceURL ? &sourceURL : nullptr;
     String* sourceMapURLParam = sourceMappingURL.isEmpty() ? nullptr : &sourceMappingURL;
 
-    m_frontendDispatcher->scriptParsed(scriptIDStr, script.url, script.startLine, script.startColumn, script.endLine, script.endColumn, isContentScript, sourceURLParam, sourceMapURLParam);
+    m_frontendDispatcher->scriptParsed(scriptIDStr, script.url, script.startLine, script.startColumn, script.endLine, script.endColumn, isContentScript, sourceURLParam, sourceMapURLParam, isModule ? &isModule : nullptr);
 
     m_scripts.set(sourceID, script);
 
index a6f2ccf..21a2d74 100644 (file)
                 { "name": "endColumn", "type": "integer", "description": "Length of the last line of the script." },
                 { "name": "isContentScript", "type": "boolean", "optional": true, "description": "Determines whether this script is a user extension script." },
                 { "name": "sourceURL", "type": "string", "optional": true, "description": "sourceURL name of the script (if any)." },
-                { "name": "sourceMapURL", "type": "string", "optional": true, "description": "URL of source map associated with script (if any)." }
+                { "name": "sourceMapURL", "type": "string", "optional": true, "description": "URL of source map associated with script (if any)." },
+                { "name": "module", "type": "boolean", "optional": true, "description": "True if this script was parsed as a module." }
             ],
             "description": "Fired when virtual machine parses script. This event is also fired for all known and uncollected scripts upon enabling debugger."
         },
index 16bf8ad..ba5b0f6 100644 (file)
@@ -1,5 +1,35 @@
 2016-12-07  Joseph Pecoraro  <pecoraro@apple.com>
 
+        Web Inspector: Add ability to distinguish if a Script was parsed as a module
+        https://bugs.webkit.org/show_bug.cgi?id=164900
+        <rdar://problem/29323817>
+
+        Reviewed by Timothy Hatcher.
+
+        * UserInterface/Models/Script.js:
+        (WebInspector.Script.prototype.get sourceType):
+        New property of Scripts. SourceType is either Program or Module.
+
+        * UserInterface/Controllers/DebuggerManager.js:
+        (WebInspector.DebuggerManager.prototype.scriptDidParse):
+        * UserInterface/Protocol/DebuggerObserver.js:
+        (WebInspector.DebuggerObserver.prototype.scriptParsed):
+        Convert incoming module boolean into SourceType when creating new Scripts.
+
+        * UserInterface/Models/ScriptSyntaxTree.js:
+        (WebInspector.ScriptSyntaxTree):
+        (WebInspector.ScriptSyntaxTree.prototype._recurse):
+        (WebInspector.ScriptSyntaxTree.prototype._createInternalSyntaxTree):
+        Update the generic AST for new module specific Esprima types.
+
+        * UserInterface/Views/SourceCodeTextEditor.js:
+        (WebInspector.SourceCodeTextEditor.prototype.textEditorScriptSourceType):
+        * UserInterface/Views/TextEditor.js:
+        (WebInspector.TextEditor.prototype._startWorkerPrettyPrint):
+        For pretty printing correctly state if this is a module or not for Esprima.
+
+2016-12-07  Joseph Pecoraro  <pecoraro@apple.com>
+
         Web Inspector: Update Esprima to support new features / syntax (**, async/await, trailing comma)
         https://bugs.webkit.org/show_bug.cgi?id=164830
         <rdar://problem/29293814>
index 32134f8..2af3426 100644 (file)
@@ -671,7 +671,7 @@ WebInspector.DebuggerManager = class DebuggerManager extends WebInspector.Object
         InspectorFrontendHost.beep();
     }
 
-    scriptDidParse(target, scriptIdentifier, url, startLine, startColumn, endLine, endColumn, isContentScript, sourceURL, sourceMapURL)
+    scriptDidParse(target, scriptIdentifier, url, startLine, startColumn, endLine, endColumn, isModule, isContentScript, sourceURL, sourceMapURL)
     {
         // Called from WebInspector.DebuggerObserver.
 
@@ -690,7 +690,9 @@ WebInspector.DebuggerManager = class DebuggerManager extends WebInspector.Object
         if (!WebInspector.isDebugUIEnabled() && isWebKitInternalScript(sourceURL))
             return;
 
-        let script = new WebInspector.Script(target, scriptIdentifier, new WebInspector.TextRange(startLine, startColumn, endLine, endColumn), url, isContentScript, sourceURL, sourceMapURL);
+        let range = new WebInspector.TextRange(startLine, startColumn, endLine, endColumn);
+        let sourceType = isModule ? WebInspector.Script.SourceType.Module : WebInspector.Script.SourceType.Program;
+        let script = new WebInspector.Script(target, scriptIdentifier, range, url, sourceType, isContentScript, sourceURL, sourceMapURL);
 
         targetData.addScript(script);
 
index 1871966..22626ce 100644 (file)
@@ -25,7 +25,7 @@
 
 WebInspector.Script = class Script extends WebInspector.SourceCode
 {
-    constructor(target, id, range, url, injected, sourceURL, sourceMapURL)
+    constructor(target, id, range, url, sourceType, injected, sourceURL, sourceMapURL)
     {
         super();
 
@@ -37,6 +37,7 @@ WebInspector.Script = class Script extends WebInspector.SourceCode
         this._id = id || null;
         this._range = range || null;
         this._url = url || null;
+        this._sourceType = sourceType || WebInspector.Script.SourceType.Program;
         this._sourceURL = sourceURL || null;
         this._sourceMappingURL = sourceMapURL || null;
         this._injected = injected || false;
@@ -77,25 +78,14 @@ WebInspector.Script = class Script extends WebInspector.SourceCode
 
     // Public
 
-    get target()
-    {
-        return this._target;
-    }
-
-    get id()
-    {
-        return this._id;
-    }
-
-    get range()
-    {
-        return this._range;
-    }
-
-    get url()
-    {
-        return this._url;
-    }
+    get target() { return this._target; }
+    get id() { return this._id; }
+    get range() { return this._range; }
+    get url() { return this._url; }
+    get sourceType() { return this._sourceType; }
+    get sourceURL() { return this._sourceURL; }
+    get sourceMappingURL() { return this._sourceMappingURL; }
+    get injected() { return this._injected; }
 
     get contentIdentifier()
     {
@@ -119,16 +109,6 @@ WebInspector.Script = class Script extends WebInspector.SourceCode
         return this._sourceURL;
     }
 
-    get sourceURL()
-    {
-        return this._sourceURL;
-    }
-
-    get sourceMappingURL()
-    {
-        return this._sourceMappingURL;
-    }
-
     get urlComponents()
     {
         if (!this._urlComponents)
@@ -179,11 +159,6 @@ WebInspector.Script = class Script extends WebInspector.SourceCode
         return null;
     }
 
-    get injected()
-    {
-        return this._injected;
-    }
-
     get dynamicallyAddedScriptElement()
     {
         return this._dynamicallyAddedScriptElement;
@@ -315,6 +290,11 @@ WebInspector.Script = class Script extends WebInspector.SourceCode
     }
 };
 
+WebInspector.Script.SourceType = {
+    Program: "script-source-type-program",
+    Module: "script-source-type-module",
+};
+
 WebInspector.Script.TypeIdentifier = "script";
 WebInspector.Script.URLCookieKey = "script-url";
 WebInspector.Script.DisplayNameCookieKey = "script-display-name";
index b013806..ca7383a 100644 (file)
@@ -34,7 +34,8 @@ WebInspector.ScriptSyntaxTree = class ScriptSyntaxTree extends WebInspector.Obje
         this._script = script;
 
         try {
-            var esprimaSyntaxTree = esprima.parse(sourceText, {range: true});
+            let sourceType = this._script.sourceType === WebInspector.Script.SourceType.Module ? "module" : "script";
+            let esprimaSyntaxTree = esprima.parse(sourceText, {range: true, sourceType});
             this._syntaxTree = this._createInternalSyntaxTree(esprimaSyntaxTree);
             this._parsedSuccessfully = true;
         } catch (error) {
@@ -487,6 +488,39 @@ WebInspector.ScriptSyntaxTree = class ScriptSyntaxTree extends WebInspector.Obje
         case WebInspector.ScriptSyntaxTree.NodeType.YieldExpression:
             this._recurse(node.argument, callback, state);
             break;
+
+        // Modules.
+
+        case WebInspector.ScriptSyntaxTree.NodeType.ExportAllDeclaration:
+            this._recurse(node.source, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ExportNamedDeclaration:
+            this._recurse(node.declaration, callback, state);
+            this._recurseArray(node.specifiers, callback, state);
+            this._recurse(node.source, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ExportDefaultDeclaration:
+            this._recurse(node.declaration, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ExportSpecifier:
+            this._recurse(node.local, callback, state);
+            this._recurse(node.exported, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ImportDeclaration:
+            this._recurseArray(node.specifiers, callback, state);
+            this._recurse(node.source, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ImportDefaultSpecifier:
+            this._recurse(node.local, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ImportNamespaceSpecifier:
+            this._recurse(node.local, callback, state);
+            break;
+        case WebInspector.ScriptSyntaxTree.NodeType.ImportSpecifier:
+            this._recurse(node.imported, callback, state);
+            this._recurse(node.local, callback, state);
+            break;
+
         // All the leaf nodes go here.
         case WebInspector.ScriptSyntaxTree.NodeType.DebuggerStatement:
         case WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement:
@@ -938,6 +972,63 @@ WebInspector.ScriptSyntaxTree = class ScriptSyntaxTree extends WebInspector.Obje
                 delegate: node.delegate
             };
             break;
+
+        // Modules.
+
+        case "ExportAllDeclaration":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ExportAllDeclaration,
+                source: this._createInternalSyntaxTree(node.source),
+            };
+            break;
+        case "ExportNamedDeclaration":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ExportNamedDeclaration,
+                declaration: this._createInternalSyntaxTree(node.declaration),
+                specifiers: node.specifiers.map(this._createInternalSyntaxTree, this),
+                source: this._createInternalSyntaxTree(node.source),
+            };
+            break;
+        case "ExportDefaultDeclaration":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ExportDefaultDeclaration,
+                declaration: this._createInternalSyntaxTree(node.declaration),
+            };
+            break;
+        case "ExportSpecifier":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ExportSpecifier,
+                local: this._createInternalSyntaxTree(node.local),
+                exported: this._createInternalSyntaxTree(node.exported),
+            };
+            break;
+        case "ImportDeclaration":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ImportDeclaration,
+                specifiers: node.specifiers.map(this._createInternalSyntaxTree, this),
+                source: this._createInternalSyntaxTree(node.source),
+            };
+            break;
+        case "ImportDefaultSpecifier":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ImportDefaultSpecifier,
+                local: this._createInternalSyntaxTree(node.local),
+            };
+            break;
+        case "ImportNamespaceSpecifier":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ImportNamespaceSpecifier,
+                local: this._createInternalSyntaxTree(node.local),
+            };
+            break;
+        case "ImportSpecifier":
+            result = {
+                type: WebInspector.ScriptSyntaxTree.NodeType.ImportSpecifier,
+                imported: this._createInternalSyntaxTree(node.imported),
+                local: this._createInternalSyntaxTree(node.local),
+            };
+            break;
+
         default:
             console.error("Unsupported Syntax Tree Node: " + node.type, node);
             return null;
@@ -977,6 +1068,10 @@ WebInspector.ScriptSyntaxTree.NodeType = {
     DebuggerStatement: Symbol("debugger-statement"),
     DoWhileStatement: Symbol("do-while-statement"),
     EmptyStatement: Symbol("empty-statement"),
+    ExportAllDeclaration: Symbol("export-all-declaration"),
+    ExportDefaultDeclaration: Symbol("export-default-declaration"),
+    ExportNamedDeclaration: Symbol("export-named-declaration"),
+    ExportSpecifier: Symbol("export-specifier"),
     ExpressionStatement: Symbol("expression-statement"),
     ForInStatement: Symbol("for-in-statement"),
     ForOfStatement: Symbol("for-of-statement"),
@@ -985,6 +1080,10 @@ WebInspector.ScriptSyntaxTree.NodeType = {
     FunctionExpression: Symbol("function-expression"),
     Identifier: Symbol("identifier"),
     IfStatement: Symbol("if-statement"),
+    ImportDeclaration: Symbol("import-declaration"),
+    ImportDefaultSpecifier: Symbol("import-default-specifier"),
+    ImportNamespaceSpecifier: Symbol("import-namespace-specifier"),
+    ImportSpecifier: Symbol("import-specifier"),
     LabeledStatement: Symbol("labeled-statement"),
     Literal: Symbol("literal"),
     LogicalExpression: Symbol("logical-expression"),
index 0d093de..8dbd536 100644 (file)
@@ -37,7 +37,7 @@ WebInspector.DebuggerObserver = class DebuggerObserver
         WebInspector.debuggerManager.reset();
     }
 
-    scriptParsed(scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, sourceURL, sourceMapURL)
+    scriptParsed(scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, sourceURL, sourceMapURL, isModule)
     {
         if (this._legacyScriptParsed) {
             // COMPATIBILITY (iOS 9): Debugger.scriptParsed had slightly different arguments.
@@ -46,11 +46,11 @@ WebInspector.DebuggerObserver = class DebuggerObserver
             let legacySourceMapURL = arguments[7];
             let hasSourceURL = arguments[8];
             let legacySourceURL = hasSourceURL ? url : undefined;
-            WebInspector.debuggerManager.scriptDidParse(this.target, scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, legacySourceURL, legacySourceMapURL);
+            WebInspector.debuggerManager.scriptDidParse(this.target, scriptId, url, startLine, startColumn, endLine, endColumn, isModule, isContentScript, legacySourceURL, legacySourceMapURL);
             return;
         }
 
-        WebInspector.debuggerManager.scriptDidParse(this.target, scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, sourceURL, sourceMapURL);
+        WebInspector.debuggerManager.scriptDidParse(this.target, scriptId, url, startLine, startColumn, endLine, endColumn, isModule, isContentScript, sourceURL, sourceMapURL);
     }
 
     scriptFailedToParse(url, scriptSource, startLine, errorLine, errorMessage)
index 72da038..8b4a4b0 100644 (file)
@@ -1183,6 +1183,12 @@ WebInspector.SourceCodeTextEditor = class SourceCodeTextEditor extends WebInspec
         return this._sourceCode.url;
     }
 
+    textEditorScriptSourceType(textEditor)
+    {
+        let script = this._getAssociatedScript();
+        return script ? script.sourceType : WebInspector.Script.SourceType.Program;
+    }
+
     textEditorShouldHideLineNumber(textEditor, lineNumber)
     {
         return lineNumber in this._invalidLineNumbers;
index 0ccb2cb..523df76 100644 (file)
@@ -839,8 +839,8 @@ WebInspector.TextEditor = class TextEditor extends WebInspector.View
         let indentString = WebInspector.indentString();
         const includeSourceMapData = true;
 
-        // FIXME: Properly pass if this is a module or script.
-        const isModule = false;
+        let sourceType = this._delegate.textEditorScriptSourceType(this);
+        const isModule = sourceType === WebInspector.Script.SourceType.Module;
 
         let workerProxy = WebInspector.FormatterWorkerProxy.singleton();
         workerProxy.formatJavaScript(sourceText, isModule, indentString, includeSourceMapData, ({formattedText, sourceMapData}) => {