@tsrx/language-server 0.3.137 → 0.4.0

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,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- const require_server = require('./server-iYrbcyFX.js');
2
+ const require_server = require('./server-CeY99KYo.js');
3
3
 
4
4
  //#region src/language-server.js
5
5
  require_server.createTsrxLanguageServer();
@@ -958,6 +958,7 @@ function resolve_inherited_config_value(layers, read_layer) {
958
958
  //#endregion
959
959
  //#region ../typescript-plugin/src/consumer-compiler.js
960
960
  /** @typedef {{actual_type: string, actual_value: string}} InvalidConfigValueDetails */
961
+ /** @typedef {'web' | 'ios' | 'android'} Platform */
961
962
  /**
962
963
  * @typedef {
963
964
  * | {state: 'absent'}
@@ -966,11 +967,19 @@ function resolve_inherited_config_value(layers, read_layer) {
966
967
  * } CompilerDeclaration
967
968
  */
968
969
  /**
970
+ * @typedef {
971
+ * | {state: 'absent'}
972
+ * | {state: 'declared', value: Platform}
973
+ * | ({state: 'invalid', target: 'tsrx' | 'platform'} & InvalidConfigValueDetails)
974
+ * } PlatformDeclaration
975
+ */
976
+ /**
969
977
  * @typedef {object} CompilerResolutionOptions
970
978
  * @property {typeof import('typescript')} [ts]
971
979
  * @property {string} [configFileName]
972
980
  * @property {import('./tsconfig-resolution.js').TsconfigHost} [configHost]
973
981
  * @property {Set<string>} [dependencies]
982
+ * @property {boolean} [requirePlatformResolution]
974
983
  */
975
984
  /**
976
985
  * @typedef {object} ConfigHostResolutionCache
@@ -1050,6 +1059,32 @@ function get_compiler_declaration(config) {
1050
1059
  };
1051
1060
  }
1052
1061
  /**
1062
+ * @param {unknown} config
1063
+ * @returns {PlatformDeclaration}
1064
+ */
1065
+ function get_platform_declaration(config) {
1066
+ const tsrx_result = get_own_config_value(config, ["tsrx"]);
1067
+ if (tsrx_result.state === "absent") return { state: "absent" };
1068
+ const tsrx_value = tsrx_result.value;
1069
+ if (tsrx_value === null || typeof tsrx_value !== "object" || Array.isArray(tsrx_value)) return {
1070
+ state: "invalid",
1071
+ target: "tsrx",
1072
+ ...describe_config_value(tsrx_value)
1073
+ };
1074
+ const platform_result = get_own_config_value(tsrx_value, ["platform"]);
1075
+ if (platform_result.state === "absent") return { state: "absent" };
1076
+ const platform = platform_result.value;
1077
+ if (platform === "web" || platform === "ios" || platform === "android") return {
1078
+ state: "declared",
1079
+ value: platform
1080
+ };
1081
+ return {
1082
+ state: "invalid",
1083
+ target: "platform",
1084
+ ...describe_config_value(platform)
1085
+ };
1086
+ }
1087
+ /**
1053
1088
  * Find the nearest tsconfig.json to use as the root of inheritance resolution.
1054
1089
  * @param {string} start_dir
1055
1090
  * @param {import('./tsconfig-resolution.js').TsconfigHost} host
@@ -1086,11 +1121,11 @@ function get_nearest_root_tsconfig(start_dir, host, cache) {
1086
1121
  * @param {string} root_config_path
1087
1122
  * @param {ConfigHostResolutionCache} cache
1088
1123
  */
1089
- function get_tsconfig_layers(typescript$2, host, root_config_path, cache) {
1124
+ function get_tsconfig_layers(typescript$3, host, root_config_path, cache) {
1090
1125
  const cache_key = get_config_cache_key(root_config_path, host);
1091
1126
  const cached = cache.resolved_config_layers.get(cache_key);
1092
1127
  if (cached && (cached.dependency_modified_times === void 0 || [...cached.dependency_modified_times].every(([dependency, modified_time]) => host.getModifiedTime?.(dependency)?.valueOf() === modified_time))) return cached.value;
1093
- const value = load_tsconfig_layers(typescript$2, host, root_config_path);
1128
+ const value = load_tsconfig_layers(typescript$3, host, root_config_path);
1094
1129
  const dependency_modified_times = host.getModifiedTime ? new Map(value.dependencies.map((dependency) => [dependency, host.getModifiedTime?.(dependency)?.valueOf()])) : void 0;
1095
1130
  cache.resolved_config_layers.set(cache_key, {
1096
1131
  value,
@@ -1120,12 +1155,12 @@ function is_extends_failure_overridden(layers, failure, declaration_config_path)
1120
1155
  * @param {string} config_path
1121
1156
  * @param {string} specifier
1122
1157
  */
1123
- function resolve_uncached_declared_compiler(typescript$3, host, config_path, specifier) {
1124
- return typescript$3.resolveModuleName(specifier, config_path, {
1125
- module: typescript$3.ModuleKind.Node16,
1126
- moduleResolution: typescript$3.ModuleResolutionKind.Node16,
1158
+ function resolve_uncached_declared_compiler(typescript$4, host, config_path, specifier) {
1159
+ return typescript$4.resolveModuleName(specifier, config_path, {
1160
+ module: typescript$4.ModuleKind.Node16,
1161
+ moduleResolution: typescript$4.ModuleResolutionKind.Node16,
1127
1162
  noDtsResolution: true
1128
- }, host, void 0, void 0, typescript$3.ModuleKind.CommonJS).resolvedModule?.resolvedFileName;
1163
+ }, host, void 0, void 0, typescript$4.ModuleKind.CommonJS).resolvedModule?.resolvedFileName;
1129
1164
  }
1130
1165
  /**
1131
1166
  * Resolve the compiler explicitly selected by a consumer tsconfig. Invalid
@@ -1137,7 +1172,7 @@ function resolve_uncached_declared_compiler(typescript$3, host, config_path, spe
1137
1172
  * @param {string} specifier
1138
1173
  * @returns {string | null}
1139
1174
  */
1140
- function resolve_declared_compiler(typescript$4, host, config_path, specifier) {
1175
+ function resolve_declared_compiler(typescript$5, host, config_path, specifier) {
1141
1176
  const cache_key = `${config_path}\0${specifier}`;
1142
1177
  if (declared_compiler_path_map.has(cache_key)) return declared_compiler_path_map.get(cache_key) ?? null;
1143
1178
  if (!bare_package_specifier_pattern.test(specifier)) {
@@ -1151,7 +1186,7 @@ function resolve_declared_compiler(typescript$4, host, config_path, specifier) {
1151
1186
  log$10("Found declared tsrx compiler at:", compiler_path, "from tsconfig:", config_path);
1152
1187
  return compiler_path;
1153
1188
  } catch {
1154
- const compiler_path = resolve_uncached_declared_compiler(typescript$4, host, config_path, specifier);
1189
+ const compiler_path = resolve_uncached_declared_compiler(typescript$5, host, config_path, specifier);
1155
1190
  if (compiler_path !== void 0) {
1156
1191
  declared_compiler_path_map.set(cache_key, compiler_path);
1157
1192
  log$10("Found declared tsrx compiler after uncached retry at:", compiler_path, "from tsconfig:", config_path);
@@ -1169,12 +1204,12 @@ function resolve_declared_compiler(typescript$4, host, config_path, specifier) {
1169
1204
  * @returns {string | null | undefined}
1170
1205
  */
1171
1206
  function resolve_consumer_compiler_for_file(normalized_file_name, options = {}) {
1172
- const typescript$5 = options.ts ?? typescript.default;
1173
- const config_host = options.configHost ?? typescript$5.sys;
1207
+ const typescript$6 = options.ts ?? typescript.default;
1208
+ const config_host = options.configHost ?? typescript$6.sys;
1174
1209
  const host_cache = get_config_host_cache(config_host);
1175
1210
  const root_config_path = options.configFileName ?? get_nearest_root_tsconfig(path.default.dirname(normalized_file_name), config_host, host_cache);
1176
1211
  if (root_config_path === null) return;
1177
- const resolved_layers = get_tsconfig_layers(typescript$5, config_host, root_config_path, host_cache);
1212
+ const resolved_layers = get_tsconfig_layers(typescript$6, config_host, root_config_path, host_cache);
1178
1213
  for (const dependency of resolved_layers.dependencies) options.dependencies?.add(dependency);
1179
1214
  const unreadable_layers = resolved_layers.layers.filter((layer) => layer.raw_source === void 0);
1180
1215
  if (unreadable_layers.length > 0) {
@@ -1189,7 +1224,7 @@ function resolve_consumer_compiler_for_file(normalized_file_name, options = {})
1189
1224
  return tsrx_key_pattern.test(layer.raw_source);
1190
1225
  });
1191
1226
  const log_parse_error = has_tsrx_intent ? logError$5 : logWarning$1;
1192
- for (const layer of malformed_layers) for (const diagnostic of layer.parse_diagnostics) log_parse_error("Unable to parse tsconfig layer:", layer.path, typescript$5.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
1227
+ for (const layer of malformed_layers) for (const diagnostic of layer.parse_diagnostics) log_parse_error("Unable to parse tsconfig layer:", layer.path, typescript$6.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
1193
1228
  return has_tsrx_intent ? null : void 0;
1194
1229
  }
1195
1230
  const declaration = resolve_inherited_config_value(resolved_layers.layers, (layer) => get_compiler_declaration(layer.config));
@@ -1204,11 +1239,64 @@ function resolve_consumer_compiler_for_file(normalized_file_name, options = {})
1204
1239
  logError$5("Unable to resolve tsconfig extends entry:", JSON.stringify(failure.extends_value), `in ${failure.config_path}`);
1205
1240
  continue;
1206
1241
  }
1207
- for (const diagnostic of failure.diagnostics) logError$5("Unable to resolve tsconfig extends entry:", JSON.stringify(failure.extends_value), `in ${failure.config_path}`, typescript$5.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
1242
+ for (const diagnostic of failure.diagnostics) logError$5("Unable to resolve tsconfig extends entry:", JSON.stringify(failure.extends_value), `in ${failure.config_path}`, typescript$6.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
1208
1243
  }
1209
1244
  return null;
1210
1245
  }
1211
- if (declaration.state === "declared") return resolve_declared_compiler(typescript$5, config_host, declaration.config_path, declaration.value);
1246
+ if (declaration.state === "declared") return resolve_declared_compiler(typescript$6, config_host, declaration.config_path, declaration.value);
1247
+ }
1248
+ /**
1249
+ * Resolve the compile-time platform selected by the active TypeScript project.
1250
+ * The same inheritance graph and dependency tracking as compiler selection are
1251
+ * used so nested projects and config edits cannot silently drift.
1252
+ *
1253
+ * @param {string} normalized_file_name
1254
+ * @param {CompilerResolutionOptions} [options]
1255
+ * @returns {Platform | undefined}
1256
+ */
1257
+ function resolve_consumer_platform_for_file(normalized_file_name, options = {}) {
1258
+ const typescript$7 = options.ts ?? typescript.default;
1259
+ const config_host = options.configHost ?? typescript$7.sys;
1260
+ const host_cache = get_config_host_cache(config_host);
1261
+ const root_config_path = options.configFileName ?? get_nearest_root_tsconfig(path.default.dirname(normalized_file_name), config_host, host_cache);
1262
+ if (root_config_path === null) return;
1263
+ const resolved_layers = get_tsconfig_layers(typescript$7, config_host, root_config_path, host_cache);
1264
+ for (const dependency of resolved_layers.dependencies) options.dependencies?.add(dependency);
1265
+ const unreadable_layer = resolved_layers.layers.find((layer) => layer.raw_source === void 0);
1266
+ if (unreadable_layer) {
1267
+ const message = `Unable to read tsconfig layer while resolving TSRX platform: ${unreadable_layer.path}`;
1268
+ logError$5(message);
1269
+ throw new Error(message);
1270
+ }
1271
+ const malformed_layers = resolved_layers.layers.filter((layer) => layer.parse_diagnostics.length > 0);
1272
+ if (malformed_layers.length > 0) {
1273
+ if (!resolved_layers.layers.some((layer) => {
1274
+ if (get_own_config_value(layer.config, ["tsrx"]).state === "found") return true;
1275
+ return layer.parse_diagnostics.length > 0 && layer.raw_source !== void 0 && tsrx_key_pattern.test(layer.raw_source);
1276
+ })) return void 0;
1277
+ const layer = malformed_layers[0];
1278
+ const detail = typescript$7.flattenDiagnosticMessageText(layer.parse_diagnostics[0].messageText, "\n");
1279
+ const message = `Unable to parse tsconfig layer while resolving TSRX platform: ${layer.path}: ${detail}`;
1280
+ logError$5(message);
1281
+ throw new Error(message);
1282
+ }
1283
+ const declaration = resolve_inherited_config_value(resolved_layers.layers, (layer) => get_platform_declaration(layer.config));
1284
+ if (declaration.state === "invalid") {
1285
+ const expected = declaration.target === "platform" ? " Expected \"web\", \"ios\", or \"android\"." : "";
1286
+ const message = `Invalid TSRX ${declaration.target} declaration: ${declaration.actual_type} ${declaration.actual_value} in ${declaration.config_path}.${expected}`;
1287
+ logError$5(message);
1288
+ throw new TypeError(message);
1289
+ }
1290
+ const effective_extends_failures = resolved_layers.extends_failures.filter((failure) => declaration.state !== "declared" || !is_extends_failure_overridden(resolved_layers.layers, failure, declaration.config_path));
1291
+ if (effective_extends_failures.length > 0) {
1292
+ if (!options.requirePlatformResolution) return declaration.state === "declared" ? declaration.value : void 0;
1293
+ const failure = effective_extends_failures[0];
1294
+ const detail = failure.diagnostics.length > 0 ? `: ${typescript$7.flattenDiagnosticMessageText(failure.diagnostics[0].messageText, "\n")}` : "";
1295
+ const message = `Unable to resolve tsconfig extends entry ${JSON.stringify(failure.extends_value)} in ${failure.config_path} while resolving TSRX platform${detail}`;
1296
+ logError$5(message);
1297
+ throw new Error(message);
1298
+ }
1299
+ return declaration.state === "declared" ? declaration.value : void 0;
1212
1300
  }
1213
1301
  /** Drop all filesystem-derived consumer compiler resolution state. */
1214
1302
  function reset_consumer_compiler_resolution_caches() {
@@ -1221,7 +1309,7 @@ function reset_consumer_compiler_resolution_caches() {
1221
1309
  /** @import * as AST from 'estree'; */
1222
1310
  /** @import {CompileError, VolarMappingsResult, CodeMapping} from '@tsrx/core/types' */
1223
1311
  /** @typedef {{ code?: string, errors?: CompileError[] }} TSRXCompileResult */
1224
- /** @typedef {{ compile?: (source: string, filename: string, options?: { loose?: boolean }) => TSRXCompileResult, compile_to_volar_mappings(source: string, filename: string, options?: { loose?: boolean }): VolarMappingsResult }} TSRXCompilerModule */
1312
+ /** @typedef {{ compile?: (source: string, filename: string, options?: { loose?: boolean, platform?: 'web' | 'ios' | 'android' }) => TSRXCompileResult, compile_to_volar_mappings(source: string, filename: string, options?: { loose?: boolean, platform?: 'web' | 'ios' | 'android' }): VolarMappingsResult }} TSRXCompilerModule */
1225
1313
  /** @typedef {Map<string, CodeMapping>} CachedMappings */
1226
1314
  /** @typedef {import('typescript').CompilerOptions} CompilerOptions */
1227
1315
  /** @typedef {import('@volar/language-core').IScriptSnapshot} IScriptSnapshot */
@@ -1326,17 +1414,52 @@ function is_tsrx_file(file_name) {
1326
1414
  return TSRX_EXTENSIONS$1.some((extension) => file_name.endsWith(extension));
1327
1415
  }
1328
1416
  /**
1417
+ * Detect exact platform property paths without matching comments or string
1418
+ * literals. TypeScript's scanner remains usable for partially authored TSRX
1419
+ * because it tokenizes independently of the parser's grammar.
1420
+ *
1421
+ * @param {string} source
1422
+ * @param {typeof import('typescript')} [typescript]
1423
+ * @returns {boolean}
1424
+ */
1425
+ function source_uses_platform_flag(source, typescript$1 = typescript.default) {
1426
+ const scanner = typescript$1.createScanner(typescript$1.ScriptTarget.Latest, true, typescript$1.LanguageVariant.JSX, source);
1427
+ /** @type {Array<{ kind: number, text: string }>} */
1428
+ const tokens = [];
1429
+ for (let kind = scanner.scan(); kind !== typescript$1.SyntaxKind.EndOfFileToken; kind = scanner.scan()) tokens.push({
1430
+ kind,
1431
+ text: scanner.getTokenText()
1432
+ });
1433
+ const prefix = [
1434
+ "import",
1435
+ ".",
1436
+ "meta",
1437
+ ".",
1438
+ "env",
1439
+ ".",
1440
+ "platform",
1441
+ "."
1442
+ ];
1443
+ for (let index = 0; index + prefix.length < tokens.length; index += 1) {
1444
+ if (index > 0 && (tokens[index - 1].kind === typescript$1.SyntaxKind.DotToken || tokens[index - 1].kind === typescript$1.SyntaxKind.QuestionDotToken)) continue;
1445
+ if (!prefix.every((text, offset) => tokens[index + offset].text === text)) continue;
1446
+ const name = tokens[index + prefix.length].text;
1447
+ if (name === "web" || name === "ios" || name === "android") return true;
1448
+ }
1449
+ return false;
1450
+ }
1451
+ /**
1329
1452
  * @param {CompilerResolutionOptions} [options]
1330
1453
  * @returns {TsrxLanguagePlugin}
1331
1454
  */
1332
1455
  function getTsrxLanguagePlugin(options = {}) {
1333
1456
  log$9("Creating TSRX language plugin...");
1334
- const typescript$1 = options.ts ?? typescript.default;
1457
+ const typescript$2 = options.ts ?? typescript.default;
1335
1458
  /** @type {CompilerResolutionOptions} */
1336
1459
  const compiler_resolution_options = {
1337
1460
  ...options,
1338
- ts: typescript$1,
1339
- configHost: options.configHost ?? typescript$1.sys
1461
+ ts: typescript$2,
1462
+ configHost: options.configHost ?? typescript$2.sys
1340
1463
  };
1341
1464
  return {
1342
1465
  getLanguageId(fileNameOrUri) {
@@ -1356,7 +1479,10 @@ function getTsrxLanguagePlugin(options = {}) {
1356
1479
  }
1357
1480
  log$9("Creating virtual code for:", file_name);
1358
1481
  try {
1359
- return new TSRXVirtualCode(file_name, snapshot, compiler);
1482
+ return new TSRXVirtualCode(file_name, snapshot, compiler, (required) => resolve_consumer_platform_for_file(file_name, {
1483
+ ...compiler_resolution_options,
1484
+ requirePlatformResolution: required
1485
+ }));
1360
1486
  } catch (err) {
1361
1487
  logError$4("Failed to create virtual code for:", file_name, ":", err);
1362
1488
  throw err;
@@ -1428,6 +1554,8 @@ var TSRXVirtualCode = class {
1428
1554
  codegenStacks = [];
1429
1555
  /** @type {TSRXCompilerModule} */
1430
1556
  tsrx;
1557
+ /** @type {((required: boolean) => 'web' | 'ios' | 'android' | undefined) | undefined} */
1558
+ platformProvider;
1431
1559
  /** @type {string} */
1432
1560
  generatedCode = "";
1433
1561
  /** @type {VirtualCode['embeddedCodes']} */
@@ -1458,11 +1586,13 @@ var TSRXVirtualCode = class {
1458
1586
  * @param {string} file_name
1459
1587
  * @param {IScriptSnapshot} snapshot
1460
1588
  * @param {TSRXCompilerModule} tsrx
1589
+ * @param {(required: boolean) => 'web' | 'ios' | 'android' | undefined} [platform_provider]
1461
1590
  */
1462
- constructor(file_name, snapshot, tsrx) {
1591
+ constructor(file_name, snapshot, tsrx, platform_provider) {
1463
1592
  log$9("Initializing TSRXVirtualCode for:", file_name);
1464
1593
  this.fileName = file_name;
1465
1594
  this.tsrx = tsrx;
1595
+ this.platformProvider = platform_provider;
1466
1596
  this.snapshot = snapshot;
1467
1597
  this.sourceSnapshot = snapshot;
1468
1598
  this.originalCode = snapshot.getText(0, snapshot.getLength());
@@ -1513,11 +1643,15 @@ var TSRXVirtualCode = class {
1513
1643
  }
1514
1644
  }
1515
1645
  try {
1646
+ const platform = this.platformProvider?.(source_uses_platform_flag(newCode));
1516
1647
  if (isDotTyped && dotPosition >= 0) {
1517
1648
  this.isDotCompletionMode = true;
1518
1649
  const codeWithoutDot = newCode.substring(0, dotPosition) + newCode.substring(dotPosition + 1);
1519
1650
  log$9("Compiling without typed dot at position", dotPosition);
1520
- transpiled = this.tsrx.compile_to_volar_mappings(codeWithoutDot, this.fileName, { loose: true });
1651
+ transpiled = this.tsrx.compile_to_volar_mappings(codeWithoutDot, this.fileName, {
1652
+ loose: true,
1653
+ platform
1654
+ });
1521
1655
  log$9("Compilation without dot successful");
1522
1656
  if (transpiled && transpiled.code && transpiled.mappings.length > 0) {
1523
1657
  const insertedDotPosition = restore_typed_dot_in_transpiled_code(transpiled, dotPosition);
@@ -1526,7 +1660,10 @@ var TSRXVirtualCode = class {
1526
1660
  }
1527
1661
  } else {
1528
1662
  log$9("Compiling TSRX code...");
1529
- transpiled = this.tsrx.compile_to_volar_mappings(newCode, this.fileName, { loose: true });
1663
+ transpiled = this.tsrx.compile_to_volar_mappings(newCode, this.fileName, {
1664
+ loose: true,
1665
+ platform
1666
+ });
1530
1667
  log$9("Compilation successful, generated code length:", transpiled?.code?.length || 0);
1531
1668
  }
1532
1669
  } catch (e) {
@@ -23562,4 +23699,4 @@ Object.defineProperty(exports, 'createTsrxLanguageServer', {
23562
23699
  return createTsrxLanguageServer;
23563
23700
  }
23564
23701
  });
23565
- //# sourceMappingURL=server-iYrbcyFX.js.map
23702
+ //# sourceMappingURL=server-CeY99KYo.js.map