Skip to content

Commit f57b60b

Browse files
author
Ives van Hoorne
committed
Fix JSX syntax highlighting, fix concurrent transpiling jobs
1 parent 717e3f9 commit f57b60b

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

src/app/components/sandbox/CodeEditor/monaco/workers/syntax-highlighter.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,35 @@ self.importScripts([
55
function getLineNumberAndOffset(start, lines) {
66
let line = 0;
77
let offset = 0;
8-
while (offset + lines[line] <= start) {
8+
while (offset + lines[line] < start) {
99
offset += lines[line] + 1;
1010
line += 1;
1111
}
1212

1313
return { line: line + 1, offset };
1414
}
1515

16+
function nodeToRange(node) {
17+
if (
18+
typeof node.getStart === 'function' &&
19+
typeof node.getEnd === 'function'
20+
) {
21+
return [node.getStart(), node.getEnd()];
22+
} else if (
23+
typeof node.pos !== 'undefined' &&
24+
typeof node.end !== 'undefined'
25+
) {
26+
return [node.pos, node.end];
27+
}
28+
return [0, 0];
29+
}
30+
1631
function addChildNodes(node, lines, classifications) {
1732
self.ts.forEachChild(node, id => {
18-
const { offset, line: startLine } = getLineNumberAndOffset(
19-
id.getStart(),
20-
lines
21-
);
22-
const { line: endLine } = getLineNumberAndOffset(id.getEnd(), lines);
33+
const [start, end] = nodeToRange(id);
34+
35+
const { offset, line: startLine } = getLineNumberAndOffset(start, lines);
36+
const { line: endLine } = getLineNumberAndOffset(end, lines);
2337
classifications.push({
2438
start: id.getStart() + 1 - offset,
2539
end: id.getEnd() + 1 - offset,

src/sandbox/eval/manager.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export default class Manager {
2222
modules: Array<Module>;
2323
directories: Array<Directory>;
2424

25+
// Mark if the manager is transpiling, because there is a chance that the
26+
// manager gets concurrent render requests. When the first finishes with
27+
// transpiling it will try to evaluate normally, but that's not possible
28+
// since another transpilation is happening. That's why we check for this value
29+
// first.
30+
transpiling: boolean;
31+
2532
constructor(
2633
id: string,
2734
modules: Array<Module>,
@@ -33,6 +40,7 @@ export default class Manager {
3340
this.directories = directories;
3441
this.preset = preset;
3542
this.transpiledModules = {};
43+
this.transpiling = false;
3644

3745
console.log(this);
3846
}
@@ -42,14 +50,18 @@ export default class Manager {
4250
}
4351

4452
evaluateModule(module: Module) {
45-
const transpiledModule = this.getTranspiledModule(module);
53+
if (!this.transpiling) {
54+
const transpiledModule = this.getTranspiledModule(module);
55+
56+
// Run post evaluate first
57+
const exports = this.evaluateTranspiledModule(transpiledModule, []);
4658

47-
// Run post evaluate first
48-
const exports = this.evaluateTranspiledModule(transpiledModule, []);
59+
this.getTranspiledModules().forEach(t => t.postEvaluate(this));
4960

50-
this.getTranspiledModules().forEach(t => t.postEvaluate(this));
61+
return exports;
62+
}
5163

52-
return exports;
64+
return null;
5365
}
5466

5567
evaluateTranspiledModule(
@@ -197,6 +209,7 @@ export default class Manager {
197209
* delete caches accordingly
198210
*/
199211
updateData(modules: Array<Module>, directories: Array<Directory>) {
212+
this.transpiling = true;
200213
// Create an object with mapping from modules
201214
const moduleObject = this.modules.reduce(
202215
(prev, next) => ({
@@ -259,11 +272,21 @@ export default class Manager {
259272

260273
return Promise.all(
261274
transpiledModulesToUpdate.map(tModule => tModule.transpile(this))
262-
).then(x => {
263-
this.modules = modules;
264-
this.directories = directories;
265-
266-
return x;
267-
});
275+
)
276+
.then(x => {
277+
this.modules = modules;
278+
this.directories = directories;
279+
this.transpiling = false;
280+
281+
return x;
282+
})
283+
.catch(e => {
284+
// Also set new module info for a catch
285+
this.modules = modules;
286+
this.directories = directories;
287+
this.transpiling = false;
288+
289+
throw e;
290+
});
268291
}
269292
}

src/sandbox/eval/transpiled-module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ export default class TranspiledModule {
317317

318318
let code = this.module.code || '';
319319
let finalSourceMap = null;
320+
320321
for (let i = 0; i < transpilers.length; i += 1) {
321322
const transpilerConfig = transpilers[i];
322323
const loaderContext = this.getLoaderContext(
@@ -338,6 +339,7 @@ export default class TranspiledModule {
338339
} catch (e) {
339340
e.fileName = loaderContext.path;
340341
e.module = this.module;
342+
this.resetTranspilation();
341343
throw e;
342344
}
343345
}

0 commit comments

Comments
 (0)