Skip to content

Commit af2cb9b

Browse files
feat(autofix): Fix additional deprecated Core APIs (#639)
JIRA: CPOUI5FOUNDATION-991 Enhances #671 by enabling more complex autofixes where migration depends on the provided arguments. Resolves partially: #619 Follow up PRs: #675 --------- Co-authored-by: Yavor Ivanov <[email protected]>
1 parent c323770 commit af2cb9b

File tree

6 files changed

+105
-33
lines changed

6 files changed

+105
-33
lines changed

src/autofix/solutions/codeReplacer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ function patchMessageFixHints(fixHints?: FixHints, apiName?: string) {
314314
`$moduleIdentifier.${fnName}(${cleanRedundantArguments(fixHints.exportCodeToBeUsed.args)})`;
315315
}
316316
}
317+
} else if (apiName === "applyTheme" && fixHints?.moduleName === "sap/ui/core/Theming") {
318+
if ((fixHints?.exportCodeToBeUsed?.args?.length ?? 0) > 1 &&
319+
fixHints?.exportCodeToBeUsed?.args?.[1]?.value !== "undefined") {
320+
fixHints = undefined; // We cannot handle this case
321+
log.verbose(`Autofix skipped for ${apiName}. Transpilation is too ambiguous.`);
322+
}
317323
}
318324

319325
return fixHints;

