Skip to content

Commit 11e0f56

Browse files
authored
Fix name collision with import in generated code (#688)
1 parent c912f62 commit 11e0f56

File tree

6 files changed

+121
-10
lines changed

6 files changed

+121
-10
lines changed

packages/protobuf-test/extra/name-clash.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
syntax = "proto3";
1616
package spec;
1717

18+
import "extra/example.proto";
19+
20+
// This message class will clash with the imported message class
21+
message User {
22+
docs.User u = 1; // Reference the import with the same name to trigger a clash
23+
}
24+
1825
message ReservedPropertyNames {
1926
message BuiltIn {
2027
string constructor = 2; // built-in constructor()

packages/protobuf-test/src/gen/js/extra/name-clash_pb.d.ts

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/protobuf-test/src/gen/js/extra/name-clash_pb.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/protobuf-test/src/gen/ts/extra/name-clash_pb.ts

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/protoplugin-test/src/file-export-decl.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ describe("file exportDecl", () => {
4848
expect(lines).toStrictEqual(["export const SomeEnum = 123;"]);
4949
});
5050

51+
test("forces import with same name to be aliased", async () => {
52+
const lines = await testGenerate((f) => {
53+
f.print(f.import("Foo", "pkg"));
54+
f.print(f.exportDecl("const", "Foo"), " = 123;");
55+
});
56+
expect(lines).toStrictEqual([
57+
`import { Foo as Foo$1 } from "pkg";`,
58+
``,
59+
`Foo$1`,
60+
`export const Foo = 123;`,
61+
]);
62+
});
63+
5164
async function testGenerate(
5265
gen: (
5366
f: GeneratedFile,

packages/protoplugin/src/ecmascript/generated-file.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,25 @@ function processImports(
431431

432432
// Walk through all symbols used and populate the collections above.
433433
for (const s of el) {
434-
if (typeof s != "object" || s.kind !== "es_symbol") {
434+
if (typeof s != "object") {
435435
continue;
436436
}
437-
symbolToIdentifier.set(s.id, s.name);
438-
if (!s.typeOnly) {
439-
// a symbol is only type-imported as long as all uses are type-only
440-
symbolToIsValue.set(s.id, true);
441-
}
442-
if (s.from === importerPath) {
443-
identifiersTaken.add(s.name);
444-
} else {
445-
foreignSymbols.push(s);
437+
switch (s.kind) {
438+
case "es_symbol":
439+
symbolToIdentifier.set(s.id, s.name);
440+
if (!s.typeOnly) {
441+
// a symbol is only type-imported as long as all uses are type-only
442+
symbolToIsValue.set(s.id, true);
443+
}
444+
if (s.from === importerPath) {
445+
identifiersTaken.add(s.name);
446+
} else {
447+
foreignSymbols.push(s);
448+
}
449+
break;
450+
case "es_export_stmt":
451+
identifiersTaken.add(s.name);
452+
break;
446453
}
447454
}
448455

0 commit comments

Comments
 (0)