Skip to content

Use language injection for ease of development in JetBrains IDE #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,83 +77,84 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
scriptsURIs.map(uri => "\"" + escapeJS(uri.toASCIIString) + "\"")
val scriptsURIsJSArray = scriptsURIsAsJSStrings.mkString("[", ", ", "]")
val jsDOMCode = {
s"""
|
|(function () {
| var jsdom = require("jsdom");
|
| if (typeof jsdom.JSDOM === "function") {
| // jsdom >= 10.0.0
| var virtualConsole = new jsdom.VirtualConsole()
| .sendTo(console, { omitJSDOMErrors: true });
| virtualConsole.on("jsdomError", function (error) {
| try {
| // Display as much info about the error as possible
| if (error.detail && error.detail.stack) {
| console.error("" + error.detail);
| console.error(error.detail.stack);
| } else {
| console.error(error);
| }
| } finally {
| // Whatever happens, kill the process so that the run fails
| process.exit(1);
| }
| });
|
| var dom = new jsdom.JSDOM("", {
| virtualConsole: virtualConsole,
| url: "http://localhost/",
|
| /* Allow unrestricted <script> tags. This is exactly as
| * "dangerous" as the arbitrary execution of script files we
| * do in the non-jsdom Node.js env.
| */
| resources: "usable",
| runScripts: "dangerously"
| });
|
| var window = dom.window;
| window["scalajsCom"] = global.scalajsCom;
|
| var scriptsSrcs = $scriptsURIsJSArray;
| for (var i = 0; i < scriptsSrcs.length; i++) {
| var script = window.document.createElement("script");
| script.src = scriptsSrcs[i];
| window.document.body.appendChild(script);
| }
| } else {
| // jsdom v9.x
| var virtualConsole = jsdom.createVirtualConsole()
| .sendTo(console, { omitJsdomErrors: true });
| virtualConsole.on("jsdomError", function (error) {
| /* This inelegant if + console.error is the only way I found
| * to make sure the stack trace of the original error is
| * printed out.
| */
| if (error.detail && error.detail.stack)
| console.error(error.detail.stack);
|
| // Throw the error anew to make sure the whole execution fails
| throw error;
| });
|
| jsdom.env({
| html: "",
| virtualConsole: virtualConsole,
| url: "http://localhost/",
| created: function (error, window) {
| if (error == null) {
| window["scalajsCom"] = global.scalajsCom;
| } else {
| throw error;
| }
| },
| scripts: $scriptsURIsJSArray
| });
| }
|})();
|""".stripMargin
// language=JavaScript
s"""
(function () {
var jsdom = require("jsdom");

if (typeof jsdom.JSDOM === "function") {
// jsdom >= 10.0.0
var virtualConsole = new jsdom.VirtualConsole()
.sendTo(console, { omitJSDOMErrors: true });
virtualConsole.on("jsdomError", function (error) {
try {
// Display as much info about the error as possible
if (error.detail && error.detail.stack) {
console.error("" + error.detail);
console.error(error.detail.stack);
} else {
console.error(error);
}
} finally {
// Whatever happens, kill the process so that the run fails
process.exit(1);
}
});

var dom = new jsdom.JSDOM("", {
virtualConsole: virtualConsole,
url: "http://localhost/",

/* Allow unrestricted <script> tags. This is exactly as
* "dangerous" as the arbitrary execution of script files we
* do in the non-jsdom Node.js env.
*/
resources: "usable",
runScripts: "dangerously"
});

var window = dom.window;
window["scalajsCom"] = global.scalajsCom;

var scriptsSrcs = $scriptsURIsJSArray;
for (var i = 0; i < scriptsSrcs.length; i++) {
var script = window.document.createElement("script");
script.src = scriptsSrcs[i];
window.document.body.appendChild(script);
}
} else {
// jsdom v9.x
var virtualConsole = jsdom.createVirtualConsole()
.sendTo(console, { omitJsdomErrors: true });
virtualConsole.on("jsdomError", function (error) {
/* This inelegant if + console.error is the only way I found
* to make sure the stack trace of the original error is
* printed out.
*/
if (error.detail && error.detail.stack)
console.error(error.detail.stack);

// Throw the error anew to make sure the whole execution fails
throw error;
});

jsdom.env({
html: "",
virtualConsole: virtualConsole,
url: "http://localhost/",
created: function (error, window) {
if (error == null) {
window["scalajsCom"] = global.scalajsCom;
window["require"] = global.require;
} else {
throw error;
}
},
scripts: $scriptsURIsJSArray
});
}
})();
"""
}
List(Files.write(
Jimfs.newFileSystem().getPath("codeWithJSDOMContext.js"),
Expand All @@ -172,6 +173,7 @@ object JSDOMNodeJSEnv {
try {
val f = path.toFile
val pathJS = "\"" + escapeJS(f.getAbsolutePath) + "\""
// language=JavaScript
p.println(s"""
require('vm').runInThisContext(
require('fs').readFileSync($pathJS, { encoding: "utf-8" }),
Expand All @@ -183,6 +185,7 @@ object JSDOMNodeJSEnv {
val code = new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
val codeJS = "\"" + escapeJS(code) + "\""
val pathJS = "\"" + escapeJS(path.toString) + "\""
// language=JavaScript
p.println(s"""
require('vm').runInThisContext(
$codeJS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class JSDOMNodeJSEnvTest {
@Test
def historyAPI: Unit = {
kit.withRun(
// language=JavaScript
"""
|console.log(window.location.href);
|window.history.pushState({}, "", "/foo");
|console.log(window.location.href);
""".stripMargin) {
console.log(window.location.href);
window.history.pushState({}, "", "/foo");
console.log(window.location.href);
""") {
_.expectOut("http://localhost/\n")
.expectOut("http://localhost/foo\n")
}
Expand Down