mcpField
F
fix-partially-deprecated-apis

fix-partially-deprecated-apis

github.com/ui5开发44142 次安装1 周前更新
代码仓库

关于 fix-partially-deprecated-apis

面向 UI5 开发者的开发技能,用于处理 UI5 linter 报告但无法自动修复的弃用 API 用法。它针对参数、视图类型、路由和格式化器等情况提供代码修改指引。

主要功能

  1. 01修正弃用的 Parameters.get 用法
  2. 02更新 JSONModel.loadData 参数
  3. 03替换弃用的 ODataModel 参数
  4. 04将视图和片段转换为 XML
  5. 05设置 Router 异步配置
  6. 06在 JS 格式化器中使用函数引用

适用场景

  1. 01处理 UI5 linter 的弃用问题
  2. 02现代化现有 UI5 应用中的 API 调用
  3. 03lint 后检查视图、片段和路由代码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ui5/plugins-coding-agents --skill fix-partially-deprecated-apis

安装后在 Claude Code 中输入/fix-partially-deprecated-apis确认已加载。

详细介绍

修复部分已弃用的 API

此技能用于修复 UI5 linter 检测到但无法自动修复的部分已弃用 API 用法,因为修复需要了解所使用的具体已弃用变体。

快速参考

API已弃用的用法修复方式
Parameters.get无参数、字符串或数组使用包含 name 和 callback 的对象
JSONModel.loadDatabAsync=false(第 3 个参数)省略或设为 true
Mobile.inithomeIcon/homeIconPrecomposed移除,改用 web manifest
ODataModel.v2.createEntrybatchGroupId使用 groupId
ODataModel.v2.createEntryproperties 为数组使用包含值的对象
View.createtype: "JS/JSON/HTML/Template"使用 "XML" 或省略
Fragment.loadtype: "HTML"使用 "XML" 或省略
Router 构造函数缺少 async 或值为 false设为 async: true
绑定 formatterJS 中使用字符串值使用函数引用

处理的 Linter 规则

规则 ID消息模式此技能的操作
no-deprecated-api使用了已弃用的 'sap/ui/core/theming/Parameters.get' 变体使用对象参数
no-deprecated-api'sap/ui/model/json/JSONModel#loadData' 的参数 '...' 使用了已弃用的值移除/更改参数
no-deprecated-api'sap/ui/util/Mobile#init' 的参数 '...' 使用了已弃用的值移除参数
no-deprecated-api'sap/ui/model/odata/v2/ODataModel#createEntry' 中使用了已弃用的参数 'batchGroupId'使用 groupId
no-deprecated-api'sap/ui/core/mvc/View.create' 的参数 'type' 使用了已弃用的值 '...'使用 XML 类型
no-deprecated-api'sap/ui/core/Fragment.load' 的参数 'type' 使用了已弃用的值 '...'使用 XML 类型
no-deprecated-api'sap/ui/core/Router' 构造函数的参数 'oConfig.async' 使用了已弃用的值设为 async: true
unsupported-api-usageJavaScript 中的 'formatter' 值不能使用字符串使用函数引用

适用场景

看到如下 linter 输出时,请应用此技能:

MyController.js:15:5 error Usage of deprecated variant of 'sap/ui/core/theming/Parameters.get'  no-deprecated-api
MyController.js:20:5 error Usage of deprecated value for parameter 'bAsync' of 'sap/ui/model/json/JSONModel#loadData'  no-deprecated-api
MyController.js:25:5 error Usage of deprecated parameter 'batchGroupId' in 'sap/ui/model/odata/v2/ODataModel#createEntry'  no-deprecated-api
MyController.js:30:5 error Do not use strings for 'formatter' values in JavaScript  unsupported-api-usage

修复策略

1. Parameters.get - 旧变体

问题:调用 Parameters.get() 时未传入对象参数。

// Before - all these variants are deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/theming/Parameters"
], function(Controller, Parameters) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            // Deprecated: no arguments
            var allParams = Parameters.get();

            // Deprecated: string argument
            var singleParam = Parameters.get("sapUiBaseColor");

            // Deprecated: array argument
            var multiParams = Parameters.get(["sapUiBaseColor", "sapUiBaseBG"]);
        }
    });
});

修复策略:使用带回调的对象参数。

// After - use object parameter
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/theming/Parameters"
], function(Controller, Parameters) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            // Correct: object parameter with name and callback
            Parameters.get({
                name: "sapUiBaseColor",
                callback: function(sValue) {
                    // Use the parameter value
                    console.log("Color:", sValue);
                }
            });

            // Correct: multiple parameters
            Parameters.get({
                name: ["sapUiBaseColor", "sapUiBaseBG"],
                callback: function(mParams) {
                    // mParams is an object with parameter values
                    console.log("Base color:", mParams.sapUiBaseColor);
                    console.log("Base BG:", mParams.sapUiBaseBG);
                }
            });
        }
    });
});
2. JSONModel.loadData - 已弃用的参数

问题:在 loadData() 中使用 bAsync=false 或 bCache=false。

// Before - bAsync=false is deprecated (3rd parameter)
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
], function(Controller, JSONModel) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            var oModel = new JSONModel();

            // Deprecated: bAsync=false (synchronous loading)
            oModel.loadData("/api/data", null, false);

            // Deprecated: bCache=false (6th parameter)
            oModel.loadData("/api/data", null, true, "GET", false, false);
        }
    });
});

修复策略:使用异步加载(默认行为),并移除缓存参数。

// After - use async loading
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
], function(Controller, JSONModel) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            var oModel = new JSONModel();

            // Correct: async loading (default or true)
            oModel.loadData("/api/data").then(function() {
                // Handle loaded data
            });

            // Or with parameters, omitting deprecated ones
            oModel.loadData("/api/data", null, true, "GET");
        }
    });
});
3. Mobile.init - 已弃用的参数

