🔧fix-library-init
- プラグイン
- ui5-modernization
- ソース
- GitHub で見る ↗
説明
`Library.init()` の `apiVersion` に関する問題を修正し、列挙型の定義を `DataType.registerEnum` を使ったモダンな形式に移行します。 次のような場合に使用: - リンターが以下を出力しているとき: - `no-deprecated-api` のメッセージに「Deprecated call to ... Use the {apiVersion: 2} parameter instead」が含まれている場合 以下のエラーが含まれる `library.js` ファイルに対してトリガーされます: - `"Deprecated call to"` - `"Lib.init"` - `"Library.init"` - `"{apiVersion: 2} parameter"` `Library.init()` の呼び出しに `apiVersion: 2` を追加し、列挙型の定義を `DataType.registerEnum` を使った形式に移行します。 なお、`DataType.registerEnum` は `apiVersion: 2` を使用する際の型バリデーションに必須です。
原文を表示
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.
ユースケース
- ✓Library.init()の非推奨APIエラーを修正するとき
- ✓列挙型定義をモダン形式に移行するとき
- ✓apiVersion: 2パラメータを追加する必要があるとき
本文(日本語訳)
Fix Library.init() モダナイゼーション
このskillは、UI5 linterが検出する Library.init() / Lib.init() 呼び出しへの apiVersion: 2 の欠落を修正し、apiVersion: 2 使用時に必須となる DataType.registerEnum を用いたenum定義のモダナイゼーションを行います。
対象 Linter ルール
| Rule ID | メッセージパターン | このSkillの対応 |
|---|---|---|
no-deprecated-api |
Deprecated call to Library.init(). Use the {apiVersion: 2} parameter instead | init呼び出しに apiVersion: 2 を追加 |
no-deprecated-api |
Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead | init呼び出しに apiVersion: 2 を追加 |
no-deprecated-api |
Deprecated call to init(). Use the {apiVersion: 2} parameter instead | apiVersion: 2 を追加(分割代入によるinit) |
次のような場合に使用
以下のようなlinter出力が出ている場合に、このskillを適用します:
library.js:9:2 error Deprecated call to Library.init(). Use the {apiVersion: 2} parameter instead no-deprecated-api
library.js:11:2 error Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead no-deprecated-api
library.js:51:2 error Deprecated call to init(). Use the {apiVersion: 2} parameter instead no-deprecated-api
背景: Library.init() の役割
sap/ui/core/Lib.init() はUI5ライブラリをフレームワークに登録し、名前・依存関係・コントロール・エレメント・型(enum)・インターフェースなどのメタデータを宣言します。
apiVersion: 2 パラメータは、そのライブラリがモダンな初期化パターンを使用していることを示します。
重要: Lib.init() に指定できるのは apiVersion: 2 のみです。
コントロールのrendererでは apiVersion: 4 も有効ですが、ライブラリの初期化が受け付けるのは 2 のみです。
修正方針
1. 引数なし
問題: Lib.init() が引数なしで呼び出されている。
// 修正前 — no-deprecated-api をトリガー
sap.ui.define([
"sap/ui/core/Lib"
], function(Library) {
"use strict";
Library.init();
});
修正: apiVersion: 2 を含むオブジェクト引数を追加します。
// 修正後
sap.ui.define([
"sap/ui/core/Lib"
], function(Library) {
"use strict";
Library.init({
apiVersion: 2
});
});
2. apiVersion のないオブジェクト引数
問題: Lib.init() にオブジェクトは渡されているが、apiVersion プロパティがない。
// 修正前 — no-deprecated-api をトリガー
Library.init({
name: "my.lib",
dependencies: ["sap.ui.core", "sap.m"]
});
修正: オブジェクトの最初のプロパティとして apiVersion: 2 を追加します。
// 修正後
Library.init({
apiVersion: 2,
name: "my.lib",
dependencies: ["sap.ui.core", "sap.m"]
});
3. apiVersion が数値でない
問題: apiVersion が数値リテラルではなく文字列になっている。
// 修正前 — no-deprecated-api をトリガー
Library.init({
apiVersion: "2"
});
修正: 数値リテラル 2 に変更します。
// 修正後
Library.init({
apiVersion: 2
});
4. apiVersion の値が誤っている
問題: apiVersion は数値だが 2 ではない。
// 修正前 — no-deprecated-api をトリガー
Library.init({
apiVersion: 1
});
修正: 2 に変更します。
// 修正後
Library.init({
apiVersion: 2
});
5. ブラケット記法によるアクセス
init をブラケット記法で呼び出している場合も同様のパターンが適用されます:
// 修正前
Library["init"]({
apiVersion: 1,
dependencies: ["sap.ui.core"]
});
// 修正後
Library["init"]({
apiVersion: 2,
dependencies: ["sap.ui.core"]
});
6. 分割代入・変数代入による init
linterは以下のパターンも検出します:
// 変数代入
const LibInit = Library.init;
LibInit({ apiVersion: 1 });
// 分割代入
const {init} = Library;
init({ apiVersion: 1 });
// リネームあり分割代入
const {init: libInit} = Library;
libInit({ apiVersion: 1 });
修正: 各呼び出し箇所で apiVersion を 2 に変更します。
Enum の DataType.registerEnum へのモダナイゼーション
Lib.init() を apiVersion: 2 にアップグレードする際、グローバル名前空間にenumを定義する旧来の方法は機能しなくなります。
コントロールのメタデータで使用される型文字列を明示的に登録しないと、フレームワークが型を解決できず、型チェックがサイレントにスキップされることで XSS脆弱性 につながる恐れがあります。
このskillは、library.js 内の 既存のenum定義を検出し、DataType.registerEnum を用いた形に モダナイズ する必要があります。
Enum の見つけ方
Lib.init()呼び出しのtypes配列を確認 — ライブラリが定義するすべての型(enum)の完全修飾名が列挙されています- グローバル名前空間への代入を検索 — 例: ライブラリ名前空間が
my.libであればmy.lib.ValueColor = { ... }のようなパターン - 文字列のキーと値のペアからなるプレーンオブジェクトとして定義された既存のenumオブジェクトを探す
モダナイゼーションの手順
-
Lib.init()の戻り値を変数に格納 (まだ行っていない場合):// 修正前 Library.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] }); // 修正後 var oThisLibrary = Library.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] }); -
enumの定義をグローバル名前空間からライブラリオブジェクトへ移動:
// 修正前 — グローバル名前空間 my.lib.ValueColor = { Color1: "Color1", Color2: "Color2" }; // 修正後 — ライブラリオブジェクト上 oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" }; -
各enum定義の直後に
DataType.registerEnum呼び出しを追加:oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" }; DataType.registerEnum("my.lib.ValueColor", oThisLibrary.ValueColor); -
sap/ui/base/DataTypeをsap.ui.defineの依存リストに追加 (未追加の場合):sap.ui.define([ "sap/ui/base/DataType", // これを追加 "sap/ui/core/Lib" ], function(DataType, Library) {
重要なルール
registerEnumの第1引数は、コントロールのメタデータでtypeとして使用される文字列と 完全に一致 しなければなりません(例:"my.lib.ValueColor")- すべてのenum定義は、ライブラリpreloadとの互換性のため
library.js内に留めてください types配列に列挙された各enumに対して、個別のregisterEnum呼び出しが必要です
完全な変換例
修正前
/*!
* ${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"
]
});
// グローバル名前空間に定義されたenum
my.lib.ValueColor = {
Good: "Good",
Critical: "Critical",
Error: "Error",
Neutral: "Neutral"
};
my.lib.StatusType = {
Active: "Active",
Inactive: "Inactive"
};
});
修正後
/*!
* ${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;
});
実装手順
-
--detailsオプション付きでlinterを実行し、追加コンテキストを取得:npx @ui5/linter --details -
library.jsファイルを読み込み、以下を特定:Lib.init()/Library.init()の呼び出しと現在の引数- 戻り値が変数に格納されているかどうか
- enumの定義(
types配列とグローバル名前空間への代入を確認)
-
apiVersionを修正:- 引数なしの場合:
{ apiVersion: 2 }を追加 apiVersionのないオブジェクトの場合: 最初のプロパティとしてapiVersion: 2を追加- 値または型が誤っている場合: 数値の
2に変更
- 引数なしの場合:
-
enumのモダナイゼーション(enumが存在する場合):
Lib.init()の戻り値を変数に格納(未実施の場合)- 各enumをグローバル名前空間からライブラリオブジェクトへ移動
- 各enumに対して
DataType.registerEnum呼び出しを追加 - 依存リストに
sap/ui/base/DataTypeを追加
-
モジュールがライブラリオブジェクトを返すことを確認 (
return oThisLibrary;) -
linterを再実行して修正を検証
注意事項
Lib.init()に指定できるのはapiVersion: 2のみです —apiVersion: 4は無効です(rendererにのみ使用可)apiVersionは文字列ではなく数値リテラルでなければなりません- enumを持たないライブラリでは
apiVersion: 2の修正のみ必要です(DataType.registerEnumは不要) - ライブラリが既にinitの戻り値を返しており、グローバルなenum定義もない場合は、
apiVersionの修正のみ必要です - init呼び出し内の
types配列が、registerEnum呼び出しが必要なenumを特定するための手がかりになります
関連 Skill
- fix-control-renderer: rendererの
apiVersionに関する問題(rendererオブジェクトへのapiVersion: 2または4の欠落)向け — ライブラリinitのapiVersionとは異なります - fix-pseudo-modules: コンシューマコードがenum疑似モジュール(例:
sap.ui.require(["my/lib/ValueColor"]))経由でインポートしている場合は、fix-pseudo-modulesを使用してライブラリモジュールのインポートに変換してください - fix-js-globals:
library.jsにenum定義以外のグローバルアクセスパターン(例:sap.ui.getCore())がある場合は、fix-js-globalsを使用してください
原文(English)を表示
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 ID | Message Pattern | This Skill's Action |
|---|---|---|
no-deprecated-api |
Deprecated call to Library.init(). Use the {apiVersion: 2} parameter instead | Add apiVersion: 2 to the init call |
no-deprecated-api |
Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead | Add apiVersion: 2 to the init call |
no-deprecated-api |
Deprecated call to init(). Use the {apiVersion: 2} parameter instead | Add 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-api
library.js:11:2 error Deprecated call to Lib.init(). Use the {apiVersion: 2} parameter instead no-deprecated-api
library.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.
// Before — triggers no-deprecated-api
sap.ui.define([
"sap/ui/core/Lib"
], function(Library) {
"use strict";
Library.init();
});
Fix: Add an object argument with apiVersion: 2.
// After
sap.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.
// Before — triggers no-deprecated-api
Library.init({
name: "my.lib",
dependencies: ["sap.ui.core", "sap.m"]
});
Fix: Add apiVersion: 2 as the first property in the object.
// After
Library.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.
// Before — triggers no-deprecated-api
Library.init({
apiVersion: "2"
});
Fix: Change to numeric literal 2.
// After
Library.init({
apiVersion: 2
});
4. apiVersion Is Wrong Number
Problem: apiVersion is a number but not 2.
// Before — triggers no-deprecated-api
Library.init({
apiVersion: 1
});
Fix: Change to 2.
// After
Library.init({
apiVersion: 2
});
5. Element Access Syntax
The same patterns apply when init is called via bracket notation:
// Before
Library["init"]({
apiVersion: 1,
dependencies: ["sap.ui.core"]
});
// After
Library["init"]({
apiVersion: 2,
dependencies: ["sap.ui.core"]
});
6. Destructured or Assigned init
The linter also detects these patterns:
// Assignment
const LibInit = Library.init;
LibInit({ apiVersion: 1 });
// Destructuring
const {init} = Library;
init({ apiVersion: 1 });
// Destructuring with rename
const {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
- Check the
typesarray in theLib.init()call — it lists the fully qualified names of all types (enums) defined by the library - Search for global namespace assignments — e.g.
my.lib.ValueColor = { ... }wheremy.libis the library namespace - Look for existing enum objects defined as plain objects with string key-value pairs
Modernization Steps
-
Capture the return value of
Lib.init()in a variable if not already done:// Before Library.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] }); // After var oThisLibrary = Library.init({ name: "my.lib", apiVersion: 2, types: ["my.lib.ValueColor"] }); -
Move enum definitions from the global namespace to the library object:
// Before — global namespace my.lib.ValueColor = { Color1: "Color1", Color2: "Color2" }; // After — on library object oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" }; -
Add
DataType.registerEnumcall immediately after each enum definition:oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" }; DataType.registerEnum("my.lib.ValueColor", oThisLibrary.ValueColor); -
Add
sap/ui/base/DataTypeto thesap.ui.definedependency list if not already present:sap.ui.define([ "sap/ui/base/DataType", // Add this "sap/ui/core/Lib" ], function(DataType, Library) {
Key Rules
- The first argument to
registerEnummust exactly match the string used astypein control metadata (e.g."my.lib.ValueColor") - All enum definitions should stay in
library.jsfor library preload compatibility - Each enum listed in the
typesarray needs its ownregisterEnumcall
Complete Example
Before
/*!
* ${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
/*!
* ${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
-
Run linter with --details to get additional context:
npx @ui5/linter --details -
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
typesarray and global namespace assignments)
- The
-
Fix the apiVersion:
- If no arguments: add
{ apiVersion: 2 } - If object without apiVersion: add
apiVersion: 2as the first property - If apiVersion is wrong value or type: change to numeric
2
- If no arguments: add
-
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.registerEnumcall for each enum - Add
sap/ui/base/DataTypeto the dependency list
- Capture
-
Ensure the module returns the library object (
return oThisLibrary;) -
Verify the fix by re-running the linter
Notes
- Only
apiVersion: 2is valid forLib.init()—apiVersion: 4is NOT valid (that is for renderers only) apiVersionmust be a numeric literal, not a string- Libraries without enums only need the
apiVersion: 2fix (noDataType.registerEnumneeded) - If the library already returns the init result and has no global enum definitions, only the apiVersion fix is needed
- The
typesarray in the init call is your guide to which enums needregisterEnumcalls
Related Skills
- fix-control-renderer: For renderer
apiVersionissues (missingapiVersion: 2or4in 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
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。