@djvlc/contracts-types 1.8.0 → 1.8.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.
package/dist/index.js CHANGED
@@ -993,7 +993,7 @@ var ALL_BUILTIN_FUNCTIONS = [
993
993
  ];
994
994
  var ALLOWED_FUNCTION_NAMES = ALL_BUILTIN_FUNCTIONS.map((f) => f.name);
995
995
  var DEFAULT_EXPRESSION_VALIDATION_CONFIG = {
996
- allowedContextPaths: ["state", "binding", "context", "props", "event"],
996
+ allowedContextPaths: ["state", "binding", "local", "props", "event"],
997
997
  allowedFunctions: ALLOWED_FUNCTION_NAMES,
998
998
  maxDepth: 10,
999
999
  maxLength: 1e3,
@@ -1002,49 +1002,147 @@ var DEFAULT_EXPRESSION_VALIDATION_CONFIG = {
1002
1002
  };
1003
1003
 
1004
1004
  // src/page/expression.ts
1005
+ var DEPENDENCY_PREFIX = {
1006
+ /** 页面状态依赖前缀 */
1007
+ STATE: "state",
1008
+ /** 数据绑定依赖前缀 */
1009
+ BINDING: "binding",
1010
+ /** 局部变量依赖前缀 */
1011
+ LOCAL: "local",
1012
+ /** 组件属性依赖前缀 */
1013
+ PROPS: "props",
1014
+ /** 事件上下文依赖前缀 */
1015
+ EVENT: "event"
1016
+ };
1017
+ function dep(prefix, path) {
1018
+ return `${prefix}.${path}`;
1019
+ }
1005
1020
  function createExpressionContext(partial) {
1006
1021
  return {
1007
1022
  state: {},
1008
1023
  binding: {},
1009
- context: {},
1024
+ local: {},
1010
1025
  ...partial
1011
1026
  };
1012
1027
  }
1013
- function createLoopContext(item, index, extras) {
1028
+ function createLoopLocal(item, index, extras) {
1014
1029
  return {
1030
+ ...extras,
1015
1031
  item,
1016
- index,
1017
- ...extras
1032
+ index
1018
1033
  };
1019
1034
  }
1020
- function literal(value) {
1021
- return value;
1022
- }
1023
1035
  function stateRef(path, fallback) {
1024
1036
  return {
1025
1037
  type: "state",
1026
1038
  value: path,
1027
1039
  fallback,
1028
- meta: { dependencies: [`state.${path}`] }
1040
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.STATE, path)] }
1029
1041
  };
1030
1042
  }
1043
+ var BINDING_ID_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*$/;
1031
1044
  function bindingRef(bindingId, path, fallback) {
1045
+ if (!BINDING_ID_PATTERN.test(bindingId)) {
1046
+ throw new Error(
1047
+ `Invalid bindingId: "${bindingId}". Must match pattern [A-Za-z_][A-Za-z0-9_-]* (no dots allowed).`
1048
+ );
1049
+ }
1032
1050
  return {
1033
1051
  type: "binding",
1034
1052
  value: `${bindingId}.${path}`,
1035
1053
  fallback,
1036
- meta: { dependencies: [`binding.${bindingId}.${path}`] }
1054
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.BINDING, `${bindingId}.${path}`)] }
1037
1055
  };
1038
1056
  }
