Library.init() の API バージョン問題を修正し、列挙型(異なる値の一覧)を DataType.registerEnum に更新するスキルです。 次のような場合に使用します: - リンター(コード品質チェック機能)が `no-deprecated-api` エラーを出し、「{apiVersion: 2} パラメータを代わりに使ってください」というメッセージが表示される library.js ファイルで以下のエラーが出ている場合にも対応: - 「Deprecated call to(廃止予定の呼び出し)」 - 「Lib.init」「Library.init」 - 「{apiVersion: 2} parameter」 このスキルは、Library.init() の呼び出しに apiVersion: 2 を追加し、列挙型の定義を DataType.registerEnum を使った形式に更新します。apiVersion: 2 を使う場合、型の検証に DataType.registerEnum が必須となるため、この更新が必要です。
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.
このskillは、UI5 linterが検出する Library.init() / Lib.init() 呼び出しへの apiVersion: 2 の欠落を修正し、apiVersion: 2 使用時に必須となる DataType.registerEnum を用いたenum定義のモダナイゼーションを行います。
| 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
sap/ui/core/Lib.init() はUI5ライブラリをフレームワークに登録し、名前・依存関係・コントロール・エレメント・型(enum)・インターフェースなどのメタデータを宣言します。
apiVersion: 2 パラメータは、そのライブラリがモダンな初期化パターンを使用していることを示します。
重要: Lib.init() に指定できるのは apiVersion: 2 のみです。
コントロールのrendererでは apiVersion: 4 も有効ですが、ライブラリの初期化が受け付けるのは 2 のみです。
問題: 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
});
});
問題: 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"]
});
問題: apiVersion が数値リテラルではなく文字列になっている。
// 修正前 — no-deprecated-api をトリガー
Library.init({
apiVersion: "2"
});
修正: 数値リテラル 2 に変更します。
// 修正後
Library.init({
apiVersion: 2
});
問題: apiVersion は数値だが 2 ではない。
// 修正前 — no-deprecated-api をトリガー
Library.init({
apiVersion: 1
});
修正: 2 に変更します。
// 修正後
Library.init({
apiVersion: 2
});
init をブラケット記法で呼び出している場合も同様のパターンが適用されます:
// 修正前
Library["init"]({
apiVersion: 1,
dependencies: ["sap.ui.core"]
});
// 修正後
Library["init"]({
apiVersion: 2,
dependencies: ["sap.ui.core"]
});
linterは以下のパターンも検出します:
// 変数代入
const LibInit = Library.init;
LibInit({ apiVersion: 1 });
// 分割代入
const {init} = Library;
init({ apiVersion: 1 });
// リネームあり分割代入
const {init: libInit} = Library;
libInit({ apiVersion: 1 });
修正: 各呼び出し箇所で apiVersion を 2 に変更します。
Lib.init() を apiVersion: 2 にアップグレードする際、グローバル名前空間にenumを定義する旧来の方法は機能しなくなります。
コントロールのメタデータで使用される型文字列を明示的に登録しないと、フレームワークが型を解決できず、型チェックがサイレントにスキップされることで XSS脆弱性 につながる恐れがあります。
このskillは、library.js 内の 既存のenum定義を検出し、DataType.registerEnum を用いた形に モダナイズ する必要があります。
Lib.init() 呼び出しの types 配列を確認 — ライブラリが定義するすべての型(enum)の完全修飾名が列挙されていますmy.lib であれば my.lib.ValueColor = { ... } のようなパターン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")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() の呼び出しと現在の引数types 配列とグローバル名前空間への代入を確認)apiVersion を修正:
{ apiVersion: 2 } を追加apiVersion のないオブジェクトの場合: 最初のプロパティとして apiVersion: 2 を追加2 に変更enumのモダナイゼーション(enumが存在する場合):
Lib.init() の戻り値を変数に格納(未実施の場合)DataType.registerEnum 呼び出しを追加sap/ui/base/DataType を追加モジュールがライブラリオブジェクトを返すことを確認 (return oThisLibrary;)
linterを再実行して修正を検証
Lib.init() に指定できるのは apiVersion: 2 のみです — apiVersion: 4 は無効です(rendererにのみ使用可)apiVersion は文字列ではなく数値リテラルでなければなりませんapiVersion: 2 の修正のみ必要です(DataType.registerEnum は不要)apiVersion の修正のみ必要ですtypes 配列が、registerEnum 呼び出しが必要なenumを特定するための手がかりになりますapiVersion に関する問題(rendererオブジェクトへの apiVersion: 2 または 4 の欠落)向け — ライブラリinitの apiVersion とは異なりますsap.ui.require(["my/lib/ValueColor"]))経由でインポートしている場合は、fix-pseudo-modulesを使用してライブラリモジュールのインポートに変換してくださいlibrary.js にenum定義以外のグローバルアクセスパターン(例: sap.ui.getCore())がある場合は、fix-js-globalsを使用してください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.
| 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) |
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
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.
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
});
});
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"]
});
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
});
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
});
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"]
});
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.
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.
types array in the Lib.init() call — it lists the fully qualified names of all types (enums) defined by the librarymy.lib.ValueColor = { ... } where my.lib is the library namespaceCapture 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.registerEnum call immediately after each enum definition:
oThisLibrary.ValueColor = { Color1: "Color1", Color2: "Color2" };
DataType.registerEnum("my.lib.ValueColor", oThisLibrary.ValueColor);
Add sap/ui/base/DataType to the sap.ui.define dependency list if not already present:
sap.ui.define([
"sap/ui/base/DataType", // Add this
"sap/ui/core/Lib"
], function(DataType, Library) {
registerEnum must exactly match the string used as type in control metadata (e.g. "my.lib.ValueColor")library.js for library preload compatibilitytypes array needs its own registerEnum call/*!
* ${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"
};
});
/*!
* ${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;
});
Run linter with --details to get additional context:
npx @ui5/linter --details
Read the library.js file and identify:
Lib.init() / Library.init() call and its current argumentstypes array and global namespace assignments)Fix the apiVersion:
{ apiVersion: 2 }apiVersion: 2 as the first property2Modernize enums (if any exist):
Lib.init() return value in a variable if not already doneDataType.registerEnum call for each enumsap/ui/base/DataType to the dependency listEnsure the module returns the library object (return oThisLibrary;)
Verify the fix by re-running the linter
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 stringapiVersion: 2 fix (no DataType.registerEnum needed)types array in the init call is your guide to which enums need registerEnum callsapiVersion issues (missing apiVersion: 2 or 4 in renderer objects) — different from library init apiVersionsap.ui.require(["my/lib/ValueColor"])), use fix-pseudo-modules to convert them to library module importssap.ui.getCore()), use fix-js-globals for those原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。