Fix Library Init

作者 UI52b8c4a944e39無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Fix Library.init() apiVersion issues and modernize enums to DataType.registerEnum. Use this skill when linter outputs: - `no-deprecated-api` with message "Deprecated call to ... Use the {apiVersion: 2} parameter instead" Trigger on library.js files with errors about: "Deprecated call to", "Lib.init", "Library.init", "{apiVersion: 2} parameter" Adds apiVersion: 2 to Library.init() calls and modernizes enum definitions to use DataType.registerEnum, which is required for type validation when using apiVersion: 2.

AI 產生的概覽

修復 UI5 library.js:為 Library.init() 加上 apiVersion: 2,並以 DataType.registerEnum 現代化列舉定義。

功能
此技能引導代理修復被 UI5 linter 的 no-deprecated-api 規則標記的 UI5 library.js 檔案,這些檔案涉及 Library.init()/Lib.init() 呼叫。它會為多種呼叫形式加上 apiVersion: 2 參數,包括無參數、物件參數、方括號記法、賦值與解構呼叫,並修正錯誤或字串形式的取值。它也會把列舉定義從全域命名空間移到函式庫物件上,加入 DataType.registerEnum 呼叫,更新 sap.ui.define 相依清單並回傳函式庫物件。
適用情境
當 linter 輸出回報對 Library.init()、Lib.init() 或 init() 的棄用呼叫,並提示改用 {apiVersion: 2} 參數時使用。適用於定義 UI5 函式庫中繼資料與列舉的 library.js 檔案。
執行需求
僅為說明性內容,不附帶指令碼。需要可用的 UI5 linter(例如透過 npx @ui5/linter),以及存取專案中的 library.js 原始檔。

Fix Library.init() Modernization

This skill fixes Library.init() / Lib.init() calls that the UI5 linter detects as missing apiVersion: 2, and modernizes enum definitions to use DataType.registerEnum — which is required when using apiVersion: 2.

Linter Rules Handled

Rule IDMessage PatternThis Skill's Action
no-deprecated-apiDeprecated call to Library.init(). Use the {apiVersion: 2} parameter insteadAdd apiVersion: 2 to the init call
no-deprecated-apiDeprecated call to Lib.init(). Use the {apiVersion: 2} parameter insteadAdd apiVersion: 2 to the init call
no-deprecated-apiDeprecated call to init(). Use the {apiVersion: 2} parameter insteadAdd apiVersion: 2 (destructured init)

When to Use

Apply this skill when you see linter output like:

library.js:9:2 error Deprecated call to Library.init(). Use the {apiVersion: 2} parameter instead  no-deprecated-apilibrary.js:11:2 error Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead  no-deprecated-apilibrary.js:51:2 error Deprecated call to init(). Use the {apiVersion: 2} parameter instead  no-deprecated-api

Background: What Library.init() Does

sap/ui/core/Lib.init() registers a UI5 library with the framework, declaring its metadata: name, dependencies, controls, elements, types (enums), and interfaces. The apiVersion: 2 parameter signals that the library uses the modern initialization pattern.

Important: Only apiVersion: 2 is valid for Lib.init(). Unlike control renderers where apiVersion: 4 is also valid, library initialization only accepts 2.

Fix Strategy

1. No Arguments

Problem: Lib.init() called with no arguments at all.

javascript
// Before — triggers no-deprecated-apisap.ui.define([    "sap/ui/core/Lib"], function(Library) {    "use strict";
    Library.init();});

Fix: Add an object argument with apiVersion: 2.

javascript
// Aftersap.ui.define([    "sap/ui/core/Lib"], function(Library) {    "use strict";
    Library.init({        apiVersion: 2    });});

2. Object Argument Without apiVersion

Problem: Lib.init() receives an object but it has no apiVersion property.

javascript
// Before — triggers no-deprecated-apiLibrary.init({    name: "my.lib",    dependencies: ["sap.ui.core", "sap.m"]});

Fix: Add apiVersion: 2 as the first property in the object.

javascript
// AfterLibrary.init({    apiVersion: 2,    name: "my.lib",    dependencies: ["sap.ui.core", "sap.m"]});

3. apiVersion Is Not a Number

Problem: apiVersion is a string instead of a numeric literal.

javascript
// Before — triggers no-deprecated-apiLibrary.init({    apiVersion: "2"});

Fix: Change to numeric literal 2.

javascript
// AfterLibrary.init({    apiVersion: 2});

4. apiVersion Is Wrong Number

Problem: apiVersion is a number but not 2.

javascript
// Before — triggers no-deprecated-apiLibrary.init({    apiVersion: 1});

Fix: Change to 2.

javascript
// AfterLibrary.init({    apiVersion: 2});

5. Element Access Syntax

The same patterns apply when init is called via bracket notation:

javascript
// BeforeLibrary["init"]({    apiVersion: 1,    dependencies: ["sap.ui.core"]});
// AfterLibrary["init"]({    apiVersion: 2,    dependencies: ["sap.ui.core"]});

6. Destructured or Assigned init

The linter also detects these patterns:

javascript
// Assignmentconst LibInit = Library.init;LibInit({ apiVersion: 1 });
// Destructuringconst {init} = Library;init({ apiVersion: 1 });
// Destructuring with renameconst {init: libInit} = Library;libInit({ apiVersion: 1 });

