Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a35a2bb

Browse files
authored
chore(website): [playground] use languageService for linting code (typescript-eslint#6806)
1 parent c036fe3 commit a35a2bb

File tree

13 files changed

+601
-460
lines changed

13 files changed

+601
-460
lines changed

packages/website/src/components/editor/LoadedEditor.tsx

Lines changed: 181 additions & 232 deletions
Large diffs are not rendered by default.

packages/website/src/components/editor/loadSandbox.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type MonacoEditor from 'monaco-editor';
22

33
import type * as SandboxFactory from '../../vendor/sandbox';
4-
import type { LintUtils } from '../linter/WebLinter';
4+
import type { WebLinterModule } from '../linter/types';
55

66
type Monaco = typeof MonacoEditor;
77
type Sandbox = typeof SandboxFactory;
88

99
export interface SandboxModel {
1010
main: Monaco;
1111
sandboxFactory: Sandbox;
12-
lintUtils: LintUtils;
12+
lintUtils: WebLinterModule;
1313
}
1414

1515
function loadSandbox(tsVersion: string): Promise<SandboxModel> {
@@ -32,7 +32,7 @@ function loadSandbox(tsVersion: string): Promise<SandboxModel> {
3232
});
3333

3434
// Grab a copy of monaco, TypeScript and the sandbox
35-
window.require<[Monaco, Sandbox, LintUtils]>(
35+
window.require<[Monaco, Sandbox, WebLinterModule]>(
3636
['vs/editor/editor.main', 'sandbox/index', 'linter/index'],
3737
(main, sandboxFactory, lintUtils) => {
3838
resolve({ main, sandboxFactory, lintUtils });

packages/website/src/components/editor/useSandboxServices.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { useEffect, useState } from 'react';
44

55
import type { createTypeScriptSandbox } from '../../vendor/sandbox';
66
import { createCompilerOptions } from '../lib/createCompilerOptions';
7-
import { WebLinter } from '../linter/WebLinter';
7+
import { createFileSystem } from '../linter/bridge';
8+
import { type CreateLinter, createLinter } from '../linter/createLinter';
9+
import type { PlaygroundSystem } from '../linter/types';
810
import type { RuleDetails } from '../types';
911
import { editorEmbedId } from './EditorEmbed';
1012
import { sandboxSingleton } from './loadSandbox';
1113
import type { CommonEditorProps } from './types';
1214

1315
export interface SandboxServicesProps {
14-
readonly jsx?: boolean;
1516
readonly onLoaded: (
1617
ruleDetails: RuleDetails[],
1718
tsVersions: readonly string[],
@@ -23,7 +24,8 @@ export type SandboxInstance = ReturnType<typeof createTypeScriptSandbox>;
2324

2425
export interface SandboxServices {
2526
sandboxInstance: SandboxInstance;
26-
webLinter: WebLinter;
27+
system: PlaygroundSystem;
28+
webLinter: CreateLinter;
2729
}
2830

2931
export const useSandboxServices = (
@@ -67,22 +69,27 @@ export const useSandboxServices = (
6769
colorMode === 'dark' ? 'vs-dark' : 'vs-light',
6870
);
6971

70-
const libEntries = new Map<string, string>();
72+
const system = createFileSystem(props, sandboxInstance.tsvfs);
73+
7174
const worker = await sandboxInstance.getWorkerProcess();
7275
if (worker.getLibFiles) {
7376
const libs = await worker.getLibFiles();
7477
for (const [key, value] of Object.entries(libs)) {
75-
libEntries.set('/' + key, value);
78+
system.writeFile('/' + key, value);
7679
}
7780
}
7881

79-
const system = sandboxInstance.tsvfs.createSystem(libEntries);
82+
window.system = system;
8083
window.esquery = lintUtils.esquery;
8184

82-
const webLinter = new WebLinter(system, compilerOptions, lintUtils);
85+
const webLinter = createLinter(
86+
system,
87+
lintUtils,
88+
sandboxInstance.tsvfs,
89+
);
8390

8491
onLoaded(
85-
Array.from(webLinter.rulesMap.values()),
92+
Array.from(webLinter.rules.values()),
8693
Array.from(
8794
new Set([...sandboxInstance.supportedVersions, window.ts.version]),
8895
)
@@ -91,8 +98,9 @@ export const useSandboxServices = (
9198
);
9299

93100
setServices({
94-
sandboxInstance,
101+
system,
95102
webLinter,
103+
sandboxInstance,
96104
});
97105
})
98106
.catch(setServices);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2+
export function createEventsBinder<T extends (...args: any[]) => void>(): {
3+
trigger: (...args: Parameters<T>) => void;
4+
register: (cb: T) => () => void;
5+
} {
6+
const events = new Set<T>();
7+
8+
return {
9+
trigger(...args: Parameters<T>): void {
10+
events.forEach(cb => cb(...args));
11+
},
12+
register(cb: T): () => void {
13+
events.add(cb);
14+
return (): void => {
15+
events.delete(cb);
16+
};
17+
},
18+
};
19+
}

packages/website/src/components/lib/jsonSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
22
import type * as ts from 'typescript';
33

4-
import type { WebLinter } from '../linter/WebLinter';
4+
import type { CreateLinter } from '../linter/createLinter';
55

66
/**
77
* Get the JSON schema for the eslint config
88
* Currently we only support the rules and extends
99
*/
10-
export function getEslintJsonSchema(linter: WebLinter): JSONSchema4 {
10+
export function getEslintJsonSchema(linter: CreateLinter): JSONSchema4 {
1111
const properties: Record<string, JSONSchema4> = {};
1212

13-
for (const [, item] of linter.rulesMap) {
13+
for (const [, item] of linter.rules) {
1414
properties[item.name] = {
1515
description: `${item.description}\n ${item.url}`,
1616
title: item.name.startsWith('@typescript') ? 'Rules' : 'Core rules',

packages/website/src/components/linter/CompilerHost.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/website/src/components/linter/WebLinter.ts

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)