From 9739d143d3f834bb3100320767ee9996ecce5b8b Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 2 Sep 2021 22:11:00 +0200 Subject: [PATCH] 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. --- .eslintrc.yaml | 16 +++++----------- README.md | 2 +- build.js | 1 + examples/demo/src/index.ts | 2 +- examples/demo/src/types.d.ts | 28 +++++++++++++++++++++++++++- src/fillers/vscode-nls.ts | 2 +- src/languageFeatures.ts | 29 +++++++++++++++-------------- src/yaml.worker.ts | 4 ++-- 8 files changed, 53 insertions(+), 31 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 8393208..2fd574c 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -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 diff --git a/README.md b/README.md index 69a3067..6fb1898 100644 --- a/README.md +++ b/README.md @@ -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'; diff --git a/build.js b/build.js index 27ab1f1..96daa4f 100755 --- a/build.js +++ b/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); }); diff --git a/examples/demo/src/index.ts b/examples/demo/src/index.ts index 3f3ab27..7cd68f4 100644 --- a/examples/demo/src/index.ts +++ b/examples/demo/src/index.ts @@ -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) { diff --git a/examples/demo/src/types.d.ts b/examples/demo/src/types.d.ts index ade23fa..3d4ae5d 100644 --- a/examples/demo/src/types.d.ts +++ b/examples/demo/src/types.d.ts @@ -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; +} + +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; } diff --git a/src/fillers/vscode-nls.ts b/src/fillers/vscode-nls.ts index 677a97e..b54aa67 100644 --- a/src/fillers/vscode-nls.ts +++ b/src/fillers/vscode-nls.ts @@ -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]; }); diff --git a/src/languageFeatures.ts b/src/languageFeatures.ts index b9d7f3e..4e19cf7 100644 --- a/src/languageFeatures.ts +++ b/src/languageFeatures.ts @@ -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 = Object.create(null); + const listeners = new Map(); const resetSchema = async (resource: Uri): Promise => { const worker = await getWorker(); @@ -62,7 +60,7 @@ export function createDiagnosticsAdapter( const doValidate = async (resource: Uri, languageId: string): Promise => { 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; - 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); }, }; } diff --git a/src/yaml.worker.ts b/src/yaml.worker.ts index 2c17ae4..4425adb 100644 --- a/src/yaml.worker.ts +++ b/src/yaml.worker.ts @@ -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))); };