Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(website): acquired types are shown in the editor but not reflected in linting #11198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 5 additions & 1 deletion 6 packages/website/src/components/editor/useSandboxServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import semverSatisfies from 'semver/functions/satisfies';

import type { createTypeScriptSandbox } from '../../vendor/sandbox';
import type { CreateLinter } from '../linter/createLinter';
import type { PlaygroundSystem } from '../linter/types';
import type { PlaygroundSystem, RegisterFile } from '../linter/types';
import type { RuleDetails } from '../types';
import type { CommonEditorProps } from './types';

import rootPackageJson from '../../../../../package.json';
import { createCompilerOptions } from '../lib/createCompilerOptions';
import { createEventsBinder } from '../lib/createEventsBinder';
import { createFileSystem } from '../linter/bridge';
import { createLinter } from '../linter/createLinter';
import { createTwoslashInlayProvider } from './createProvideTwoslashInlay';
Expand Down Expand Up @@ -79,6 +80,7 @@ export const useSandboxServices = (
sandboxInstance.language,
createTwoslashInlayProvider(sandboxInstance),
);
const onModelCreate = createEventsBinder<RegisterFile>();

const system = createFileSystem(props, sandboxInstance.tsvfs);

Expand All @@ -89,6 +91,7 @@ export const useSandboxServices = (
}
const path = model.uri.path.replace('/file:///', '/');
system.writeFile(path, model.getValue());
onModelCreate.trigger(path, model.getValue());
});
// Delete files in vfs when a model is disposed in the editor (this is used only for ATA types)
sandboxInstance.monaco.editor.onWillDisposeModel(model => {
Expand Down Expand Up @@ -117,6 +120,7 @@ export const useSandboxServices = (
lintUtils,
sandboxInstance.tsvfs,
);
onModelCreate.register(webLinter.registerFile);

onLoaded(
[...webLinter.rules.values()],
Expand Down
10 changes: 8 additions & 2 deletions 10 packages/website/src/components/linter/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function createFileSystem(
const files = new Map<string, string>();
files.set(`/.eslintrc`, config.eslintrc);
files.set(`/tsconfig.json`, config.tsconfig);
files.set(`/input${config.fileType}`, config.code);
if (config.code !== '') {
files.set(`/input${config.fileType}`, config.code);
}
Copy link
Member

Choose a reason for hiding this comment

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

[Question] Why wouldn't we want to set '' for code? What if the user explicitly puts in a blank file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The above code is executed when the playground is first loaded.

return vfs.createVirtualTypeScriptEnvironment(
system,
[...registeredFiles],
window.ts,
compilerOptions,

When we set '' for code in the system, a "File '/input.tsx' not found." error occurs in createVirtualTypeScriptEnvironment

If the above code is missing, the playground will not load successfully when the user clicks the playground button in this page

Therefore, we should avoid setting '' fo code in the system.

// if text is empty use empty line to avoid error
const code = text || '\n';

Also, when the user changes the code to '', this code prevents the code field from being set to '' in the system.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, that makes sense. Thanks!


const fileWatcherCallbacks = new Map<RegExp, Set<ts.FileWatcherCallback>>();

Expand Down Expand Up @@ -78,6 +80,10 @@ export function createFileSystem(
const expPath = getPathRegExp(path);
return [...files.keys()].filter(fileName => expPath.test(fileName));
};

system.getScriptFileNames = (): string[] => {
return [...files.keys()]
.filter(fileName => !fileName.startsWith('/lib.'))
mdm317 marked this conversation as resolved.
Show resolved Hide resolved
.filter(f => !f.endsWith('/.eslintrc') && !f.endsWith('.json'));
mdm317 marked this conversation as resolved.
Show resolved Hide resolved
};
return system;
}
8 changes: 8 additions & 0 deletions 8 packages/website/src/components/linter/createLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
LinterOnLint,
LinterOnParse,
PlaygroundSystem,
RegisterFile,
WebLinterModule,
} from './types';

Expand All @@ -36,6 +37,7 @@ export interface CreateLinter {
triggerFix(filename: string): Linter.FixReport | undefined;
triggerLint(filename: string): void;
updateParserOptions(sourceType?: SourceType): void;
registerFile: RegisterFile;
}

export function createLinter(
Expand Down Expand Up @@ -164,6 +166,11 @@ export function createLinter(
}
};

const registerFile = (fileName: string, code: string) => {
parser.registerFile(fileName, code);
triggerLintAll();
};
mdm317 marked this conversation as resolved.
Show resolved Hide resolved

const triggerLintAll = (): void => {
system.searchFiles('/input.*').forEach(triggerLint);
};
Expand All @@ -185,6 +192,7 @@ export function createLinter(
configs: [...configs.keys()],
onLint: onLint.register,
onParse: onParse.register,
registerFile,
rules,
triggerFix,
triggerLint,
Expand Down
12 changes: 7 additions & 5 deletions 12 packages/website/src/components/linter/createParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type * as ts from 'typescript';
import type {
ParseSettings,
PlaygroundSystem,
RegisterFile,
UpdateModel,
WebLinterModule,
} from './types';
Expand All @@ -20,15 +21,14 @@ export function createParser(
vfs: typeof tsvfs,
): {
updateConfig: (compilerOptions: ts.CompilerOptions) => void;
registerFile: RegisterFile;
} & Parser.ParserModule {
const registeredFiles = new Set<string>();

const createEnv = (
compilerOptions: ts.CompilerOptions,
): tsvfs.VirtualTypeScriptEnvironment => {
return vfs.createVirtualTypeScriptEnvironment(
system,
[...registeredFiles],
system.getScriptFileNames(),
window.ts,
compilerOptions,
);
Expand All @@ -46,10 +46,9 @@ export function createParser(
// if text is empty use empty line to avoid error
const code = text || '\n';

if (registeredFiles.has(filePath)) {
if (system.fileExists(filePath)) {
compilerHost.updateFile(filePath, code);
} else {
registeredFiles.add(filePath);
compilerHost.createFile(filePath, code);
}

Expand Down Expand Up @@ -108,6 +107,9 @@ export function createParser(
visitorKeys: utils.visitorKeys,
};
},
registerFile(filePath: string, code: string): void {
compilerHost.createFile(filePath, code);
},
mdm317 marked this conversation as resolved.
Show resolved Hide resolved
updateConfig(compilerOptions): void {
compilerHost = createEnv(compilerOptions);
},
Expand Down
3 changes: 3 additions & 0 deletions 3 packages/website/src/components/linter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface WebLinterModule {
export type PlaygroundSystem = {
removeFile: (fileName: string) => void;
searchFiles: (path: string) => string[];
getScriptFileNames: () => string[];
} & Required<Pick<ts.System, 'deleteFile' | 'watchFile'>> &
ts.System;

Expand All @@ -39,3 +40,5 @@ export type LinterOnLint = (
) => void;

export type LinterOnParse = (fileName: string, model: UpdateModel) => void;

export type RegisterFile = (fileName: string, code: string) => void;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.