Skip to content

Commit

Permalink
feat: Detect deprecated 'type' in Fragment.load / <core:Fragment>
Browse files Browse the repository at this point in the history
This adds the detection of the partial deprecation of the Fragment.load
factory.

In addition, nested fragments within XML are now also detected when
using a deprecated 'type'.

JIRA: CPOUI5FOUNDATION-907
  • Loading branch information
matz3 committed Jan 14, 2025
1 parent 099c1f9 commit 16e3f94
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 60 deletions.
10 changes: 10 additions & 0 deletions src/linter/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export enum MESSAGE {
PARTIALLY_DEPRECATED_ODATA_MODEL_V2_CREATE_ENTRY_PROPERTIES_ARRAY,
PARTIALLY_DEPRECATED_PARAMETERS_GET,
PARTIALLY_DEPRECATED_VIEW_CREATE,
PARTIALLY_DEPRECATED_FRAGMENT_LOAD,
PREFER_TEST_STARTER,
REDUNDANT_BOOTSTRAP_PARAM,
REDUNDANT_BOOTSTRAP_PARAM_ERROR,
Expand Down Expand Up @@ -494,6 +495,15 @@ export const MESSAGE_INFO = {
details: () => `{@link sap.ui.core.mvc.View#sap.ui.core.mvc.View.create View.create}`,
},

[MESSAGE.PARTIALLY_DEPRECATED_FRAGMENT_LOAD]: {
severity: LintMessageSeverity.Error,
ruleId: RULES["no-deprecated-api"],

message: ({typeValue}: {typeValue: string}) =>
`Usage of deprecated value '${typeValue}' for parameter 'type' in 'sap/ui/core/Fragment.load'`,
details: () => `{@link sap.ui.core.Fragment#sap.ui.core.Fragment.load Fragment.load}`,
},

[MESSAGE.REDUNDANT_BOOTSTRAP_PARAM]: {
severity: LintMessageSeverity.Warning,
ruleId: RULES["no-deprecated-api"],
Expand Down
38 changes: 38 additions & 0 deletions src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ export default class SourceFileLinter {
this.#analyzeThemingSetTheme(node);
} else if (symbolName === "create" && moduleName === "sap/ui/core/mvc/View") {
this.#analyzeViewCreate(node);
} else if (symbolName === "load" && moduleName === "sap/ui/core/Fragment") {
this.#analyzeFragmentLoad(node);
} else if (/\.qunit\.(js|ts)$/.test(this.sourceFile.fileName) &&
symbolName === "ready" && moduleName === "sap/ui/core/Core") {
this.#reportTestStarter(node);
Expand Down Expand Up @@ -1164,6 +1166,42 @@ export default class SourceFileLinter {
}
}

#analyzeFragmentLoad(node: ts.CallExpression) {
if (!node.arguments?.length) {
return;
}

const optionsArg = node.arguments[0];

if (!ts.isObjectLiteralExpression(optionsArg)) {
return;
}

// Find "type" property
let typeProperty;
for (const property of optionsArg.properties) {
if (!ts.isPropertyAssignment(property)) {
continue;
}
if (
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) &&
property.name.text === "type"
) {
typeProperty = property;
break;
}
}

if (typeProperty && ts.isStringLiteralLike(typeProperty.initializer)) {
const typeValue = typeProperty.initializer.text;
if (typeValue === "HTML") {
this.#reporter.addMessage(MESSAGE.PARTIALLY_DEPRECATED_FRAGMENT_LOAD, {
typeValue,
}, node);
}
}
}

