Skip to content
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

fix: avoid type conversion #578

Open
wants to merge 17 commits into
base: master
Choose a base branch
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
8 changes: 8 additions & 0 deletions .changeset/modern-hats-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ui5-language-assistant/xml-views-validation": patch
"@ui5-language-assistant/fe": patch
"vscode-ui5-language-assistant": patch
"@ui5-language-assistant/vscode-ui5-language-assistant-bas-ext": patch
---

Avoid type casting
56 changes: 32 additions & 24 deletions packages/fe/test/services/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { AnnotationIssue, getCompletionItems } from "../../src/api";
import { CompletionItem } from "vscode-languageserver-types";
import { TestFramework } from "@ui5-language-assistant/test-framework";
import { getContext } from "@ui5-language-assistant/context";
import { getContext, isContext } from "@ui5-language-assistant/context";
import type { Context } from "@ui5-language-assistant/context";
import { validateXMLView } from "@ui5-language-assistant/xml-views-validation";
import {
UI5XMLViewIssue,
validateXMLView,
} from "@ui5-language-assistant/xml-views-validation";

import { CURSOR_ANCHOR } from "@ui5-language-assistant/test-framework";
import { XMLAttribute } from "@xml-tools/ast";
Expand All @@ -16,7 +19,9 @@ export const completionItemToSnapshot = (item: CompletionItem): string =>
item.sortText ? item.sortText[0] : ""
}`;

export const issueToSnapshot = (item: AnnotationIssue): string =>
export const issueToSnapshot = (
item: AnnotationIssue | UI5XMLViewIssue
): string =>
`kind: ${item.kind}; text: ${item.message}; severity:${item.severity}; offset:${item.offsetRange.start}-${item.offsetRange.end}`;

export type ViewCompletionProviderType = (
Expand Down Expand Up @@ -53,17 +58,18 @@ export const getViewCompletionProvider = (
content,
offset
);
const context = (await getContext(documentPath)) as Context;

result = getCompletionItems({
ast,
context: contextAdapter ? contextAdapter(context) : context,
cst,
document,
documentSettings: settings,
textDocumentPosition,
tokenVector,
});
const context = await getContext(documentPath);
if (isContext(context)) {
result = getCompletionItems({
ast,
context: contextAdapter ? contextAdapter(context) : context,
cst,
document,
documentSettings: settings,
textDocumentPosition,
tokenVector,
});
}
} finally {
// reversal update
await framework.updateFileContent(viewFilePathSegments, "", {
Expand Down Expand Up @@ -103,16 +109,18 @@ export const getViewValidator = (
insertAfter: "<content>",
});
const { ast } = await framework.readFile(viewFilePathSegments);
const context = (await getContext(documentPath)) as Context;
result = validateXMLView({
validators: {
attribute: [validator],
document: [],
element: [],
},
context: contextAdapter ? contextAdapter(context) : context,
xmlView: ast,
}) as AnnotationIssue[];
const context = await getContext(documentPath);
if (isContext(context)) {
result = validateXMLView({
validators: {
attribute: [validator],
document: [],
element: [],
},
context: contextAdapter ? contextAdapter(context) : context,
xmlView: ast,
});
}
} finally {
// reversal update
await framework.updateFileContent(viewFilePathSegments, "", {
Expand Down
6 changes: 3 additions & 3 deletions packages/xml-views-validation/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { OffsetRange } from "@ui5-language-assistant/logic-utils";
import { UI5ValidatorsConfig } from "./src/validate-xml-views";
import { Context } from "@ui5-language-assistant/context";

export function validateXMLView<ExternalXMLViewIssue>(opts: {
validators: UI5ValidatorsConfig<UI5XMLViewIssue | ExternalXMLViewIssue>;
export function validateXMLView<T = UI5XMLViewIssue>(opts: {
validators: UI5ValidatorsConfig<T>;
context: Context;
xmlView: XMLDocument;
}): (UI5XMLViewIssue | ExternalXMLViewIssue)[];
}): T[];

export declare const defaultValidators: UI5ValidatorsConfig;

Expand Down
6 changes: 3 additions & 3 deletions packages/xml-views-validation/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const validators: Validators = {
validateNonStableId,
};

export function validateXMLView<ExternalXMLViewIssue>(opts: {
validators: UI5ValidatorsConfig<UI5XMLViewIssue | ExternalXMLViewIssue>;
export function validateXMLView<T = UI5XMLViewIssue>(opts: {
validators: UI5ValidatorsConfig<T>;
context: Context;
xmlView: XMLDocument;
}): (UI5XMLViewIssue | ExternalXMLViewIssue)[] {
}): T[] {
const validatorVisitor = new ValidatorVisitor(opts.context, opts.validators);
accept(opts.xmlView, validatorVisitor);
const issues = validatorVisitor.collectedIssues;
Expand Down