Skip to content

feat: Detect usage of deprecated enum string values #621

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

Open
wants to merge 3 commits into
base: main
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
32 changes: 32 additions & 0 deletions src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,38 @@ export default class SourceFileLinter {
if (!propertySymbol) {
return;
}

const propertyType = this.checker.getTypeOfSymbol(propertySymbol);

if (propertyType.isUnion()) {
const valueType = this.checker.getTypeAtLocation(prop.initializer);
if (valueType?.isStringLiteral()) {
for (const type of propertyType.types) {
// Find out whether the value is assignable to one of the types
if (!this.checker.isTypeAssignableTo(valueType, type)) {
continue;
}
// If the type is just a string literal (no enum value), we need to look for a matching
// enum value in the list of types to check for its deprecation
if (!(type.flags & ts.TypeFlags.EnumLiteral) && type.isStringLiteral()) {
const enumType = propertyType.types.find((t) => {
return t.symbol?.name === type.value;
});
if (!enumType) {
continue;
}
const deprecationInfo = this.getDeprecationInfo(enumType.symbol);
if (deprecationInfo) {
this.#reporter.addMessage(MESSAGE.DEPRECATED_PROPERTY, {
propertyName: type.value,
details: deprecationInfo.messageDetails,
}, prop);
}
}
}
}
}

const deprecationInfo = this.getDeprecationInfo(propertySymbol);
if (!deprecationInfo) {
return;
Expand Down
26 changes: 24 additions & 2 deletions test/fixtures/linter/rules/NoDeprecatedApi/NoDeprecatedApi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
sap.ui.define([
"sap/m/Button", "sap/m/DateTimeInput", "sap/base/util/includes", "sap/ui/Device", "sap/ui/core/library", "sap/ui/generic/app/navigation/service/NavigationHandler",
"sap/ui/table/Table", "sap/ui/table/plugins/MultiSelectionPlugin", "sap/ui/core/Configuration", "sap/m/library"
], function(Button, DateTimeInput, includes, Device, coreLib, NavigationHandler, Table, MultiSelectionPlugin, Configuration, mobileLib) {
"sap/ui/table/Table", "sap/ui/table/plugins/MultiSelectionPlugin", "sap/ui/core/Configuration", "sap/m/library",
"sap/m/Input", "sap/m/TileContent", "sap/ui/layout/form/SimpleForm", "sap/m/ResponsivePopover"
], function(Button, DateTimeInput, includes, Device, coreLib, NavigationHandler, Table, MultiSelectionPlugin, Configuration, mobileLib, Input, TileContent, SimpleForm, ResponsivePopover) {
"use strict";
var dateTimeInput = new DateTimeInput(); // Control is deprecated. A finding only appears for the module dependency, not for the usage.

Expand Down Expand Up @@ -77,4 +78,25 @@ sap.ui.define([
// (via local function name)
const sapUiXmlView = sap.ui.xmlview;
const view3 = sapUiXmlView("com.ui5.troublesome.app.view.MyView");

const simpleForm = new SimpleForm({
layout: "ResponsiveLayout" // SimpleFormLayout.ResponsiveLayout is deprecated
});

const tileContent = new TileContent({
frameType: "TwoThirds" // FrameType.TwoThirds is deprecated
});

const input = new Input({
type: "Date" // InputType.Date is deprecated
});

const simpleForm2 = new SimpleForm({
layout: "ResponsiveGridLayout" // Negative test: ResponsiveGridLayout is not deprecated
});

const responsivePopover = new ResponsivePopover({
// ui5lint-disable-next-line no-globals
placement: sap.m.PlacementType.Auto // Negative test: PlacementType.Auto is not deprecated
});
});
14 changes: 14 additions & 0 deletions test/fixtures/linter/rules/NoDeprecatedApi/XMLView.view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:core="sap.ui.core"
xmlns:table="sap.ui.table"
xmlns:tablePlugins="sap.ui.table.plugins"
xmlns:form="sap.ui.layout.form"
>

<DateTimeInput /> <!-- DateTimeInput is deprecated -->
Expand Down Expand Up @@ -41,4 +42,17 @@
<core:Fragment type="XML" fragmentName="myapp.fragment.Details" />
<core:Fragment type="JS" fragmentName="myapp.fragment.Details" />


<!-- SimpleFormLayout.ResponsiveLayout is deprecated -->
<form:SimpleForm layout="ResponsiveLayout" />

<!-- FrameType.TwoThirds is deprecated -->
<TileContent frameType="TwoThirds" />

<!-- InputType.Date is deprecated -->
<Input type="Date" />

<!-- Negative test: ResponsiveGridLayout is not deprecated -->
<form:SimpleForm layout="ResponsiveGridLayout" />

</mvc:View>
Loading