eslint-plugin-light 1.0.20 → 1.0.22
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/CHANGELOG.md +15 -0
- package/README.md +213 -0
- package/lib/configs/recommended.js +1 -0
- package/lib/rules/classname-per-line.js +9 -6
- package/lib/rules/img-v-lazy.js +11 -4
- package/lib/rules/json-parse-try-catch.js +9 -3
- package/lib/rules/no-complex-key.js +6 -3
- package/lib/rules/no-complex-style-class.js +83 -0
- package/lib/rules/no-decimal-in-brackets.js +7 -2
- package/lib/rules/no-import-vant.js +1 -1
- package/lib/rules/no-js-file.js +1 -1
- package/lib/rules/no-multiple-script.js +2 -2
- package/lib/rules/no-plus-turn-number.js +6 -3
- package/lib/rules/no-src-imports-in-components.js +101 -0
- package/lib/rules/no-todo-comment.js +4 -4
- package/lib/rules/valid-file-name.js +1 -1
- package/lib/rules/valid-pmd-import.js +2 -2
- package/lib/rules/valid-vue-comp-import.js +1 -1
- package/lib/test/index.js +1 -0
- package/lib/test/no-complex-style-class.js +85 -0
- package/lib/utils/parser-services.js +26 -0
- package/package.json +7 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## <small>1.0.22 (2026-03-24)</small>
|
|
2
|
+
|
|
3
|
+
* feat: add no-complex-style-class rule ([2cc25ee](https://github.com/novlan1/plugin-light/commits/2cc25ee))
|
|
4
|
+
* feat: optmize eslint rules ([b3e85c6](https://github.com/novlan1/plugin-light/commits/b3e85c6))
|
|
5
|
+
* chore: update deps ([6828b0c](https://github.com/novlan1/plugin-light/commits/6828b0c))
|
|
6
|
+
* chore: update package.json ([338b104](https://github.com/novlan1/plugin-light/commits/338b104))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## <small>1.0.21 (2026-01-04)</small>
|
|
11
|
+
|
|
12
|
+
* feat(eslint-plugin-light): add no-src-imports-in-components rule ([b960951](https://github.com/novlan1/plugin-light/commits/b960951))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
1
16
|
## <small>1.0.20 (2025-12-18)</small>
|
|
2
17
|
|
|
3
18
|
* feat(project-config-pixui): add try-catch for version-info ([775fb7b](https://github.com/novlan1/plugin-light/commits/775fb7b))
|
package/README.md
CHANGED
|
@@ -657,6 +657,219 @@ module.exports = {
|
|
|
657
657
|
- 动态 `import()` 表达式
|
|
658
658
|
- `export ... from` 语句
|
|
659
659
|
|
|
660
|
+
### 4.18. no-src-imports-in-components
|
|
661
|
+
|
|
662
|
+
#### 4.18.1. 规则说明
|
|
663
|
+
|
|
664
|
+
禁止在指定目录下使用特定前缀的导入路径,强制使用相对路径。这个规则主要用于确保某些目录(如 `src/components`)中的代码可以被独立发布为 npm 包,不依赖项目的绝对路径导入。
|
|
665
|
+
|
|
666
|
+
#### 4.18.2. 为什么需要这个规则?
|
|
667
|
+
|
|
668
|
+
当你的项目中某些目录(如 `src/components`)需要被发布为独立的 npm 包时,这些目录中的代码不应该使用项目特定的绝对路径(如 `src/...`),而应该使用相对路径。这样可以确保:
|
|
669
|
+
|
|
670
|
+
1. 代码的可移植性
|
|
671
|
+
2. 包的独立性
|
|
672
|
+
3. 避免发布后的路径解析问题
|
|
673
|
+
|
|
674
|
+
#### 4.18.3. 配置选项
|
|
675
|
+
|
|
676
|
+
```typescript
|
|
677
|
+
{
|
|
678
|
+
restrictedPaths: string[], // 需要限制的目录路径列表
|
|
679
|
+
bannedPrefixes: string[], // 禁止使用的导入路径前缀列表
|
|
680
|
+
message?: string // 自定义错误消息(可选)
|
|
681
|
+
}
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
#### 4.18.4. 使用示例
|
|
685
|
+
|
|
686
|
+
##### 4.18.4.1. 基本用法(使用默认配置)
|
|
687
|
+
|
|
688
|
+
```typescript
|
|
689
|
+
// .eslintrc.js
|
|
690
|
+
module.exports = {
|
|
691
|
+
rules: {
|
|
692
|
+
'light/no-src-imports-in-components': 'error',
|
|
693
|
+
},
|
|
694
|
+
};
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
默认配置:
|
|
698
|
+
|
|
699
|
+
- `restrictedPaths`: `['src/components']`
|
|
700
|
+
- `bannedPrefixes`: `['src']`
|
|
701
|
+
|
|
702
|
+
##### 4.18.4.2. 自定义配置
|
|
703
|
+
|
|
704
|
+
```typescript
|
|
705
|
+
// .eslintrc.js
|
|
706
|
+
module.exports = {
|
|
707
|
+
rules: {
|
|
708
|
+
'light/no-src-imports-in-components': ['error', {
|
|
709
|
+
// 指定多个需要限制的目录
|
|
710
|
+
restrictedPaths: ['src/components', 'src/lib', 'packages/ui'],
|
|
711
|
+
|
|
712
|
+
// 指定多个禁止的导入前缀
|
|
713
|
+
bannedPrefixes: ['src', '@/', '~/'],
|
|
714
|
+
|
|
715
|
+
// 可选:自定义错误消息
|
|
716
|
+
message: '该目录将被发布为独立包,请使用相对路径导入',
|
|
717
|
+
}],
|
|
718
|
+
},
|
|
719
|
+
};
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
##### 4.18.4.3. 只检查特定目录
|
|
723
|
+
|
|
724
|
+
```typescript
|
|
725
|
+
// .eslintrc.js
|
|
726
|
+
module.exports = {
|
|
727
|
+
rules: {
|
|
728
|
+
'light/no-src-imports-in-components': ['error', {
|
|
729
|
+
restrictedPaths: ['src/components'],
|
|
730
|
+
bannedPrefixes: ['src'],
|
|
731
|
+
}],
|
|
732
|
+
},
|
|
733
|
+
};
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
#### 4.18.5. 错误示例
|
|
737
|
+
|
|
738
|
+
假设配置为:
|
|
739
|
+
|
|
740
|
+
```typescript
|
|
741
|
+
{
|
|
742
|
+
restrictedPaths: ['src/components'],
|
|
743
|
+
bannedPrefixes: ['src']
|
|
744
|
+
}
|
|
745
|
+
```
|
|
746
|
+
|
|
747
|
+
在 `src/components/Button/Button.tsx` 中:
|
|
748
|
+
|
|
749
|
+
```typescript
|
|
750
|
+
// ❌ 错误:使用了 src 开头的导入
|
|
751
|
+
import { back } from 'src/app/route/route';
|
|
752
|
+
import { utils } from 'src/utils/helper';
|
|
753
|
+
|
|
754
|
+
// ✅ 正确:使用相对路径
|
|
755
|
+
import { back } from '../../app/route/route';
|
|
756
|
+
import { utils } from '../../utils/helper';
|
|
757
|
+
|
|
758
|
+
// ✅ 正确:导入 npm 包
|
|
759
|
+
import React from 'react';
|
|
760
|
+
import { Button } from 'antd';
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
#### 4.18.6. 配置参数详解
|
|
764
|
+
|
|
765
|
+
##### 4.18.6.1. restrictedPaths
|
|
766
|
+
|
|
767
|
+
类型:`string[]`
|
|
768
|
+
默认值:`['src/components']`
|
|
769
|
+
|
|
770
|
+
指定需要应用此规则的目录列表。规则会检查文件路径是否包含这些目录。
|
|
771
|
+
|
|
772
|
+
示例:
|
|
773
|
+
|
|
774
|
+
```typescript
|
|
775
|
+
restrictedPaths: ['src/components', 'src/lib', 'packages/ui']
|
|
776
|
+
```
|
|
777
|
+
|
|
778
|
+
##### 4.18.6.2. bannedPrefixes
|
|
779
|
+
|
|
780
|
+
类型:`string[]`
|
|
781
|
+
默认值:`['src']`
|
|
782
|
+
|
|
783
|
+
指定禁止使用的导入路径前缀列表。任何以这些前缀开头的导入都会被标记为错误。
|
|
784
|
+
|
|
785
|
+
示例:
|
|
786
|
+
|
|
787
|
+
```typescript
|
|
788
|
+
bannedPrefixes: ['src', '@/', '~/']
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
##### 4.18.6.3. message
|
|
792
|
+
|
|
793
|
+
类型:`string`
|
|
794
|
+
默认值:无(使用默认错误消息)
|
|
795
|
+
|
|
796
|
+
自定义错误消息。如果不设置,将使用默认消息模板。
|
|
797
|
+
|
|
798
|
+
示例:
|
|
799
|
+
|
|
800
|
+
```typescript
|
|
801
|
+
message: '该目录将被发布为独立的 npm 包,请使用相对路径导入'
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
### 4.19. no-complex-style-class
|
|
805
|
+
|
|
806
|
+
禁止在 Vue 模板的 `:style` 或 `:class` 绑定中**直接**使用函数调用、模板字符串等复杂表达式。
|
|
807
|
+
|
|
808
|
+
背景:
|
|
809
|
+
|
|
810
|
+
> uni-app Vue2 小程序中,`:style` 和 `:class` 的**顶层表达式**不支持函数调用和模板字符串等语法。例如 `:style="tool._style(xxx)"` 或 `:class="\`${prefix}-item\`"` 在小程序中会解析失败。
|
|
811
|
+
>
|
|
812
|
+
> 应使用计算属性或字符串拼接 `'' + xxx` 代替。
|
|
813
|
+
>
|
|
814
|
+
> 注意:在数组元素、对象属性值、条件表达式分支中使用函数调用是**允许**的,例如 `:class="[getClass(), 'base']"` 和 `:style="flag ? getStyle() : ''"` 在小程序中可以正常工作。
|
|
815
|
+
|
|
816
|
+
Usage:
|
|
817
|
+
|
|
818
|
+
```js
|
|
819
|
+
// .eslintrc.js
|
|
820
|
+
|
|
821
|
+
module.exports = {
|
|
822
|
+
plugins: [
|
|
823
|
+
'light',
|
|
824
|
+
],
|
|
825
|
+
rules: {
|
|
826
|
+
'light/no-complex-style-class': 2,
|
|
827
|
+
},
|
|
828
|
+
}
|
|
829
|
+
```
|
|
830
|
+
|
|
831
|
+
错误示例:
|
|
832
|
+
|
|
833
|
+
```html
|
|
834
|
+
<!-- ❌ 顶层函数调用 -->
|
|
835
|
+
<div :style="tool._style(xxx)" />
|
|
836
|
+
<div :class="getClass(item)" />
|
|
837
|
+
|
|
838
|
+
<!-- ❌ 顶层模板字符串 -->
|
|
839
|
+
<div :style="`color: ${color}`" />
|
|
840
|
+
<div :class="`${prefix}-item`" />
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
正确示例:
|
|
844
|
+
|
|
845
|
+
```html
|
|
846
|
+
<!-- ✅ 变量引用 -->
|
|
847
|
+
<div :style="myStyle" />
|
|
848
|
+
<div :class="item.className" />
|
|
849
|
+
|
|
850
|
+
<!-- ✅ 对象表达式(值为变量) -->
|
|
851
|
+
<div :style="{ color: myColor }" />
|
|
852
|
+
<div :class="{ active: isActive }" />
|
|
853
|
+
|
|
854
|
+
<!-- ✅ 字面量数组 -->
|
|
855
|
+
<div :class="['a', 'b']" />
|
|
856
|
+
|
|
857
|
+
<!-- ✅ 条件表达式(值为变量) -->
|
|
858
|
+
<div :style="flag ? styleA : styleB" />
|
|
859
|
+
|
|
860
|
+
<!-- ✅ 数组元素中的函数调用 -->
|
|
861
|
+
<div :class="[utils.getClass(classPrefix, 'medium'), tClassImage]" />
|
|
862
|
+
|
|
863
|
+
<!-- ✅ 条件表达式分支中的函数调用 -->
|
|
864
|
+
<div :style="inChat ? imageStyle(item) : ''" />
|
|
865
|
+
|
|
866
|
+
<!-- ✅ 数组 + 条件混合 -->
|
|
867
|
+
<div :class="[classPrefix, inChat ? classPrefix + '--chatting' : '', getFileTypeClass(inChat, files)]" />
|
|
868
|
+
|
|
869
|
+
<!-- ✅ 对象属性值中的函数调用 -->
|
|
870
|
+
<div :style="{ color: getColor() }" />
|
|
871
|
+
```
|
|
872
|
+
|
|
660
873
|
## 5. 更新日志
|
|
661
874
|
|
|
662
875
|
[点此查看](./CHANGELOG.md)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
const DEFAULT_THRESHOLD = 3;
|
|
@@ -26,16 +26,20 @@ module.exports = {
|
|
|
26
26
|
], // 无配置选项
|
|
27
27
|
},
|
|
28
28
|
create(context) {
|
|
29
|
-
|
|
29
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
30
|
+
|
|
31
|
+
const parserServices = getParserServices(context);
|
|
32
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
30
37
|
'VAttribute[key.name=\'class\']'(node) {
|
|
31
38
|
if (!node.value || node.value.type !== 'VLiteral') return;
|
|
32
39
|
|
|
33
40
|
const options = context.options[0] || {};
|
|
34
41
|
const threshold = options.spelling || DEFAULT_THRESHOLD;
|
|
35
42
|
|
|
36
|
-
|
|
37
|
-
const sourceCode = context.getSourceCode();
|
|
38
|
-
|
|
39
43
|
const text = sourceCode.getText(node.value);
|
|
40
44
|
const classValue = node.value.value.trim();
|
|
41
45
|
|
|
@@ -56,7 +60,6 @@ module.exports = {
|
|
|
56
60
|
loc: node.value.loc,
|
|
57
61
|
message: 'CSS classes should be one per line',
|
|
58
62
|
fix(fixer) {
|
|
59
|
-
const sourceCode = context.getSourceCode();
|
|
60
63
|
const [startQuote, endQuote] = getQuoteChars(sourceCode, node.value);
|
|
61
64
|
|
|
62
65
|
const indentation = ' '.repeat(node.loc.start.column);
|
package/lib/rules/img-v-lazy.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
4
|
meta: {
|
|
@@ -12,7 +12,14 @@ module.exports = {
|
|
|
12
12
|
schema: [],
|
|
13
13
|
},
|
|
14
14
|
create(context) {
|
|
15
|
-
|
|
15
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
16
|
+
|
|
17
|
+
const parserServices = getParserServices(context);
|
|
18
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
16
23
|
'VElement[name=\'img\']'(node) {
|
|
17
24
|
const srcBinding = node.startTag.attributes.find(attr => attr.directive
|
|
18
25
|
&& attr.key.name
|
|
@@ -30,10 +37,10 @@ module.exports = {
|
|
|
30
37
|
message: 'Use v-lazy instead of :src for image loading',
|
|
31
38
|
fix(fixer) {
|
|
32
39
|
// 获取 :src 绑定的完整表达式文本
|
|
33
|
-
const srcText =
|
|
40
|
+
const srcText = sourceCode.getText(srcBinding.value.expression);
|
|
34
41
|
|
|
35
42
|
// 获取 :src 属性的完整文本
|
|
36
|
-
const fullSrcText =
|
|
43
|
+
const fullSrcText = sourceCode.getText(srcBinding);
|
|
37
44
|
|
|
38
45
|
// 计算替换文本
|
|
39
46
|
let replacement;
|
|
@@ -10,6 +10,9 @@ module.exports = {
|
|
|
10
10
|
description: desc,
|
|
11
11
|
category: 'Best Practices',
|
|
12
12
|
},
|
|
13
|
+
messages: {
|
|
14
|
+
requireTryCatch: desc,
|
|
15
|
+
},
|
|
13
16
|
},
|
|
14
17
|
schema: [{
|
|
15
18
|
type: 'object',
|
|
@@ -32,12 +35,12 @@ module.exports = {
|
|
|
32
35
|
const calleePropertyName = node.callee?.property?.name;
|
|
33
36
|
|
|
34
37
|
const subNode = node.arguments?.[0];
|
|
35
|
-
const
|
|
38
|
+
const subCalleeObjectName = subNode?.callee?.object?.name;
|
|
36
39
|
const subCalleePropertyName = subNode?.callee?.property?.name;
|
|
37
40
|
|
|
38
41
|
if (calleeObjectName === 'JSON'
|
|
39
42
|
&& calleePropertyName === 'parse'
|
|
40
|
-
&& (strict || !(
|
|
43
|
+
&& (strict || !(subCalleeObjectName === 'JSON' && subCalleePropertyName === 'stringify'))
|
|
41
44
|
) {
|
|
42
45
|
let p = node.parent;
|
|
43
46
|
let hasTryFlg = false;
|
|
@@ -48,7 +51,10 @@ module.exports = {
|
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
if (!hasTryFlg) {
|
|
51
|
-
context.report(
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
messageId: 'requireTryCatch',
|
|
57
|
+
});
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
},
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
|
+
|
|
1
3
|
module.exports = {
|
|
2
4
|
meta: {
|
|
3
5
|
type: 'problem',
|
|
@@ -14,16 +16,17 @@ module.exports = {
|
|
|
14
16
|
},
|
|
15
17
|
|
|
16
18
|
create(context) {
|
|
17
|
-
const fileName = context.getFilename();
|
|
19
|
+
const fileName = context.filename || context.getFilename();
|
|
18
20
|
if (!fileName.endsWith('.vue')) {
|
|
19
21
|
return {};
|
|
20
22
|
}
|
|
21
|
-
|
|
23
|
+
const parserServices = getParserServices(context);
|
|
24
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
22
25
|
return {
|
|
23
26
|
};
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
return
|
|
29
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
27
30
|
VAttribute(node) {
|
|
28
31
|
if (!node.key
|
|
29
32
|
|| node.key.type !== 'VDirectiveKey'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'problem',
|
|
6
|
+
schema: [],
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'vue模板中不要在 :style 或 :class 中使用函数调用、模板字符串等复杂表达式(uni-app 小程序下不支持)',
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
funcStyleError: 'Do not use function call in :style binding, use computed property or `\'\' + xxx` instead',
|
|
12
|
+
tplStyleError: 'Do not use template literal in :style binding, use computed property or `\'\' + xxx` instead',
|
|
13
|
+
funcClassError: 'Do not use function call in :class binding, use computed property or `\'\' + xxx` instead',
|
|
14
|
+
tplClassError: 'Do not use template literal in :class binding, use computed property or `\'\' + xxx` instead',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
create(context) {
|
|
19
|
+
const fileName = context.filename || context.getFilename();
|
|
20
|
+
if (!fileName.endsWith('.vue')) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
const parserServices = getParserServices(context);
|
|
24
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 检查顶层表达式是否为函数调用或模板字符串
|
|
30
|
+
*
|
|
31
|
+
* 只检测直接绑定到 :style/:class 的顶层表达式,不递归检查子表达式。
|
|
32
|
+
* 因为数组元素、对象属性值、条件表达式分支中的函数调用/模板字符串
|
|
33
|
+
* 在 uni-app 小程序中是支持的,例如:
|
|
34
|
+
* :class="[utils.getClass(...), tClassImage]" — 合法
|
|
35
|
+
* :style="inChat ? imageStyle(item) : ''" — 合法
|
|
36
|
+
* :class="[classPrefix, inChat ? classPrefix + '--chatting' : '', getFileTypeClass(...)]" — 合法
|
|
37
|
+
*/
|
|
38
|
+
function checkExpression(expression, attrName) {
|
|
39
|
+
if (!expression) return;
|
|
40
|
+
|
|
41
|
+
const isStyle = attrName === 'style';
|
|
42
|
+
const funcMessageId = isStyle ? 'funcStyleError' : 'funcClassError';
|
|
43
|
+
const tplMessageId = isStyle ? 'tplStyleError' : 'tplClassError';
|
|
44
|
+
|
|
45
|
+
if (expression.type === 'CallExpression') {
|
|
46
|
+
// :style="tool._style(xxx)" / :class="getClass(xxx)"
|
|
47
|
+
context.report({
|
|
48
|
+
node: expression,
|
|
49
|
+
messageId: funcMessageId,
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (expression.type === 'TemplateLiteral') {
|
|
55
|
+
// :style="`color: ${color}`" / :class="`${prefix}-item`"
|
|
56
|
+
context.report({
|
|
57
|
+
node: expression,
|
|
58
|
+
messageId: tplMessageId,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
64
|
+
VAttribute(node) {
|
|
65
|
+
if (!node.key
|
|
66
|
+
|| node.key.type !== 'VDirectiveKey'
|
|
67
|
+
|| node.key?.argument?.type !== 'VIdentifier'
|
|
68
|
+
) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const attrName = node.key?.argument?.name;
|
|
73
|
+
if (attrName !== 'style' && attrName !== 'class') {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (node?.value?.type === 'VExpressionContainer' && node.value.expression) {
|
|
78
|
+
checkExpression(node.value.expression, attrName);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
2
|
const TEST_REGEX = /\[[^\](]*\.\d+[a-zA-Z]+[^\]]*\]/;
|
|
3
3
|
|
|
4
4
|
|
|
@@ -27,7 +27,12 @@ module.exports = {
|
|
|
27
27
|
const options = context.options[0] || {};
|
|
28
28
|
const regex = options.regexp ?? TEST_REGEX;
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
const parserServices = getParserServices(context);
|
|
31
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
31
36
|
'VAttribute[key.name=\'class\']'(node) {
|
|
32
37
|
if (!node.value || node.value.type !== 'VLiteral') return;
|
|
33
38
|
|
package/lib/rules/no-js-file.js
CHANGED
|
@@ -50,7 +50,7 @@ module.exports = {
|
|
|
50
50
|
const include = options.include || DEFAULT_INCLUDE;
|
|
51
51
|
const exclude = options.exclude || DEFAULT_EXCLUDE;
|
|
52
52
|
|
|
53
|
-
const filename = context.getFilename();
|
|
53
|
+
const filename = context.filename || context.getFilename();
|
|
54
54
|
const pureFilePath = path.relative(projectRoot, filename);
|
|
55
55
|
|
|
56
56
|
if (!pureFilePath) {
|
|
@@ -10,9 +10,9 @@ module.exports = {
|
|
|
10
10
|
create(context) {
|
|
11
11
|
return {
|
|
12
12
|
Program(node) {
|
|
13
|
-
const sourceCode = context.getSourceCode();
|
|
13
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
14
14
|
const vueFileContent = sourceCode.getText();
|
|
15
|
-
const filename = context.getFilename();
|
|
15
|
+
const filename = context.filename || context.getFilename();
|
|
16
16
|
|
|
17
17
|
if (!filename.endsWith('.vue')) {
|
|
18
18
|
return {};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const { getParserServices } = require('../utils/parser-services');
|
|
2
|
+
|
|
1
3
|
module.exports = {
|
|
2
4
|
meta: {
|
|
3
5
|
type: 'problem',
|
|
@@ -12,16 +14,17 @@ module.exports = {
|
|
|
12
14
|
},
|
|
13
15
|
|
|
14
16
|
create(context) {
|
|
15
|
-
const fileName = context.getFilename();
|
|
17
|
+
const fileName = context.filename || context.getFilename();
|
|
16
18
|
if (!fileName.endsWith('.vue')) {
|
|
17
19
|
return {};
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
const parserServices = getParserServices(context);
|
|
22
|
+
if (!parserServices?.defineTemplateBodyVisitor) {
|
|
20
23
|
return {
|
|
21
24
|
};
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
return
|
|
27
|
+
return parserServices.defineTemplateBodyVisitor({
|
|
25
28
|
VAttribute(node) {
|
|
26
29
|
if (node.value && node.value.type === 'VExpressionContainer' && node.value.expression) {
|
|
27
30
|
const { operator } = node.value.expression;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'problem',
|
|
6
|
+
docs: {
|
|
7
|
+
description: '禁止在指定目录下使用特定前缀的导入路径,必须使用相对路径',
|
|
8
|
+
category: 'Best Practices',
|
|
9
|
+
recommended: true,
|
|
10
|
+
},
|
|
11
|
+
fixable: null,
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
restrictedPaths: {
|
|
17
|
+
type: 'array',
|
|
18
|
+
description: '需要限制的目录路径列表',
|
|
19
|
+
items: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
},
|
|
22
|
+
default: ['src/components'],
|
|
23
|
+
},
|
|
24
|
+
bannedPrefixes: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
description: '禁止使用的导入路径前缀列表',
|
|
27
|
+
items: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
},
|
|
30
|
+
default: ['src', 'src/'],
|
|
31
|
+
},
|
|
32
|
+
message: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
description: '自定义错误消息',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
messages: {
|
|
41
|
+
noSrcImport: '在 {{directory}} 目录下不允许使用 "{{prefix}}" 开头的导入路径,请使用相对路径。该目录会被发布为独立的 npm 包。',
|
|
42
|
+
customMessage: '{{message}}',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
create(context) {
|
|
47
|
+
// 获取配置选项
|
|
48
|
+
const options = context.options[0] || {};
|
|
49
|
+
const restrictedPaths = options.restrictedPaths || ['src/components'];
|
|
50
|
+
const bannedPrefixes = options.bannedPrefixes || ['src'];
|
|
51
|
+
const customMessage = options.message;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
ImportDeclaration(node) {
|
|
55
|
+
// 获取当前文件的绝对路径
|
|
56
|
+
const filename = context.filename || context.getFilename();
|
|
57
|
+
|
|
58
|
+
// 标准化路径(统一使用正斜杠)
|
|
59
|
+
const normalizedPath = filename.replace(/\\/g, '/');
|
|
60
|
+
|
|
61
|
+
// 检查文件是否在任何受限目录下
|
|
62
|
+
const matchedDirectory = restrictedPaths.find((restrictedPath) => {
|
|
63
|
+
const normalizedRestrictedPath = restrictedPath.replace(/\\/g, '/');
|
|
64
|
+
return normalizedPath.includes(`/${normalizedRestrictedPath}/`)
|
|
65
|
+
|| normalizedPath.includes(`/${normalizedRestrictedPath}`);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (!matchedDirectory) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 获取导入路径
|
|
73
|
+
const importPath = node.source.value;
|
|
74
|
+
|
|
75
|
+
// 检查是否使用了任何被禁止的前缀
|
|
76
|
+
const matchedPrefix = bannedPrefixes.find(prefix => importPath === prefix || importPath.startsWith(`${prefix}/`));
|
|
77
|
+
|
|
78
|
+
if (matchedPrefix) {
|
|
79
|
+
if (customMessage) {
|
|
80
|
+
context.report({
|
|
81
|
+
node: node.source,
|
|
82
|
+
messageId: 'customMessage',
|
|
83
|
+
data: {
|
|
84
|
+
message: customMessage,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
context.report({
|
|
89
|
+
node: node.source,
|
|
90
|
+
messageId: 'noSrcImport',
|
|
91
|
+
data: {
|
|
92
|
+
directory: matchedDirectory,
|
|
93
|
+
prefix: matchedPrefix,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
};
|
|
@@ -22,13 +22,13 @@ module.exports = {
|
|
|
22
22
|
],
|
|
23
23
|
},
|
|
24
24
|
create(context) {
|
|
25
|
-
const sourceCode = context.getSourceCode();
|
|
25
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
26
26
|
const comments = sourceCode.getAllComments();
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const keyword = options.keyword ?? DEFAULT_KEYWORD;
|
|
28
|
+
const options = context.options[0] || {};
|
|
29
|
+
const keyword = options.keyword ?? DEFAULT_KEYWORD;
|
|
31
30
|
|
|
31
|
+
comments.forEach((comment) => {
|
|
32
32
|
if (comment.value.includes(keyword)) {
|
|
33
33
|
context.report({
|
|
34
34
|
node: comment,
|
|
@@ -27,8 +27,9 @@ module.exports = {
|
|
|
27
27
|
create(context) {
|
|
28
28
|
const options = context.options[0] || {};
|
|
29
29
|
const baseDir = options.baseDir || 'src';
|
|
30
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
30
31
|
|
|
31
|
-
const filename = context.getFilename();
|
|
32
|
+
const filename = context.filename || context.getFilename();
|
|
32
33
|
const absolutePath = path.resolve(filename);
|
|
33
34
|
|
|
34
35
|
// 提取当前包名
|
|
@@ -114,7 +115,6 @@ module.exports = {
|
|
|
114
115
|
*/
|
|
115
116
|
const checkAndReport = (node, importPath) => {
|
|
116
117
|
if (importPath && importPath.startsWith(pmdPackageName)) {
|
|
117
|
-
const sourceCode = context.getSourceCode();
|
|
118
118
|
const fixNode = getFixNode(node);
|
|
119
119
|
|
|
120
120
|
context.report({
|
|
@@ -180,7 +180,7 @@ module.exports = {
|
|
|
180
180
|
create(context) {
|
|
181
181
|
// const sourceCode = context.getSourceCode();
|
|
182
182
|
// const cwd = context.getCwd();
|
|
183
|
-
const fileName = context.getFilename();
|
|
183
|
+
const fileName = context.filename || context.getFilename();
|
|
184
184
|
|
|
185
185
|
const dirname = path.dirname(fileName);
|
|
186
186
|
if (!fileName.endsWith('.vue')) {
|
package/lib/test/index.js
CHANGED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const { RuleTester } = require('eslint');
|
|
2
|
+
|
|
3
|
+
const rule = require('../rules/no-complex-style-class');
|
|
4
|
+
|
|
5
|
+
const FILENAME = 'test.vue';
|
|
6
|
+
|
|
7
|
+
const ruleTester = new RuleTester({
|
|
8
|
+
parser: require.resolve('vue-eslint-parser'),
|
|
9
|
+
parserOptions: {
|
|
10
|
+
ecmaVersion: 'latest',
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
ruleTester.run('no-complex-style-class', rule, {
|
|
16
|
+
valid: [
|
|
17
|
+
// :style 绑定变量 — 合法
|
|
18
|
+
{ code: '<template><div :style="myStyle"></div></template>', filename: FILENAME },
|
|
19
|
+
|
|
20
|
+
// :style 绑定对象(值为变量) — 合法
|
|
21
|
+
{ code: '<template><div :style="{ color: myColor }"></div></template>', filename: FILENAME },
|
|
22
|
+
|
|
23
|
+
// :class 绑定变量 — 合法
|
|
24
|
+
{ code: '<template><div :class="item.className"></div></template>', filename: FILENAME },
|
|
25
|
+
|
|
26
|
+
// :class 绑定字面量数组 — 合法
|
|
27
|
+
{ code: '<template><div :class="[\'a\', \'b\']"></div></template>', filename: FILENAME },
|
|
28
|
+
|
|
29
|
+
// :class 绑定对象 — 合法
|
|
30
|
+
{ code: '<template><div :class="{ active: isActive }"></div></template>', filename: FILENAME },
|
|
31
|
+
|
|
32
|
+
// :style 绑定条件表达式(值为变量) — 合法
|
|
33
|
+
{ code: '<template><div :style="flag ? styleA : styleB"></div></template>', filename: FILENAME },
|
|
34
|
+
|
|
35
|
+
// 静态 style — 合法(非绑定)
|
|
36
|
+
{ code: '<template><div style="color: red"></div></template>', filename: FILENAME },
|
|
37
|
+
|
|
38
|
+
// 静态 class — 合法(非绑定)
|
|
39
|
+
{ code: '<template><div class="foo bar"></div></template>', filename: FILENAME },
|
|
40
|
+
|
|
41
|
+
// 数组元素中的函数调用 — 合法(小程序支持)
|
|
42
|
+
{ code: '<template><div :class="[utils.getClass(classPrefix, dataSize || \'medium\', dataShape, dataBordered), tClassImage]"></div></template>', filename: FILENAME },
|
|
43
|
+
|
|
44
|
+
// 数组中混合使用函数调用和条件表达式 — 合法
|
|
45
|
+
{ code: '<template><div :class="[classPrefix, inChat ? classPrefix + \'--chatting\' : \'\', getFileTypeClass(inChat, files)]"></div></template>', filename: FILENAME },
|
|
46
|
+
|
|
47
|
+
// 条件表达式中的函数调用 — 合法(小程序支持)
|
|
48
|
+
{ code: '<template><div :style="inChat ? imageStyle(item) : \'\'"></div></template>', filename: FILENAME },
|
|
49
|
+
|
|
50
|
+
// 对象属性值中的函数调用 — 合法(小程序支持)
|
|
51
|
+
{ code: '<template><div :style="{ color: getColor() }"></div></template>', filename: FILENAME },
|
|
52
|
+
|
|
53
|
+
// 条件表达式分支中的函数调用 — 合法
|
|
54
|
+
{ code: '<template><div :style="flag ? getStyle() : defaultStyle"></div></template>', filename: FILENAME },
|
|
55
|
+
|
|
56
|
+
// 数组中有多个函数调用 — 合法
|
|
57
|
+
{ code: '<template><div :class="[getA(), getB()]"></div></template>', filename: FILENAME },
|
|
58
|
+
],
|
|
59
|
+
invalid: [
|
|
60
|
+
// :style 中直接使用函数调用(顶层)
|
|
61
|
+
{
|
|
62
|
+
code: '<template><div :style="tool._style(xxx)"></div></template>',
|
|
63
|
+
filename: FILENAME,
|
|
64
|
+
errors: [{ messageId: 'funcStyleError' }],
|
|
65
|
+
},
|
|
66
|
+
// :class 中直接使用函数调用(顶层)
|
|
67
|
+
{
|
|
68
|
+
code: '<template><div :class="getClass(item)"></div></template>',
|
|
69
|
+
filename: FILENAME,
|
|
70
|
+
errors: [{ messageId: 'funcClassError' }],
|
|
71
|
+
},
|
|
72
|
+
// :style 中直接使用模板字符串(顶层)
|
|
73
|
+
{
|
|
74
|
+
code: '<template><div :style="`color: ${color}`"></div></template>',
|
|
75
|
+
filename: FILENAME,
|
|
76
|
+
errors: [{ messageId: 'tplStyleError' }],
|
|
77
|
+
},
|
|
78
|
+
// :class 中直接使用模板字符串(顶层)
|
|
79
|
+
{
|
|
80
|
+
code: '<template><div :class="`${prefix}-item`"></div></template>',
|
|
81
|
+
filename: FILENAME,
|
|
82
|
+
errors: [{ messageId: 'tplClassError' }],
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取 parserServices,兼容 ESLint v8 和 v9
|
|
3
|
+
*
|
|
4
|
+
* ESLint v9 中 context.parserServices 已被废弃,
|
|
5
|
+
* 应使用 context.sourceCode.parserServices 代替。
|
|
6
|
+
*
|
|
7
|
+
* @param {import('eslint').Rule.RuleContext} context
|
|
8
|
+
* @returns {object} parserServices
|
|
9
|
+
*/
|
|
10
|
+
function getParserServices(context) {
|
|
11
|
+
// ESLint v9+: 优先使用 sourceCode.parserServices
|
|
12
|
+
if (context.sourceCode && context.sourceCode.parserServices) {
|
|
13
|
+
return context.sourceCode.parserServices;
|
|
14
|
+
}
|
|
15
|
+
// ESLint v8: getSourceCode() 方式
|
|
16
|
+
if (typeof context.getSourceCode === 'function') {
|
|
17
|
+
const sourceCode = context.getSourceCode();
|
|
18
|
+
if (sourceCode && sourceCode.parserServices) {
|
|
19
|
+
return sourceCode.parserServices;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// 最终回退到 context.parserServices
|
|
23
|
+
return context.parserServices || {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { getParserServices };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-light",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.22",
|
|
4
4
|
"description": "Simple Eslint Plugin",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
"CHANGELOG.md"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"@tdesign/uniapp": "^0.7.3",
|
|
29
|
+
"@tdesign/uniapp-chat": "^0.2.1",
|
|
28
30
|
"minimatch": "^10.0.1",
|
|
29
31
|
"requireindex": "^1.2.0",
|
|
30
32
|
"t-comm": "^3.0.22"
|
|
@@ -48,6 +50,10 @@
|
|
|
48
50
|
"engines": {
|
|
49
51
|
"node": "12.x || 14.x || >= 16"
|
|
50
52
|
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"registry": "https://registry.npmjs.org/"
|
|
56
|
+
},
|
|
51
57
|
"scripts": {
|
|
52
58
|
"build": "rm -rf lib && node script/build",
|
|
53
59
|
"bump": "node ../../script/monorepo/version-simple $PWD",
|