Skip to content

Commit a0d67b3

Browse files
committed
wasm: refactor: split DocumentLinter
DocumentLinter has two responsibilities: 1. sync text changes to the DocumentForVSCode 2. propagate diagnostics from the synced document Config files need #1, but will have a different way of dealing with #2. Separate these responsibilities into two classes: AbstractSyncedDocument (for #1) and DocumentLinter (for #2). This commit should not change behavior.
1 parent 8d9d53f commit a0d67b3

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

wasm/quick-lint-js.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ exports.LintingCrashed = LintingCrashed;
4141
class DocumentLinterDisposed extends Error {}
4242
exports.DocumentLinterDisposed = DocumentLinterDisposed;
4343

44-
class DocumentLinter {
45-
// document has the following methods:
46-
//
47-
// getText(): string;
48-
// setDiagnostics(diagnostics: Object[]): void;
49-
// removeDiagnostics(): void;
50-
constructor(document, documentProcessManager) {
51-
this._document = document;
44+
class AbstractSyncedDocument {
45+
constructor(documentProcessManager) {
5246
this._documentProcessManager = documentProcessManager;
5347
this._state = DocumentLinterState.NO_WASM_DOC;
5448

@@ -136,7 +130,6 @@ class DocumentLinter {
136130
default:
137131
throw new Error(`Unexpected linter state: ${this._state}`);
138132
}
139-
this._document.removeDiagnostics();
140133
}
141134

142135
async editorChangedVisibilityAsync() {
@@ -196,8 +189,7 @@ class DocumentLinter {
196189
this._pendingChanges.length = 0;
197190
// END CRITICAL SECTION (no awaiting above)
198191

199-
let diags = this._wasmDoc.lint();
200-
this._document.setDiagnostics(diags);
192+
this._documentSynced();
201193
} catch (e) {
202194
// END CRITICAL SECTION (no awaiting above)
203195
if (e instanceof ProcessCrashed) {
@@ -234,14 +226,13 @@ class DocumentLinter {
234226
start: { line: 0, character: 0 },
235227
end: { line: 0, character: 0 },
236228
},
237-
this._document.getText()
229+
this._getText()
238230
);
239231
this._pendingChanges.length = 0;
240232
this._state = DocumentLinterState.WASM_DOC_LOADED;
241233
// END CRITICAL SECTION (no awaiting above)
242234

243-
let diags = this._wasmDoc.lint();
244-
this._document.setDiagnostics(diags);
235+
this._documentSynced();
245236
} catch (e) {
246237
if (e instanceof ProcessCrashed) {
247238
await this._recoverFromCrashAsync(e);
@@ -272,14 +263,14 @@ class DocumentLinter {
272263
start: { line: 0, character: 0 },
273264
end: { line: 0, character: 0 },
274265
},
275-
this._document.getText()
266+
this._getText()
276267
);
277268
this._pendingChanges.length = 0;
278269
this._wasmDoc = wasmDoc;
279270
this._state = DocumentLinterState.WASM_DOC_LOADED;
280271
// END CRITICAL SECTION (no awaiting above)
281272

282-
diags = wasmDoc.lint();
273+
this._documentSynced();
283274
} catch (e) {
284275
this._wasmDoc = null;
285276
this._wasmDocPromise = null;
@@ -290,11 +281,49 @@ class DocumentLinter {
290281
throw e;
291282
}
292283
}
293-
this._document.setDiagnostics(diags);
294284
})();
295285
// END CRITICAL SECTION (no awaiting above)
296286
await this._recoveryPromise;
297287
}
288+
289+
// Abstract:
290+
_getText() {
291+
throw new Error("Please override _getText in a derived class");
292+
}
293+
294+
// Abstract:
295+
_documentSynced() {
296+
throw new Error("Please override _documentSynced in a derived class");
297+
}
298+
}
299+
300+
class DocumentLinter extends AbstractSyncedDocument {
301+
// document has the following methods:
302+
//
303+
// getText(): string;
304+
// setDiagnostics(diagnostics: Object[]): void;
305+
// removeDiagnostics(): void;
306+
constructor(document, documentProcessManager) {
307+
super(documentProcessManager);
308+
this._document = document;
309+
}
310+
311+
// Override:
312+
_getText() {
313+
return this._document.getText();
314+
}
315+
316+
// Override:
317+
_documentSynced() {
318+
let diags = this._wasmDoc.lint();
319+
this._document.setDiagnostics(diags);
320+
}
321+
322+
// Override:
323+
async disposeAsync() {
324+
await super.disposeAsync();
325+
this._document.removeDiagnostics();
326+
}
298327
}
299328
exports.DocumentLinter = DocumentLinter;
300329

0 commit comments

Comments
 (0)