mirror of
https://github.com/danbulant/monaco-yaml
synced 2026-06-19 14:31:13 +00:00
Apply various stylistic fixes
- ESLint config `remcohaszing/typechecking` is extended. - Various previously disabled ESLint rules have now been enabled. - Various `any` types have been fixed. - Removed useless type check of diagnostic code. - The diagnostics adapter listener has been turned into an actual map.
This commit is contained in:
parent
3541f69831
commit
9739d143d3
8 changed files with 53 additions and 31 deletions
|
|
@ -1,24 +1,18 @@
|
|||
extends: remcohaszing
|
||||
extends:
|
||||
- remcohaszing
|
||||
- remcohaszing/typechecking
|
||||
rules:
|
||||
class-methods-use-this: off
|
||||
max-classes-per-file: off
|
||||
no-console: off
|
||||
no-restricted-globals: off
|
||||
no-underscore-dangle: off
|
||||
no-useless-constructor: off
|
||||
|
||||
'@typescript-eslint/naming-convention': off
|
||||
'@typescript-eslint/no-parameter-properties': off
|
||||
'@typescript-eslint/no-misused-promises': off
|
||||
'@typescript-eslint/no-shadow': off
|
||||
'@typescript-eslint/prefer-optional-chain': off
|
||||
'@typescript-eslint/no-unnecessary-condition': off
|
||||
|
||||
import/no-extraneous-dependencies: off
|
||||
import/no-unresolved: off
|
||||
import/no-webpack-loader-syntax: off
|
||||
|
||||
jsdoc/require-jsdoc: off
|
||||
|
||||
node/no-extraneous-import: off
|
||||
node/no-unpublished-import: off
|
||||
node/no-unsupported-features/es-syntax: off
|
||||
node/no-unsupported-features/node-builtins: off
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ npm install monaco-yaml
|
|||
|
||||
Import `monaco-yaml` and configure it before an editor instance is created.
|
||||
|
||||
```ts
|
||||
```typescript
|
||||
import { editor, Uri } from 'monaco-editor';
|
||||
import { setDiagnosticsOptions } from 'monaco-yaml';
|
||||
|
||||
|
|
|
|||
1
build.js
1
build.js
|
|
@ -66,6 +66,7 @@ fs.rm(join(__dirname, 'lib'), { force: true, recursive: true })
|
|||
}),
|
||||
)
|
||||
.catch((error) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ fetch('https://www.schemastore.org/api/json/catalog.json').then(async (response)
|
|||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
const catalog: JSONSchemaForSchemaStoreOrgCatalogFiles = await response.json();
|
||||
const catalog = (await response.json()) as JSONSchemaForSchemaStoreOrgCatalogFiles;
|
||||
const schemas = [defaultSchema];
|
||||
catalog.schemas.sort((a, b) => a.name.localeCompare(b.name));
|
||||
for (const { fileMatch, name, url } of catalog.schemas) {
|
||||
|
|
|
|||
28
examples/demo/src/types.d.ts
vendored
28
examples/demo/src/types.d.ts
vendored
|
|
@ -1,4 +1,30 @@
|
|||
declare module 'monaco-editor/esm/vs/base/common/cancellation' {
|
||||
export enum CancellationToken {
|
||||
None,
|
||||
}
|
||||
}
|
||||
|
||||
declare module 'monaco-editor/esm/vs/editor/contrib/documentSymbols/documentSymbols' {
|
||||
import { ITextModel, languages } from 'monaco-editor';
|
||||
// eslint-disable-next-line import/order
|
||||
import { CancellationToken } from 'monaco-editor/esm/vs/base/common/cancellation';
|
||||
|
||||
export function getDocumentSymbols(
|
||||
model: ITextModel,
|
||||
flat: boolean,
|
||||
token: CancellationToken,
|
||||
): Promise<languages.DocumentSymbol[]>;
|
||||
}
|
||||
|
||||
declare module 'monaco-editor/esm/vs/editor/editor.worker' {
|
||||
import { worker } from 'monaco-editor/esm/vs/editor/editor.api';
|
||||
|
||||
export function initialize(
|
||||
fn: (ctx: worker.IWorkerContext, createData: unknown) => unknown,
|
||||
): void;
|
||||
}
|
||||
|
||||
declare module '*.json' {
|
||||
declare const uri;
|
||||
declare const uri: string;
|
||||
export default uri;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ export type LoadFunc = (file?: string) => LocalizeFunc;
|
|||
function format(message: string, args: string[]): string {
|
||||
return args.length === 0
|
||||
? message
|
||||
: message.replace(/{(\d+)}/g, (match, rest) => {
|
||||
: message.replace(/{(\d+)}/g, (match, rest: number[]) => {
|
||||
const [index] = rest;
|
||||
return typeof args[index] === 'undefined' ? match : args[index];
|
||||
});
|
||||
|
|
|
|||
|
|
@ -32,9 +32,7 @@ function toSeverity(lsSeverity: ls.DiagnosticSeverity): MarkerSeverity {
|
|||
}
|
||||
}
|
||||
|
||||
function toDiagnostics(resource: Uri, diag: ls.Diagnostic): editor.IMarkerData {
|
||||
const code = typeof diag.code === 'number' ? String(diag.code) : (diag.code as string);
|
||||
|
||||
function toDiagnostics(diag: ls.Diagnostic): editor.IMarkerData {
|
||||
return {
|
||||
severity: toSeverity(diag.severity),
|
||||
startLineNumber: diag.range.start.line + 1,
|
||||
|
|
@ -42,7 +40,7 @@ function toDiagnostics(resource: Uri, diag: ls.Diagnostic): editor.IMarkerData {
|
|||
endLineNumber: diag.range.end.line + 1,
|
||||
endColumn: diag.range.end.character + 1,
|
||||
message: diag.message,
|
||||
code,
|
||||
code: String(diag.code),
|
||||
source: diag.source,
|
||||
};
|
||||
}
|
||||
|
|
@ -52,7 +50,7 @@ export function createDiagnosticsAdapter(
|
|||
getWorker: WorkerAccessor,
|
||||
defaults: languages.yaml.LanguageServiceDefaults,
|
||||
): void {
|
||||
const listeners: Record<string, IDisposable> = Object.create(null);
|
||||
const listeners = new Map<string, IDisposable>();
|
||||
|
||||
const resetSchema = async (resource: Uri): Promise<void> => {
|
||||
const worker = await getWorker();
|
||||
|
|
@ -62,7 +60,7 @@ export function createDiagnosticsAdapter(
|
|||
const doValidate = async (resource: Uri, languageId: string): Promise<void> => {
|
||||
const worker = await getWorker(resource);
|
||||
const diagnostics = await worker.doValidation(String(resource));
|
||||
const markers = diagnostics.map((d) => toDiagnostics(resource, d));
|
||||
const markers = diagnostics.map(toDiagnostics);
|
||||
const model = editor.getModel(resource);
|
||||
if (model.getModeId() === languageId) {
|
||||
editor.setModelMarkers(model, languageId, markers);
|
||||
|
|
@ -76,10 +74,13 @@ export function createDiagnosticsAdapter(
|
|||
}
|
||||
|
||||
let handle: ReturnType<typeof setTimeout>;
|
||||
listeners[String(toString)] = model.onDidChangeContent(() => {
|
||||
clearTimeout(handle);
|
||||
handle = setTimeout(() => doValidate(model.uri, modeId), 500);
|
||||
});
|
||||
listeners.set(
|
||||
String(model.uri),
|
||||
model.onDidChangeContent(() => {
|
||||
clearTimeout(handle);
|
||||
handle = setTimeout(() => doValidate(model.uri, modeId), 500);
|
||||
}),
|
||||
);
|
||||
|
||||
doValidate(model.uri, modeId);
|
||||
};
|
||||
|
|
@ -87,10 +88,10 @@ export function createDiagnosticsAdapter(
|
|||
const onModelRemoved = (model: editor.IModel): void => {
|
||||
editor.setModelMarkers(model, languageId, []);
|
||||
const uriStr = String(model.uri);
|
||||
const listener = listeners[uriStr];
|
||||
const listener = listeners.get(uriStr);
|
||||
if (listener) {
|
||||
listener.dispose();
|
||||
delete listeners[uriStr];
|
||||
listeners.delete(uriStr);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -361,7 +362,7 @@ function toDocumentSymbol(item: ls.DocumentSymbol): languages.DocumentSymbol {
|
|||
name: item.name,
|
||||
kind: toSymbolKind(item.kind),
|
||||
selectionRange: toRange(item.selectionRange),
|
||||
children: item.children.map((child) => toDocumentSymbol(child)),
|
||||
children: item.children.map(toDocumentSymbol),
|
||||
tags: [],
|
||||
};
|
||||
}
|
||||
|
|
@ -378,7 +379,7 @@ export function createDocumentSymbolProvider(
|
|||
if (!items) {
|
||||
return;
|
||||
}
|
||||
return items.map((item) => toDocumentSymbol(item));
|
||||
return items.map(toDocumentSymbol);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { initialize } from 'monaco-editor/esm/vs/editor/editor.worker';
|
||||
|
||||
import { createYAMLWorker } from './yamlWorker';
|
||||
import { createYAMLWorker, ICreateData } from './yamlWorker';
|
||||
|
||||
self.onmessage = () => {
|
||||
initialize((ctx, createData) => Object.create(createYAMLWorker(ctx, createData)));
|
||||
initialize((ctx, createData: ICreateData) => Object.create(createYAMLWorker(ctx, createData)));
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue