Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy 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 fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionspackages/website/src/components/linter/bridge.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
}

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
ContributorAuthor

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.

returnvfs.createVirtualTypeScriptEnvironment(
system,
[...registeredFiles],
window.ts,
compilerOptions,

When we set'' for code in thesystem, a"File '/input.tsx' not found." error occurs increateVirtualTypeScriptEnvironment

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

Therefore, we should avoid setting'' fo code in thesystem.

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

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

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 DownExpand 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.'))
.filter(f => !f.endsWith('/.eslintrc') && !f.endsWith('.json'));
};
return system;
}
8 changes: 8 additions & 0 deletionspackages/website/src/components/linter/createLinter.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -164,6 +166,11 @@ export function createLinter(
}
};

const registerFile = (fileName: string, code: string) => {
parser.registerFile(fileName, code);
triggerLintAll();
};

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 deletionspackages/website/src/components/linter/createParser.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -108,6 +107,9 @@ export function createParser(
visitorKeys: utils.visitorKeys,
};
},
registerFile(filePath: string, code: string): void {
compilerHost.createFile(filePath, code);
},
updateConfig(compilerOptions): void {
compilerHost = createEnv(compilerOptions);
},
Expand Down
3 changes: 3 additions & 0 deletionspackages/website/src/components/linter/types.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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;

[8]ページ先頭

©2009-2025 Movatter.jp