问题:在 Mobile.init() 中使用 homeIcon 或 homeIconPrecomposed。

// Before - homeIcon and homeIconPrecomposed are deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/util/Mobile"
], function(Controller, Mobile) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            Mobile.init({
                homeIcon: "icons/icon.png",              // Deprecated
                homeIconPrecomposed: true,              // Deprecated
                statusBar: "default"
            });
        }
    });
});

修复策略:移除已弃用的参数。

// After - remove homeIcon and homeIconPrecomposed
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/util/Mobile"
], function(Controller, Mobile) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            Mobile.init({
                statusBar: "default"
            });

            // Note: Use web app manifest (manifest.json) for home screen icons instead
        }
    });
});

替代方案:在 web 应用清单中定义主屏幕图标:

{
    "icons": [
        {
            "src": "icons/icon-192.png",
            "sizes": "192x192",
            "type": "image/png"
        }
    ]
}
4. ODataModel.v2.createEntry - 已弃用的参数

问题:在 createEntry() 中使用 batchGroupId,或将 properties 用作数组。

// Before - batchGroupId and properties array are deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function(Controller) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onCreate: function() {
            var oModel = this.getView().getModel();

            var oContext = oModel.createEntry("/Products", {
                batchGroupId: "myBatch",                      // Deprecated: use groupId
                properties: ["Name", "Price", "Category"]    // Deprecated: use object
            });
        }
    });
});

修复策略:使用 groupId,并将属性作为包含初始值的对象传递。

// After - use groupId and properties object
sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function(Controller) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onCreate: function() {
            var oModel = this.getView().getModel();

            var oContext = oModel.createEntry("/Products", {
                groupId: "myBatch",                          // Correct: groupId
                properties: {                                // Correct: object with initial values
                    Name: "",
                    Price: 0,
                    Category: ""
                }
            });
        }
    });
});
5. View.create - 已弃用的视图类型

问题:使用已弃用的类型(JS、JSON、HTML、Template)创建视图。

// Before - non-XML view types are deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/mvc/View"
], function(Controller, View) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            View.create({
                viewName: "my.app.view.Detail",
                type: "JS"          // Deprecated: JS, JSON, HTML, Template
            }).then(function(oView) {
                // Use view
            });
        }
    });
});

修复策略:转换为 XML 视图类型。

// After - use XML type (or omit type, XML is default)
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/mvc/View"
], function(Controller, View) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            View.create({
                viewName: "my.app.view.Detail",
                type: "XML"         // Or omit - XML is default
            }).then(function(oView) {
                // Use view
            });
        }
    });
});

注意:这需要将 JS/JSON/HTML 视图文件转换为 XML 视图文件。

6. Fragment.load - 已弃用的片段类型

问题:使用 HTML 类型加载片段。

// Before - HTML fragment type is deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/Fragment"
], function(Controller, Fragment) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onOpenDialog: function() {
            Fragment.load({
                name: "my.app.fragment.Dialog",
                type: "HTML"        // Deprecated
            }).then(function(oFragment) {
                // Use fragment
            });
        }
    });
});

修复策略:转换为 XML 片段。

// After - use XML type (or omit, XML is default)
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/Fragment"
], function(Controller, Fragment) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onOpenDialog: function() {
            Fragment.load({
                name: "my.app.fragment.Dialog",
                type: "XML"         // Or omit - XML is default
            }).then(function(oFragment) {
                // Use fragment
            });
        }
    });
});
7. Router 构造函数 - 缺少 async 标志

问题:创建 Router 时未设置 async: true。

// Before - missing or false async flag is deprecated
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/routing/Router"
], function(Controller, Router) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            // Deprecated: no config
            var oRouter1 = new Router([]);

            // Deprecated: async not set or false
            var oRouter2 = new Router([], {
                async: false
            });
        }
    });
});

修复策略:在配置中设置 async: true。

// After - set async: true
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/routing/Router"
], function(Controller, Router) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        onInit: function() {
            var oRouter = new Router([], {
                async: true         // Required
            });
        }
    });
});

注意:大多数应用通过 manifest.json 使用 Component 内置的路由器;当 Component.js 中实现了 IAsyncContentCreation 时,该路由器会自动处理此设置(参见 fix-component-async 技能)。

8. JS 绑定中的字符串格式化器

问题:在 JavaScript 中将字符串用作格式化器。

// Before - string formatter doesn't work in JS
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/m/Input",
    "../model/formatter"
], function(Controller, Input, formatter) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        formatter: formatter,

        onInit: function() {
            var oInput = new Input({
                value: {
                    path: "/status",
                    formatter: "formatter.statusText"   // Error: string won't work
                }
            });
        }
    });
});

修复策略:使用函数引用。

// After - use function reference
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/m/Input",
    "../model/formatter"
], function(Controller, Input, formatter) {
    "use strict";

    return Controller.extend("my.app.controller.Main", {
        formatter: formatter,

        onInit: function() {
            var oInput = new Input({
                value: {
                    path: "/status",
                    formatter: formatter.statusText    // Correct: function reference
                }
            });
        }
    });
});

注意:字符串格式化器在 XML 视图中可以使用(在运行时解析),但在 JS 中无法使用,因为你可以直接访问函数。

注意事项

  • 这些内容只是“部分”弃用,因为弃用的只是特定用法模式,而非整个 API
  • 请始终查看 npx @ui5/linter --details,获取现代化文档的链接
  • 视图/片段类型变更要求将实际的视图/片段文件转换为 XML
  • 在 Component.js 中使用 IAsyncContentCreation 时,Router 的 async: true 会自动处理(正确的放置位置请参阅 fix-component-async 技能)

评论

支持 Markdown