From 8fff87d18fb051ab2dc62ca9a52d01419de28ecb Mon Sep 17 00:00:00 2001 From: mr25mr <100434800+mr25mr@users.noreply.github.com> Date: Wed, 2 Aug 2023 13:47:04 +0200 Subject: [PATCH] fix: relax diagnostic for typedefs (#644) * fix: relax diagnostic for typedefs * fix: add change set --- .changeset/beige-eagles-warn.md | 8 +++ packages/logic-utils/api.d.ts | 1 + packages/logic-utils/src/api.ts | 1 + .../src/utils/xml-node-to-ui5-node.ts | 17 +++++- packages/xml-views-validation/package.json | 3 +- .../validators/elements/unknown-tag-name.ts | 4 +- .../element/unknown-tag-name.test.ts | 56 +++++++++++++++---- 7 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 .changeset/beige-eagles-warn.md diff --git a/.changeset/beige-eagles-warn.md b/.changeset/beige-eagles-warn.md new file mode 100644 index 000000000..09680a878 --- /dev/null +++ b/.changeset/beige-eagles-warn.md @@ -0,0 +1,8 @@ +--- +"@ui5-language-assistant/vscode-ui5-language-assistant-bas-ext": patch +"vscode-ui5-language-assistant": patch +"@ui5-language-assistant/xml-views-validation": patch +"@ui5-language-assistant/logic-utils": patch +--- + +releax typedefs validation diff --git a/packages/logic-utils/api.d.ts b/packages/logic-utils/api.d.ts index 698804e42..9db0b28b1 100644 --- a/packages/logic-utils/api.d.ts +++ b/packages/logic-utils/api.d.ts @@ -274,6 +274,7 @@ export { tryFetch, getLocalUrl, isXMLView, + getUI5KindByXMLElement, } from "./src/api"; export { FetchResponse } from "./src/utils/types"; diff --git a/packages/logic-utils/src/api.ts b/packages/logic-utils/src/api.ts index ce1673a86..c83b43c77 100644 --- a/packages/logic-utils/src/api.ts +++ b/packages/logic-utils/src/api.ts @@ -16,6 +16,7 @@ export { isRootSymbol, getRootSymbolParent } from "./utils/root-symbols"; export { typeToString } from "./utils/type-to-string"; export { getUI5ClassByXMLElement, + getUI5KindByXMLElement, getUI5ClassByXMLElementClosingTag, getUI5AggregationByXMLElement, getUI5PropertyByXMLAttributeKey, diff --git a/packages/logic-utils/src/utils/xml-node-to-ui5-node.ts b/packages/logic-utils/src/utils/xml-node-to-ui5-node.ts index 41a98173e..03aaf41ad 100644 --- a/packages/logic-utils/src/utils/xml-node-to-ui5-node.ts +++ b/packages/logic-utils/src/utils/xml-node-to-ui5-node.ts @@ -18,16 +18,29 @@ import { BaseUI5Node, UI5Event, UI5Association, + UI5Enum, + UI5Typedef, + UI5Function, } from "@ui5-language-assistant/semantic-model-types"; import { find } from "lodash"; import { findSymbol } from "@ui5-language-assistant/semantic-model"; +export function getUI5KindByXMLElement< + T = UI5Class | UI5Enum | UI5Typedef | UI5Function | UI5Enum | undefined +>( + element: XMLElement, + model: UI5SemanticModel, + kind: "classes" | "enums" | "typedefs" | "functions" | "enums" +): T { + const fqn = xmlToFQN(element); + return model[kind][fqn] as T; +} + export function getUI5ClassByXMLElement( element: XMLElement, model: UI5SemanticModel ): UI5Class | undefined { - const elementTagFqn = xmlToFQN(element); - const ui5Class = model.classes[elementTagFqn]; + const ui5Class = getUI5KindByXMLElement(element, model, "classes"); // The class name might not be the same as the element name in case the element name contained a dot // (example: using core:mvc.View instead of mvc:View), which is not allowed. if (ui5Class === undefined || ui5Class.name !== element.name) { diff --git a/packages/xml-views-validation/package.json b/packages/xml-views-validation/package.json index 8d0af445f..a74e7c46e 100644 --- a/packages/xml-views-validation/package.json +++ b/packages/xml-views-validation/package.json @@ -32,7 +32,8 @@ "devDependencies": { "@ui5-language-assistant/semantic-model": "4.0.11", "@ui5-language-assistant/test-utils": "4.0.9", - "@xml-tools/parser": "1.0.7" + "@xml-tools/parser": "1.0.7", + "@ui5-language-assistant/test-framework": "4.0.11" }, "scripts": { "ci": "npm-run-all clean compile lint coverage", diff --git a/packages/xml-views-validation/src/validators/elements/unknown-tag-name.ts b/packages/xml-views-validation/src/validators/elements/unknown-tag-name.ts index a348b277b..1591ac482 100644 --- a/packages/xml-views-validation/src/validators/elements/unknown-tag-name.ts +++ b/packages/xml-views-validation/src/validators/elements/unknown-tag-name.ts @@ -12,6 +12,7 @@ import { getUI5AggregationByXMLElement, isSameXMLNS, resolveXMLNS, + getUI5KindByXMLElement, } from "@ui5-language-assistant/logic-utils"; import { validations, @@ -91,10 +92,11 @@ function validateTagWithNamespace( return []; } - // Check if it's a known class or aggregaion, or an element that should be ignored + // Check if it's a known class, typedefs or aggregation, or an element that should be ignored if ( shouldIgnoreElement(xmlElement) || getUI5ClassByXMLElement(xmlElement, model) !== undefined || + getUI5KindByXMLElement(xmlElement, model, "typedefs") !== undefined || getUI5AggregationByXMLElement(xmlElement, model) !== undefined ) { return []; diff --git a/packages/xml-views-validation/test/unit/validators/element/unknown-tag-name.test.ts b/packages/xml-views-validation/test/unit/validators/element/unknown-tag-name.test.ts index 7ff085524..d1bded07d 100644 --- a/packages/xml-views-validation/test/unit/validators/element/unknown-tag-name.test.ts +++ b/packages/xml-views-validation/test/unit/validators/element/unknown-tag-name.test.ts @@ -1,7 +1,5 @@ +import { join } from "path"; import { partial } from "lodash"; -import { UI5SemanticModel } from "@ui5-language-assistant/semantic-model-types"; -import { generateModel } from "@ui5-language-assistant/test-utils"; -import { generate } from "@ui5-language-assistant/semantic-model"; import { validations, buildMessage, @@ -12,9 +10,17 @@ import { assertSingleIssue as assertSingleIssueBase, testValidationsScenario, computeExpectedRanges, - getDefaultContext, } from "../../test-utils"; -import { Context as AppContext } from "@ui5-language-assistant/context"; +import { + Context as AppContext, + getContext, +} from "@ui5-language-assistant/context"; +import { + Config, + ProjectName, + ProjectType, + TestFramework, +} from "@ui5-language-assistant/test-framework"; const { UNKNOWN_CLASS_IN_NS, @@ -28,15 +34,29 @@ const { } = validations; describe("the unknown tag name validation", () => { - let ui5SemanticModel: UI5SemanticModel; let appContext: AppContext; + let framework: TestFramework; + const viewFilePathSegments = [ + "app", + "manage_travels", + "webapp", + "ext", + "main", + "Main.view.xml", + ]; beforeAll(async () => { - ui5SemanticModel = await generateModel({ - framework: "SAPUI5", - version: "1.71.49", - modelGenerator: generate, - }); - appContext = getDefaultContext(ui5SemanticModel); + const config: Config = { + projectInfo: { + name: ProjectName.cap, + type: ProjectType.CAP, + npmInstall: false, + deleteBeforeCopy: false, + }, + }; + framework = new TestFramework(config); + appContext = (await getContext( + join(framework.getProjectRoot(), ...viewFilePathSegments) + )) as AppContext; }); describe("true positive scenarios", () => { @@ -563,6 +583,18 @@ describe("the unknown tag name validation", () => { ); }); + it("will not detect an issue for typedefs as element [macrosTable:Action]", async () => { + assertNoIssues( + ` + + ` + ); + }); it("will detect an issue for sap.ui.core.ExtensionPoint in the root tag", () => { assertSingleIssue( `<šŸ¢‚ExtensionPointšŸ¢€ name="extension1">`,