@tsplc/compiler 0.6.0 → 0.6.1

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
@@ -2999,7 +2999,7 @@ class Analyzer {
2999
2999
  sources;
3000
3000
  context;
3001
3001
  moduleScope;
3002
- constructor(program, host, root, sdk, diagnostics, positions, serviceRecord) {
3002
+ constructor(program, host, root, sdk, diagnostics, positions, serviceRecord, reduced) {
3003
3003
  this.program = program;
3004
3004
  this.host = host;
3005
3005
  this.root = root;
@@ -3029,7 +3029,7 @@ class Analyzer {
3029
3029
  variables: new Map
3030
3030
  });
3031
3031
  const roots = new Set(program.getRootFileNames());
3032
- this.sources = runtimeSources(program).filter((source) => source.fileName !== sdk && !inSdk(sdk, source.fileName) && !program.isSourceFileDefaultLibrary(source) && (!source.isDeclarationFile || roots.has(source.fileName)));
3032
+ this.sources = runtimeSources(program).filter((source) => source.fileName !== sdk && !reduced.has(source.fileName) && !inSdk(sdk, source.fileName) && !program.isSourceFileDefaultLibrary(source) && (!source.isDeclarationFile || roots.has(source.fileName)));
3033
3033
  }
3034
3034
  fail = (node, code, message) => {
3035
3035
  throw new SemanticError(error2(code, message, this.positions.node(node)));
@@ -14201,6 +14201,7 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
14201
14201
  const origins = new Map;
14202
14202
  const folded = [];
14203
14203
  const literals = new Map;
14204
+ const reduced = new Set;
14204
14205
  const references = [];
14205
14206
  const referenceFields = [];
14206
14207
  const result = {
@@ -14209,6 +14210,7 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
14209
14210
  origins,
14210
14211
  folded,
14211
14212
  literals,
14213
+ reduced,
14212
14214
  references: { objects: references, fields: referenceFields }
14213
14215
  };
14214
14216
  const runtime = plcSources(program).filter((source) => !inSdk(sdk, source.fileName));
@@ -14336,9 +14338,21 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
14336
14338
  });
14337
14339
  }
14338
14340
  }
14341
+ const survives = (symbol) => {
14342
+ const declaration = symbol.valueDeclaration;
14343
+ if (!(symbol.flags & ts40.SymbolFlags.Value) || !declaration) {
14344
+ return true;
14345
+ }
14346
+ const file = declaration.getSourceFile().fileName;
14347
+ if (file === entryFile) {
14348
+ return false;
14349
+ }
14350
+ return isKept(file) || !runtime.some((source) => source.fileName === file) || literalConstant(checker, symbol) !== undefined || serviceClients(declaration.getSourceFile(), checker, sdk).some((statement) => within(declaration, statement));
14351
+ };
14339
14352
  for (const source of runtime) {
14340
14353
  if (source.fileName !== entryFile && !isKept(source.fileName)) {
14341
- sources.set(source.fileName, typesOnly(source, checker, sdk));
14354
+ sources.set(source.fileName, typesOnly(source, checker, sdk, survives));
14355
+ reduced.add(source.fileName);
14342
14356
  }
14343
14357
  }
14344
14358
  } catch (cause) {
@@ -14415,14 +14429,95 @@ var storageGlobals = (specializer, retention, item) => {
14415
14429
  }
14416
14430
  return [...sdkImports(), ...lines];
14417
14431
  };
