-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
266 lines (240 loc) · 8.47 KB
/
index.js
File metadata and controls
266 lines (240 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* smarthr-ui のバージョン間移行を支援する自動修正ルール
*
* このルールは一時的に使用するもので、移行完了後は無効化してください。
*
* 使用例:
* {
* "rules": {
* "smarthr/autofixer-smarthr-ui-migration": ["error", { "from": "90", "to": "91" }]
* }
* }
*
* 複数バージョンをまたぐ移行も可能です(例: 90→93)。
* この場合、存在する移行ルール(v90→v91など)を自動的に適用し、
* 実装されていないバージョンについては警告を表示します。
*/
const v90ToV91 = require('./versions/v90-to-v91/index')
const v91ToV92 = require('./versions/v91-to-v92/index')
const v92ToV93 = require('./versions/v92-to-v93/index')
const v93ToV94 = require('./versions/v93-to-v94/index')
const v94ToV95 = require('./versions/v94-to-v95/index')
// サポートしているバージョン間の移行モジュール
const VERSION_MODULES = {
'v90-v91': v90ToV91,
'v91-v92': v91ToV92,
'v92-v93': v92ToV93,
'v93-v94': v93ToV94,
'v94-v95': v94ToV95,
}
module.exports = {
meta: {
type: 'problem',
fixable: 'code',
schema: [
{
type: 'object',
properties: {
from: {
type: 'string',
pattern: '^[0-9]+$',
},
to: {
type: 'string',
pattern: '^[0-9]+$',
},
smarthrUiAlias: {
type: 'string',
},
},
required: ['from', 'to'],
additionalProperties: false,
},
],
messages: {
missingOptions: 'オプションで from と to を指定してください。例: { "from": "90", "to": "91" }',
unsupportedVersion: 'サポートされていないバージョンです: {{from}} to {{to}}',
conflictingMigration: 'v{{from}}→v{{to}}の一気実行はコンポーネント名の衝突により正しく動作しません。段階的に実行してください: 1. { "from": "{{from}}", "to": "{{middle}}" } を実行 2. { "from": "{{middle}}", "to": "{{to}}" } を実行',
skippedVersion: 'v{{version}} の自動修正ルールが実装されていません。変更内容は https://github.com/kufu/smarthr-ui/releases から対応するversionの情報を確認してください',
...v90ToV91.messages,
...v91ToV92.messages,
...v92ToV93.messages,
...v93ToV94.messages,
...v94ToV95.messages,
},
},
create(context) {
const options = context.options[0]
// オプション必須チェック
if (!options || !options.from || !options.to) {
return {
Program(node) {
context.report({
node,
messageId: 'missingOptions',
})
},
}
}
const { from, to } = options
const sourceCode = context.getSourceCode()
// バージョン間の移行パスを生成
const migrationResult = getMigrationPath(from, to)
if (!migrationResult) {
// サポートされていないバージョン
return {
Program(node) {
context.report({
node,
messageId: 'unsupportedVersion',
data: { from, to },
})
},
}
}
// コンポーネント名衝突の検出
if (migrationResult.conflict) {
return {
Program(node) {
context.report({
node,
messageId: 'conflictingMigration',
data: migrationResult.conflictData,
})
},
}
}
const { path, skipped } = migrationResult
// 各ステップのチェッカーを収集してマージ
// 例: v90→v92 なら [v90→v91のチェッカー] を収集
const checkersList = path.map((stepKey) => {
const module = VERSION_MODULES[stepKey]
return module.createCheckers(context, sourceCode, options)
})
const mergedCheckers = mergeCheckers(checkersList)
// スキップされたバージョンがある場合は警告を追加
// 例: v90→v93 で v92 の移行ルールがない場合、v92 について警告
if (skipped.length > 0) {
addSkippedVersionWarnings(mergedCheckers, context, skipped)
}
return mergedCheckers
},
}
/**
* バージョン間の移行パスを生成する
*
* @param {string} from - 移行元バージョン(例: "90")
* @param {string} to - 移行先バージョン(例: "91")
* @returns {{ path: string[], skipped: number[], conflict?: boolean, conflictData?: object } | null} 移行パス情報、または無効な場合はnull
*
* @example
* getMigrationPath('90', '91')
* // => { path: ['v90-v91'], skipped: [] }
*
* @example
* getMigrationPath('90', '93')
* // 92のモジュールがない場合
* // => { path: ['v90-v91'], skipped: [92, 93] }
*
* @example
* getMigrationPath('90', '92')
* // v90-v91とv91-v92の組み合わせは衝突
* // => { path: ['v90-v91', 'v91-v92'], skipped: [], conflict: true, conflictData: {...} }
*/
function getMigrationPath(from, to) {
const fromNum = parseInt(from)
const toNum = parseInt(to)
if (fromNum >= toNum || isNaN(fromNum) || isNaN(toNum)) {
return null
}
const path = []
const skipped = []
// fromからtoまでの各ステップについて、移行モジュールが存在するかチェック
// 内部的にはvプレフィックス付きのキーで管理(ファイル名と統一)
for (let i = fromNum; i < toNum; i++) {
const stepKey = `v${i}-v${i + 1}`
if (VERSION_MODULES[stepKey]) {
path.push(stepKey)
} else {
// モジュールが存在しない = 自動修正ルールが未実装
// major versionなので必ず破壊的変更があるはずだが、ルールは未実装
skipped.push(i + 1)
}
}
// 適用可能なステップが1つもない場合は、完全にサポート外
if (path.length === 0) {
return null
}
// コンポーネント名衝突の検出
// v90→v91とv91→v92の両方が含まれる場合、ActionDialogの名前が衝突する
// (v90のActionDialog→ControlledActionDialog、v90のRemoteTriggerActionDialog→ActionDialog)
if (path.includes('v90-v91') && path.includes('v91-v92')) {
return {
path,
skipped,
conflict: true,
conflictData: {
from,
to,
middle: '91',
},
}
}
return { path, skipped }
}
/**
* 複数のチェッカーオブジェクトを1つにマージする
*
* 同じセレクターに対する複数のハンドラーがある場合、両方を実行するようにラップする。
*
* @param {Array<Object>} checkersList - チェッカーオブジェクトの配列
* @returns {Object} マージされたチェッカーオブジェクト
*/
function mergeCheckers(checkersList) {
const merged = {}
checkersList.forEach((checkers) => {
Object.keys(checkers).forEach((selector) => {
if (!merged[selector]) {
// 初めて見るセレクターはそのまま追加
merged[selector] = checkers[selector]
} else {
// 既に存在するセレクターの場合、両方のハンドラーを順次実行するようラップ
// 例: v90→v91とv91→v92で同じImportDeclarationを処理する場合
const existing = merged[selector]
const additional = checkers[selector]
merged[selector] = function (node) {
existing.call(this, node)
additional.call(this, node)
}
}
})
})
return merged
}
/**
* スキップされたバージョンについての警告を追加する
*
* Programノードのハンドラーをラップして、スキップされた各バージョンについて
* 警告メッセージを表示する。
*
* @param {Object} checkers - チェッカーオブジェクト(この関数内で直接変更される)
* @param {Object} context - ESLintのcontext
* @param {number[]} skippedVersions - スキップされたバージョン番号の配列(例: [92, 93])
*/
function addSkippedVersionWarnings(checkers, context, skippedVersions) {
const existingProgramHandler = checkers.Program
checkers.Program = function (node) {
// 既存のProgramハンドラーがあれば先に実行
if (existingProgramHandler) {
existingProgramHandler.call(this, node)
}
// スキップされた各バージョンについて警告を表示
skippedVersions.forEach((versionNumber) => {
context.report({
node,
messageId: 'skippedVersion',
data: { version: `v${versionNumber}` },
})
})
}
}