From 84e1db447f045f19fcdb6c5135e73f59352a4614 Mon Sep 17 00:00:00 2001 From: Matsuuu Date: Thu, 15 Sep 2022 14:00:31 +0300 Subject: [PATCH 1/3] Skip setting file to language service if exists and has not changed --- src/typescript-worker/language-service-context.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/typescript-worker/language-service-context.ts b/src/typescript-worker/language-service-context.ts index 00d9cdbf..2ea57154 100644 --- a/src/typescript-worker/language-service-context.ts +++ b/src/typescript-worker/language-service-context.ts @@ -66,12 +66,17 @@ class WorkerLanguageServiceHost implements ts.LanguageServiceHost { */ updateFileContentIfNeeded(fileName: string, content: string) { const file = this.files.get(fileName); - if (file && file.content !== content) { - file.content = content; - file.version += 1; - } else { - this.files.set(fileName, {content, version: 0}); + if (file) { + if (file.content === content) { + // The file hasn't changed, exit early. + return; + } + file.content = content; + file.version += 1; + return; } + + this.files.set(fileName, {content, version: 0}); } /** From 5c0fb31ac24af164eb164cd728a3109fd49c4179 Mon Sep 17 00:00:00 2001 From: Matsuuu Date: Thu, 15 Sep 2022 14:07:22 +0300 Subject: [PATCH 2/3] Lint & format --- src/typescript-worker/language-service-context.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/typescript-worker/language-service-context.ts b/src/typescript-worker/language-service-context.ts index 2ea57154..41ab03e1 100644 --- a/src/typescript-worker/language-service-context.ts +++ b/src/typescript-worker/language-service-context.ts @@ -67,13 +67,13 @@ class WorkerLanguageServiceHost implements ts.LanguageServiceHost { updateFileContentIfNeeded(fileName: string, content: string) { const file = this.files.get(fileName); if (file) { - if (file.content === content) { - // The file hasn't changed, exit early. - return; - } - file.content = content; - file.version += 1; + if (file.content === content) { + // The file hasn't changed, exit early. return; + } + file.content = content; + file.version += 1; + return; } this.files.set(fileName, {content, version: 0}); From e889da4777b9974e31cda823b72fee7b15bb11ab Mon Sep 17 00:00:00 2001 From: Matsuuu Date: Thu, 15 Sep 2022 16:38:21 +0300 Subject: [PATCH 3/3] Allow completions and diagnostics in javascript files --- src/playground-code-editor.ts | 2 +- .../language-service-context.ts | 1 + src/typescript-worker/typescript-builder.ts | 38 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/playground-code-editor.ts b/src/playground-code-editor.ts index 74873047..1085ef80 100644 --- a/src/playground-code-editor.ts +++ b/src/playground-code-editor.ts @@ -632,7 +632,7 @@ export class PlaygroundCodeEditor extends LitElement { private _currentFiletypeSupportsCompletion() { // Currently we are only supporting code completion for TS. Change // this in a case that we start to support it for other languages too. - return this.type === 'ts'; + return this.type === 'ts' || this.type === 'js'; } override focus() { diff --git a/src/typescript-worker/language-service-context.ts b/src/typescript-worker/language-service-context.ts index 41ab03e1..54dec804 100644 --- a/src/typescript-worker/language-service-context.ts +++ b/src/typescript-worker/language-service-context.ts @@ -13,6 +13,7 @@ const compilerOptions = { skipDefaultLibCheck: true, skipLibCheck: true, allowJs: true, + checkJs: true, moduleResolution: ts.ModuleResolutionKind.NodeJs, jsx: ts.JsxEmit.React, lib: ['dom', 'esnext'], diff --git a/src/typescript-worker/typescript-builder.ts b/src/typescript-worker/typescript-builder.ts index 544d4e44..67a9dc90 100644 --- a/src/typescript-worker/typescript-builder.ts +++ b/src/typescript-worker/typescript-builder.ts @@ -10,6 +10,8 @@ import {PackageJson} from './util.js'; import {makeLspDiagnostic} from './diagnostic.js'; import {WorkerContext} from './worker-context.js'; +const PROCESSED_FILE_ENDINGS = ['.ts', '.jsx', '.tsx']; + export async function* processTypeScriptFiles( workerContext: WorkerContext, results: AsyncIterable | Iterable @@ -20,22 +22,32 @@ export async function* processTypeScriptFiles( let packageJson: PackageJson | undefined; const compilerInputs = []; for await (const result of results) { + if (result.kind !== 'file') continue; + + // Collect filetypes that need to be compiled. They will be handled later on in the process. if ( - result.kind === 'file' && - (result.file.name.endsWith('.ts') || - result.file.name.endsWith('.jsx') || - result.file.name.endsWith('.tsx')) + PROCESSED_FILE_ENDINGS.some((ending) => result.file.name.endsWith(ending)) ) { compilerInputs.push(result.file); - } else { - yield result; - if (result.kind === 'file' && result.file.name === 'package.json') { - try { - packageJson = JSON.parse(result.file.content) as PackageJson; - } catch (e) { - // A bit hacky, but BareModuleTransformer already emits a diagnostic - // for this case, so we don't need another one. - } + continue; + } + // Everything that reaches this point should be usable out of the box without compiling. + // Therefore we can just yield the result to the caller. + yield result; + + // Even though we don't need to compile javascript files, we want to append them to our + // compilerinputs to get completions and diagnostics for those files. + if (result.file.name.endsWith('.js')) { + compilerInputs.push(result.file); + continue; + } + + if (result.file.name === 'package.json') { + try { + packageJson = JSON.parse(result.file.content) as PackageJson; + } catch (e) { + // A bit hacky, but BareModuleTransformer already emits a diagnostic + // for this case, so we don't need another one. } } }