Skip to content

Commit

Permalink
use latest parser
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Oct 5, 2022
1 parent 9ed1b2c commit ccaa62e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 301 deletions.
229 changes: 45 additions & 184 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@asyncapi/avro-schema-parser": "^1.1.0",
"@asyncapi/converter": "^1.0.0",
"@asyncapi/openapi-schema-parser": "^2.0.1",
"@asyncapi/parser": "file:../parser-js/asyncapi-parser-2.0.0.tgz",
"@asyncapi/parser": "^2.0.0-next-major.2",
"@asyncapi/react-component": "^1.0.0-next.42",
"@asyncapi/specs": "^3.1.0",
"@headlessui/react": "1.4.1",
Expand Down
5 changes: 3 additions & 2 deletions src/components/Terminal/ProblemsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const ProblemsTabContent: React.FunctionComponent<ProblemsTabProps> = ()
<SeverityButtons diagnostics={diagnostics} active={active} setActive={setActive} />
<div className='mx-3 flex-1 flex flex-row items-center justify-center rounded-md border border-transparent shadow-xs px-2 py-1 bg-gray-700 text-xs font-medium'>
<VscSearch />
<input ref={inputRef} className='w-full ml-2 bg-gray-700 border-transparent focus:border-transparent focus:ring-0 focus:outline-none' onChange={debounce((e) => setSearch(e.target.value), 250)} />
<input ref={inputRef} placeholder='Filter diagnostics...' className='w-full ml-2 bg-gray-700 border-transparent focus:border-transparent focus:ring-0 focus:outline-none' onChange={debounce((e) => setSearch(e.target.value), 250)} />
<button type='button' className={`hover:bg-gray-900 rounded-sm border border-transparent ${search ? 'opacity-100' : 'opacity-0'}`} onClick={() => {
if (inputRef.current) {
inputRef.current.value = '';
Expand Down Expand Up @@ -207,7 +207,8 @@ export const ProblemsTabContent: React.FunctionComponent<ProblemsTabProps> = ()
if (active !== 'all' && severity !== active) {
return null;
}
if (search && !message.includes(search)) {
const lowerCasingSearch = search.toLowerCase();
if (lowerCasingSearch && !message.toLowerCase().includes(lowerCasingSearch)) {
return null;
}

Expand Down
5 changes: 3 additions & 2 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/// <reference types="react-scripts" />

import type * as monacoAPI from 'monaco-editor/esm/vs/editor/editor.api';
import type { OldAsyncAPIDocument as AsyncAPIDocument } from '@asyncapi/parser/esm';
import type { OldAsyncAPIDocument as AsyncAPIDocument, ParseOutput } from '@asyncapi/parser/esm';

declare global {
interface Window {
// needed by monaco YAML plugin and Studio
monaco: typeof monacoAPI;
Editor: monacoAPI.editor.IStandaloneCodeEditor;
MonacoEnvironment: monacoAPI.Environment | undefined;
ParsedSpec: AsyncAPIDocument;
ParsedSpec?: AsyncAPIDocument;
ParsedExtras?: ParseOutput['extras'];
}
}
17 changes: 1 addition & 16 deletions src/services/editor.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,7 @@ export class EditorService {
const newMarkers: monacoAPI.editor.IMarkerData[] = [];

diagnostics.forEach(diagnostic => {
const { message, path, severity } = diagnostic;
let { range } = diagnostic;

if (path.length === 0) {
const fullRange = model.getFullModelRange();
range = {
start: {
line: fullRange.startLineNumber,
character: fullRange.startColumn
},
end: {
line: fullRange.endLineNumber,
character: fullRange.endColumn
},
};
}
const { message, range, severity } = diagnostic;

newMarkers.push({
startLineNumber: range.start.line + 1,
Expand Down
10 changes: 4 additions & 6 deletions src/services/navigation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ export class NavigationService {
language = 'yaml',
) {
try {
// const location: LocationOf = getLocationOf(jsonPointer, spec, language);
// if (!location || typeof location.startLine !== 'number') {
// return;
// }
const range = SpecificationService.getRangeForJsonPath(jsonPointer);
if (range) {
this.scrollToEditorLine(range.start.line + 1);
}

language;
this.scrollToHash(hash);
// this.scrollToEditorLine(location.startLine);
this.emitHashChangeEvent(hash);
} catch (e) {
console.error(e);
Expand Down
111 changes: 21 additions & 90 deletions src/services/specification.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-ignore
import { convert, ConvertVersion } from '@asyncapi/converter';
import { Parser, convertToOldAPI } from '@asyncapi/parser/esm';
import { hasErrorDiagnostic } from '@asyncapi/parser/esm/utils';
import { untilde } from '@asyncapi/parser/esm/utils';
import { OpenAPISchemaParser } from '@asyncapi/parser/esm/schema-parser/openapi-schema-parser';
import { AvroSchemaParser } from '@asyncapi/parser/esm/schema-parser/avro-schema-parser';
// @ts-ignore
Expand Down Expand Up @@ -30,19 +30,23 @@ export class SpecificationService {
return window.ParsedSpec || null;
}

static getParsedExtras() {
return window.ParsedExtras || null;
}

static async parseSpec(rawSpec: string): Promise<AsyncAPIDocument | void> {
const parserState = state.parser;
const { document, diagnostics } = await parser.parse(rawSpec);
const hasErrorDiagnostics = hasErrorDiagnostic(diagnostics);
const { document, diagnostics, extras } = await parser.parse(rawSpec);

if (document) {
const oldDocument = convertToOldAPI(document);
window.ParsedSpec = oldDocument;
window.ParsedExtras = extras;
parserState.set({
parsedSpec: oldDocument,
valid: true,
diagnostics,
hasErrorDiagnostics,
hasErrorDiagnostics: false,
});

const version = oldDocument.version();
Expand All @@ -59,6 +63,8 @@ export class SpecificationService {
return oldDocument;
}

window.ParsedSpec = undefined;
window.ParsedExtras = undefined;
try {
const asyncapiSpec = YAML.load(rawSpec) as { asyncapi: string };
MonacoService.updateLanguageConfig(asyncapiSpec.asyncapi);
Expand All @@ -70,7 +76,7 @@ export class SpecificationService {
parsedSpec: null,
valid: false,
diagnostics,
hasErrorDiagnostics,
hasErrorDiagnostics: true,
});
EditorService.applyMarkers(diagnostics);
}
Expand Down Expand Up @@ -122,91 +128,16 @@ export class SpecificationService {
return false;
}

static errorHasLocation(err: any) {
return (
this.isValidationError(err) ||
this.isJsonError(err) ||
this.isYamlError(err) ||
this.isDereferenceError(err) ||
this.isUnsupportedVersionError(err)
);
}

private static notSupportedVersions = /('|"|)asyncapi('|"|): ('|"|)(1.0.0|1.1.0|1.2.0|2.0.0-rc1|2.0.0-rc2)('|"|)/;

private static filterErrors(err: any, rawSpec: string) {
const errors = [];
if (this.isUnsupportedVersionError(err)) {
errors.push({
type: err.type,
title: err.message,
location: err.validationErrors,
});
this.isNotSupportedVersion(rawSpec) &&
state.spec.set({
shouldOpenConvertModal: true,
convertOnlyToLatest: false,
forceConvert: true,
});
}
if (this.isValidationError(err)) {
errors.push(...err.validationErrors);
}
if (this.isYamlError(err) || this.isJsonError(err)) {
errors.push(err);
}
if (this.isDereferenceError(err)) {
errors.push(
...err.refs.map((ref: any) => ({
type: err.type,
title: err.title,
location: { ...ref },
})),
);
}
if (errors.length === 0) {
errors.push(err);
}
return errors;
}

private static isValidationError(err: any) {
return (
err &&
err.type === 'https://github.com/asyncapi/parser-js/validation-errors'
);
}

private static isJsonError(err: any) {
return (
err && err.type === 'https://github.com/asyncapi/parser-js/invalid-json'
);
}

private static isYamlError(err: any) {
return (
err && err.type === 'https://github.com/asyncapi/parser-js/invalid-yaml'
);
}

private static isUnsupportedVersionError(err: any) {
return (
err &&
err.type === 'https://github.com/asyncapi/parser-js/unsupported-version'
);
}

private static isDereferenceError(err: any) {
return (
err &&
err.type === 'https://github.com/asyncapi/parser-js/dereference-error'
);
}

static isNotSupportedVersion(rawSpec: string): boolean {
if (this.notSupportedVersions.test(rawSpec.trim())) {
return true;
static getRangeForJsonPath(jsonPath: string | Array<string | number>) {
try {
const extras = this.getParsedExtras();
if (extras) {
jsonPath = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('/').map(untilde);
if (jsonPath[0] === '') jsonPath.shift();
return extras.document.getRangeForJsonPath(jsonPath, true);
}
} catch(err: any) {
return;
}
return false;
}
}

0 comments on commit ccaa62e

Please sign in to comment.