@tilua/parser 1.0.2 → 2.0.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.
package/dist/index.d.ts CHANGED
@@ -326,8 +326,8 @@ interface FunctionSignature extends BaseNode {
326
326
  name?: Identifier;
327
327
  generics: GenericTypeParameter[];
328
328
  params: FunctionParameter[];
329
+ /** The last parameter is a rest parameter (`...rest: T[]`). */
329
330
  hasVarargs: boolean;
330
- varargTypeAnnotation?: TypeNode;
331
331
  returnType?: TypeNode;
332
332
  /** `: v is T` / `: asserts v` instead of a plain return type. */
333
333
  predicate?: TypePredicateNode;
@@ -360,6 +360,12 @@ interface ClassDeclaration extends BaseNode {
360
360
  superclass?: Identifier;
361
361
  /** The arguments `extends Box<number>` was written with. */
362
362
  superArguments?: TypeNode[];
363
+ /** `implements Shape, Named` — shapes the instances are checked against.
364
+ * Nothing at run time. */
365
+ implements?: TypeNode[];
366
+ /** `abstract class` — has no `new`; a class extending it does, once it
367
+ * has written every `abstract` member. */
368
+ isAbstract?: boolean;
363
369
  members: ClassMember[];
364
370
  }
365
371
  /** `class ... end` written where a value goes: `const Counter = class ... end`,
@@ -371,19 +377,32 @@ interface ClassExpression extends BaseNode {
371
377
  name?: Identifier;
372
378
  superclass?: Identifier;
373
379
  superArguments?: TypeNode[];
380
+ implements?: TypeNode[];
381
+ isAbstract?: boolean;
374
382
  members: ClassMember[];
375
383
  }
376
384
  type ClassMember = ClassField | ClassMethod | ClassAccessor | ClassConstructor;
377
385
  /** The two ways a class is written. Everything after parsing treats them
378
386
  * alike: only the name and the type parameters differ. */
379
387
  type ClassLike = ClassDeclaration | ClassExpression;
388
+ /** `public` / `private` / `protected` written before a member. Only the type
389
+ * checker reads it: a private member is still an ordinary key at runtime.
390
+ * `protected` is reachable from the class and the classes extending it. */
391
+ type ClassAccessibility = "public" | "private" | "protected";
380
392
  /** `x: number` / `x = 1` / `static count = 0`. An instance field is assigned
381
393
  * when the instance is built, before the constructor body runs; a `static`
382
394
  * one is assigned on the class table, once. */
383
395
  interface ClassField extends BaseNode {
384
396
  type: "ClassField";
397
+ /** Left out, the member is public. */
398
+ accessibility?: ClassAccessibility;
385
399
  name: Identifier;
386
400
  isStatic: boolean;
401
+ /** `readonly` — assigned where it is declared or in the constructor of
402
+ * its own class, and nowhere else. */
403
+ isReadonly?: boolean;
404
+ /** `override` — the class it extends has a member of this name. */
405
+ isOverride?: boolean;
387
406
  typeAnnotation?: TypeNode;
388
407
  init?: Expression;
389
408
  }
@@ -391,20 +410,31 @@ interface ClassField extends BaseNode {
391
410
  * A `static` one is a plain function on the class table, with no `this`. */
392
411
  interface ClassMethod extends BaseNode {
393
412
  type: "ClassMethod";
413
+ /** Left out, the member is public. */
414
+ accessibility?: ClassAccessibility;
394
415
  name: Identifier;
395
416
  isStatic: boolean;
396
417
  func: FunctionBody;
397
418
  /** TS-style overload signatures preceding the implementation. */
398
419
  signatures?: FunctionSignature[];
420
+ /** `abstract function area(): number` — a head with no body, which every
421
+ * class extending this one that is not abstract itself has to write. */
422
+ isAbstract?: boolean;
423
+ /** `override` — the class it extends has a member of this name. */
424
+ isOverride?: boolean;
399
425
  }
400
426
  /** `get name(): T ... end` / `set name(v: T) ... end` — read and written as a
401
427
  * property, run as a function. */
402
428
  interface ClassAccessor extends BaseNode {
403
429
  type: "ClassAccessor";
430
+ /** Left out, the member is public. */
431
+ accessibility?: ClassAccessibility;
404
432
  kind: "get" | "set";
405
433
  name: Identifier;
406
434
  isStatic: boolean;
407
435
  func: FunctionBody;
436
+ /** `override` — the class it extends has a member of this name. */
437
+ isOverride?: boolean;
408
438
  }
409
439
  /** `constructor(...) ... end` — runs on a fresh instance. A class that
410
440
  * extends another must call `super(...)` before touching `this`. */
@@ -412,14 +442,6 @@ interface ClassConstructor extends BaseNode {
412
442
  type: "ClassConstructor";
413
443
  func: FunctionBody;
414
444
  }
415
- /** `new Name(args)` — builds an instance. Lowers to the class's own
416
- * `Name.new(args)`, which is also callable by hand. */
417
- interface NewExpression extends BaseNode {
418
- type: "NewExpression";
419
- callee: Expression;
420
- arguments: Expression[];
421
- typeArguments?: (TypeNode | TypePackNode)[];
422
- }
423
445
  /** `super` — only inside a class that extends another: `super(...)` in the
424
446
  * constructor runs the base's on this instance, and `super.method(...)` in a
425
447
  * method calls the base's version of it. */
@@ -439,7 +461,7 @@ interface CompoundAssignmentStatement extends BaseNode {
439
461
  }
440
462
  interface CallStatement extends BaseNode {
441
463
  type: "CallStatement";
442
- expression: CallExpression | MethodCallExpression | NewExpression;
464
+ expression: CallExpression | MethodCallExpression;
443
465
  }
444
466
  /** An expression written as a statement, where Lua would want a call or an
445
467
  * assignment: `value` on a line of its own.
@@ -485,15 +507,21 @@ interface NumericForStatement extends BaseNode {
485
507
  step?: Expression;
486
508
  body: Block;
487
509
  }
510
+ /** `for (const item in source)`. One value each time — several are an
511
+ * array, taken apart with a pattern: `for (const [k, v] in pairs(t))`.
512
+ * `let` instead of `const` when the body assigns to the name. */
488
513
  interface GenericForStatement extends BaseNode {
489
514
  type: "GenericForStatement";
490
- variables: BindingTarget[];
491
- iterators: Expression[];
515
+ kind: "const" | "let";
516
+ variable: BindingTarget;
517
+ iterator: Expression;
492
518
  body: Block;
493
519
  }
520
+ /** `return` / `return value`. A function gives back one value; several are
521
+ * an array — `return [a, b]`, a table. */
494
522
  interface ReturnStatement extends BaseNode {
495
523
  type: "ReturnStatement";
496
- arguments: Expression[];
524
+ argument?: Expression;
497
525
  }
498
526
  interface BreakStatement extends BaseNode {
499
527
  type: "BreakStatement";
@@ -516,15 +544,14 @@ interface GenericTypeParameter extends BaseNode {
516
544
  name: string;
517
545
  /** The name as a node. Absent on parameters the analyzer synthesizes. */
518
546
  id?: Identifier;
519
- isPack?: boolean;
520
547
  /** `<const T>` — infer the argument at its narrowest instead of widening
521
548
  * it: literals stay literal and array literals become tuples. */
522
549
  isConst?: boolean;
523
550
  /** `<T extends C>` upper bound (TS style). */
524
551
  constraint?: TypeNode;
525
- default?: TypeNode | TypePackNode;
552
+ default?: TypeNode;
526
553
  }
527
- type Expression = Identifier | NilLiteral | BooleanLiteral | NumberLiteral | StringLiteral | InterpolatedStringExpression | VarargExpression | FunctionExpression | TableExpression | ArrayExpression | BinaryExpression | UnaryExpression | MemberExpression | IndexExpression | CallExpression | MethodCallExpression | NewExpression | SuperExpression | ClassExpression | SpreadElement | ParenthesizedExpression | TypeAssertionExpression | SatisfiesExpression | AsConstExpression | IfElseExpression | ErrorExpression;
554
+ type Expression = Identifier | NilLiteral | BooleanLiteral | NumberLiteral | StringLiteral | InterpolatedStringExpression | FunctionExpression | TableExpression | ArrayExpression | BinaryExpression | UnaryExpression | MemberExpression | IndexExpression | CallExpression | MethodCallExpression | SuperExpression | ClassExpression | SpreadElement | ParenthesizedExpression | TypeAssertionExpression | SatisfiesExpression | AsConstExpression | IfElseExpression | ErrorExpression;
528
555
  /** An expression that could not be parsed. Only produced in recovery mode
529
556
  * (`parseWithRecovery`), where a broken initializer, condition, field value or
530
557
  * argument keeps its place in the tree; its span covers the skipped tokens
@@ -571,15 +598,12 @@ interface InterpolatedStringExpression extends BaseNode {
571
598
  type: "InterpolatedStringExpression";
572
599
  parts: InterpolatedStringPart[];
573
600
  }
574
- interface VarargExpression extends BaseNode {
575
- type: "VarargExpression";
576
- }
577
601
  interface FunctionParameter extends BaseNode {
578
602
  type: "FunctionParameter";
579
603
  /** `...rest: T[]` — every argument from this position on, as an array,
580
- * the way JavaScript's rest parameter collects them. It is always last,
581
- * and the function is a vararg function: `...` still means Lua's pack
582
- * (`const a, b = ...`), and this is the array of it. */
604
+ * the way JavaScript's rest parameter collects them. It is always last.
605
+ * It is the only way a function takes a varying number of arguments:
606
+ * tilua has no bare `...`. */
583
607
  rest?: boolean;
584
608
  /** `name?: T` — the argument may be omitted, and its type admits `nil`. */
585
609
  optional?: boolean;
@@ -595,8 +619,9 @@ interface FunctionBody extends BaseNode {
595
619
  type: "FunctionBody";
596
620
  generics: GenericTypeParameter[];
597
621
  params: FunctionParameter[];
622
+ /** The last parameter is a rest parameter (`...rest: T[]`): the Lua
623
+ * function takes varargs, and the body sees them as that array. */
598
624
  hasVarargs: boolean;
599
- varargTypeAnnotation?: TypeNode;
600
625
  returnType?: TypeNode;
601
626
  /** `: v is T` / `: asserts v` instead of a plain return type. */
602
627
  predicate?: TypePredicateNode;
@@ -685,7 +710,7 @@ interface CallExpression extends BaseNode {
685
710
  callee: Expression;
686
711
  arguments: Expression[];
687
712
  /** `f<T>(x)` — type arguments written out rather than inferred. */
688
- typeArguments?: (TypeNode | TypePackNode)[];
713
+ typeArguments?: TypeNode[];
689
714
  /** `f?.(...)` — see `MemberExpression.optional`. The call does not happen,
690
715
  * and the arguments are not evaluated, when `callee` is nil. */
691
716
  optional?: boolean;
@@ -705,7 +730,7 @@ interface MethodCallExpression extends BaseNode {
705
730
  method: Identifier;
706
731
  arguments: Expression[];
707
732
  /** `obj:m<T>(x)` — see `CallExpression.typeArguments`. */
708
- typeArguments?: (TypeNode | TypePackNode)[];
733
+ typeArguments?: TypeNode[];
709
734
  /** `object?:method(...)` — see `MemberExpression.optional`. The
710
735
  * arguments are not evaluated when `object` is nil. */
711
736
  optional?: boolean;
@@ -744,7 +769,7 @@ interface IfElseExpression extends BaseNode {
744
769
  }[];
745
770
  alternate: Expression;
746
771
  }
747
- type TypeNode = TypeReference | TypeLiteralString | TypeLiteralBoolean | TypeLiteralNumber | TableTypeNode | ArrayTypeNode | TupleTypeNode | FunctionTypeNode | UnionTypeNode | IntersectionTypeNode | ParenthesizedTypeNode | TypeofTypeNode | VariadicTypeNode | TypePackNode | KeyofTypeNode | IndexedAccessTypeNode | ConditionalTypeNode | InferTypeNode | MappedTypeNode | TemplateLiteralTypeNode | DifferenceTypeNode;
772
+ type TypeNode = TypeReference | TypeLiteralString | TypeLiteralBoolean | TypeLiteralNumber | TableTypeNode | ArrayTypeNode | TupleTypeNode | FunctionTypeNode | UnionTypeNode | IntersectionTypeNode | ParenthesizedTypeNode | TypeofTypeNode | KeyofTypeNode | IndexedAccessTypeNode | ConditionalTypeNode | InferTypeNode | MappedTypeNode | TemplateLiteralTypeNode | DifferenceTypeNode;
748
773
  /** `A - B` — every value of `A` that is not a `B`. Binds tighter than `|`
749
774
  * and looser than `&`, so `A | B - C` is `A | (B - C)`.
750
775
  *
@@ -833,17 +858,11 @@ interface TypePredicateNode extends BaseNode {
833
858
  /** Absent for a bare `asserts x` (a truthiness assertion). */
834
859
  typeAnnotation?: TypeNode;
835
860
  }
836
- interface TypePackNode extends BaseNode {
837
- type: "TypePackNode";
838
- types: TypeNode[];
839
- hasVarargs: boolean;
840
- varargType?: TypeNode;
841
- }
842
861
  interface TypeReference extends BaseNode {
843
862
  type: "TypeReference";
844
863
  base: string;
845
864
  namespace?: string;
846
- typeArguments: (TypeNode | TypePackNode)[];
865
+ typeArguments: TypeNode[];
847
866
  }
848
867
  interface TypeLiteralString extends BaseNode {
849
868
  type: "TypeLiteralString";
@@ -884,10 +903,13 @@ interface ArrayTypeNode extends BaseNode {
884
903
  type: "ArrayTypeNode";
885
904
  element: TypeNode;
886
905
  }
887
- /** `[number, string]` — fixed-length tuple type. */
906
+ /** `[number, string]` — a tuple type; `[string, ...number[]]` ends in a rest
907
+ * of as many more as there are. */
888
908
  interface TupleTypeNode extends BaseNode {
889
909
  type: "TupleTypeNode";
890
910
  elements: TypeNode[];
911
+ /** `...number[]` at the end: the array type the rest of it is. */
912
+ rest?: TypeNode;
891
913
  }
892
914
  interface FunctionTypeParameter extends BaseNode {
893
915
  type: "FunctionTypeParameter";
@@ -904,8 +926,6 @@ interface FunctionTypeNode extends BaseNode {
904
926
  type: "FunctionTypeNode";
905
927
  generics: GenericTypeParameter[];
906
928
  params: FunctionTypeParameter[];
907
- hasVarargs: boolean;
908
- varargType?: TypeNode;
909
929
  returnType: TypeNode;
910
930
  /** `(v: unknown) -> v is string` — a guard written as a function *type*. */
911
931
  predicate?: TypePredicateNode;
@@ -928,10 +948,6 @@ interface TypeofTypeNode extends BaseNode {
928
948
  type: "TypeofTypeNode";
929
949
  expression: Expression;
930
950
  }
931
- interface VariadicTypeNode extends BaseNode {
932
- type: "VariadicTypeNode";
933
- typeAnnotation: TypeNode;
934
- }
935
951
  type Node = Program | Block | Statement | Expression | TypeNode | FunctionBody | FunctionParameter | FunctionName | IfClause | TypedIdentifier | GenericTypeParameter | FunctionSignature | TypePredicateNode | TableField | SpreadElement | FunctionTypeParameter | TableTypeProperty | BindingTarget | ObjectPatternProperty | ArrayPatternElement;
936
952
 
937
953
  declare class ParseError extends Error {
@@ -1058,6 +1074,10 @@ declare function isGlobal(binding: Binding): boolean;
1058
1074
  * undeclared, which is almost always a typo rather than an intentional
1059
1075
  * implicit global. Handy for a "possibly undefined global" diagnostic. */
1060
1076
  declare function isUnassignedGlobal(binding: Binding): boolean;
1077
+ /** Globals tilua itself declares, whatever the type libraries do:
1078
+ * `scriptArgs` is the arguments the script was started with — Lua's
1079
+ * top-level `...` — as an array. */
1080
+ declare const LANGUAGE_GLOBALS: readonly string[];
1061
1081
  declare function analyzeScopes(program: Program, options?: AnalyzeScopesOptions): ScopeAnalysis;
1062
1082
 
1063
1083
  type Type = AnyType | UnknownType | NeverType | PrimitiveType | LiteralType | ArrayType | TupleType | ObjectType | FunctionType | UnionType | IntersectionType | TypeParamType | GenericRefType | KeyofType | IndexedAccessType | ConditionalType | InferType | MappedType | TemplateLiteralType | DifferenceType;
@@ -1100,23 +1120,31 @@ interface ArrayType {
1100
1120
  element: Type;
1101
1121
  alias?: string;
1102
1122
  }
1103
- /** `[A, B, C]` — fixed length.
1104
- *
1105
- * `isPack` marks the other thing this shape is used for: a *type pack*, the
1106
- * several values a Lua function returns (`(number, string)`). The two are
1107
- * structurally identical but behave differently in an expression list — a
1108
- * pack spreads across several names, a tuple is one value — so they have to
1109
- * be told apart. */
1123
+ /** `[A, B, C]` — fixed length; `[A, ...B[]]` — at least that, then any
1124
+ * number of `B`. `rest` is the element type of that tail. A tuple is an
1125
+ * array, a table like any other: several results are one of these. */
1110
1126
  interface TupleType {
1111
1127
  kind: "tuple";
1112
1128
  elements: Type[];
1113
- isPack?: boolean;
1129
+ rest?: Type;
1114
1130
  alias?: string;
1115
1131
  }
1116
1132
  interface ObjectProperty {
1117
1133
  type: Type;
1118
1134
  optional: boolean;
1119
1135
  readonly?: boolean;
1136
+ /** A class member written `private`: reachable only from inside the class
1137
+ * that declared it. `owner` is that class's node, compared by identity;
1138
+ * `className` is how a diagnostic names it. Checked, never lowered.
1139
+ * `protected` widens that to the classes extending it. */
1140
+ private?: {
1141
+ owner: object;
1142
+ className: string;
1143
+ protected?: boolean;
1144
+ };
1145
+ /** An `abstract` class member: declared, with nothing behind it until a
1146
+ * class extending this one writes it. */
1147
+ abstract?: boolean;
1120
1148
  }
1121
1149
  interface ObjectType {
1122
1150
  kind: "object";
@@ -1178,8 +1206,14 @@ interface TypePredicate {
1178
1206
  interface FunctionType {
1179
1207
  kind: "function";
1180
1208
  params: FunctionParam[];
1209
+ /** The type of each argument a rest parameter (`...args: T[]`) collects —
1210
+ * or, with `restIsWhole`, the rest parameter's own type. */
1181
1211
  varargs?: Type;
1182
- /** A tuple when the function returns multiple values. */
1212
+ /** `...args: A` where `A` is a tuple, or a type parameter standing for
1213
+ * one (`<A extends unknown[]>`): `varargs` is `A` itself, and once `A`
1214
+ * is known its elements are the parameters. */
1215
+ restIsWhole?: boolean;
1216
+ /** What the function returns: one value — several are an array. */
1183
1217
  returns: Type;
1184
1218
  /** Names of the function's own generic parameters (`function f<T>(...)`).
1185
1219
  * `params` / `returns` may contain `typeParam` nodes for these. */
@@ -1328,7 +1362,9 @@ declare const bufferType: PrimitiveType;
1328
1362
  declare function primitive(name: PrimitiveName): PrimitiveType;
1329
1363
  declare function literal(value: string | number | boolean): LiteralType;
1330
1364
  declare function arrayOf(element: Type): ArrayType;
1331
- declare function tuple(elements: Type[], isPack?: boolean): TupleType;
1365
+ declare function tuple(elements: Type[], rest?: Type): TupleType;
1366
+ /** Every element type a tuple can hold, the rest's included. */
1367
+ declare function tupleMembers(t: TupleType): Type[];
1332
1368
  declare function objectType(entries: Iterable<[string, ObjectProperty]>, indexer?: ObjectType["indexer"], frozen?: boolean): ObjectType;
1333
1369
  declare function fn(params: FunctionParam[], returns: Type, varargs?: Type, typeParams?: string[], predicate?: TypePredicate): FunctionType;
1334
1370
  declare function substitute(t: Type, subst: Map<string, Type>): Type;
@@ -1393,6 +1429,20 @@ interface TypeDiagnostic {
1393
1429
  node: Expression | Statement | TypeNode | Block | ClassMember;
1394
1430
  message: string;
1395
1431
  }
1432
+ /** How a `for (const item in source)` walks its source — what lowering needs,
1433
+ * decided with the item's type so the two cannot drift apart.
1434
+ * - `values`: an array or a table, walked directly; the item is each value.
1435
+ * - `function`: an iterator function, called until it answers nil; the item
1436
+ * is each answer.
1437
+ * - `iteration`: `[step, state, first]`, as `pairs(t)` answers: `step` is
1438
+ * called with the state and the item before (`first` the first time) until
1439
+ * it answers nil.
1440
+ * `viaIter`: the source is an object whose `__iter` method gives the function
1441
+ * or the iteration. */
1442
+ interface LoopForm {
1443
+ readonly walks: "values" | "function" | "iteration";
1444
+ readonly viaIter: boolean;
1445
+ }
1396
1446
  interface TypeAnalysis {
1397
1447
  /** Inferred type of every expression node. */
1398
1448
  readonly typeOf: Map<Expression, Type>;
@@ -1404,7 +1454,7 @@ interface TypeAnalysis {
1404
1454
  /** What every type annotation node resolves to — `number`, `Shape`,
1405
1455
  * `typeof x`, a property's type inside `{ ... }`. Inside a generic alias or
1406
1456
  * function its parameters stay unresolved (`T`). */
1407
- readonly typeOfTypeNode: Map<TypeNode | TypePackNode, Type>;
1457
+ readonly typeOfTypeNode: Map<TypeNode, Type>;
1408
1458
  /** What each call argument is expected to be: the parameter it lands on,
1409
1459
  * with the signature's type parameters replaced by their constraints — a
1410
1460
  * union when an overload set disagrees. Recorded even for a call that does
@@ -1414,13 +1464,9 @@ interface TypeAnalysis {
1414
1464
  /** Top-level type aliases, resolved — and the type names this module
1415
1465
  * imports, so tooling treats both alike. */
1416
1466
  readonly aliases: Map<string, Type>;
1417
- /** The `for x in t` loops that walk a table directly, where tilua's one
1418
- * variable takes the *value* — not an iterator such as `pairs(t)` or
1419
- * `string.gmatch(s, p)`, whose variables are bound as written. Luau hands
1420
- * the key over first, so lowering these needs a key variable in front.
1421
- * Deciding that here keeps it next to the types the same choice produces:
1422
- * the variable's type and the code bound to it cannot drift apart. */
1423
- readonly iteratesValues: ReadonlySet<GenericForStatement>;
1467
+ /** How each `for (const item in source)` walks its source; see
1468
+ * `LoopForm`. A loop missing here walks values. */
1469
+ readonly loops: ReadonlyMap<GenericForStatement, LoopForm>;
1424
1470
  readonly diagnostics: TypeDiagnostic[];
1425
1471
  }
1426
1472
  /** A type a module exports: the resolved type, plus the parameter names of a
@@ -1491,11 +1537,15 @@ resolveModule?: (specifier: string) => ModuleExports | undefined): ModuleExports
1491
1537
  * (`tilua.methods` in its package.json). Nothing about `filter` is written
1492
1538
  * into the analyzer.
1493
1539
  */
1494
- declare const PRELUDE_SOURCE = "\n-- In Luau only `nil` and `false` are falsy: `0` and `\"\"` are truthy.\n-- These are what truthiness narrowing computes, made available to write down.\ntype Falsy = nil | false\ntype Truthy<T> = T - Falsy\n\n-- `-` is set difference. Over a union it drops members; over a concrete type\n-- it simplifies away; over an opaque type (`unknown`, an unresolved parameter)\n-- it is kept, so `Exclude<unknown, 1>` stays `unknown - 1`.\ntype Exclude<T, U> = T - U\ntype Extract<T, U> = T extends U ? T : never\ntype NonNullable<T> = T - nil\n\ntype ReturnType<T> = T extends (...unknown) => infer R ? R : never\ntype Parameters<T> = T extends (...infer P) => unknown ? P : never\n\ntype Partial<T> = { [K in keyof T]?: T[K] }\ntype Required<T> = { [K in keyof T]-?: T[K] }\ntype Readonly<T> = { readonly [K in keyof T]: T[K] }\ntype Mutable<T> = { -readonly [K in keyof T]: T[K] }\n\ntype Pick<T, K> = { [P in K]: T[P] }\ntype Omit<T, K> = Pick<T, Exclude<keyof T, K>>\ntype Record<K, V> = { [P in K]: V }\n";
1540
+ declare const PRELUDE_SOURCE = "\n-- In Luau only `nil` and `false` are falsy: `0` and `\"\"` are truthy.\n-- These are what truthiness narrowing computes, made available to write down.\ntype Falsy = nil | false\ntype Truthy<T> = T - Falsy\n\n-- `-` is set difference. Over a union it drops members; over a concrete type\n-- it simplifies away; over an opaque type (`unknown`, an unresolved parameter)\n-- it is kept, so `Exclude<unknown, 1>` stays `unknown - 1`.\ntype Exclude<T, U> = T - U\ntype Extract<T, U> = T extends U ? T : never\ntype NonNullable<T> = T - nil\n\ntype ReturnType<T> = T extends (...args: unknown[]) => infer R ? R : never\ntype Parameters<T> = T extends (...args: infer P) => unknown ? P : never\n\ntype Partial<T> = { [K in keyof T]?: T[K] }\ntype Required<T> = { [K in keyof T]-?: T[K] }\ntype Readonly<T> = { readonly [K in keyof T]: T[K] }\ntype Mutable<T> = { -readonly [K in keyof T]: T[K] }\n\ntype Pick<T, K> = { [P in K]: T[P] }\ntype Omit<T, K> = Pick<T, Exclude<keyof T, K>>\ntype Record<K, V> = { [P in K]: V }\n";
1495
1541
 
1496
1542
  interface ProjectHost {
1497
1543
  /** A file's text, or `undefined` when there is no such file. */
1498
1544
  readFile(path: string): string | undefined;
1545
+ /** The names of the folders in a folder, or `undefined` when there is no
1546
+ * such folder. Only a `types` entry with a `*` in it lists anything; a
1547
+ * host without this cannot load one. */
1548
+ readDirectory?(path: string): string[] | undefined;
1499
1549
  }
1500
1550
  declare const nodeHost: ProjectHost;
1501
1551
 
@@ -1609,9 +1659,20 @@ declare function resolveTypeLibraries(config: TiluaConfig, host?: ProjectHost):
1609
1659
 
1610
1660
  interface LoweringPlugin {
1611
1661
  /** Luau the plugin needs in the output, by a key it chooses. Each is a
1612
- * file's worth of source with `__NAME__` standing for the local the
1613
- * compiler gives it, and each is emitted once, at the top of the output,
1614
- * only if `use` asked for it:
1662
+ * file's worth of source, emitted once, at the top of the output, only if
1663
+ * `use` asked for it. Two names in it are filled in by the compiler:
1664
+ *
1665
+ * - `__NAME__`, the local the compiler gives this runtime;
1666
+ * - `__LINES__`, an expression holding the bundle's line map —
1667
+ * `{ [bundleLine] = { "src/a.tilua", sourceLine } }` — or `nil` when
1668
+ * the output is not a bundle. It is what turns a line Luau reports at
1669
+ * runtime back into a place in the project;
1670
+ * - `__FAIL__`, the bundle's error reporter, or `nil` outside a bundle:
1671
+ * a function that takes an error and returns it with the bundle's
1672
+ * positions replaced by the project's, followed by a traceback of
1673
+ * those. It is an `xpcall` handler — the one the entry runs under —
1674
+ * so a runtime that runs a callback on a thread of its own can report
1675
+ * that callback's errors the same way.
1615
1676
  *
1616
1677
  * local __NAME__ = {}
1617
1678
  * function __NAME__.filter(t, test) ... end
@@ -1621,24 +1682,84 @@ interface LoweringPlugin {
1621
1682
  * method call, which is what a value that answers to the method itself
1622
1683
  * wants — `text:upper()` reaches Lua's own. */
1623
1684
  methodCall?(call: MethodCall): MethodLowering | undefined;
1685
+ /** What a call to a global — `print(...)`, `error(...)` — becomes. Asked
1686
+ * only when the name really is the global: a local the author called
1687
+ * `print` is theirs. `undefined` leaves the call as it was written. */
1688
+ globalCall?(call: GlobalCall): CallLowering | undefined;
1689
+ /** What a global read as a value — `task.spawn(print, x)`, `local p =
1690
+ * print` — becomes: a Luau expression, usually built from `use(...)`.
1691
+ * There is no call site to describe here, so this is where a plugin
1692
+ * that rewrites calls keeps the rest of the program consistent with it. */
1693
+ globalValue?(value: GlobalValue): string | undefined;
1694
+ }
1695
+ /** Where a call was written. */
1696
+ interface CallSite {
1697
+ /** The file, relative to the project root with `/` separators, or
1698
+ * `undefined` when a single source was compiled with no name. */
1699
+ readonly file: string | undefined;
1700
+ /** 1-based. */
1701
+ readonly line: number;
1702
+ /** 1-based. */
1703
+ readonly column: number;
1704
+ }
1705
+ /** What the compiler knows about one argument that the runtime will not. */
1706
+ interface ArgumentInfo {
1707
+ /** Its type, as the analyzer worked it out. */
1708
+ readonly type: Type | undefined;
1709
+ /** That type as the editor would show it — `(x: number) => number` —
1710
+ * so a library can print it without depending on this package. */
1711
+ readonly typeText: string | undefined;
1712
+ /** The code it was written as, at the call. */
1713
+ readonly source: string;
1714
+ /** For a name, the code of what it names — the value of `const f = ...`,
1715
+ * or the whole `function f() { ... }` — when this file declares it. */
1716
+ readonly declaration?: string;
1717
+ /** Written `...xs`. */
1718
+ readonly spread: boolean;
1719
+ }
1720
+ interface CallContext {
1721
+ /** Where the call was written. */
1722
+ readonly at: CallSite;
1723
+ /** Each written argument, in order. */
1724
+ readonly arguments: readonly ArgumentInfo[];
1725
+ /** The local name the output gives one of `runtime`'s entries, emitting
1726
+ * it if this is the first call that needed it. */
1727
+ use(runtime: string): string;
1624
1728
  }
1625
- interface MethodCall {
1729
+ interface MethodCall extends CallContext {
1626
1730
  /** The name written after `:`. */
1627
1731
  readonly method: string;
1628
1732
  /** The receiver's type, as the analyzer worked it out. `undefined` when
1629
1733
  * nothing typed it, where a plugin should decline rather than guess. */
1630
1734
  readonly receiver: Type | undefined;
1735
+ /** The receiver's name when it is a global — `console` in
1736
+ * `console:log(x)` — and `undefined` for anything else, a local of the
1737
+ * same name included. */
1738
+ readonly receiverGlobal: string | undefined;
1631
1739
  /** How many arguments were written. */
1632
1740
  readonly argumentCount: number;
1633
- /** The local name the output gives one of `runtime`'s entries, emitting
1634
- * it if this is the first call that needed it. */
1741
+ }
1742
+ interface GlobalCall extends CallContext {
1743
+ /** The global called. */
1744
+ readonly name: string;
1745
+ }
1746
+ interface GlobalValue {
1747
+ /** The global read. */
1748
+ readonly name: string;
1749
+ readonly at: CallSite;
1635
1750
  use(runtime: string): string;
1636
1751
  }
1637
- interface MethodLowering {
1752
+ interface CallLowering {
1638
1753
  /** What to call instead: a name, or a `table.member` path — usually built
1639
1754
  * from `use(...)`. */
1640
1755
  readonly callee: string;
1641
- /** Pass the receiver as the first argument. Default: yes. */
1756
+ /** Luau expressions passed ahead of the written arguments — the call
1757
+ * site, say, or a table of what the compiler knew about them. */
1758
+ readonly prepend?: readonly string[];
1759
+ }
1760
+ interface MethodLowering extends CallLowering {
1761
+ /** Pass the receiver as the first argument, after `prepend`. Default:
1762
+ * yes. */
1642
1763
  readonly passReceiver?: boolean;
1643
1764
  }
1644
1765
 
@@ -1688,4 +1809,4 @@ declare const tilua: {
1688
1809
  readonly analyzeTypes: typeof analyzeTypes;
1689
1810
  };
1690
1811
 
1691
- export { type AnalyzeTypesOptions, type AnyType, type ArrayExpression, type ArrayPattern, type ArrayPatternElement, type ArrayType, type ArrayTypeNode, type AsConstExpression, type AssignmentStatement, type BaseNode, type BaseToken, type BinaryExpression, BinaryOperators, type Binding, type BindingId, type BindingKind, type BindingTarget, type Block, type BooleanLiteral, type BreakStatement, type BuildTarget, CONFIG_FILE_NAMES, type CallExpression, type CallStatement, type ClassAccessor, type ClassConstructor, type ClassDeclaration, type ClassExpression, type ClassField, type ClassInfo, type ClassLike, type ClassMember, type ClassMethod, type CompoundAssignmentStatement, type ConditionalType, type ConditionalTypeNode, type ConfigLookup, type ConfigProblem, type ContinueStatement, type DeclareClassStatement, type DeclareStatement, type DifferenceType, type DifferenceTypeNode, type Directive, type DirectiveKind, type DirectiveOutcome, type Directives, type DoStatement, type EOFToken, type ErrorExpression, type ErrorStatement, type ExportAllStatement, type ExportDefaultStatement, type ExportNamedStatement, type ExportSpecifier, type ExportStatement, type ExportTypeAliasStatement, type ExportedType, type Expression, type ExpressionStatement, type FunctionBody, type FunctionDeclaration, type FunctionDeclarationStatement, type FunctionExpression, type FunctionName, type FunctionParam, type FunctionParameter, type FunctionSignature, type FunctionType, type FunctionTypeNode, type FunctionTypeParameter, type GenericForStatement, type GenericRefType, type GenericTypeParameter, type Identifier, type IdentifierPattern, type IdentifierToken, type IfClause, type IfElseExpression, type IfStatement, type ImportSpecifier, type ImportStatement, type IndexExpression, type IndexedAccessType, type IndexedAccessTypeNode, type InferType, type InferTypeNode, type InterpolatedStringExpression, type InterpolatedStringPart, type InterpolatedStringPart_Expression, type InterpolatedStringPart_String, type InterpolatedStringToken, type IntersectionType, type IntersectionTypeNode, type KeyofType, type KeyofTypeNode, type KeywordToken, Keywords, LexError, type LiteralToken, type LiteralType, type LoweringModule, type LoweringPlugin, type MappedType, type MappedTypeNode, type MemberExpression, type MethodCall, type MethodCallExpression, type MethodLowering, type ModuleExports, type NeverType, type NewExpression, type NilLiteral, type Node, type NumberLiteral, type NumericForStatement, type ObjectPattern, type ObjectPatternProperty, type ObjectProperty, type ObjectType, type OperatorToken, Operators, PRELUDE_SOURCE, type ParenthesizedExpression, type ParenthesizedTypeNode, ParseError, type ParserOptions, type PrimitiveName, type PrimitiveType, type Program, type ProjectHost, type PunctuatorToken, Punctuators, type RecoverResult, type RepeatStatement, type ReturnStatement, type SatisfiesExpression, type ScopeAnalysis, type ScopeDiagnostic, type SourceComment, type SourceMapNode, type SourceMapOptions, type SourceMapTypes, type SpreadElement, type Statement, type StringLiteral, type SuperExpression, type TableExpression, type TableField, type TableTypeNode, type TableTypeProperty, type TemplateLiteralType, type TemplateLiteralTypeNode, type TiluaConfig, type Token, type TokenizeOptions, type TupleType, type TupleTypeNode, type Type, type TypeAliasStatement, type TypeAnalysis, type TypeAssertionExpression, type TypeDiagnostic, type TypeLibraries, type TypeLiteralBoolean, type TypeLiteralNumber, type TypeLiteralString, type TypeNode, type TypeOrigin, type TypePackNode, type TypeParamType, type TypePredicate, type TypePredicateNode, type TypeReference, type TypedIdentifier, type TypeofTypeNode, UNUSED_EXPECT_ERROR, type UnaryExpression, UnaryOperators, type UnionType, type UnionTypeNode, type UnknownType, type VarargExpression, type VariableDeclaration, type VariadicTypeNode, type WhileStatement, aliasNameOf, analyzeScopes, analyzeTypes, anyType, applyDirectives, arrayOf, booleanType, bufferType, containsTypeParam, declaredUnknownType, tilua as default, difference, directivesOf, equalTypes, falsyType, findConfig, fn, formatType, getBinding, intersection, isAssignable, isClassType, isGlobal, isPossiblyFalsy, isPossiblyTruthy, isUnassignedGlobal, literal, loadConfig, matchInfer, moduleCandidates, moduleExports, narrowExclude, narrowFalsy, narrowTo, narrowTruthy, neverType, nilType, nodeHost, numberType, objectType, optional, overlaps, parse, parseExpressionFromSource, parseTokens, parseWithRecovery, primitive, readDirectives, resolveModulePath, resolveTypeLibraries, setAliasExpander, setDeferredBound, sourceMapTypes, stringType, stripJsonComments, substitute, templateMatches, threadType, tilua, tokenize, tuple, typeParam, unify, union, unknownType, widen, withAliasName, withoutAliasName };
1812
+ export { type AnalyzeTypesOptions, type AnyType, type ArgumentInfo, type ArrayExpression, type ArrayPattern, type ArrayPatternElement, type ArrayType, type ArrayTypeNode, type AsConstExpression, type AssignmentStatement, type BaseNode, type BaseToken, type BinaryExpression, BinaryOperators, type Binding, type BindingId, type BindingKind, type BindingTarget, type Block, type BooleanLiteral, type BreakStatement, type BuildTarget, CONFIG_FILE_NAMES, type CallExpression, type CallLowering, type CallSite, type CallStatement, type ClassAccessibility, type ClassAccessor, type ClassConstructor, type ClassDeclaration, type ClassExpression, type ClassField, type ClassInfo, type ClassLike, type ClassMember, type ClassMethod, type CompoundAssignmentStatement, type ConditionalType, type ConditionalTypeNode, type ConfigLookup, type ConfigProblem, type ContinueStatement, type DeclareClassStatement, type DeclareStatement, type DifferenceType, type DifferenceTypeNode, type Directive, type DirectiveKind, type DirectiveOutcome, type Directives, type DoStatement, type EOFToken, type ErrorExpression, type ErrorStatement, type ExportAllStatement, type ExportDefaultStatement, type ExportNamedStatement, type ExportSpecifier, type ExportStatement, type ExportTypeAliasStatement, type ExportedType, type Expression, type ExpressionStatement, type FunctionBody, type FunctionDeclaration, type FunctionDeclarationStatement, type FunctionExpression, type FunctionName, type FunctionParam, type FunctionParameter, type FunctionSignature, type FunctionType, type FunctionTypeNode, type FunctionTypeParameter, type GenericForStatement, type GenericRefType, type GenericTypeParameter, type GlobalCall, type GlobalValue, type Identifier, type IdentifierPattern, type IdentifierToken, type IfClause, type IfElseExpression, type IfStatement, type ImportSpecifier, type ImportStatement, type IndexExpression, type IndexedAccessType, type IndexedAccessTypeNode, type InferType, type InferTypeNode, type InterpolatedStringExpression, type InterpolatedStringPart, type InterpolatedStringPart_Expression, type InterpolatedStringPart_String, type InterpolatedStringToken, type IntersectionType, type IntersectionTypeNode, type KeyofType, type KeyofTypeNode, type KeywordToken, Keywords, LANGUAGE_GLOBALS, LexError, type LiteralToken, type LiteralType, type LoweringModule, type LoweringPlugin, type MappedType, type MappedTypeNode, type MemberExpression, type MethodCall, type MethodCallExpression, type MethodLowering, type ModuleExports, type NeverType, type NilLiteral, type Node, type NumberLiteral, type NumericForStatement, type ObjectPattern, type ObjectPatternProperty, type ObjectProperty, type ObjectType, type OperatorToken, Operators, PRELUDE_SOURCE, type ParenthesizedExpression, type ParenthesizedTypeNode, ParseError, type ParserOptions, type PrimitiveName, type PrimitiveType, type Program, type ProjectHost, type PunctuatorToken, Punctuators, type RecoverResult, type RepeatStatement, type ReturnStatement, type SatisfiesExpression, type ScopeAnalysis, type ScopeDiagnostic, type SourceComment, type SourceMapNode, type SourceMapOptions, type SourceMapTypes, type SpreadElement, type Statement, type StringLiteral, type SuperExpression, type TableExpression, type TableField, type TableTypeNode, type TableTypeProperty, type TemplateLiteralType, type TemplateLiteralTypeNode, type TiluaConfig, type Token, type TokenizeOptions, type TupleType, type TupleTypeNode, type Type, type TypeAliasStatement, type TypeAnalysis, type TypeAssertionExpression, type TypeDiagnostic, type TypeLibraries, type TypeLiteralBoolean, type TypeLiteralNumber, type TypeLiteralString, type TypeNode, type TypeOrigin, type TypeParamType, type TypePredicate, type TypePredicateNode, type TypeReference, type TypedIdentifier, type TypeofTypeNode, UNUSED_EXPECT_ERROR, type UnaryExpression, UnaryOperators, type UnionType, type UnionTypeNode, type UnknownType, type VariableDeclaration, type WhileStatement, aliasNameOf, analyzeScopes, analyzeTypes, anyType, applyDirectives, arrayOf, booleanType, bufferType, containsTypeParam, declaredUnknownType, tilua as default, difference, directivesOf, equalTypes, falsyType, findConfig, fn, formatType, getBinding, intersection, isAssignable, isClassType, isGlobal, isPossiblyFalsy, isPossiblyTruthy, isUnassignedGlobal, literal, loadConfig, matchInfer, moduleCandidates, moduleExports, narrowExclude, narrowFalsy, narrowTo, narrowTruthy, neverType, nilType, nodeHost, numberType, objectType, optional, overlaps, parse, parseExpressionFromSource, parseTokens, parseWithRecovery, primitive, readDirectives, resolveModulePath, resolveTypeLibraries, setAliasExpander, setDeferredBound, sourceMapTypes, stringType, stripJsonComments, substitute, templateMatches, threadType, tilua, tokenize, tuple, tupleMembers, typeParam, unify, union, unknownType, widen, withAliasName, withoutAliasName };