Skip to content

Commit

Permalink
fix: generate modules for empty files with esModuleInterop (#992)
Browse files Browse the repository at this point in the history
* fix: generate modules for empty files with esModuleInterop

* generate extension.ts

* also add empty.ts
  • Loading branch information
jk-apple authored Jan 26, 2024
1 parent f9ddb2e commit f0629ab
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions integration/extension-import/base.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto2";

package foo;

message Extendable{
required string field = 1;

extensions 4 to max;
}
13 changes: 13 additions & 0 deletions integration/extension-import/extension-import-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as path from "node:path";
import * as fs from "node:fs";
import * as ts from "typescript";

describe('extension-only-files', () => {
it("generate as external module", () => {
const generatedPath = path.join(__dirname, "extension.ts");
const generatedCode = fs.readFileSync(generatedPath, "utf8");
const source = ts.createSourceFile(generatedPath, generatedCode, ts.ScriptTarget.ES2018);

expect(ts.isExternalModule(source)).toBe(true);
})
});
9 changes: 9 additions & 0 deletions integration/extension-import/extension.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto2";

import "base.proto";

package foo;

extend Extendable{
optional int32 bar = 10;
}
3 changes: 3 additions & 0 deletions integration/extension-import/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */

export {};
1 change: 1 addition & 0 deletions integration/extension-import/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputIndex=true,esModuleInterop=true
7 changes: 7 additions & 0 deletions integration/simple-esmodule-interop/empty-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { } from './empty';

describe('empty', () => {
it('compiles', () => {
// if ./empty was not a module, this would not compile.
});
});
2 changes: 2 additions & 0 deletions integration/simple-esmodule-interop/empty.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
syntax = "proto2";
package empty;
3 changes: 3 additions & 0 deletions integration/simple-esmodule-interop/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */

export const protobufPackage = "empty";
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri
chunks.push(...generateSchema(ctx, fileDesc, sourceInfo));
}

// https://www.typescriptlang.org/docs/handbook/2/modules.html:
// > In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.
// > Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
//
// Thus, to mark an empty file a module, we need to add `export {}` to it.
if (options.esModuleInterop && chunks.length === 0) {
chunks.push(code`export {};`);
}

chunks.push(
...Object.values(utils).map((v) => {
if (v instanceof ConditionalOutput) {
Expand Down

0 comments on commit f0629ab

Please sign in to comment.