14418
- var typesOnly = (source, checker, sdk) => {
14432
+ var typesOnly = (source, checker, sdk, survives) => {
14419
14433
  const clients = serviceClients(source, checker, sdk);
14420
14434
  return joined([
14421
- ...source.statements.filter((statement) => ts40.isInterfaceDeclaration(statement) || ts40.isTypeAliasDeclaration(statement) || ts40.isImportDeclaration(statement) && (statement.importClause?.isTypeOnly || clients.length > 0 && importsSdk(statement)) || clients.includes(statement)).map(copied),
14435
+ ...source.statements.flatMap((statement) => {
14436
+ if (ts40.isInterfaceDeclaration(statement) || ts40.isTypeAliasDeclaration(statement) || clients.includes(statement)) {
14437
+ return [copied(statement)];
14438
+ }
14439
+ if (ts40.isImportDeclaration(statement)) {
14440
+ return statement.importClause?.isTypeOnly || clients.length > 0 && importsSdk(statement) ? [copied(statement)] : typeImports(statement, checker, survives);
14441
+ }
14442
+ if (ts40.isExportDeclaration(statement) && statement.moduleSpecifier) {
14443
+ return reExports(statement, checker, survives);
14444
+ }
14445
+ return [];
14446
+ }),
14447
+ ...literalConstants(source, checker),
14422
14448
  "export {};"
14423
14449
  ], `
14424
14450
  `);
14425
14451
  };
14452
+ var typeImports = (statement, checker, survives) => {
14453
+ const clause = statement.importClause;
14454
+ const surviving = (name) => {
14455
+ const symbol = checker.getSymbolAtLocation(name);
14456
+ return symbol !== undefined && survives(unaliased(checker, symbol));
14457
+ };
14458
+ const specifier = statement.moduleSpecifier.getText();
14459
+ if (!clause) {
14460
+ return [];
14461
+ }
14462
+ const bindings = clause.namedBindings;
14463
+ const named = bindings && ts40.isNamedImports(bindings) ? bindings.elements.filter((element) => surviving(element.name)).map((element) => element.propertyName ? `${element.propertyName.getText()} as ${element.name.text}` : element.name.text) : [];
14464
+ return [
14465
+ ...clause.name && surviving(clause.name) ? [`import type ${clause.name.text} from ${specifier};`] : [],
14466
+ ...bindings && ts40.isNamespaceImport(bindings) ? [`import type * as ${bindings.name.text} from ${specifier};`] : [],
14467
+ ...named.length ? [`import type { ${named.join(", ")} } from ${specifier};`] : []
14468
+ ].map((line) => from(statement, line));
14469
+ };
14470
+ var reExports = (statement, checker, survives) => {
14471
+ const clause = statement.exportClause;
14472
+ if (statement.isTypeOnly || !clause || !ts40.isNamedExports(clause)) {
14473
+ return [copied(statement)];
14474
+ }
14475
+ const kept = clause.elements.filter((element) => {
14476
+ const symbol = checker.getSymbolAtLocation(element.name);
14477
+ return element.isTypeOnly || symbol !== undefined && survives(unaliased(checker, symbol));
14478
+ });
14479
+ return kept.length ? [
14480
+ from(statement, `export { ${kept.map((element) => element.getText()).join(", ")} } from ${statement.moduleSpecifier?.getText()};`)
14481
+ ] : [];
14482
+ };
14483
+ var literalType = (checker, type) => {
14484
+ if (type.isLiteral() || type.flags & ts40.TypeFlags.BooleanLiteral) {
14485
+ return true;
14486
+ }
14487
+ if (!(type.flags & ts40.TypeFlags.Object) || type.getCallSignatures().length || type.getConstructSignatures().length) {
14488
+ return false;
14489
+ }
14490
+ if (checker.isTupleType(type)) {
14491
+ return checker.getTypeArguments(type).every((item) => literalType(checker, item));
14492
+ }
14493
+ const properties = type.getProperties();
14494
+ return properties.length > 0 && !type.getStringIndexType() && !type.getNumberIndexType() && properties.every((property) => {
14495
+ const declaration = property.valueDeclaration;
14496
+ return declaration !== undefined && ts40.isPropertyAssignment(declaration) && literalType(checker, checker.getTypeOfSymbol(property));
14497
+ });
14498
+ };
14499
+ var literalConstant = (checker, symbol) => {
14500
+ const declaration = symbol.valueDeclaration;
14501
+ return declaration && ts40.isVariableDeclaration(declaration) && ts40.isIdentifier(declaration.name) && ts40.isVariableStatement(declaration.parent.parent) && ts40.isSourceFile(declaration.parent.parent.parent) && declaration.parent.flags & ts40.NodeFlags.Const && literalType(checker, checker.getTypeAtLocation(declaration.name)) ? declaration : undefined;
14502
+ };
14503
+ var literalConstants = (source, checker) => {
14504
+ const module = checker.getSymbolAtLocation(source);
14505
+ if (!module) {
14506
+ return [];
14507
+ }
14508
+ return checker.getExportsOfModule(module).flatMap((exported) => {
14509
+ const local = exported.declarations?.some((declaration) => declaration.getSourceFile() === source && !(ts40.isExportSpecifier(declaration) && declaration.parent.parent.moduleSpecifier));
14510
+ const declaration = local && literalConstant(checker, unaliased(checker, exported));
14511
+ if (!declaration || declaration.getSourceFile() !== source) {
14512
+ return [];
14513
+ }
14514
+ const type = checker.typeToString(checker.getTypeAtLocation(declaration.name), undefined, ts40.TypeFormatFlags.NoTruncation);
14515
+ return [
14516
+ from(declaration, exported.name === "default" ? `declare const tsplcDefault: ${type};
14517
+ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};`)
14518
+ ];
14519
+ });
14520
+ };
14426
14521
  var serviceClients = (source, checker, sdk) => source.statements.filter((statement) => ts40.isVariableStatement(statement) && statement.declarationList.declarations.every((declaration) => declaration.initializer && ts40.isCallExpression(declaration.initializer) && namesSdk(checker, sdk, declaration.initializer.expression, "createServiceClient")));
14427
14522
  var importsSdk = (statement) => {
14428
14523
  const specifier = statement.moduleSpecifier;
@@ -15167,6 +15262,7 @@ function compileFrontend(options, dialect) {
15167
15262
  return { diagnostics: [...diagnostics, ...uses] };
15168
15263
  }
15169
15264
  }
15265
+ const lost = [];
15170
15266
  const lowerings = new LoweringPipeline(program, (program, lowering) => {
15171
15267
  diagnostics.push(...lowering.diagnostics);
15172
15268
  for (const source of runtimeSources(program)) {
@@ -15197,7 +15293,16 @@ function compileFrontend(options, dialect) {
15197
15293
  host,
15198
15294
  oldProgram: program
15199
15295
  });
15200
- diagnostics.push(...sourceDiagnostics(lowered, plcFiles(lowered)));
15296
+ const plc = plcFiles(lowered);
15297
+ diagnostics.push(...sourceDiagnostics(lowered, plc));
15298
+ lost.push(...lostImports({
15299
+ before: program,
15300
+ after: lowered,
15301
+ rewritten: lowering.sources,
15302
+ plc: new Set([...plc, ...plcFiles(program)].map(({ fileName }) => fileName)),
15303
+ root,
15304
+ positions
15305
+ }));
15201
15306
  return lowered;
15202
15307
  });
15203
15308
  const configuration = lowerings.run((program) => lowerEnvironment(program, sdk, positions, environment));
@@ -15215,12 +15320,12 @@ function compileFrontend(options, dialect) {
15215
15320
  const lowering = lowerings.run((program) => lowerAsync(program, sdk, positions, root));
15216
15321
  const lowered = lowerings.program;
15217
15322
  if (!lowered || !elaboration || !services || !promises || !lowering) {
15218
- return { diagnostics };
15323
+ return { diagnostics: [...diagnostics, ...lost] };
15219
15324
  }
15220
- const analyzer = new Analyzer(lowered, host, root, sdk, diagnostics, positions, serviceRecords(lowered, services));
15325
+ const analyzer = new Analyzer(lowered, host, root, sdk, diagnostics, positions, serviceRecords(lowered, services), elaboration.reduced);
15221
15326
  const module = analyzer.run({ ...options.programs, ...lowering.programs });
15222
15327
  attachLowerings(module, root, { elaboration, promises, lowering });
15223
- return hasErrors(diagnostics) ? { diagnostics } : { module, diagnostics };
15328
+ return hasErrors(diagnostics) ? { diagnostics: [...diagnostics, ...lost] } : { module, diagnostics };
15224
15329
  }
15225
15330
  function storesGeneratedGlobal(program, diagnostic, generated) {
15226
15331
  const { file, start, length } = diagnostic;
@@ -15236,6 +15341,58 @@ function storesGeneratedGlobal(program, diagnostic, generated) {
15236
15341
  const source = namespace && checker.getSymbolAtLocation(namespace.parent.parent.moduleSpecifier)?.valueDeclaration;
15237
15342
  return Boolean(source && ts43.isSourceFile(source) && generated(source));
15238
15343
  }
15344
+ var missingExportCodes = new Set([
15345
+ 2305,
15346
+ 2459,
15347
+ 2460,
15348
+ 2614,
15349
+ 2724
15350
+ ]);
15351
+ function importedFile(checker, statement) {
15352
+ if (!(ts43.isImportDeclaration(statement) || ts43.isExportDeclaration(statement)) || !statement.moduleSpecifier) {
15353
+ return;
15354
+ }
15355
+ const module = checker.getSymbolAtLocation(statement.moduleSpecifier)?.valueDeclaration;
15356
+ return module && ts43.isSourceFile(module) ? module.fileName : undefined;
15357
+ }
15358
+ function lostImports({
15359
+ before,
15360
+ after,
15361
+ rewritten,
15362
+ plc,
15363
+ root,
15364
+ positions
15365
+ }) {
15366
+ const checker = after.getTypeChecker();
15367
+ return after.getSourceFiles().flatMap((file) => {
15368
+ const imports = file.statements.flatMap((statement) => {
15369
+ const imported = importedFile(checker, statement);
15370
+ return imported && rewritten.has(imported) ? [{ statement, imported }] : [];
15371
+ });
15372
+ const original = before.getSourceFile(file.fileName);
15373
+ if (!imports.length || !original || file.isDeclarationFile || plc.has(file.fileName) || rewritten.has(file.fileName)) {
15374
+ return [];
15375
+ }
15376
+ const missing = (program, source) => program.getSemanticDiagnostics(source).filter((diagnostic) => missingExportCodes.has(diagnostic.code) && diagnostic.start !== undefined);
15377
+ const known = new Set(missing(before, original).map(({ start, code }) => `${start}:${code}`));
15378
+ return missing(after, file).flatMap(({ start = 0, length = 1, code }) => {
15379
+ const broken = imports.find(({ statement }) => statement.pos <= start && start < statement.end);
15380
+ if (!broken || known.has(`${start}:${code}`)) {
15381
+ return [];
15382
+ }
15383
+ const name = file.text.slice(start, start + length);
15384
+ return [
15385
+ {
15386
+ origin: "plc",
15387
+ code: "PLC1016",
15388
+ severity: "warning",
15389
+ message: `tsPLC dropped ${name} from ${relative4(root, broken.imported)} while compiling the PLC program, but ${relative4(root, file.fileName)} imports it, so tsPLC reads that file's types without it. If an error here is in what that file declares, this is its cause, and a tsPLC bug.`,
15390
+ span: positions.span(file, start, start + length)
15391
+ }
15392
+ ];
15393
+ });
15394
+ });
15395
+ }
15239
15396
  var readOnlyAssignment = 2540;
15240
15397
  function reportedAccess(file, start, end) {
15241
15398
  const visit = (node) => {
@@ -15361,5 +15518,5 @@ export {
15361
15518
  typeCheckOnly
15362
15519
  };
15363
15520
 
15364
- //# debugId=AA61B96D6A9DD42B64756E2164756E21
15521
+ //# debugId=48385E19441E661B64756E2164756E21
15365
15522
  //# sourceMappingURL=index.js.map