Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/playground-code-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also support completions for tsx and jsx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think we could see if it works out of the box with those too

}

override focus() {
Expand Down
12 changes: 9 additions & 3 deletions src/typescript-worker/language-service-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -66,12 +67,17 @@ class WorkerLanguageServiceHost implements ts.LanguageServiceHost {
*/
updateFileContentIfNeeded(fileName: string, content: string) {
const file = this.files.get(fileName);
if (file && file.content !== content) {
if (file) {
if (file.content === content) {
// The file hasn't changed, exit early.
return;
}
file.content = content;
file.version += 1;
} else {
this.files.set(fileName, {content, version: 0});
return;
}

this.files.set(fileName, {content, version: 0});
}

/**
Expand Down
38 changes: 25 additions & 13 deletions src/typescript-worker/typescript-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const PROCESSED_FILE_ENDINGS = ['.ts', '.jsx', '.tsx'];
const COMPILED_FILE_ENDINGS = ['.ts', '.jsx', '.tsx'];

(Just a nit, since .js is "processed", it's just not compiled)


export async function* processTypeScriptFiles(
workerContext: WorkerContext,
results: AsyncIterable<BuildOutput> | Iterable<BuildOutput>
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (result.kind !== 'file') continue;
if (result.kind !== 'file') {
continue;
}

Style nit


// 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.
}
}
}
Expand Down