1039
- function template(templateString, fallback) {
1040
- const dependencies = [];
1057
+ function localRef(path, fallback) {
1058
+ return {
1059
+ type: "local",
1060
+ value: path,
1061
+ fallback,
1062
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.LOCAL, path)] }
1063
+ };
1064
+ }
1065
+ var FULL_REF_PATTERN = /\b(state|binding|local|props|event)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\.\d+|\[\d+\])*/g;
1066
+ var BARE_PATH_PATTERN = /\b[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\.\d+|\[\d+\])*\b/g;
1067
+ var RESERVED_WORDS = /* @__PURE__ */ new Set([
1068
+ "true",
1069
+ "false",
1070
+ "null",
1071
+ "undefined",
1072
+ "NaN",
1073
+ "Infinity",
1074
+ // 常见运算符相关
1075
+ "typeof",
1076
+ "instanceof",
1077
+ "new",
1078
+ "void",
1079
+ "delete",
1080
+ // 常见内置对象(不应作为依赖)
1081
+ "Math",
1082
+ "Date",
1083
+ "JSON",
1084
+ "Array",
1085
+ "Object",
1086
+ "String",
1087
+ "Number",
1088
+ "Boolean"
1089
+ ]);
1090
+ var KNOWN_PREFIXES = ["state", "binding", "local", "props", "event"];
1091
+ function extractFullRefs(content) {
1092
+ const set = /* @__PURE__ */ new Set();
1093
+ for (const match of content.matchAll(FULL_REF_PATTERN)) {
1094
+ set.add(match[0]);
1095
+ }
1096
+ return [...set];
1097
+ }
1098
+ function findBarePaths(content) {
1099
+ const set = /* @__PURE__ */ new Set();
1100
+ for (const match of content.matchAll(BARE_PATH_PATTERN)) {
1101
+ const token = match[0];
1102
+ if (RESERVED_WORDS.has(token)) continue;
1103
+ if (KNOWN_PREFIXES.some((prefix) => token.startsWith(`${prefix}.`))) {
1104
+ continue;
1105
+ }
1106
+ if (KNOWN_PREFIXES.includes(token)) {
1107
+ continue;
1108
+ }
1109
+ set.add(token);
1110
+ }
1111
+ return [...set];
1112
+ }
1113
+ var TemplateParseError = class extends Error {
1114
+ constructor(message, bareVariables, expression) {
1115
+ super(message);
1116
+ this.bareVariables = bareVariables;
1117
+ this.expression = expression;
1118
+ this.name = "TemplateParseError";
1119
+ }
1120
+ };
1121
+ function parseTemplateDependencies(templateString, mode = "strict") {
1122
+ const depSet = /* @__PURE__ */ new Set();
1041
1123
  const pattern = /\$\{([^}]+)\}/g;
