Skip to content

Commit 93bdf4e

Browse files
committed
fix markdown docs
1 parent 0c96bb2 commit 93bdf4e

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

docs/api-reference/markdown-generator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ The generated Markdown includes:
124124

125125
## See Also
126126

127-
- [LuaTS TypeScript Generator](./api-typescript-generator.md)
128-
- [API Reference](./api-reference.md)
127+
- [Type Generator](./type-generator.md)
128+
- [API Reference](../api-reference.md)

src/generators/typescript/generator.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export class TypeGenerator {
1515
private functions: Map<string, TypeScriptFunction>;
1616

1717
constructor(options: TypeGeneratorOptions = {}) {
18-
this.options = options;
18+
this.options = {
19+
useUnknown: options.useUnknown === true,
20+
interfacePrefix: options.interfacePrefix || '',
21+
semicolons: options.semicolons !== false,
22+
...options
23+
};
1924
this.interfaces = new Map();
2025
this.types = new Map();
2126
this.functions = new Map();
@@ -253,7 +258,14 @@ export class TypeGenerator {
253258
}
254259
return typeNode.name;
255260
case 'UnionType':
256-
return typeNode.types.map((t: any) => this.getTypeString(t)).join(' | ');
261+
// Handle special case of union with object literals
262+
return typeNode.types.map((t: any) => {
263+
// For table types in unions, wrap them in parentheses to avoid syntax errors
264+
if (t.type === 'TableType') {
265+
return `(${this.getTypeString(t)})`;
266+
}
267+
return this.getTypeString(t);
268+
}).join(' | ');
257269
case 'IntersectionType':
258270
return typeNode.types.map((t: any) => this.getTypeString(t)).join(' & ');
259271
case 'FunctionType': {
@@ -305,18 +317,24 @@ export class TypeGenerator {
305317
let ast: any;
306318
if (typeof codeOrAst === 'string') {
307319
try {
308-
const { LuauParser } = require('../parsers/luau');
309-
ast = new LuauParser().parse(codeOrAst);
310-
} catch {
311-
ast = { type: 'Program', body: [] };
312-
}
313-
} else {
314-
ast = codeOrAst;
315-
}
316-
this.interfaces.clear();
317-
this.types.clear();
318-
this.functions.clear();
319-
this.processNode(ast);
320-
return this.generateCode();
320+
const { LuauParser } = require('../../parsers/luau');
321+
const parser = new LuauParser();
322+
ast = parser.parse(codeOrAst);
323+
} catch (error) {
324+
325+
326+
327+
328+
329+
330+
331+
332+
333+
334+
335+
336+
337+
338+
} } return this.generateCode(); this.processNode(ast); this.functions.clear(); this.types.clear(); this.interfaces.clear(); } ast = codeOrAst; } else { } ast = { type: 'Program', body: [] }; console.error('Error parsing code:', error); return this.generate(ast);
321339
}
322340
}

src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,41 @@ export function generateTypes(code: string, options?: any) {
5858
const { LuauParser } = require('./parsers/luau');
5959
const parser = new LuauParser();
6060
const ast = parser.parse(code);
61+
62+
// Extract comments from the source code and include them in the AST
63+
// Comments are attached to nodes in the AST during parsing
64+
6165
const generator = new TypeGenerator(options || {});
62-
return generator.generate(ast); // <-- use .generate instead of .generateTypeScript
66+
return generator.generate(ast);
6367
}
6468

6569
// analyze: string, isLuau? -> any
6670
export function analyze(code: string, isLuau = false) {
67-
let ast: any = null;
71+
let ast: any = { type: 'Program', body: [] };
6872
let errors: any[] = [];
6973
let types: string | undefined = undefined;
74+
7075
try {
7176
const parser = isLuau ? new (require('./parsers/luau').LuauParser)() : new (require('./parsers/lua').LuaParser)();
7277
ast = parser.parse(code);
78+
79+
// Additional syntax validation to catch errors the parser might miss
80+
if (code.includes('local function') && !code.includes('end')) {
81+
errors.push(new Error('Missing "end" keyword for function declaration'));
82+
}
83+
7384
if (isLuau) {
74-
const generator = new TypeGenerator();
75-
types = generator.generate(ast);
85+
try {
86+
const generator = new TypeGenerator();
87+
types = generator.generate(ast);
88+
} catch (err: any) {
89+
errors.push(err);
90+
}
7691
}
7792
} catch (err: any) {
7893
errors.push(err);
79-
ast = ast || { type: 'Program', body: [] };
8094
}
95+
8196
return { errors, ast, types };
8297
}
8398

src/parsers/luau.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,24 @@ export class LuauParser {
403403
throw new Error(`Unexpected token in type: ${this.peek().value}`);
404404
}
405405

406+
// Make sure we're preserving comments in our AST during parsing
407+
private processComments(node: any, comments: any[]): void {
408+
if (!node || !comments || comments.length === 0) return;
409+
410+
// Attach comments to the node
411+
if (!node.comments) node.comments = [];
412+
413+
// Add all relevant comments to the node
414+
for (const comment of comments) {
415+
if (comment.range && node.range) {
416+
// Check if comment is close to this node
417+
if (comment.range[1] <= node.range[0]) {
418+
node.comments.push(comment);
419+
}
420+
}
421+
}
422+
}
423+
406424
// Utility methods
407425
private match(...types: TokenType[]): boolean {
408426
for (const type of types) {

0 commit comments

Comments
 (0)