feima-shortcuts 0.3.0-beta.2 → 0.3.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feima-shortcuts",
3
- "version": "0.3.0-beta.2",
3
+ "version": "0.3.0-beta.3",
4
4
  "description": "快捷指令",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -29,7 +29,6 @@ function loadLocaleKeys(localePath) {
29
29
  ts.isExportAssignment(node) &&
30
30
  ts.isObjectLiteralExpression(node.expression)
31
31
  ) {
32
- // eval 需要谨慎,这里保持原逻辑
33
32
  obj = eval(`(${node.expression.getText()})`)
34
33
  }
35
34
  ts.forEachChild(node, visit)
@@ -111,24 +110,25 @@ function checkPageLocale(pageDir) {
111
110
  const localeKeys = loadLocaleKeys(localePath)
112
111
  const usedKeys = extractUsedKeys(code)
113
112
 
114
- // locale.ts 中定义但未使用的 key
113
+ // 检测代码用到但 locale.ts 未定义的 key
114
+ const undefinedKeys = Array.from(usedKeys).filter((k) => !localeKeys.includes(k))
115
+ if (undefinedKeys.length) {
116
+ console.log(`\n❗ 页面中使用了 locale.ts 中不存在的 key:`)
117
+ console.log(` ${indexVue}`)
118
+ undefinedKeys.forEach((k) => console.log(` - ${k}`))
119
+ }
120
+
121
+ // 检测 locale.ts 里未使用的 key
115
122
  const unused = localeKeys.filter((k) => !usedKeys.has(k))
123
+
116
124
  if (unused.length) {
117
125
  console.log(`\n❌ 页面未使用 locale key`)
118
126
  console.log(` ${localePath}`)
119
127
  unused.forEach((k) => console.log(` - ${k}`))
120
- }
121
-
122
- // 页面中用到了 locale.ts 中未定义的 key
123
- const undefinedKeys = [...usedKeys].filter((k) => !localeKeys.includes(k))
124
- if (undefinedKeys.length) {
125
- console.log(`\n❌ 页面使用了 locale.ts 中未定义的 key`)
126
- console.log(` ${indexVue}`)
127
- undefinedKeys.forEach((k) => console.log(` - ${k}`))
128
128
  return true
129
129
  }
130
130
 
131
- return unused.length > 0
131
+ return false
132
132
  }
133
133
 
134
134
  /* 组件 locale 检测 */
@@ -147,6 +147,8 @@ function checkComponentLocale(componentDir) {
147
147
  const invalidFiles = []
148
148
  const skippedFiles = []
149
149
 
150
+ const undefinedKeyMap = new Map() // 文件 -> [key]
151
+
150
152
  vueFiles.forEach((file) => {
151
153
  const code = fs.readFileSync(file, 'utf-8')
152
154
  const localeImport = detectLocaleImport(code)
@@ -162,12 +164,18 @@ function checkComponentLocale(componentDir) {
162
164
  return
163
165
  }
164
166
 
165
- extractUsedKeys(code).forEach((k) => usedKeys.add(k))
167
+ const keys = extractUsedKeys(code)
168
+ keys.forEach((k) => usedKeys.add(k))
169
+
170
+ // 检测未定义 key
171
+ const undefinedKeys = Array.from(keys).filter((k) => !localeKeys.includes(k))
172
+ if (undefinedKeys.length) {
173
+ undefinedKeyMap.set(file, undefinedKeys)
174
+ }
166
175
  })
167
176
 
168
177
  let hasError = false
169
178
 
170
- // locale.ts 中定义但未使用的 key
171
179
  const unused = localeKeys.filter((k) => !usedKeys.has(k))
172
180
  if (unused.length) {
173
181
  hasError = true
@@ -176,15 +184,6 @@ function checkComponentLocale(componentDir) {
176
184
  unused.forEach((k) => console.log(` - ${k}`))
177
185
  }
178
186
 
179
- // 组件代码中使用但 locale.ts 未定义的 key
180
- const undefinedKeys = [...usedKeys].filter((k) => !localeKeys.includes(k))
181
- if (undefinedKeys.length) {
182
- hasError = true
183
- console.log(`\n❌ 组件使用了 locale.ts 中未定义的 key`)
184
- console.log(` ${componentDir}`)
185
- undefinedKeys.forEach((k) => console.log(` - ${k}`))
186
- }
187
-
188
187
  if (invalidFiles.length) {
189
188
  hasError = true
190
189
  console.log(`\n🚫 组件存在错误的 locale 引入方式`)
@@ -194,12 +193,21 @@ function checkComponentLocale(componentDir) {
194
193
  })
195
194
  }
196
195
 
197
- // if (skippedFiles.length) {
198
- // console.log(`\n⚠️ 组件中以下文件未绑定本地 locale(已跳过)`)
199
- // skippedFiles.forEach((f) =>
200
- // console.log(` ${path.relative(process.cwd(), f)}`)
201
- // )
202
- // }
196
+ if (skippedFiles.length) {
197
+ console.log(`\n⚠️ 组件中以下文件未绑定本地 locale(已跳过)`)
198
+ skippedFiles.forEach((f) =>
199
+ console.log(` ${path.relative(process.cwd(), f)}`)
200
+ )
201
+ }
202
+
203
+ if (undefinedKeyMap.size) {
204
+ hasError = true
205
+ console.log(`\n❗ 组件中存在未定义的 locale key:`)
206
+ for (const [file, keys] of undefinedKeyMap.entries()) {
207
+ console.log(` ${path.relative(process.cwd(), file)}`)
208
+ keys.forEach((k) => console.log(` - ${k}`))
209
+ }
210
+ }
203
211
 
204
212
  return hasError
205
213
  }
@@ -231,3 +239,7 @@ function run() {
231
239
  }
232
240
 
233
241
  exports.run = run
242
+
243
+ if (require.main === module) {
244
+ run()
245
+ }