1042
- let match;
1043
- while ((match = pattern.exec(templateString)) !== null) {
1044
- if (match[1]) {
1045
- dependencies.push(match[1]);
1124
+ for (const match of templateString.matchAll(pattern)) {
1125
+ const content = (match[1] ?? "").trim();
1126
+ if (!content) continue;
1127
+ const fullRefs = extractFullRefs(content);
1128
+ for (const ref of fullRefs) {
1129
+ depSet.add(ref);
1130
+ }
1131
+ if (mode === "strict") {
1132
+ const bare = findBarePaths(content);
1133
+ if (bare.length > 0) {
1134
+ throw new TemplateParseError(
1135
+ `Template variable must use prefix (state./binding./local./props./event.). Found bare variables: ${bare.join(", ")} in \${${content}}`,
1136
+ bare,
1137
+ content
1138
+ );
1139
+ }
1046
1140
  }
1047
1141
  }
1142
+ return [...depSet];
1143
+ }
1144
+ function template(templateString, fallback) {
1145
+ const dependencies = parseTemplateDependencies(templateString, "strict");
1048
1146
  return {
1049
1147
  type: "template",
1050
1148
  value: templateString,
@@ -1052,9 +1150,6 @@ function template(templateString, fallback) {
1052
1150
  meta: { dependencies }
1053
1151
  };
1054
1152
  }
1055
- function queryRef(queryId, path, fallback) {
1056
- return bindingRef(queryId, path, fallback);
1057
- }
1058
1153
 
1059
1154
  // src/component/meta.ts
1060
1155
  var CURRENT_COMPONENT_META_VERSION = "2.0.0";
@@ -1081,27 +1176,21 @@ var CURRENT_ACTION_SPEC_VERSION = "2.0.0";
1081
1176
  var CURRENT_DATA_QUERY_SPEC_VERSION = "2.0.0";
1082
1177
 
1083
1178
  // src/index.ts
1084
- var VERSION = "2.1.0";
1179
+ var VERSION = "2.0.0";
1085
1180
  var PROTOCOL_VERSION = "2.0.0";
1086
- var PAGE_SCHEMA_VERSION = "2.0.0";
1087
- var COMPONENT_META_SCHEMA_VERSION = "2.0.0";
1088
- var ACTION_SPEC_VERSION = "2.0.0";
1089
- var DATA_QUERY_SPEC_VERSION = "2.0.0";
1090
1181
 
1091
- exports.ACTION_SPEC_VERSION = ACTION_SPEC_VERSION;
1092
1182
  exports.ALLOWED_FUNCTION_NAMES = ALLOWED_FUNCTION_NAMES;
1093
1183
  exports.ALL_BUILTIN_FUNCTIONS = ALL_BUILTIN_FUNCTIONS;
1094
1184
  exports.ARRAY_FUNCTIONS = ARRAY_FUNCTIONS;
1095
1185
  exports.ActionType = ActionType;
1096
1186
  exports.AuditAction = AuditAction;
1097
- exports.COMPONENT_META_SCHEMA_VERSION = COMPONENT_META_SCHEMA_VERSION;
1098
1187
  exports.CURRENT_ACTION_SPEC_VERSION = CURRENT_ACTION_SPEC_VERSION;
1099
1188
  exports.CURRENT_COMPONENT_META_VERSION = CURRENT_COMPONENT_META_VERSION;
1100
1189
  exports.CURRENT_DATA_QUERY_SPEC_VERSION = CURRENT_DATA_QUERY_SPEC_VERSION;
1101
1190
  exports.CURRENT_SCHEMA_VERSION = CURRENT_SCHEMA_VERSION;
1102
- exports.DATA_QUERY_SPEC_VERSION = DATA_QUERY_SPEC_VERSION;
1103
1191
  exports.DATE_FUNCTIONS = DATE_FUNCTIONS;
1104
1192
  exports.DEFAULT_EXPRESSION_VALIDATION_CONFIG = DEFAULT_EXPRESSION_VALIDATION_CONFIG;
1193
+ exports.DEPENDENCY_PREFIX = DEPENDENCY_PREFIX;
1105
1194
  exports.ErrorCategory = ErrorCategory;
1106
1195
  exports.ErrorCode = ErrorCode;
1107
1196
  exports.ErrorCodeToHttpStatus = ErrorCodeToHttpStatus;
@@ -1109,21 +1198,21 @@ exports.ErrorMessages = ErrorMessages;
1109
1198
  exports.FORMAT_FUNCTIONS = FORMAT_FUNCTIONS;
1110
1199
  exports.LOGIC_FUNCTIONS = LOGIC_FUNCTIONS;
1111
1200
  exports.NUMBER_FUNCTIONS = NUMBER_FUNCTIONS;
1112
- exports.PAGE_SCHEMA_VERSION = PAGE_SCHEMA_VERSION;
1113
1201
  exports.PROTOCOL_VERSION = PROTOCOL_VERSION;
1114
1202
  exports.PageStatus = PageStatus;
1115
1203
  exports.PublishChannel = PublishChannel;
1116
1204
  exports.PublishStatus = PublishStatus;
1117
1205
  exports.STRING_FUNCTIONS = STRING_FUNCTIONS;
1206
+ exports.TemplateParseError = TemplateParseError;
1118
1207
  exports.UTILITY_FUNCTIONS = UTILITY_FUNCTIONS;
1119
1208
  exports.VERSION = VERSION;
1120
1209
  exports.VersionStatus = VersionStatus;
1121
1210
  exports.bindingRef = bindingRef;
1122
1211
  exports.createDjvlcError = createDjvlcError;
1123
1212
  exports.createExpressionContext = createExpressionContext;
1124
- exports.createLoopContext = createLoopContext;
1213
+ exports.createLoopLocal = createLoopLocal;
1214
+ exports.dep = dep;
1125
1215
  exports.isDjvlcError = isDjvlcError;
1126
- exports.literal = literal;
1127
- exports.queryRef = queryRef;
1216
+ exports.localRef = localRef;
1128
1217
  exports.stateRef = stateRef;
1129
1218
  exports.template = template;
package/dist/index.mjs CHANGED
@@ -991,7 +991,7 @@ var ALL_BUILTIN_FUNCTIONS = [
991
991
  ];
992
992
  var ALLOWED_FUNCTION_NAMES = ALL_BUILTIN_FUNCTIONS.map((f) => f.name);
993
993
  var DEFAULT_EXPRESSION_VALIDATION_CONFIG = {
994
- allowedContextPaths: ["state", "binding", "context", "props", "event"],
994
+ allowedContextPaths: ["state", "binding", "local", "props", "event"],
995
995
  allowedFunctions: ALLOWED_FUNCTION_NAMES,
996
996
  maxDepth: 10,
997
997
  maxLength: 1e3,
@@ -1000,49 +1000,147 @@ var DEFAULT_EXPRESSION_VALIDATION_CONFIG = {
1000
1000
  };
1001
1001
 
1002
1002
  // src/page/expression.ts
1003
+ var DEPENDENCY_PREFIX = {
1004
+ /** 页面状态依赖前缀 */
1005
+ STATE: "state",
1006
+ /** 数据绑定依赖前缀 */
1007
+ BINDING: "binding",
1008
+ /** 局部变量依赖前缀 */
1009
+ LOCAL: "local",
1010
+ /** 组件属性依赖前缀 */
1011
+ PROPS: "props",
1012
+ /** 事件上下文依赖前缀 */
1013
+ EVENT: "event"
1014
+ };
1015
+ function dep(prefix, path) {
1016
+ return `${prefix}.${path}`;
1017
+ }
1003
1018
  function createExpressionContext(partial) {
1004
1019
  return {
1005
1020
  state: {},
1006
1021
  binding: {},
1007
- context: {},
1022
+ local: {},
1008
1023
  ...partial
1009
1024
  };
1010
1025
  }
1011
- function createLoopContext(item, index, extras) {
1026
+ function createLoopLocal(item, index, extras) {
1012
1027
  return {
1028
+ ...extras,
1013
1029
  item,
1014
- index,
1015
- ...extras
1030
+ index
1016
1031
  };
1017
1032
  }
1018
- function literal(value) {
1019
- return value;
1020
- }
1021
1033
  function stateRef(path, fallback) {
1022
1034
  return {
1023
1035
  type: "state",
1024
1036
  value: path,
1025
1037
  fallback,
1026
- meta: { dependencies: [`state.${path}`] }
1038
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.STATE, path)] }
1027
1039
  };
1028
1040
  }
1041
+ var BINDING_ID_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*$/;
1029
1042
  function bindingRef(bindingId, path, fallback) {
1043
+ if (!BINDING_ID_PATTERN.test(bindingId)) {
1044
+ throw new Error(
1045
+ `Invalid bindingId: "${bindingId}". Must match pattern [A-Za-z_][A-Za-z0-9_-]* (no dots allowed).`
1046
+ );
1047
+ }
1030
1048
  return {
1031
1049
  type: "binding",
1032
1050
  value: `${bindingId}.${path}`,
1033
1051
  fallback,
1034
- meta: { dependencies: [`binding.${bindingId}.${path}`] }
1052
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.BINDING, `${bindingId}.${path}`)] }
1035
1053
  };
