luaut-parser 2.0.0 → 3.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/README.md CHANGED
@@ -71,10 +71,10 @@ npm i -D @luaut/roblox # or just @luaut/luau
71
71
  - **Which config applies** — the nearest one in the file's folder or above.
72
72
  `luaut.config.json` and `luaut.config.jsonc` in the same folder is an error.
73
73
  Both forms accept comments and trailing commas.
74
- - **`types`** — `"luau"` is looked up as `@luaut/luau`, then as a package named
75
- `luau`, in `node_modules` from the config upward. A full package name or a
76
- relative path (`"./types"`, `"./defs.d.luaut"`) works too. A type library's
77
- own type-library dependencies load first.
74
+ - **`types`** — any name, looked up as the package `@luaut/<name>` in
75
+ `node_modules` from the config upward; one that is not installed is an
76
+ error. A relative path (`"./types"`, `"./defs.d.luaut"`) loads the project's
77
+ own definitions. A type library's own type-library dependencies load first.
78
78
  - **`paths`** — tsconfig rules: an exact pattern wins, then the `*` pattern
79
79
  with the longest prefix; targets resolve from `baseUrl` (default: the
80
80
  config's folder).
@@ -106,6 +106,22 @@ must.
106
106
 
107
107
  **Declarations** — `const` and `let` only; Lua's `local` is gone.
108
108
 
109
+ **Functions** — `function name() ... end` declares `name` in the enclosing
110
+ scope; like a TypeScript function declaration it cannot be reassigned.
111
+ `const` and `let` do not apply to functions. `function T.name()` and
112
+ `function T:name()` define a member.
113
+
114
+ **Modules** — `import { a, b as c } from "./m"`, `import D from "./m"` and
115
+ `import * as M from "./m"`; `export const`, `export function`, `export default`,
116
+ `export { a as b }`, `export { a } from "./m"` and `export * from "./m"`.
117
+ Imports are read-only: assigning to an imported name, or to a member of a
118
+ namespace (`M.x = 1`), is an error.
119
+
120
+ `import type { A } from "./m"` (also `import type D` and `import type * as M`)
121
+ brings in names that are types and nothing else: unlike TypeScript, using one
122
+ as a value is an error, and only type positions — `typeof A` included — may
123
+ name it. Compiled code keeps no trace of it.
124
+
109
125
  **Optionality** — there is no `T?` shorthand. `?` in type position always
110
126
  belongs to a conditional type, and in expression position to a ternary.
111
127
 
@@ -117,6 +133,43 @@ name: T | nil -- must be written, but may be nil
117
133
  Omitting an argument requires `?` (or a default), as in TypeScript — a
118
134
  parameter typed `T | nil` still has to be passed something.
119
135
 
136
+ **Classes** — types are structural, except for classes. A definitions file
137
+ declares one with `declare class`, and it is nominal, as Roblox's classes are:
138
+
139
+ ```luau
140
+ declare class BasePart extends PVInstance { Size: Vector3 }
141
+ declare class Part extends BasePart { Shape: EnumItem }
142
+ ```
143
+
144
+ A `Part` is a `BasePart` and an `Instance` because it extends them. A
145
+ `ReplicatedStorage` is not a `Part`, and no table literal is an `Instance`,
146
+ however alike their members. A class still fits a shape that names members it
147
+ has (`{ Name: string }`). It is not a table, though, so `typeof(part)` picks
148
+ the `"Instance"` overload, not `"table"`. Members are inherited, and a subclass
149
+ may narrow one (`Parent: SomeFolder`).
150
+
151
+ **Callbacks** — a function written where a function type is expected takes
152
+ its parameter types from it: in `signal:Connect(function(player) ... end)`,
153
+ `player` is typed from `Connect`. The same applies to an annotated `const`
154
+ and to an assignment such as `remote.OnServerInvoke = function(player) ...`.
155
+
156
+ **Type packs** — `type Signal<T... = ...any> = { Connect: (self, cb: (T...) -> ()) -> () }`.
157
+ A pack parameter takes every type argument from its position on:
158
+ `Signal<Player, string>`, `Signal<()>` for none.
159
+
160
+ **Operators** — on a type that declares metamethods (`__add`, `__mul`,
161
+ `__unm`, ...), an operator has the metamethod's result, tried on the left
162
+ operand and then the right one, as Luau does. So `Vector3 + Vector3` and
163
+ `2 * vector` are both `Vector3`.
164
+
165
+ **Qualified type names** — a definitions file may declare `Enum.Material`
166
+ (`declare class Enum.Material extends EnumItem {}`), and code writes it the
167
+ same way.
168
+
169
+ **Contextual typing** — an expression takes its type from where it is
170
+ written, as in TypeScript: `let queue: thread[] = []` is a `thread[]`, and so
171
+ is `[]` passed where one is expected, including inside an object literal.
172
+
120
173
  **Calls** — every argument is checked against its parameter, and a generic
121
174
  parameter against its constraint (`GetService<K extends keyof Services>`
122
175
  rejects `""`).