@formspec/build 0.1.0-alpha.48 → 0.1.0-alpha.49
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/analyzer/class-analyzer.d.ts.map +1 -1
- package/dist/analyzer/tsdoc-parser.d.ts.map +1 -1
- package/dist/cli.cjs +113 -3
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +113 -3
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +112 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +112 -2
- package/dist/index.js.map +1 -1
- package/dist/internals.cjs +98 -1
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.js +98 -1
- package/dist/internals.js.map +1 -1
- package/package.json +1 -1
package/dist/internals.cjs
CHANGED
|
@@ -1034,6 +1034,92 @@ function supportsConstraintCapability(type, checker, capability) {
|
|
|
1034
1034
|
}
|
|
1035
1035
|
return false;
|
|
1036
1036
|
}
|
|
1037
|
+
var MAX_HINT_CANDIDATES = 5;
|
|
1038
|
+
var MAX_HINT_DEPTH = 3;
|
|
1039
|
+
function stripHintNullishUnion(type) {
|
|
1040
|
+
if (!type.isUnion()) {
|
|
1041
|
+
return type;
|
|
1042
|
+
}
|
|
1043
|
+
const nonNullish = type.types.filter(
|
|
1044
|
+
(member) => (member.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) === 0
|
|
1045
|
+
);
|
|
1046
|
+
if (nonNullish.length === 1 && nonNullish[0] !== void 0) {
|
|
1047
|
+
return nonNullish[0];
|
|
1048
|
+
}
|
|
1049
|
+
return type;
|
|
1050
|
+
}
|
|
1051
|
+
function isCallableType(type) {
|
|
1052
|
+
return type.getCallSignatures().length > 0 || type.getConstructSignatures().length > 0;
|
|
1053
|
+
}
|
|
1054
|
+
function isUserEmittableHintProperty(property, declaration) {
|
|
1055
|
+
if (property.name.startsWith("__")) {
|
|
1056
|
+
return false;
|
|
1057
|
+
}
|
|
1058
|
+
if ("name" in declaration && declaration.name !== void 0) {
|
|
1059
|
+
const name = declaration.name;
|
|
1060
|
+
if (ts.isComputedPropertyName(name) || ts.isPrivateIdentifier(name)) {
|
|
1061
|
+
return false;
|
|
1062
|
+
}
|
|
1063
|
+
if (!ts.isIdentifier(name) && !ts.isStringLiteral(name) && !ts.isNumericLiteral(name)) {
|
|
1064
|
+
return false;
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
return true;
|
|
1068
|
+
}
|
|
1069
|
+
function collectObjectSubfieldCandidates(type, checker, capability) {
|
|
1070
|
+
const out = [];
|
|
1071
|
+
const visit = (current, prefix, depth) => {
|
|
1072
|
+
if (depth > MAX_HINT_DEPTH) {
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
const stripped = stripHintNullishUnion(current);
|
|
1076
|
+
if (isCallableType(stripped)) {
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
if (!(0, import_internal.hasTypeSemanticCapability)(stripped, checker, "object-like")) {
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
for (const property of stripped.getProperties()) {
|
|
1083
|
+
const declaration = property.valueDeclaration ?? property.declarations?.[0];
|
|
1084
|
+
if (declaration === void 0) {
|
|
1085
|
+
continue;
|
|
1086
|
+
}
|
|
1087
|
+
if (!isUserEmittableHintProperty(property, declaration)) {
|
|
1088
|
+
continue;
|
|
1089
|
+
}
|
|
1090
|
+
const propertyType = checker.getTypeOfSymbolAtLocation(property, declaration);
|
|
1091
|
+
const path3 = [...prefix, property.name];
|
|
1092
|
+
if (supportsConstraintCapability(propertyType, checker, capability)) {
|
|
1093
|
+
out.push(path3.join("."));
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
const strippedPropertyType = stripHintNullishUnion(propertyType);
|
|
1097
|
+
if (!isCallableType(strippedPropertyType) && (0, import_internal.hasTypeSemanticCapability)(strippedPropertyType, checker, "object-like")) {
|
|
1098
|
+
visit(strippedPropertyType, path3, depth + 1);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
visit(type, [], 0);
|
|
1103
|
+
return out;
|
|
1104
|
+
}
|
|
1105
|
+
function buildPathTargetHint(subjectType, checker, capability, tagName, argumentText) {
|
|
1106
|
+
if (!(0, import_internal.hasTypeSemanticCapability)(subjectType, checker, "object-like")) {
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
const candidates = collectObjectSubfieldCandidates(subjectType, checker, capability);
|
|
1110
|
+
const primary = candidates[0];
|
|
1111
|
+
if (primary === void 0) {
|
|
1112
|
+
return null;
|
|
1113
|
+
}
|
|
1114
|
+
const argText = argumentText?.trim() ?? "";
|
|
1115
|
+
const renderExample = (path3) => argText === "" ? `@${tagName} :${path3}` : `@${tagName} :${path3} ${argText}`;
|
|
1116
|
+
if (candidates.length === 1) {
|
|
1117
|
+
return `Hint: use a path target to constrain a subfield, e.g. ${renderExample(primary)}`;
|
|
1118
|
+
}
|
|
1119
|
+
const shown = candidates.slice(0, MAX_HINT_CANDIDATES);
|
|
1120
|
+
const overflow = candidates.length > MAX_HINT_CANDIDATES ? ", \u2026" : "";
|
|
1121
|
+
return `Hint: use a path target to constrain a subfield (candidates: ${shown.join(", ")}${overflow}), e.g. ${renderExample(primary)}`;
|
|
1122
|
+
}
|
|
1037
1123
|
function makeDiagnostic(code, message, provenance) {
|
|
1038
1124
|
return {
|
|
1039
1125
|
code,
|
|
@@ -1201,10 +1287,18 @@ function buildCompilerBackedConstraintDiagnostics(node, sourceFile, tagName, par
|
|
|
1201
1287
|
const requiredCapability = definition.capabilities[0];
|
|
1202
1288
|
if (requiredCapability !== void 0 && !supportsConstraintCapability(subjectType, checker, requiredCapability)) {
|
|
1203
1289
|
const actualType = checker.typeToString(subjectType, node, SYNTHETIC_TYPE_FORMAT_FLAGS);
|
|
1290
|
+
const baseMessage = `Target "${node.getText(sourceFile)}": constraint "${tagName}" is only valid on ${capabilityLabel(requiredCapability)} targets, but field type is "${actualType}"`;
|
|
1291
|
+
const hint = buildPathTargetHint(
|
|
1292
|
+
subjectType,
|
|
1293
|
+
checker,
|
|
1294
|
+
requiredCapability,
|
|
1295
|
+
tagName,
|
|
1296
|
+
parsedTag?.argumentText
|
|
1297
|
+
);
|
|
1204
1298
|
return [
|
|
1205
1299
|
makeDiagnostic(
|
|
1206
1300
|
"TYPE_MISMATCH",
|
|
1207
|
-
|
|
1301
|
+
hint === null ? baseMessage : `${baseMessage}. ${hint}`,
|
|
1208
1302
|
provenance
|
|
1209
1303
|
)
|
|
1210
1304
|
];
|
|
@@ -2854,6 +2948,9 @@ function resolveTypeNode(type, checker, file, typeRegistry, visiting, sourceNode
|
|
|
2854
2948
|
if (type.flags & ts3.TypeFlags.Undefined) {
|
|
2855
2949
|
return { kind: "primitive", primitiveKind: "null" };
|
|
2856
2950
|
}
|
|
2951
|
+
if (type.flags & ts3.TypeFlags.Void) {
|
|
2952
|
+
return { kind: "primitive", primitiveKind: "null" };
|
|
2953
|
+
}
|
|
2857
2954
|
if (type.isStringLiteral()) {
|
|
2858
2955
|
return {
|
|
2859
2956
|
kind: "enum",
|