1036
1054
  }
1037
- function template(templateString, fallback) {
1038
- const dependencies = [];
1055
+ function localRef(path, fallback) {
1056
+ return {
1057
+ type: "local",
1058
+ value: path,
1059
+ fallback,
1060
+ meta: { dependencies: [dep(DEPENDENCY_PREFIX.LOCAL, path)] }
1061
+ };
1062
+ }
1063
+ var FULL_REF_PATTERN = /\b(state|binding|local|props|event)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\.\d+|\[\d+\])*/g;
1064
+ var BARE_PATH_PATTERN = /\b[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\.\d+|\[\d+\])*\b/g;
1065
+ var RESERVED_WORDS = /* @__PURE__ */ new Set([
1066
+ "true",
1067
+ "false",
1068
+ "null",
1069
+ "undefined",
1070
+ "NaN",
1071
+ "Infinity",
1072
+ // 常见运算符相关
1073
+ "typeof",
1074
+ "instanceof",
1075
+ "new",
1076
+ "void",
1077
+ "delete",
1078
+ // 常见内置对象(不应作为依赖)
1079
+ "Math",
1080
+ "Date",
1081
+ "JSON",
1082
+ "Array",
1083
+ "Object",
1084
+ "String",
1085
+ "Number",
1086
+ "Boolean"
1087
+ ]);
1088
+ var KNOWN_PREFIXES = ["state", "binding", "local", "props", "event"];
1089
+ function extractFullRefs(content) {
1090
+ const set = /* @__PURE__ */ new Set();
1091
+ for (const match of content.matchAll(FULL_REF_PATTERN)) {
1092
+ set.add(match[0]);
1093
+ }
1094
+ return [...set];
1095
+ }
1096
+ function findBarePaths(content) {
1097
+ const set = /* @__PURE__ */ new Set();
1098
+ for (const match of content.matchAll(BARE_PATH_PATTERN)) {
1099
+ const token = match[0];
1100
+ if (RESERVED_WORDS.has(token)) continue;
1101
+ if (KNOWN_PREFIXES.some((prefix) => token.startsWith(`${prefix}.`))) {
1102
+ continue;
1103
+ }
1104
+ if (KNOWN_PREFIXES.includes(token)) {
1105
+ continue;
1106
+ }
1107
+ set.add(token);
1108
+ }
1109
+ return [...set];
1110
+ }
1111
+ var TemplateParseError = class extends Error {
1112
+ constructor(message, bareVariables, expression) {
1113
+ super(message);
1114
+ this.bareVariables = bareVariables;
1115
+ this.expression = expression;
1116
+ this.name = "TemplateParseError";
1117
+ }
1118
+ };
1119
+ function parseTemplateDependencies(templateString, mode = "strict") {
1120
+ const depSet = /* @__PURE__ */ new Set();
1039
1121
  const pattern = /\$\{([^}]+)\}/g;