Fix: Same — change apiVersion to 2 in each call.

Modernize Enums to DataType.registerEnum

When upgrading Lib.init() to apiVersion: 2, the old approach of defining enums on the global namespace no longer works. The framework cannot resolve type strings used in control metadata without explicit registration, which can lead to XSS vulnerabilities because type checking is silently skipped.

This skill must find existing enum definitions in the library.js and modernize them to use DataType.registerEnum.

How to Find Enums

  1. Check the types array in the Lib.init() call — it lists the fully qualified names of all types (enums) defined by the library
  2. Search for global namespace assignments — e.g. my.lib.ValueColor = { ... } where my.lib is the library namespace
  3. Look for existing enum objects defined as plain objects with string key-value pairs

Modernization Steps

  1. Capture the return value of Lib.init() in a variable if not already done:

    javascript
    // BeforeLibrary.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] });
    // Aftervar oThisLibrary = Library.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] });
  2. Move enum definitions from the global namespace to the library object:

    javascript
    // Before — global namespacemy.lib.ValueColor = { Color1: "Color1", Color2: "Color2" };
    // After — on library objectoThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" };
  3. Add DataType.registerEnum call immediately after each enum definition:

    javascript
    oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" };DataType.registerEnum("my.lib.ValueColor", oThisLibrary.ValueColor);
  4. Add sap/ui/base/DataType to the sap.ui.define dependency list if not already present:

    javascript
    sap.ui.define([    "sap/ui/base/DataType",  // Add this    "sap/ui/core/Lib"], function(DataType, Library) {

Key Rules

  • The first argument to registerEnum must exactly match the string used as type in control metadata (e.g. "my.lib.ValueColor")
  • All enum definitions should stay in library.js for library preload compatibility
  • Each enum listed in the types array needs its own registerEnum call

Complete Example

Before

javascript
/*! * ${copyright} */sap.ui.define([    "sap/ui/core/Lib"], function(Library) {    "use strict";
    Library.init({        name: "my.lib",        dependencies: ["sap.ui.core", "sap.m"],        types: [            "my.lib.ValueColor",            "my.lib.StatusType"        ],        controls: [            "my.lib.MyControl"        ]    });
    // Enums defined on global namespace    my.lib.ValueColor = {        Good: "Good",        Critical: "Critical",        Error: "Error",        Neutral: "Neutral"    };
    my.lib.StatusType = {        Active: "Active",        Inactive: "Inactive"    };});

After

javascript
/*! * ${copyright} */sap.ui.define([    "sap/ui/base/DataType",    "sap/ui/core/Lib"], function(DataType, Library) {    "use strict";
    var oThisLibrary = Library.init({        apiVersion: 2,        name: "my.lib",        dependencies: ["sap.ui.core", "sap.m"],        types: [            "my.lib.ValueColor",            "my.lib.StatusType"        ],        controls: [            "my.lib.MyControl"        ]    });
    oThisLibrary.ValueColor = {        Good: "Good",        Critical: "Critical",        Error: "Error",        Neutral: "Neutral"    };    DataType.registerEnum("my.lib.ValueColor", oThisLibrary.ValueColor);
    oThisLibrary.StatusType = {        Active: "Active",        Inactive: "Inactive"    };    DataType.registerEnum("my.lib.StatusType", oThisLibrary.StatusType);
    return oThisLibrary;});

Implementation Steps

  1. Run linter with --details to get additional context:

    bash
    npx @ui5/linter --details
  2. Read the library.js file and identify:

    • The Lib.init() / Library.init() call and its current arguments
    • Whether the return value is captured in a variable
    • Any enum definitions (check types array and global namespace assignments)
  3. Fix the apiVersion:

    • If no arguments: add { apiVersion: 2 }
    • If object without apiVersion: add apiVersion: 2 as the first property
    • If apiVersion is wrong value or type: change to numeric 2
  4. Modernize enums (if any exist):

    • Capture Lib.init() return value in a variable if not already done
    • Move each enum from global namespace to the library object
    • Add DataType.registerEnum call for each enum
    • Add sap/ui/base/DataType to the dependency list
  5. Ensure the module returns the library object (return oThisLibrary;)

  6. Verify the fix by re-running the linter

Notes

  • Only apiVersion: 2 is valid for Lib.init() — apiVersion: 4 is NOT valid (that is for renderers only)
  • apiVersion must be a numeric literal, not a string
  • Libraries without enums only need the apiVersion: 2 fix (no DataType.registerEnum needed)
  • If the library already returns the init result and has no global enum definitions, only the apiVersion fix is needed
  • The types array in the init call is your guide to which enums need registerEnum calls

Related Skills

  • fix-control-renderer: For renderer apiVersion issues (missing apiVersion: 2 or 4 in renderer objects) — different from library init apiVersion
  • fix-pseudo-modules: If consumer code imports enums via pseudo-modules (e.g. sap.ui.require(["my/lib/ValueColor"])), use fix-pseudo-modules to convert them to library module imports
  • fix-js-globals: If library.js also has global access patterns beyond enum definitions (e.g. sap.ui.getCore()), use fix-js-globals for those

來源與署名

來源:UI5/plugins-coding-agents位於plugins/ui5-modernization/skills/fix-library-init提交2b8c4a9

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架