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

Open
wants to merge 3 commits into
base: main
Choose a base branch
Loading
from
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
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);
}

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.'))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lib files will be excluded from the Monaco Editor GitHub repository. monaco-editor

  const models = allModels.filter((uri) => !fileNameIsLib(uri)).map((uri) => uri.toString());

.filter(f => !f.endsWith('/.eslintrc') && !f.endsWith('.json'));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These files cause an error in the code below. typescript-website

  if (diagnostics.length) {
    const compilerHost = createVirtualCompilerHost(sys, compilerOptions, ts)
    throw new Error(ts.formatDiagnostics(diagnostics, compilerHost.compilerHost))
  }

};
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();
};
Comment on lines +169 to +172
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need this code to add the library and trigger linting after the parser is created, in case the user starts typing code in an empty playground.


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);
},
Comment on lines +110 to +112
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to add library files using compilerHost.createFile, because adding them directly to the system doesn't make them appear in getScriptFileNames, and thus they aren't included in the rootNames when creating program. typescript-website

export function createVirtualLanguageServiceHost(
  sys: System,
  rootFiles: string[],
  compilerOptions: CompilerOptions,
  ts: TS,
  customTransformers?: CustomTransformers
) {
  const fileNames = [...rootFiles]
  ...somecode

  const languageServiceHost: LanguageServiceHost = {
	
    getScriptFileNames: () => fileNames.slice(),

  }
  ...somecode
}

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.