@lark-apaas/fullstack-presets 1.1.3 → 1.1.4-alpha.2

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.
@@ -1,3 +1,5 @@
1
1
  export declare const customRules: {
2
2
  'no-nested-styled-jsx': import("eslint").Rule.RuleModule;
3
+ 'require-app-container': import("eslint").Rule.RuleModule;
4
+ 'no-direct-capability-api': import("eslint").Rule.RuleModule;
3
5
  };
@@ -5,6 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.customRules = void 0;
7
7
  const no_nested_styled_jsx_1 = __importDefault(require("./no-nested-styled-jsx"));
8
+ const require_app_container_1 = __importDefault(require("./require-app-container"));
9
+ const no_direct_capability_api_1 = __importDefault(require("./no-direct-capability-api"));
8
10
  exports.customRules = {
9
11
  'no-nested-styled-jsx': no_nested_styled_jsx_1.default,
12
+ 'require-app-container': require_app_container_1.default,
13
+ 'no-direct-capability-api': no_direct_capability_api_1.default,
10
14
  };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * ESLint 规则:禁止直接使用 axios 调用 /__innerapi__/capability/ 路径
3
+ *
4
+ * 此规则确保开发者使用 capabilityClient.load().call() 方式调用能力,
5
+ * 而不是直接通过 axios 调用内部 API 路径。
6
+ *
7
+ * 错误示例:
8
+ * axios.post('/__innerapi__/capability/xxx')
9
+ * axiosForBackend.get('/__innerapi__/capability/list')
10
+ *
11
+ * 正确示例:
12
+ * capabilityClient.load('capabilityId').call('action', params)
13
+ */
14
+ import type { Rule } from 'eslint';
15
+ declare const rule: Rule.RuleModule;
16
+ export default rule;
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const DEFAULT_MESSAGE = "Direct API calls to /__innerapi__/capability/ are not allowed. Use capabilityClient.load('capabilityId').call('action', params) instead.";
4
+ // 匹配 capability 内部 API 路径的正则表达式
5
+ const CAPABILITY_PATH_PATTERN = /__innerapi__\/capability\//;
6
+ // HTTP 方法列表
7
+ const HTTP_METHODS = [
8
+ 'get',
9
+ 'post',
10
+ 'put',
11
+ 'delete',
12
+ 'patch',
13
+ 'request',
14
+ 'head',
15
+ 'options',
16
+ ];
17
+ // 需要检测的 axios 标识符
18
+ const AXIOS_IDENTIFIERS = ['axios', 'axiosForBackend'];
19
+ /**
20
+ * 从 AST 节点中提取 URL 字符串值
21
+ * 支持:字符串字面量、模板字符串、字符串拼接
22
+ */
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ function extractUrlValue(node) {
25
+ if (!node)
26
+ return null;
27
+ switch (node.type) {
28
+ case 'Literal':
29
+ return typeof node.value === 'string' ? node.value : null;
30
+ case 'TemplateLiteral':
31
+ // 拼接所有静态部分(quasis)
32
+ return node.quasis.map((q) => q.value.raw).join('');
33
+ case 'BinaryExpression':
34
+ // 处理字符串拼接 'a' + 'b'
35
+ if (node.operator === '+') {
36
+ const left = extractUrlValue(node.left);
37
+ const right = extractUrlValue(node.right);
38
+ if (left !== null || right !== null) {
39
+ return (left || '') + (right || '');
40
+ }
41
+ }
42
+ return null;
43
+ default:
44
+ return null;
45
+ }
46
+ }
47
+ const rule = {
48
+ meta: {
49
+ type: 'problem',
50
+ docs: {
51
+ description: '禁止直接使用 axios 调用 /__innerapi__/capability/ 路径',
52
+ category: 'Best Practices',
53
+ recommended: true,
54
+ },
55
+ schema: [
56
+ {
57
+ type: 'object',
58
+ properties: {
59
+ message: {
60
+ type: 'string',
61
+ description: '自定义错误信息',
62
+ },
63
+ axiosIdentifiers: {
64
+ type: 'array',
65
+ items: { type: 'string' },
66
+ description: '额外需要检测的 axios 实例标识符',
67
+ },
68
+ },
69
+ additionalProperties: false,
70
+ },
71
+ ],
72
+ messages: {
73
+ noDirectCapabilityApi: DEFAULT_MESSAGE,
74
+ },
75
+ },
76
+ create(context) {
77
+ const options = context.options[0];
78
+ const customMessage = options?.message;
79
+ const additionalIdentifiers = options?.axiosIdentifiers || [];
80
+ // 合并默认和自定义的 axios 标识符
81
+ const allAxiosIdentifiers = [
82
+ ...AXIOS_IDENTIFIERS,
83
+ ...additionalIdentifiers,
84
+ ];
85
+ return {
86
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ CallExpression(node) {
88
+ // 1. 检查是否为 MemberExpression 调用 (xxx.method())
89
+ if (node.callee.type !== 'MemberExpression')
90
+ return;
91
+ // 2. 检查方法名是否为 HTTP 方法
92
+ const methodName = node.callee.property?.name;
93
+ if (!methodName || !HTTP_METHODS.includes(methodName.toLowerCase())) {
94
+ return;
95
+ }
96
+ // 3. 检查调用者是否为 axios 相关标识符
97
+ const objectName = node.callee.object?.name;
98
+ if (!objectName || !allAxiosIdentifiers.includes(objectName)) {
99
+ return;
100
+ }
101
+ // 4. 获取第一个参数(URL)
102
+ const urlArg = node.arguments[0];
103
+ if (!urlArg)
104
+ return;
105
+ // 5. 提取 URL 并检查是否包含 capability 路径
106
+ const urlValue = extractUrlValue(urlArg);
107
+ if (urlValue && CAPABILITY_PATH_PATTERN.test(urlValue)) {
108
+ context.report({
109
+ node,
110
+ ...(customMessage
111
+ ? { message: customMessage }
112
+ : { messageId: 'noDirectCapabilityApi' }),
113
+ });
114
+ }
115
+ },
116
+ };
117
+ },
118
+ };
119
+ exports.default = rule;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * ESLint 规则:要求入口文件中必须包含 AppContainer 组件
3
+ *
4
+ * 此规则确保 client/src/app.tsx 或 client/src/index.tsx 中
5
+ * 包含 AppContainer 组件,以保证平台功能正常运行。
6
+ *
7
+ * 跨文件检查逻辑:
8
+ * - 只在 app.tsx 上执行检查和报错
9
+ * - 如果 app.tsx 没有 AppContainer,会检查 index.tsx
10
+ * - 只要任一文件使用了 AppContainer 就算通过
11
+ * - 只有两个文件都没有时才在 app.tsx 上报错
12
+ */
13
+ import type { Rule } from 'eslint';
14
+ declare const rule: Rule.RuleModule;
15
+ export default rule;
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const path = __importStar(require("path"));
37
+ const fs = __importStar(require("fs"));
38
+ const DEFAULT_MESSAGE = 'Missing platform component AppContainer. Please ensure that App is wrapped with AppContainer (in app.tsx or index.tsx) to enable platform features.';
39
+ // 主检查文件(报错在此文件上)
40
+ const PRIMARY_FILE = 'app.tsx';
41
+ // 备选检查文件
42
+ const SECONDARY_FILE = 'index.tsx';
43
+ // 匹配 client/src/ 目录的正则表达式
44
+ const CLIENT_SRC_PATTERN = /[/\\]client[/\\]src[/\\]$/;
45
+ // 检测文件内容是否包含 <AppContainer 的正则(比完整 AST 解析更快)
46
+ const APP_CONTAINER_PATTERN = /<AppContainer[\s/>]/;
47
+ /**
48
+ * 同步检查文件是否包含 AppContainer 使用
49
+ * 使用正则匹配,性能优于完整 AST 解析
50
+ */
51
+ function checkFileForAppContainer(filePath) {
52
+ try {
53
+ if (!fs.existsSync(filePath)) {
54
+ return false;
55
+ }
56
+ const content = fs.readFileSync(filePath, 'utf-8');
57
+ return APP_CONTAINER_PATTERN.test(content);
58
+ }
59
+ catch {
60
+ // 文件不存在或无法读取,视为没有使用
61
+ return false;
62
+ }
63
+ }
64
+ const rule = {
65
+ meta: {
66
+ type: 'problem',
67
+ docs: {
68
+ description: '要求入口文件中必须包含 AppContainer 组件',
69
+ category: 'Possible Errors',
70
+ recommended: true,
71
+ },
72
+ schema: [
73
+ {
74
+ type: 'object',
75
+ properties: {
76
+ message: {
77
+ type: 'string',
78
+ description: '自定义错误信息',
79
+ },
80
+ },
81
+ additionalProperties: false,
82
+ },
83
+ ],
84
+ messages: {
85
+ missingAppContainer: DEFAULT_MESSAGE,
86
+ },
87
+ },
88
+ create(context) {
89
+ const options = context.options[0];
90
+ const customMessage = options?.message;
91
+ const filename = context.filename || context.getFilename();
92
+ // 获取文件名和目录名
93
+ const basename = path.basename(filename).toLowerCase();
94
+ const dirname = path.dirname(filename);
95
+ // 判断是否在 client/src/ 目录下
96
+ const isInClientSrc = CLIENT_SRC_PATTERN.test(dirname + path.sep);
97
+ // 只在 app.tsx 上执行检查和报错
98
+ // index.tsx 不执行检查(避免重复报错)
99
+ const isPrimaryFile = basename === PRIMARY_FILE && isInClientSrc;
100
+ if (!isPrimaryFile) {
101
+ return {};
102
+ }
103
+ // 标记当前文件是否使用了 AppContainer
104
+ let hasAppContainerInCurrentFile = false;
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ let programNode = null;
107
+ return {
108
+ // 检查 JSX 中是否使用了 <AppContainer> 组件
109
+ // 注意:仅导入不使用不算通过,必须在 JSX 中实际使用
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ JSXOpeningElement(node) {
112
+ const elementName = node.name;
113
+ if (elementName?.type === 'JSXIdentifier' &&
114
+ elementName?.name === 'AppContainer') {
115
+ hasAppContainerInCurrentFile = true;
116
+ }
117
+ },
118
+ // 保存 Program 节点,用于在文件末尾报错时定位
119
+ Program(node) {
120
+ programNode = node;
121
+ },
122
+ // 文件解析完成后,检查是否在 JSX 中使用了 AppContainer
123
+ 'Program:exit'() {
124
+ // 如果当前文件(app.tsx)有 AppContainer,通过
125
+ if (hasAppContainerInCurrentFile) {
126
+ return;
127
+ }
128
+ // 当前文件没有,检查 index.tsx
129
+ const secondaryFilePath = path.join(dirname, SECONDARY_FILE);
130
+ const hasAppContainerInSecondary = checkFileForAppContainer(secondaryFilePath);
131
+ // 如果 index.tsx 有 AppContainer,也算通过
132
+ if (hasAppContainerInSecondary) {
133
+ return;
134
+ }
135
+ // 两个文件都没有,报错
136
+ if (programNode) {
137
+ context.report({
138
+ node: programNode,
139
+ ...(customMessage
140
+ ? { message: customMessage }
141
+ : { messageId: 'missingAppContainer' }),
142
+ });
143
+ }
144
+ },
145
+ };
146
+ },
147
+ };
148
+ exports.default = rule;
@@ -36,6 +36,10 @@ exports.default = {
36
36
  rules: {
37
37
  // React Hooks 推荐规则
38
38
  ...reactHooks.configs.recommended.rules,
39
+ // 平台规则:确保入口文件包含 AppContainer 组件
40
+ '@lark-apaas/require-app-container': 'error',
41
+ // 平台规则:禁止直接调用 capability 内部 API,应使用 capabilityClient
42
+ '@lark-apaas/no-direct-capability-api': 'error',
39
43
  // TypeScript 规则
40
44
  '@typescript-eslint/no-unused-vars': 'off', // 未使用变量检查关闭
41
45
  '@typescript-eslint/no-explicit-any': 'off', // 允许使用 any 类型
@@ -18,6 +18,8 @@ declare const _default: ({
18
18
  '@lark-apaas'?: {
19
19
  rules: {
20
20
  'no-nested-styled-jsx': import("eslint").Rule.RuleModule;
21
+ 'require-app-container': import("eslint").Rule.RuleModule;
22
+ 'no-direct-capability-api': import("eslint").Rule.RuleModule;
21
23
  };
22
24
  } | undefined;
23
25
  'react-hooks': any;
@@ -17,6 +17,8 @@ export declare const eslintPresets: {
17
17
  '@lark-apaas'?: {
18
18
  rules: {
19
19
  'no-nested-styled-jsx': import("eslint").Rule.RuleModule;
20
+ 'require-app-container': import("eslint").Rule.RuleModule;
21
+ 'no-direct-capability-api': import("eslint").Rule.RuleModule;
20
22
  };
21
23
  } | undefined;
22
24
  'react-hooks': any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/fullstack-presets",
3
- "version": "1.1.3",
3
+ "version": "1.1.4-alpha.2",
4
4
  "files": [
5
5
  "lib"
6
6
  ],
@@ -45,6 +45,5 @@
45
45
  "peerDependencies": {
46
46
  "eslint": "^9.0.0",
47
47
  "typescript-eslint": "^8.44.0"
48
- },
49
- "gitHead": "bc9469110ed352267d992d7406578d00bc3ffe03"
48
+ }
50
49
  }