Skip to content

Commit

Permalink
do not add type imports in .js files
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Stamm <ts@timostamm.de>
  • Loading branch information
timostamm committed Dec 2, 2024
1 parent 437d32e commit 6c145a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ describe("v2.0.0 transform class references", () => {
const result = updateSourceFileInMemory(transform, input, "foo.ts");
expect(result.source).toEqual(output);
});
it("does not add type import to .js file when transforming new", () => {
const input = [
`import {Foo} from "./x_pb.js";`,
`const foo = new Foo();`,
`foo instanceof Foo;`,
].join("\n");
const output = [
`import { FooSchema } from "./x_pb.js";`,
`import { create } from "@bufbuild/protobuf";`,
`const foo = create(FooSchema);`,
`foo instanceof Foo;`,
].join("\n");
const result = updateSourceFileInMemory(transform, input, "foo.js");
expect(result.source).toEqual(output);
});
it("adds create import to existing when transforming new", () => {
const input = [
`import {fake} from "@bufbuild/protobuf";`,
Expand Down Expand Up @@ -111,6 +126,22 @@ describe("v2.0.0 transform class references", () => {
const result = updateSourceFileInMemory(transform, input, "foo.ts");
expect(result.source).toEqual(output);
});
it("does not add type import to .ts file when transforming isMessage()", () => {
const input = [
`import {isMessage} from "@bufbuild/protobuf";`,
`import {Foo} from "./x_pb.js";`,
`isMessage(1, Foo);`,
`foo instanceof Foo;`,
].join("\n");
const output = [
`import {isMessage} from "@bufbuild/protobuf";`,
`import { FooSchema } from "./x_pb.js";`,
`isMessage(1, FooSchema);`,
`foo instanceof Foo;`,
].join("\n");
const result = updateSourceFileInMemory(transform, input, "foo.js");
expect(result.source).toEqual(output);
});
it("transforms wkt", () => {
const input = [
`import {Timestamp} from "@bufbuild/protobuf";`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,21 @@ const transform: j.Transform = (file, { j }, options) => {
}

// Add type import when the name was used outside of new and isMessage()
for (const name of pbNames) {
if (root.find(j.Identifier, { name }).size() > 0) {
const pbImports = findPbImports(name + "Schema", root);
const firstImport = pbImports.at(0);
const from = (firstImport.get() as j.ASTPath<j.ImportDeclaration>).value
.source;
firstImport.insertAfter(
j.importDeclaration(
[j.importSpecifier(j.identifier(name))],
from,
"type",
),
);
if (file.path.endsWith(".ts")) {
for (const name of pbNames) {
if (root.find(j.Identifier, { name }).size() > 0) {
const pbImports = findPbImports(name + "Schema", root);
const firstImport = pbImports.at(0);
const from = (firstImport.get() as j.ASTPath<j.ImportDeclaration>).value
.source;
firstImport.insertAfter(
j.importDeclaration(
[j.importSpecifier(j.identifier(name))],
from,
"type",
),
);
}
}
}

Expand Down

0 comments on commit 6c145a3

Please sign in to comment.