@tilua/compiler 8.1.0 → 9.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.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as T from '@tilua/parser';
2
2
  import { OutputMode, TiluaConfig, ConfigProblem, LoweringPlugin, TypeAnalysis, ScopeAnalysis } from '@tilua/parser';
3
- export { LoweringPlugin, LuaSyntax, MethodCall, MethodLowering } from '@tilua/parser';
3
+ export { EntryExports, EntryExportsLowering, FilesAnswer, FilesLayout, LoweringPlugin, LuaSyntax, MethodCall, MethodLowering, PluginContext } from '@tilua/parser';
4
4
  import * as L from 'luau-parser';
5
5
 
6
6
  /** What `tilua.config.json` holds. */
@@ -15,8 +15,15 @@ interface TiluaConfigJson {
15
15
  sourceMap?: string | null;
16
16
  /** How many files the output is. `"single"` (the default) is one file;
17
17
  * `"chunks"` also writes a file for what only an `import()` reaches,
18
- * loaded when it is first asked for; `"modules"` writes one per module. */
18
+ * loaded when it is first asked for; `"files"` has no entry, and
19
+ * compiles every file under `rootDir` to one of its own under `outDir`. */
19
20
  output?: OutputMode;
21
+ /** With `"files"`, the folder whose files are compiled. Default: the
22
+ * config's folder. */
23
+ rootDir?: string;
24
+ /** With `"files"`, the folder the output goes in, laid out as `rootDir`
25
+ * is. Default: `rootDir`, each `.luau` beside its `.tilua`. */
26
+ outDir?: string;
20
27
  /** Leave out Lua's standard library — `print`, `string`, `pcall` — which
21
28
  * is otherwise there for every file, as JavaScript's is in TypeScript. */
22
29
  noLib?: boolean;
@@ -25,6 +32,15 @@ interface TiluaConfigJson {
25
32
  /** Write the map from the bundle's lines back to the project's, which
26
33
  * error reports and `console` read. Default: true. */
27
34
  lineMap?: boolean;
35
+ /** Indent the output, a block level at a time: `"\t"` or a number of
36
+ * spaces. Default: no indentation. */
37
+ indent?: "\t" | number;
38
+ /** Write the output on one line, with as few `;` as it can take, and
39
+ * without the line map. Default: false. */
40
+ minify?: boolean;
41
+ /** The options a type library in `types` declares, such as
42
+ * `@tilua-types/luau`'s `typedExports`. */
43
+ [option: string]: unknown;
28
44
  }
29
45
  /** A config file's path, or the config itself. Relative paths — to a config
30
46
  * file, and inside an inline config — resolve from `cwd`. */
@@ -38,8 +54,9 @@ interface ResolvedConfig {
38
54
  declare function resolveConfig(input: ConfigInput | undefined, entry: string, cwd?: string): ResolvedConfig;
39
55
 
40
56
  interface BundleOptions {
41
- /** The file the bundle runs. */
42
- readonly entry: string;
57
+ /** The file the bundle runs. Needed unless the output is `"files"`,
58
+ * which has no entry: every file under the config's `rootDir` is one. */
59
+ readonly entry?: string;
43
60
  /** A `tilua.config.json` path or contents. Default: the nearest config above the entry. */
44
61
  readonly config?: ConfigInput;
45
62
  /** The project folder. Modules are named by their path from it, and an
@@ -49,7 +66,8 @@ interface BundleOptions {
49
66
  /** Check types against the config's `types` too. Default: true. */
50
67
  readonly typeCheck?: boolean;
51
68
  /** How many files to make. Overrides the config's `output`; `"single"`
52
- * without either. */
69
+ * without either. With `"files"`, every file under the config's
70
+ * `rootDir` is compiled to a file of its own, its path from `rootDir`. */
53
71
  readonly output?: OutputMode;
54
72
  /** Leave out what the entry never reaches. Overrides the config's
55
73
  * `treeShaking`; off without either. */
@@ -58,11 +76,26 @@ interface BundleOptions {
58
76
  * the config's `lineMap`; on without either. Without it an error names
59
77
  * the bundle's lines, and the entry does not run under `xpcall`. */
60
78
  readonly lineMap?: boolean;
79
+ /** Indent the output: `"\t"` or a number of spaces. Overrides the
80
+ * config's `indent`; no indentation without either. */
81
+ readonly indent?: "\t" | number;
82
+ /** Write the output on one line, with as few `;` as it can take — and so
83
+ * without the line map, which one line cannot have. Overrides the
84
+ * config's `minify`; off without either. */
85
+ readonly minify?: boolean;
86
+ /** With `"files"`, the folder the output is written to — where a host's
87
+ * layout (a Rojo project) finds it. Overrides the config's `outDir`;
88
+ * `rootDir` without either. */
89
+ readonly outDir?: string;
61
90
  /** What the entry's file is called: `main.luau`. The other files of
62
91
  * `"chunks"` are named after it (`main_shop.luau`), and every file takes
63
92
  * its extension. Default: the entry's own name, with `.luau`. */
64
93
  readonly outFile?: string;
65
94
  }
95
+ /** How the last line of every file `"files"` writes starts: what says a
96
+ * file is tilua's to replace or remove, and not one written by hand. On the
97
+ * last line, so no line the line map counts moves. */
98
+ declare const WRITTEN_BY_TILUA = "-- Written by tilua";
66
99
  /** One file of the output. */
67
100
  interface BundleFile {
68
101
  /** Where it goes, from the folder the output is written to: `main.luau`,
@@ -88,9 +121,9 @@ interface BundleDiagnostic {
88
121
  readonly severity?: "warning";
89
122
  }
90
123
  interface BundleResult {
91
- /** The bundle — the entry's file, when there are several — or `undefined`
92
- * when a module could not be compiled. Type errors are reported but do
93
- * not stop it. */
124
+ /** The bundle — the entry's file, when there are several, and the first
125
+ * module's with `"files"` — or `undefined` when a module could not be
126
+ * compiled. Type errors are reported but do not stop it. */
94
127
  readonly code?: string;
95
128
  /** Every file of the output, the entry's first. Empty when there is no
96
129
  * bundle. */
@@ -145,6 +178,9 @@ interface LoadedLowering {
145
178
  * analyzer was given: a method read from a metatable they declare is
146
179
  * this lowering's to answer. Absent for the language's own. */
147
180
  readonly library?: number;
181
+ /** The library's own options, as the config sets them. None for the
182
+ * language's own. */
183
+ readonly options?: Readonly<Record<string, boolean | number | string | undefined>>;
148
184
  }
149
185
  interface LoweringProblem {
150
186
  readonly file: string;
@@ -157,6 +193,7 @@ declare function loadLowerings(modules: readonly {
157
193
  file: string;
158
194
  from: string;
159
195
  library?: number;
196
+ options?: LoadedLowering["options"];
160
197
  }[]): Promise<{
161
198
  lowerings: LoadedLowering[];
162
199
  problems: LoweringProblem[];
@@ -209,6 +246,12 @@ interface ModuleContext {
209
246
  /** What `import("./x")` calls, with the module's name: the bundle's
210
247
  * wrapper around its `require`. Default: `require` itself. */
211
248
  readonly import?: L.Expression;
249
+ /** Where a module is a file of its own: a function, passed after its
250
+ * name, that loads the file it is in — `function() return
251
+ * require("./shape") end`, with the path written out, for the tools that
252
+ * follow a `require`. `require` calls it only if the module is not
253
+ * already there. */
254
+ loader?(name: string): L.Expression | undefined;
212
255
  /** Luau reaching the bundle's line map — what a library's runtime reads,
213
256
  * as `__LINES__`, to name the file a frame was written in. */
214
257
  readonly lines?: string;
@@ -343,4 +386,4 @@ interface ModuleInfo {
343
386
  }
344
387
  declare function lower(program: T.Program, scopes: ScopeAnalysis, options?: LowerOptions): LowerResult;
345
388
 
346
- export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, bundle, compile, loadLowerings, lower, resolveConfig };
389
+ export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, WRITTEN_BY_TILUA, bundle, compile, loadLowerings, lower, resolveConfig };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as T from '@tilua/parser';
2
2
  import { OutputMode, TiluaConfig, ConfigProblem, LoweringPlugin, TypeAnalysis, ScopeAnalysis } from '@tilua/parser';
3
- export { LoweringPlugin, LuaSyntax, MethodCall, MethodLowering } from '@tilua/parser';
3
+ export { EntryExports, EntryExportsLowering, FilesAnswer, FilesLayout, LoweringPlugin, LuaSyntax, MethodCall, MethodLowering, PluginContext } from '@tilua/parser';
4
4
  import * as L from 'luau-parser';
5
5
 
6
6
  /** What `tilua.config.json` holds. */
@@ -15,8 +15,15 @@ interface TiluaConfigJson {
15
15
  sourceMap?: string | null;
16
16
  /** How many files the output is. `"single"` (the default) is one file;
17
17
  * `"chunks"` also writes a file for what only an `import()` reaches,
18
- * loaded when it is first asked for; `"modules"` writes one per module. */
18
+ * loaded when it is first asked for; `"files"` has no entry, and
19
+ * compiles every file under `rootDir` to one of its own under `outDir`. */
19
20
  output?: OutputMode;
21
+ /** With `"files"`, the folder whose files are compiled. Default: the
22
+ * config's folder. */
23
+ rootDir?: string;
24
+ /** With `"files"`, the folder the output goes in, laid out as `rootDir`
25
+ * is. Default: `rootDir`, each `.luau` beside its `.tilua`. */
26
+ outDir?: string;
20
27
  /** Leave out Lua's standard library — `print`, `string`, `pcall` — which
21
28
  * is otherwise there for every file, as JavaScript's is in TypeScript. */
22
29
  noLib?: boolean;
@@ -25,6 +32,15 @@ interface TiluaConfigJson {
25
32
  /** Write the map from the bundle's lines back to the project's, which
26
33
  * error reports and `console` read. Default: true. */
27
34
  lineMap?: boolean;
35
+ /** Indent the output, a block level at a time: `"\t"` or a number of
36
+ * spaces. Default: no indentation. */
37
+ indent?: "\t" | number;
38
+ /** Write the output on one line, with as few `;` as it can take, and
39
+ * without the line map. Default: false. */
40
+ minify?: boolean;
41
+ /** The options a type library in `types` declares, such as
42
+ * `@tilua-types/luau`'s `typedExports`. */
43
+ [option: string]: unknown;
28
44
  }
29
45
  /** A config file's path, or the config itself. Relative paths — to a config
30
46
  * file, and inside an inline config — resolve from `cwd`. */
@@ -38,8 +54,9 @@ interface ResolvedConfig {
38
54
  declare function resolveConfig(input: ConfigInput | undefined, entry: string, cwd?: string): ResolvedConfig;
39
55
 
40
56
  interface BundleOptions {
41
- /** The file the bundle runs. */
42
- readonly entry: string;
57
+ /** The file the bundle runs. Needed unless the output is `"files"`,
58
+ * which has no entry: every file under the config's `rootDir` is one. */
59
+ readonly entry?: string;
43
60
  /** A `tilua.config.json` path or contents. Default: the nearest config above the entry. */
44
61
  readonly config?: ConfigInput;
45
62
  /** The project folder. Modules are named by their path from it, and an
@@ -49,7 +66,8 @@ interface BundleOptions {
49
66
  /** Check types against the config's `types` too. Default: true. */
50
67
  readonly typeCheck?: boolean;
51
68
  /** How many files to make. Overrides the config's `output`; `"single"`
52
- * without either. */
69
+ * without either. With `"files"`, every file under the config's
70
+ * `rootDir` is compiled to a file of its own, its path from `rootDir`. */
53
71
  readonly output?: OutputMode;
54
72
  /** Leave out what the entry never reaches. Overrides the config's
55
73
  * `treeShaking`; off without either. */
@@ -58,11 +76,26 @@ interface BundleOptions {
58
76
  * the config's `lineMap`; on without either. Without it an error names
59
77
  * the bundle's lines, and the entry does not run under `xpcall`. */
60
78
  readonly lineMap?: boolean;
79
+ /** Indent the output: `"\t"` or a number of spaces. Overrides the
80
+ * config's `indent`; no indentation without either. */
81
+ readonly indent?: "\t" | number;
82
+ /** Write the output on one line, with as few `;` as it can take — and so
83
+ * without the line map, which one line cannot have. Overrides the
84
+ * config's `minify`; off without either. */
85
+ readonly minify?: boolean;
86
+ /** With `"files"`, the folder the output is written to — where a host's
87
+ * layout (a Rojo project) finds it. Overrides the config's `outDir`;
88
+ * `rootDir` without either. */
89
+ readonly outDir?: string;
61
90
  /** What the entry's file is called: `main.luau`. The other files of
62
91
  * `"chunks"` are named after it (`main_shop.luau`), and every file takes
63
92
  * its extension. Default: the entry's own name, with `.luau`. */
64
93
  readonly outFile?: string;
65
94
  }
95
+ /** How the last line of every file `"files"` writes starts: what says a
96
+ * file is tilua's to replace or remove, and not one written by hand. On the
97
+ * last line, so no line the line map counts moves. */
98
+ declare const WRITTEN_BY_TILUA = "-- Written by tilua";
66
99
  /** One file of the output. */
67
100
  interface BundleFile {
68
101
  /** Where it goes, from the folder the output is written to: `main.luau`,
@@ -88,9 +121,9 @@ interface BundleDiagnostic {
88
121
  readonly severity?: "warning";
89
122
  }
90
123
  interface BundleResult {
91
- /** The bundle — the entry's file, when there are several — or `undefined`
92
- * when a module could not be compiled. Type errors are reported but do
93
- * not stop it. */
124
+ /** The bundle — the entry's file, when there are several, and the first
125
+ * module's with `"files"` — or `undefined` when a module could not be
126
+ * compiled. Type errors are reported but do not stop it. */
94
127
  readonly code?: string;
95
128
  /** Every file of the output, the entry's first. Empty when there is no
96
129
  * bundle. */
@@ -145,6 +178,9 @@ interface LoadedLowering {
145
178
  * analyzer was given: a method read from a metatable they declare is
146
179
  * this lowering's to answer. Absent for the language's own. */
147
180
  readonly library?: number;
181
+ /** The library's own options, as the config sets them. None for the
182
+ * language's own. */
183
+ readonly options?: Readonly<Record<string, boolean | number | string | undefined>>;
148
184
  }
149
185
  interface LoweringProblem {
150
186
  readonly file: string;
@@ -157,6 +193,7 @@ declare function loadLowerings(modules: readonly {
157
193
  file: string;
158
194
  from: string;
159
195
  library?: number;
196
+ options?: LoadedLowering["options"];
160
197
  }[]): Promise<{
161
198
  lowerings: LoadedLowering[];
162
199
  problems: LoweringProblem[];
@@ -209,6 +246,12 @@ interface ModuleContext {
209
246
  /** What `import("./x")` calls, with the module's name: the bundle's
210
247
  * wrapper around its `require`. Default: `require` itself. */
211
248
  readonly import?: L.Expression;
249
+ /** Where a module is a file of its own: a function, passed after its
250
+ * name, that loads the file it is in — `function() return
251
+ * require("./shape") end`, with the path written out, for the tools that
252
+ * follow a `require`. `require` calls it only if the module is not
253
+ * already there. */
254
+ loader?(name: string): L.Expression | undefined;
212
255
  /** Luau reaching the bundle's line map — what a library's runtime reads,
213
256
  * as `__LINES__`, to name the file a frame was written in. */
214
257
  readonly lines?: string;
@@ -343,4 +386,4 @@ interface ModuleInfo {
343
386
  }
344
387
  declare function lower(program: T.Program, scopes: ScopeAnalysis, options?: LowerOptions): LowerResult;
345
388
 
346
- export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, bundle, compile, loadLowerings, lower, resolveConfig };
389
+ export { type BundleDiagnostic, type BundleFile, type BundleOptions, type BundleResult, type CompileResult, type ConfigInput, type Diagnostic, type LoadedLowering, type LowerDiagnostic, type LowerOptions, type LowerResult, type LoweringProblem, type ModuleContext, type ResolvedConfig, type TiluaConfigJson, WRITTEN_BY_TILUA, bundle, compile, loadLowerings, lower, resolveConfig };
package/dist/index.js CHANGED
@@ -1,16 +1,19 @@
1
1
  import {
2
+ WRITTEN_BY_TILUA,
2
3
  bundle,
3
4
  loadLowerings,
4
5
  lower,
6
+ printOptions,
7
+ printOutput,
5
8
  program,
6
- resolveConfig
7
- } from "./chunk-WOBHMGGC.js";
9
+ resolveConfig,
10
+ syntaxOf
11
+ } from "./chunk-VUIF62OE.js";
8
12
 
9
13
  // src/compile.ts
10
14
  import { readFileSync } from "fs";
11
15
  import { resolve } from "path";
12
16
  import { parse, analyzeScopes, analyzeTypes, resolveTypeLibraries, ParseError, LexError } from "@tilua/parser";
13
- import { print } from "luau-parser";
14
17
  async function compile(source, config) {
15
18
  let program2;
16
19
  try {
@@ -30,7 +33,7 @@ async function compile(source, config) {
30
33
  line: d.node.line.start,
31
34
  column: d.node.column.start
32
35
  }));
33
- const libraries = resolved ? resolveTypeLibraries(resolved) : { files: [], lowerings: [], problems: [] };
36
+ const libraries = resolved ? resolveTypeLibraries(resolved) : { files: [], lowerings: [], options: [], problems: [] };
34
37
  const { lowerings, problems: loweringProblems } = await loadLowerings(libraries.lowerings);
35
38
  for (const p of [...problems, ...libraries.problems]) {
36
39
  diagnostics.push({ message: p.message, line: p.line ?? 1, column: p.column ?? 1 });
@@ -45,9 +48,15 @@ async function compile(source, config) {
45
48
  });
46
49
  diagnostics.push(...lowered.diagnostics);
47
50
  if (diagnostics.length) return { diagnostics };
48
- return { code: print(program(lowered.statements)) + "\n", diagnostics };
51
+ const printed = printOutput(program(lowered.statements), printOptions(syntaxOf(lowerings), {
52
+ indent: resolved?.indent,
53
+ minify: resolved?.minify
54
+ }));
55
+ if ("problem" in printed) return { diagnostics: [{ message: printed.problem, line: 1, column: 1 }] };
56
+ return { code: printed.code + "\n", diagnostics };
49
57
  }
50
58
  export {
59
+ WRITTEN_BY_TILUA,
51
60
  bundle,
52
61
  compile,
53
62
  loadLowerings,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tilua/compiler",
3
- "version": "8.1.0",
3
+ "version": "9.0.0",
4
4
  "description": "Compiles tilua to Luau: lowers the @tilua/parser AST to a luau-parser AST and prints it",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -37,8 +37,8 @@
37
37
  "license": "ISC",
38
38
  "type": "module",
39
39
  "dependencies": {
40
- "luau-parser": "^1.0.3",
41
- "@tilua/parser": "^8.1.0"
40
+ "@tilua/parser": "^9.0.0",
41
+ "luau-parser": "^1.1.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/node": "^22.20.2",