getDeprecationInfoForAccess(node: ts.AccessExpression): DeprecationInfo | null {
let symbol;
if (ts.isPropertyAccessExpression(node)) {
Expand Down
39 changes: 27 additions & 12 deletions src/linter/xmlTemplate/generator/AbstractGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,7 @@ export default abstract class AbstractGenerator {
controlDeclaration.variableName = this._getUniqueVariableName(`o${controlDeclaration.name}`);

// Create the control
this._body.write(`const ${controlDeclaration.variableName} = `);

// Special case: Use View.create for nested views
if (!rootControl && moduleName === "sap/ui/core/mvc/View") {
this._body.writeln(
`await ${importVariableName}.create({`, controlDeclaration.start, controlDeclaration.end
);
} else {
this._body.writeln(
`new ${importVariableName}({`, controlDeclaration.start, controlDeclaration.end
);
}
this._writeControlFactoryCall(moduleName, importVariableName, controlDeclaration, rootControl);

// Write properties
controlDeclaration.properties.forEach((attribute) => {
Expand Down Expand Up @@ -139,4 +128,30 @@ export default abstract class AbstractGenerator {
this._variableNames.add(variableNameCandidate);
return variableNameCandidate;
}

_writeControlFactoryCall(
moduleName: string, importVariableName: string, controlDeclaration: ControlDeclaration, rootControl: boolean
) {
this._body.write(`const ${controlDeclaration.variableName} = `);
if (!rootControl) {
// Special case: Use View.create for nested views
if (moduleName === "sap/ui/core/mvc/View") {
this._body.writeln(
`await ${importVariableName}.create({`, controlDeclaration.start, controlDeclaration.end
);
return;
}
// Special case: Use Fragment.load for nested fragments
if (moduleName === "sap/ui/core/Fragment") {
this._body.writeln(
`await ${importVariableName}.load({`, controlDeclaration.start, controlDeclaration.end
);
return;
}
}
// Default case: Use new for controls
this._body.writeln(
`new ${importVariableName}({`, controlDeclaration.start, controlDeclaration.end
);
}
}
26 changes: 24 additions & 2 deletions test/fixtures/linter/rules/NoDeprecatedApi/PartialDeprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ sap.ui.define([
"sap/ui/core/routing/Router",
"sap/ui/util/Mobile",
"sap/ui/core/mvc/View",
"sap/ui/core/mvc/ViewType"
], function(Parameters, JSONModel, ODataModelV4, ODataModelV2, Component, Router, Mobile, View, ViewType) {
"sap/ui/core/mvc/ViewType",
"sap/ui/core/Fragment",
], function(Parameters, JSONModel, ODataModelV4, ODataModelV2, Component, Router, Mobile, View, ViewType, Fragment) {

Parameters.get(); // (deprecated since 1.92) If no parameter is given
Parameters.get("sapUiParam1"); // (deprecated since 1.94) If a string is given as first parameter
Expand Down Expand Up @@ -127,4 +128,25 @@ sap.ui.define([
viewName: "myapp.view.Home"
});


Fragment.load({
type: "HTML", // Deprecated type HTML
name: "myapp.fragment.Details"
});

// Negative test: Default type is XML
Fragment.load({
name: "myapp.fragment.Details"
});
// Negative test: XML
Fragment.load({
type: "XML",
name: "myapp.fragment.Details"
});
// Negative test: JS
Fragment.load({
type: "JS",
name: "myapp.fragment.Details"
});

});
9 changes: 9 additions & 0 deletions test/fixtures/linter/rules/NoDeprecatedApi/XMLView.view.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<mvc:View xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:core="sap.ui.core"
xmlns:table="sap.ui.table"
xmlns:tablePlugins="sap.ui.table.plugins"
>
Expand Down Expand Up @@ -32,4 +33,12 @@
<mvc:View type="XML" viewName="myapp.view.Home"/>
<mvc:XMLView viewName="myapp.view.Home"/>


<core:Fragment type="HTML" fragmentName="myapp.fragment.Details" />

<!-- Negative test: XML/JS fragment -->
<core:Fragment fragmentName="myapp.fragment.Details" /> <!-- type defaults to XML -->
<core:Fragment type="XML" fragmentName="myapp.fragment.Details" />
<core:Fragment type="JS" fragmentName="myapp.fragment.Details" />

</mvc:View>
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
<mvc:HTMLView viewName="com.myapp.view.Home"/>
<mvc:TemplateView viewName="com.myapp.view.Home"/>

<core:Fragment fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="XML" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="JS" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="HTML" fragmentName="com.myapp.fragment.Details" />

</core:FragmentDefinition>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<VBox xmlns="sap.m"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
>

Expand All @@ -14,4 +15,9 @@
<mvc:HTMLView viewName="com.myapp.view.Home"/>
<mvc:TemplateView viewName="com.myapp.view.Home"/>

<core:Fragment fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="XML" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="JS" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="HTML" fragmentName="com.myapp.fragment.Details" />

</VBox>
6 changes: 6 additions & 0 deletions test/fixtures/transpiler/xml/XMLViewNestedViews.view.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<mvc:View xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:core="sap.ui.core"
controllerName="com.myapp.controller.Main"
>

Expand All @@ -15,4 +16,9 @@
<mvc:HTMLView viewName="com.myapp.view.Home"/>
<mvc:TemplateView viewName="com.myapp.view.Home"/>

<core:Fragment fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="XML" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="JS" fragmentName="com.myapp.fragment.Details" />
<core:Fragment type="HTML" fragmentName="com.myapp.fragment.Details" />

</mvc:View>
Loading

0 comments on commit 16e3f94

Please sign in to comment.