@formspec/build 0.1.0-alpha.52 → 0.1.0-alpha.53

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.
@@ -1044,39 +1044,85 @@ function isNonReferenceIdentifier(node) {
1044
1044
  }
1045
1045
  return false;
1046
1046
  }
1047
- function statementReferencesImportedName(statement, importedNames) {
1047
+ function astReferencesImportedName(root, importedNames) {
1048
1048
  if (importedNames.size === 0) {
1049
1049
  return false;
1050
1050
  }
1051
- let referencesImportedName = false;
1051
+ let found = false;
1052
1052
  const visit = (node) => {
1053
- if (referencesImportedName) {
1054
- return;
1055
- }
1053
+ if (found) return;
1056
1054
  if (ts4.isIdentifier(node) && importedNames.has(node.text) && !isNonReferenceIdentifier(node)) {
1057
- referencesImportedName = true;
1055
+ found = true;
1058
1056
  return;
1059
1057
  }
1060
1058
  ts4.forEachChild(node, visit);
1061
1059
  };
1062
- visit(statement);
1063
- return referencesImportedName;
1060
+ visit(root);
1061
+ return found;
1062
+ }
1063
+ function getObjectMembers(statement) {
1064
+ if (ts4.isInterfaceDeclaration(statement)) {
1065
+ return statement.members;
1066
+ }
1067
+ if (ts4.isTypeLiteralNode(statement.type)) {
1068
+ return statement.type.members;
1069
+ }
1070
+ return void 0;
1071
+ }
1072
+ function rewriteImportedMemberTypes(statement, sourceFile, importedNames) {
1073
+ const members = getObjectMembers(statement);
1074
+ if (members === void 0) {
1075
+ return null;
1076
+ }
1077
+ const replacements = [];
1078
+ for (const member of members) {
1079
+ if (!ts4.isPropertySignature(member)) {
1080
+ if (astReferencesImportedName(member, importedNames)) {
1081
+ return null;
1082
+ }
1083
+ continue;
1084
+ }
1085
+ const typeAnnotation = member.type;
1086
+ if (typeAnnotation === void 0) continue;
1087
+ if (astReferencesImportedName(typeAnnotation, importedNames)) {
1088
+ replacements.push({
1089
+ start: typeAnnotation.getStart(sourceFile),
1090
+ end: typeAnnotation.getEnd()
1091
+ });
1092
+ }
1093
+ }
1094
+ if (replacements.length === 0) {
1095
+ return statement.getText(sourceFile);
1096
+ }
1097
+ const stmtStart = statement.getStart(sourceFile);
1098
+ let result = statement.getText(sourceFile);
1099
+ for (const { start, end } of [...replacements].reverse()) {
1100
+ result = result.slice(0, start - stmtStart) + "unknown" + result.slice(end - stmtStart);
1101
+ }
1102
+ return result;
1064
1103
  }
1065
1104
  function buildSupportingDeclarations(sourceFile, extensionTypeNames) {
1066
1105
  const importedNames = collectImportedNames(sourceFile);
1067
1106
  const importedNamesToSkip = new Set(
1068
1107
  [...importedNames].filter((name) => !extensionTypeNames.has(name))
1069
1108
  );
1070
- return sourceFile.statements.filter((statement) => {
1071
- if (ts4.isImportDeclaration(statement)) return false;
1072
- if (ts4.isImportEqualsDeclaration(statement)) return false;
1073
- if (ts4.isExportDeclaration(statement) && statement.moduleSpecifier !== void 0)
1074
- return false;
1075
- if (statementReferencesImportedName(statement, importedNamesToSkip)) {
1076
- return false;
1109
+ const result = [];
1110
+ for (const statement of sourceFile.statements) {
1111
+ if (ts4.isImportDeclaration(statement)) continue;
1112
+ if (ts4.isImportEqualsDeclaration(statement)) continue;
1113
+ if (ts4.isExportDeclaration(statement) && statement.moduleSpecifier !== void 0) continue;
1114
+ if (!astReferencesImportedName(statement, importedNamesToSkip)) {
1115
+ result.push(statement.getText(sourceFile));
1116
+ continue;
1077
1117
  }
1078
- return true;
1079
- }).map((statement) => statement.getText(sourceFile));
1118
+ if (ts4.isInterfaceDeclaration(statement) || ts4.isTypeAliasDeclaration(statement)) {
1119
+ const rewritten = rewriteImportedMemberTypes(statement, sourceFile, importedNamesToSkip);
1120
+ if (rewritten !== null) {
1121
+ result.push(rewritten);
1122
+ }
1123
+ }
1124
+ }
1125
+ return result;
1080
1126
  }
1081
1127
  function pushUniqueCompilerDiagnostics(target, additions) {
1082
1128
  for (const diagnostic of additions) {