Skip to content

Commit 3ee2bf3

Browse files
lbogdanCompuIves
authored andcommitted
Fix eslint issues in the sandbox editor. (codesandbox#182)
* Check if eslint is already initialized. * Save eslint's window.require and use it when validating.
1 parent 52a87ab commit 3ee2bf3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/app/components/sandbox/CodeEditor/CodeMirror.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ export default class CodeEditor extends React.PureComponent<Props, State> {
413413
}
414414

415415
if (preferences.lintEnabled) {
416+
const initialized = 'eslint' in window;
416417
System.import('app/utils/codemirror/eslint-lint')
417-
.then(initializer => initializer.default())
418+
.then(initializer => !initialized && initializer.default())
418419
.then(() => {
419420
this.codemirror.setOption('lint', true);
420421
});

src/app/utils/codemirror/eslint-lint.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ const defaultConfig = {
307307
};
308308

309309
let linter = null;
310+
let eslintRequire = null;
310311

311312
function getPos(error, from) {
312313
let line = error.line - 1;
@@ -336,7 +337,11 @@ function getSeverity(error) {
336337

337338
function eslintValidate(text) {
338339
if (!linter) return [];
339-
return linter.verify(text, defaultConfig);
340+
const saveRequire = window.require;
341+
window.require = eslintRequire;
342+
const result = linter.verify(text, defaultConfig);
343+
window.require = saveRequire;
344+
return result;
340345
}
341346

342347
export function validator(text: string) {
@@ -377,6 +382,7 @@ export default (async function initialize() {
377382
await delay(100);
378383
}
379384

385+
eslintRequire = window.require;
380386
window.require = origRequire;
381387

382388
linter = new window.eslint();

0 commit comments

Comments
 (0)