src/linter/ui5Types/fixHints/CoreFixHintsGenerator.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ const coreModulesReplacements = new Map<string, FixHints>([
3939
["byFieldGroupId", {
4040
moduleName: "sap/ui/core/Control", exportCodeToBeUsed: "$moduleIdentifier.getControlsByFieldGroupId($1)",
4141
}],
42+
["getCurrentFocusedControlId", {
43+
// The legacy API used to return null if no control was focused.
44+
moduleName: "sap/ui/core/Element", exportCodeToBeUsed: "$moduleIdentifier.getActiveElement()?.getId() || null",
45+
}],
4246
["isStaticAreaRef", {
4347
moduleName: "sap/ui/core/StaticArea",
4448
exportCodeToBeUsed: "$moduleIdentifier.getDomRef() === $1",
4549
}],
46-
// TODO: The legacy API return null if the control is not found.
47-
// ["getCurrentFocusedControlId", {
48-
// moduleName: "sap/ui/core/Element", exportNameToBeUsed: "getActiveElement()?.getId",
49-
// }],
50-
// // Migrate only if second argument is omitted or undefined
51-
// ["applyTheme", {
52-
// moduleName: "sap/ui/core/Theming",
53-
// exportCodeToBeUsed: "$moduleIdentifier.setTheme($1)",
54-
// }],
55-
// // Individual arguments must be mapped to "options" object
56-
// // The new API has no sync loading option, replacement is only safe when the options contain async:true
50+
// Migrate only if second argument is omitted or undefined
51+
["applyTheme", {
52+
moduleName: "sap/ui/core/Theming",
53+
exportCodeToBeUsed: "$moduleIdentifier.setTheme($1)",
54+
}],
55+
// Individual arguments must be mapped to "options" object
56+
// The new API has no sync loading option, replacement is only safe when the options contain async:true
5757
// ["loadLibrary", {
5858
// moduleName: "sap/ui/core/Lib", exportCodeToBeUsed: "$moduleIdentifier.load($1)",
5959
// }],
@@ -63,14 +63,14 @@ const coreModulesReplacements = new Map<string, FixHints>([
6363
// ["createComponent", {
6464
// moduleName: "sap/ui/core/Component", exportCodeToBeUsed: "$moduleIdentifier.create($1)",
6565
// }],
66-
// // Note that alternative replacement Component.get is meanwhile deprecated, too
67-
// ["getComponent", {
68-
// moduleName: "sap/ui/core/Component", exportNameToBeUsed: "getComponentById",
69-
// }],
70-
// // Parameter bAsync has to be omitted or set to false since the new API returns
71-
// // the resource bundle synchronously. When bAsync is true, the new API is not a replacement
72-
// // as it does not return a promise. In an await expression, it would be okay, but otherwise not.
73-
// // TODO: To be discussed: sLibrary must be a library, that might not be easy to check
66+
// Note that alternative replacement Component.get is meanwhile deprecated, too
67+
["getComponent", {
68+
moduleName: "sap/ui/core/Component", exportNameToBeUsed: "getComponentById",
69+
}],
70+
// Parameter bAsync has to be omitted or set to false since the new API returns
71+
// the resource bundle synchronously. When bAsync is true, the new API is not a replacement
72+
// as it does not return a promise. In an await expression, it would be okay, but otherwise not.
73+
// TODO: To be discussed: sLibrary must be a library, that might not be easy to check
7474
// ["getLibraryResourceBundle", {
7575
// moduleName: "sap/ui/core/Lib", exportCodeToBeUsed: "$moduleIdentifier.getResourceBundleFor($1, $2)",
7676
// }],

test/fixtures/autofix/coreApi/DeprecatedCoreApi.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
sap.ui.define(["sap/ui/core/Core",], function(CoreRenamed) {
2+
CoreRenamed.applyTheme("themeName");
3+
CoreRenamed.applyTheme("customTheme", "find/my/theme/here"); // Should not be autofixed if there is a 2nd argument
4+
CoreRenamed.applyTheme("customTheme", undefined); // Can be migrated when the 2nd argument is undefined
5+
26
CoreRenamed.attachInit(function() {console.log();});
37

48
CoreRenamed.attachInitEvent(function() {console.log();});
@@ -8,8 +12,12 @@ sap.ui.define(["sap/ui/core/Core",], function(CoreRenamed) {
812

913
CoreRenamed.byId("id");
1014

15+
CoreRenamed.getComponent("componentId");
16+
1117
CoreRenamed.getControl("controlId");
1218

19+
CoreRenamed.getCurrentFocusedControlId();
20+
1321
CoreRenamed.getElementById("elementId");
1422

1523
CoreRenamed.getEventBus();

test/fixtures/autofix/coreApi/DeprecatedCoreApiWithoutImport.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
sap.ui.define([], function() {
22
const globalCore = sap.ui.getCore();
3-
4-
globalCore.attachInit(function() {console.log();});
3+
4+
sap.ui.getCore().applyTheme("themeName");
5+
sap.ui.getCore().applyTheme("customTheme", "find/my/theme/here"); // Should not be autofixed if there is a 2nd argument
6+
sap.ui.getCore().applyTheme("customTheme", undefined); // Can be autofixed when the 2nd argument is undefined
7+
58
sap.ui.getCore().attachInit(function() {console.log();});
69

710
globalCore.attachInitEvent(function() {console.log();});
@@ -13,10 +16,14 @@ sap.ui.define([], function() {
1316
globalCore.byId("id");
1417
sap.ui.getCore().byId("id");
1518

16-
globalCore.getControl("controlId");
19+
globalCore.getComponent("componentId");
20+
sap.ui.getCore().getComponent("componentId");
21+
1722
sap.ui.getCore().getControl("controlId");
1823

19-
globalCore.getElementById("elementId");
24+
globalCore.getCurrentFocusedControlId();
25+
sap.ui.getCore().getCurrentFocusedControlId();
26+
2027
sap.ui.getCore().getElementById("elementId");
2128

2229
globalCore.getEventBus();

test/lib/autofix/snapshots/autofix.fixtures.ts.md

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,7 +5213,12 @@ Generated by [AVA](https://avajs.dev).
52135213

52145214
> AutofixResult: /coreApi/DeprecatedCoreApi.js
52155215
5216-
`sap.ui.define(["sap/ui/core/Core", "sap/ui/core/Control", "sap/ui/core/Element", "sap/ui/core/EventBus", "sap/ui/core/Lib", "sap/ui/core/StaticArea", "sap/ui/core/Theming", "sap/ui/Device",], function(CoreRenamed, Control, Element, EventBus, Lib, StaticArea, Theming, Device) {␊
5216+
`sap.ui.define(["sap/ui/core/Core", "sap/ui/core/Component", "sap/ui/core/Control", "sap/ui/core/Element", "sap/ui/core/EventBus", "sap/ui/core/Lib", "sap/ui/core/StaticArea", "sap/ui/core/Theming",␊
5217+
"sap/ui/Device",], function(CoreRenamed, Component, Control, Element, EventBus, Lib, StaticArea, Theming, Device) {␊
5218+
Theming.setTheme("themeName");␊
5219+
CoreRenamed.applyTheme("customTheme", "find/my/theme/here"); // Should not be autofixed if there is a 2nd argument␊
5220+
Theming.setTheme("customTheme"); // Can be migrated when the 2nd argument is undefined␊
5221+
52175222
CoreRenamed.ready(function() {console.log();});␊
52185223
52195224
CoreRenamed.ready(function() {console.log();});␊
@@ -5222,8 +5227,12 @@ Generated by [AVA](https://avajs.dev).
52225227
Control.getControlsByFieldGroupId(["id", "id2"]);␊
52235228
52245229
Element.getElementById("id");␊
5230+
5231+
Component.getComponentById("componentId");␊
52255232
52265233
Element.getElementById("controlId");␊
5234+
5235+
Element.getActiveElement()?.getId() || null;␊
52275236
52285237
Element.getElementById("elementId");␊
52295238
@@ -5258,13 +5267,21 @@ Generated by [AVA](https://avajs.dev).
52585267
[
52595268
{
52605269
coverageInfo: [],
5261-
errorCount: 1,
5270+
errorCount: 2,
52625271
fatalErrorCount: 0,
52635272
filePath: 'DeprecatedCoreApi.js',
52645273
messages: [
5274+
{
5275+
column: 14,
5276+
line: 4,
5277+
message: 'Call to deprecated function \'applyTheme\' of class \'Core\'',
5278+
messageDetails: 'Deprecated test message',
5279+
ruleId: 'no-deprecated-api',
5280+
severity: 2,
5281+
},
52655282
{
52665283
column: 2,
5267-
line: 19,
5284+
line: 28,
52685285
message: 'Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead',
52695286
messageDetails: 'Lib.init (https://ui5.sap.com/1.120/#/api/sap.ui.core.Lib)',
52705287
ruleId: 'no-deprecated-api',
@@ -5279,9 +5296,14 @@ Generated by [AVA](https://avajs.dev).
52795296

52805297
> AutofixResult: /coreApi/DeprecatedCoreApiWithoutImport.js
52815298
5282-
`sap.ui.define(["sap/ui/core/Control", "sap/ui/core/Core", "sap/ui/core/Element", "sap/ui/core/EventBus", "sap/ui/core/Lib", "sap/ui/core/StaticArea", "sap/ui/core/Theming", "sap/ui/Device"], function(Control, Core, Element, EventBus, Lib, StaticArea, Theming, Device) {␊
5299+
`sap.ui.define(["sap/ui/core/Component", "sap/ui/core/Control", "sap/ui/core/Core", "sap/ui/core/Element", "sap/ui/core/EventBus", "sap/ui/core/Lib", "sap/ui/core/StaticArea", "sap/ui/core/Theming",␊
5300+
"sap/ui/Device"], function(Component, Control, Core, Element, EventBus, Lib, StaticArea, Theming, Device) {␊
52835301
const globalCore = sap.ui.getCore();␊
5284-
5302+
5303+
Theming.setTheme("themeName");␊
5304+
sap.ui.getCore().applyTheme("customTheme", "find/my/theme/here"); // Should not be autofixed if there is a 2nd argument␊
5305+
Theming.setTheme("customTheme"); // Can be autofixed when the 2nd argument is undefined␊
5306+
52855307
Core.ready(function() {console.log();});␊
52865308
Core.ready(function() {console.log();});␊
52875309
@@ -5293,9 +5315,14 @@ Generated by [AVA](https://avajs.dev).
52935315
52945316
Element.getElementById("id");␊
52955317
Element.getElementById("id");␊
5318+
5319+
Component.getComponentById("componentId");␊
5320+
Component.getComponentById("componentId");␊
52965321
52975322
Element.getElementById("controlId");␊
5298-
Element.getElementById("controlId");␊
5323+
5324+
Element.getActiveElement()?.getId() || null;␊
5325+
Element.getActiveElement()?.getId() || null;␊
52995326
53005327
Element.getElementById("elementId");␊
53015328
Element.getElementById("elementId");␊
@@ -5336,29 +5363,53 @@ Generated by [AVA](https://avajs.dev).
53365363
[
53375364
{
53385365
coverageInfo: [],
5339-
errorCount: 3,
5366+
errorCount: 6,
53405367
fatalErrorCount: 0,
53415368
filePath: 'DeprecatedCoreApiWithoutImport.js',
53425369
messages: [
53435370
{
53445371
column: 21,
5345-
line: 2,
5372+
line: 3,
53465373
message: 'Access of global variable \'sap\' (sap.ui.getCore)',
53475374
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
53485375
ruleId: 'no-globals',
53495376
severity: 2,
53505377
},
53515378
{
53525379
column: 28,
5353-
line: 2,
5380+
line: 3,
53545381
message: 'Call to deprecated function \'getCore\' (sap.ui.getCore)',
53555382
messageDetails: 'Deprecated test message',
53565383
ruleId: 'no-deprecated-api',
53575384
severity: 2,
53585385
},
53595386
{
53605387
column: 2,
5361-
line: 28,
5388+
line: 6,
5389+
message: 'Access of global variable \'sap\' (sap.ui.getCore)',
5390+
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
5391+
ruleId: 'no-globals',
5392+
severity: 2,
5393+
},
5394+
{
5395+
column: 9,
5396+
line: 6,
5397+
message: 'Call to deprecated function \'getCore\' (sap.ui.getCore)',
5398+
messageDetails: 'Deprecated test message',
5399+
ruleId: 'no-deprecated-api',
5400+
severity: 2,
5401+
},
5402+
{
5403+
column: 19,
5404+
line: 6,
5405+
message: 'Call to deprecated function \'applyTheme\' of class \'Core\'',
5406+
messageDetails: 'Deprecated test message',
5407+
ruleId: 'no-deprecated-api',
5408+
severity: 2,
5409+
},
5410+
{
5411+
column: 2,
5412+
line: 36,
53625413
message: 'Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead',
53635414
messageDetails: 'Lib.init (https://ui5.sap.com/1.120/#/api/sap.ui.core.Lib)',
53645415
ruleId: 'no-deprecated-api',
333 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)