@ai-i18n/eslint-plugin 1.0.0-alpha.5 → 1.0.0-alpha.7
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/README.md +94 -21
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +619 -71
- package/dist/index.js.map +1 -1
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
1
|
# @ai-i18n/eslint-plugin
|
|
2
2
|
|
|
3
|
-
用于提前报告无法被 Vite/Yuku
|
|
4
|
-
`virtual:ai-i18n` 的 `t` binding,以及 Vue/React 模式下
|
|
5
|
-
`t`。其他库或局部同名函数不受影响。
|
|
3
|
+
用于提前报告无法被 Vite/Yuku 静态提取、不符合推荐语法,或不会随语言切换刷新的 `t()`
|
|
4
|
+
用法。规则检查解析到 `virtual:ai-i18n` 的 `t` / Vue-only `tRef` binding,以及 Vue/React 模式下
|
|
5
|
+
`useI18n()` 解构或对象成员得到的 `t`。其他库或局部同名函数不受影响。
|
|
6
6
|
|
|
7
7
|
alpha 阶段请安装 `@ai-i18n/eslint-plugin@alpha`;peer 支持 ESLint 9 和 10。
|
|
8
8
|
|
|
9
9
|
## 按模式配置
|
|
10
10
|
|
|
11
|
-
显式 import
|
|
12
|
-
Vite `framework` 一致的 preset;它会声明对应只读全局,并启用静态参数报错与候选数量
|
|
13
|
-
警告:
|
|
11
|
+
显式 import 时,Vanilla / React 使用 `recommended`,Vue 使用覆盖 SFC 的 `vue`:
|
|
14
12
|
|
|
15
13
|
```js
|
|
16
14
|
import aiI18n from '@ai-i18n/eslint-plugin';
|
|
17
15
|
|
|
18
16
|
export default [
|
|
19
|
-
...aiI18n.configs.
|
|
17
|
+
...aiI18n.configs.recommended, // Vue 改用 .vue
|
|
20
18
|
];
|
|
21
19
|
```
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
启用 `aiI18n({ autoImport: true })` 时,改用与 Vite 框架模式一致的自动导入 preset:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
export default [
|
|
25
|
+
...aiI18n.configs['vanilla-auto-import'],
|
|
26
|
+
// Vue:...aiI18n.configs['vue-auto-import']
|
|
27
|
+
// React:...aiI18n.configs['react-auto-import']
|
|
28
|
+
];
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
只有 `*-auto-import` preset 会声明 Runtime 全局。所有 preset 都声明只读的
|
|
24
32
|
`defineI18nMessages` 编译宏。
|
|
25
33
|
|
|
26
34
|
## Vue SFC
|
|
27
35
|
|
|
28
36
|
`.vue` 是可选文件格式。Vue 项目应先通过 `eslint-plugin-vue` 配置
|
|
29
|
-
`vue-eslint-parser
|
|
37
|
+
`vue-eslint-parser`,再按 Vite 的 `autoImport` 值选择 preset:
|
|
30
38
|
|
|
31
39
|
```js
|
|
32
40
|
import aiI18n from '@ai-i18n/eslint-plugin';
|
|
@@ -41,22 +49,64 @@ export default [
|
|
|
41
49
|
parserOptions: { parser: tseslint.parser },
|
|
42
50
|
},
|
|
43
51
|
},
|
|
44
|
-
...aiI18n.configs.vue,
|
|
52
|
+
...aiI18n.configs.vue, // autoImport: false
|
|
53
|
+
// ...aiI18n.configs['vue-auto-import'], // autoImport: true
|
|
45
54
|
];
|
|
46
55
|
```
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
`
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
不会安装到 React/Vanilla 项目。
|
|
57
|
+
两个 Vue preset 都复用宿主的 Vue parser,并启用四条适用规则。语义分析使用
|
|
58
|
+
Vue 项目已有的 `vue/compiler-sfc` Node 入口,与 Vite 提取器复用相同分析语义和
|
|
59
|
+
source-map 映射,覆盖 `<script>`、`<script setup>`、模板插值和指令表达式。Vue、
|
|
60
|
+
TypeScript 与 SFC 编译相关依赖均为可选 peer,不会安装到 React/Vanilla 项目。
|
|
53
61
|
|
|
54
62
|
Vue preset 同时覆盖 Vue JSX/TSX,但宿主仍需用 `@vitejs/plugin-vue-jsx` 编译这些文件。
|
|
55
|
-
|
|
63
|
+
同一个 Vite build 不支持两种框架模式混用。
|
|
64
|
+
|
|
65
|
+
## 生命周期检查
|
|
66
|
+
|
|
67
|
+
静态提取成功不代表字符串会自动刷新。所有 preset 都启用
|
|
68
|
+
`ai-i18n/no-eager-translation`:在模块或 `<script setup>` 初始化期间保存 `t()` 结果会
|
|
69
|
+
收到 warning。Vue SFC 的 `export default { setup() {} }`,以及 `.vue` / `.ts` / `.tsx`
|
|
70
|
+
中从 `vue` 导入的 `defineComponent({ setup() {} })` 和函数签名也按一次性 setup
|
|
71
|
+
初始化检查。
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
export const label = t('保存'); // warning:只保存初始化时的译文
|
|
75
|
+
export const getLabel = () => t('保存'); // 允许:每次调用重新读取当前语言
|
|
76
|
+
export const label = tRef('保存'); // Vue:允许,返回响应式 ComputedRef
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`recommended`、`vue`、`vue-auto-import` 与 `react-auto-import` 还启用
|
|
80
|
+
`ai-i18n/no-unsubscribed-t`。Vue / React JSX 或 TSX 的组件渲染函数不能只调用 Runtime
|
|
81
|
+
顶层 `t`,应从 `useI18n()` 获取 `t` 来建立框架订阅。React Compiler 的 `"use memo"`
|
|
82
|
+
与 `"use no memo"` 都不会替代订阅。
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
function SaveButton() {
|
|
86
|
+
return <button>{t('保存')}</button>; // warning
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function SaveButton() {
|
|
90
|
+
const { t } = useI18n();
|
|
91
|
+
return <button>{t('保存')}</button>; // 允许
|
|
92
|
+
}
|
|
93
|
+
```
|
|
56
94
|
|
|
57
|
-
|
|
58
|
-
`
|
|
59
|
-
|
|
95
|
+
事件回调和普通延迟函数中的 Runtime `t` 允许,独立的 `console.log` / `warn` / `error` /
|
|
96
|
+
`info` / `debug` 调用也视为即时消费。其他未知调用仍会 warning。第一版不追踪跨函数、
|
|
97
|
+
跨文件或 `useMemo` / `useState` 等任意数据流,因此规则提示的是可以确定的常见问题,
|
|
98
|
+
不代表覆盖所有译文生命周期错误。两条规则都不提供自动修复。
|
|
99
|
+
|
|
100
|
+
四条规则可独立启用。分析无法启动时,同一文件只报告一次双语错误;官方 preset 由
|
|
101
|
+
`t-static-args` 优先报告,避免次级规则重复提示。
|
|
102
|
+
|
|
103
|
+
规则与 Vite 共用静态参数语义,包括从 `useI18n()` 获得的对象成员调用
|
|
104
|
+
`i18n.t()`、`i18n['t']()`、省略式 `t('source', undefined)` 和 tagged template。
|
|
105
|
+
Vue 模板必须在 `<script setup>` 中绑定 `useI18n()` 返回的 `t`;自动导入只省略 import,
|
|
106
|
+
不会自动合成 Hook。`vue-auto-import` 会把裸 template-only `t()` 作为 error;模板中已
|
|
107
|
+
绑定到 Runtime 顶层 `t` 的调用则由 `no-unsubscribed-t` warning。
|
|
108
|
+
在 template 或 JSX/TSX 渲染期间调用 `tRef()` 会重复创建 `computed`,同一规则会提示在
|
|
109
|
+
Vue setup 中只创建一次并使用返回的 Ref。
|
|
60
110
|
|
|
61
111
|
对象或数组成员只有在根集合由 `defineI18nMessages()` 标记后才属于推荐写法。字符串拼接、
|
|
62
112
|
逻辑表达式、`let` 文案、普通集合成员、`const tr = t`、命名空间调用、二次 Hook 解构、
|
|
@@ -70,14 +120,33 @@ import aiI18n from '@ai-i18n/eslint-plugin';
|
|
|
70
120
|
export default [
|
|
71
121
|
{
|
|
72
122
|
languageOptions: {
|
|
73
|
-
globals: {
|
|
123
|
+
globals: {
|
|
124
|
+
t: 'readonly',
|
|
125
|
+
tRef: 'readonly',
|
|
126
|
+
useI18n: 'readonly',
|
|
127
|
+
defineI18nMessages: 'readonly',
|
|
128
|
+
},
|
|
74
129
|
},
|
|
75
130
|
plugins: { 'ai-i18n': aiI18n },
|
|
76
131
|
rules: {
|
|
132
|
+
'ai-i18n/no-eager-translation': [
|
|
133
|
+
'warn',
|
|
134
|
+
{
|
|
135
|
+
autoImport: ['t', 'tRef', 'useI18n'],
|
|
136
|
+
tsconfigPath: './tsconfig.json',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
'ai-i18n/no-unsubscribed-t': [
|
|
140
|
+
'warn',
|
|
141
|
+
{
|
|
142
|
+
autoImport: ['t', 'tRef', 'useI18n'],
|
|
143
|
+
tsconfigPath: './tsconfig.json',
|
|
144
|
+
},
|
|
145
|
+
],
|
|
77
146
|
'ai-i18n/static-candidate-limit': [
|
|
78
147
|
'warn',
|
|
79
148
|
{
|
|
80
|
-
autoImport:
|
|
149
|
+
autoImport: ['t', 'tRef', 'useI18n'],
|
|
81
150
|
tsconfigPath: './tsconfig.json',
|
|
82
151
|
maxStaticCandidates: 2_000,
|
|
83
152
|
},
|
|
@@ -85,6 +154,7 @@ export default [
|
|
|
85
154
|
'ai-i18n/t-static-args': [
|
|
86
155
|
'error',
|
|
87
156
|
{
|
|
157
|
+
autoImport: ['t', 'tRef', 'useI18n'],
|
|
88
158
|
tsconfigPath: './tsconfig.json',
|
|
89
159
|
},
|
|
90
160
|
],
|
|
@@ -93,6 +163,9 @@ export default [
|
|
|
93
163
|
];
|
|
94
164
|
```
|
|
95
165
|
|
|
166
|
+
上例匹配 Vue 模式;React 应移除 `tRef`,Vanilla 只使用 `autoImport: ['t']`,并声明对应的
|
|
167
|
+
顶层 Runtime globals。日常接入优先使用预设,避免 Vite 与 ESLint 的 API 集合不一致。
|
|
168
|
+
|
|
96
169
|
`ai-i18n/static-candidate-limit` 默认在单个 `t()` 的 source 与 options 组合超过 1000 个
|
|
97
170
|
时警告。`maxStaticCandidates` 必须是正整数,只改变 ESLint 的提示阈值;Vite 提取不设
|
|
98
171
|
上限,也没有对应插件选项。
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { ESLint, Rule } from "eslint";
|
|
2
|
+
//#region src/rules/no-eager-translation.d.ts
|
|
3
|
+
declare const noEagerTranslation: Rule.RuleModule;
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/rules/no-unsubscribed-t.d.ts
|
|
6
|
+
declare const noUnsubscribedT: Rule.RuleModule;
|
|
7
|
+
//#endregion
|
|
2
8
|
//#region src/rules/static-candidate-limit.d.ts
|
|
3
9
|
declare const staticCandidateLimit: Rule.RuleModule;
|
|
4
10
|
//#endregion
|
|
@@ -8,5 +14,5 @@ declare const tStaticArgs: Rule.RuleModule;
|
|
|
8
14
|
//#region src/index.d.ts
|
|
9
15
|
declare const plugin: ESLint.Plugin;
|
|
10
16
|
//#endregion
|
|
11
|
-
export { plugin as default, staticCandidateLimit, tStaticArgs };
|
|
17
|
+
export { plugin as default, noEagerTranslation, noUnsubscribedT, staticCandidateLimit, tStaticArgs };
|
|
12
18
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/rules/static-candidate-limit.ts","../src/rules/t-static-args.ts","../src/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/rules/no-eager-translation.ts","../src/rules/no-unsubscribed-t.ts","../src/rules/static-candidate-limit.ts","../src/rules/t-static-args.ts","../src/index.ts"],"mappings":";;cAea,oBAAoB,KAAK;;;cCOzB,iBAAiB,KAAK;;;cCTtB,sBAAsB,KAAK;;;cC6C3B,aAAa,KAAK;;;cC/CzB,QAAQ,OAAO"}
|