1040
- let match;
1041
- while ((match = pattern.exec(templateString)) !== null) {
1042
- if (match[1]) {
1043
- dependencies.push(match[1]);
1122
+ for (const match of templateString.matchAll(pattern)) {
1123
+ const content = (match[1] ?? "").trim();
1124
+ if (!content) continue;
1125
+ const fullRefs = extractFullRefs(content);
1126
+ for (const ref of fullRefs) {
1127
+ depSet.add(ref);
1128
+ }
1129
+ if (mode === "strict") {
1130
+ const bare = findBarePaths(content);
1131
+ if (bare.length > 0) {
1132
+ throw new TemplateParseError(
1133
+ `Template variable must use prefix (state./binding./local./props./event.). Found bare variables: ${bare.join(", ")} in \${${content}}`,
1134
+ bare,
1135
+ content
1136
+ );
1137
+ }
1044
1138
  }
1045
1139
  }
1140
+ return [...depSet];
1141
+ }
1142
+ function template(templateString, fallback) {
1143
+ const dependencies = parseTemplateDependencies(templateString, "strict");
1046
1144
  return {
1047
1145
  type: "template",
1048
1146
  value: templateString,
@@ -1050,9 +1148,6 @@ function template(templateString, fallback) {
1050
1148
  meta: { dependencies }
1051
1149
  };
1052
1150
  }
1053
- function queryRef(queryId, path, fallback) {
1054
- return bindingRef(queryId, path, fallback);
1055
- }
1056
1151
 
1057
1152
  // src/component/meta.ts
1058
1153
  var CURRENT_COMPONENT_META_VERSION = "2.0.0";
@@ -1079,11 +1174,7 @@ var CURRENT_ACTION_SPEC_VERSION = "2.0.0";
1079
1174
  var CURRENT_DATA_QUERY_SPEC_VERSION = "2.0.0";
1080
1175
 
1081
1176
  // src/index.ts
1082
- var VERSION = "2.1.0";
1177
+ var VERSION = "2.0.0";
1083
1178
  var PROTOCOL_VERSION = "2.0.0";
1084
- var PAGE_SCHEMA_VERSION = "2.0.0";
1085
- var COMPONENT_META_SCHEMA_VERSION = "2.0.0";
1086
- var ACTION_SPEC_VERSION = "2.0.0";
1087
- var DATA_QUERY_SPEC_VERSION = "2.0.0";
1088
1179
 
1089
- export { ACTION_SPEC_VERSION, ALLOWED_FUNCTION_NAMES, ALL_BUILTIN_FUNCTIONS, ARRAY_FUNCTIONS, ActionType, AuditAction, COMPONENT_META_SCHEMA_VERSION, CURRENT_ACTION_SPEC_VERSION, CURRENT_COMPONENT_META_VERSION, CURRENT_DATA_QUERY_SPEC_VERSION, CURRENT_SCHEMA_VERSION, DATA_QUERY_SPEC_VERSION, DATE_FUNCTIONS, DEFAULT_EXPRESSION_VALIDATION_CONFIG, ErrorCategory, ErrorCode, ErrorCodeToHttpStatus, ErrorMessages, FORMAT_FUNCTIONS, LOGIC_FUNCTIONS, NUMBER_FUNCTIONS, PAGE_SCHEMA_VERSION, PROTOCOL_VERSION, PageStatus, PublishChannel, PublishStatus, STRING_FUNCTIONS, UTILITY_FUNCTIONS, VERSION, VersionStatus, bindingRef, createDjvlcError, createExpressionContext, createLoopContext, isDjvlcError, literal, queryRef, stateRef, template };
1180
+ export { ALLOWED_FUNCTION_NAMES, ALL_BUILTIN_FUNCTIONS, ARRAY_FUNCTIONS, ActionType, AuditAction, CURRENT_ACTION_SPEC_VERSION, CURRENT_COMPONENT_META_VERSION, CURRENT_DATA_QUERY_SPEC_VERSION, CURRENT_SCHEMA_VERSION, DATE_FUNCTIONS, DEFAULT_EXPRESSION_VALIDATION_CONFIG, DEPENDENCY_PREFIX, ErrorCategory, ErrorCode, ErrorCodeToHttpStatus, ErrorMessages, FORMAT_FUNCTIONS, LOGIC_FUNCTIONS, NUMBER_FUNCTIONS, PROTOCOL_VERSION, PageStatus, PublishChannel, PublishStatus, STRING_FUNCTIONS, TemplateParseError, UTILITY_FUNCTIONS, VERSION, VersionStatus, bindingRef, createDjvlcError, createExpressionContext, createLoopLocal, dep, isDjvlcError, localRef, stateRef, template };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djvlc/contracts-types",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "DJV Low-code Platform - TypeScript 类型定义包",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",