Skip to content

Commit cd6fc1d

Browse files
committed
Allow (CLI) specifying --tsdModuleName for .d.ts
1 parent ed7570f commit cd6fc1d

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

cli/asc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,21 @@ exports.main = function main(argv, options, callback) {
896896

897897
// Write TypeScript definition
898898
if (opts.tsdFile != null) {
899+
let moduleName;
899900
let tsd;
901+
if (opts.tsdModuleName != null) {
902+
moduleName = opts.tsdModuleName;
903+
}
900904
if (opts.tsdFile.length) {
901905
stats.emitCount++;
902906
stats.emitTime += measure(() => {
903-
tsd = assemblyscript.buildTSD(program);
907+
tsd = assemblyscript.buildTSD(program, moduleName);
904908
});
905909
writeFile(opts.tsdFile, tsd, baseDir);
906910
} else if (!hasStdout) {
907911
stats.emitCount++;
908912
stats.emitTime += measure(() => {
909-
tsd = assemblyscript.buildTSD(program);
913+
tsd = assemblyscript.buildTSD(program, moduleName);
910914
});
911915
writeStdout(tsd);
912916
hasStdout = true;

cli/asc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@
118118
"type": "s",
119119
"alias": "d"
120120
},
121+
"tsdModuleName": {
122+
"category": "Output",
123+
"description": "Specifies the TypeScript definition output file module name.",
124+
"type": "s",
125+
"default": "ASModule"
126+
},
121127

122128
"sourceMap": {
123129
"category": "Debugging",

src/definitions.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,23 @@ export class IDLBuilder extends ExportsWalker {
373373
export class TSDBuilder extends ExportsWalker {
374374

375375
/** Builds TypeScript definitions for the specified program. */
376-
static build(program: Program): string {
377-
return new TSDBuilder(program).build();
376+
static build(program: Program, moduleName?: string): string {
377+
return new TSDBuilder(program, false, moduleName).build();
378378
}
379379

380+
private moduleName: string = "ASModule";
380381
private sb: string[] = [];
381382
private indentLevel: i32 = 0;
382383

383384
/** Constructs a new WebIDL builder. */
384-
constructor(program: Program, includePrivate: bool = false) {
385+
constructor(program: Program, includePrivate: bool = false, moduleName?: string) {
385386
super(program, includePrivate);
387+
388+
if (moduleName) {
389+
if (moduleName.trim().match(/^[a-z0-9_]+$/i)) {
390+
this.moduleName = moduleName.trim();
391+
}
392+
}
386393
}
387394

388395
visitGlobal(name: string, element: Global): void {
@@ -592,7 +599,7 @@ export class TSDBuilder extends ExportsWalker {
592599
build(): string {
593600
var sb = this.sb;
594601
var isWasm64 = this.program.options.isWasm64;
595-
sb.push("declare module ASModule {\n");
602+
sb.push("declare module " + this.moduleName + " {\n");
596603
sb.push(" type i8 = number;\n");
597604
sb.push(" type i16 = number;\n");
598605
sb.push(" type i32 = number;\n");
@@ -618,7 +625,7 @@ export class TSDBuilder extends ExportsWalker {
618625
this.walk();
619626
--this.indentLevel;
620627
sb.push("}\n");
621-
sb.push("export default ASModule;\n");
628+
sb.push("export default " + this.moduleName + ";\n");
622629
return this.sb.join("");
623630
}
624631
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ export function buildIDL(program: Program): string {
253253
}
254254

255255
/** Builds TypeScript definitions for the specified program. */
256-
export function buildTSD(program: Program): string {
257-
return TSDBuilder.build(program);
256+
export function buildTSD(program: Program, moduleName?: string): string {
257+
return TSDBuilder.build(program, moduleName);
258258
}
259259

260260
// Full API

0 commit comments

Comments
 (0)