f044f3ba75
- Added language-specific import management for TypeScript, JavaScript, Python, Rust, Go, and C/C++ with automatic deduplication and grouping. - Added `imports` parameter to patch, hashline, and replace edit operations for structured import/include requests with named, default, namespace, and alias support. - Added `edit.manageImports` setting to control post-edit import management behavior across all edit operations. - Implemented language-specific import handlers with parsing, merging, and rendering logic for each supported language. - Integrated import application into EditTool with feature flag support and comprehensive test coverage across all languages.
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { goImportHandler } from "../../../src/patch/imports/go";
|
|
import type { ImportSpec } from "../../../src/patch/types";
|
|
|
|
function apply(content: string, imports: ImportSpec[]) {
|
|
return goImportHandler.apply(content, imports);
|
|
}
|
|
|
|
describe("go import handler", () => {
|
|
it("converts a single import into a block when adding another import", () => {
|
|
const source = ["package main", "", 'import "fmt"', "", "func main() {}", ""].join("\n");
|
|
|
|
const result = apply(source, [{ from: "os" }]);
|
|
|
|
expect(result.content).toBe(
|
|
["package main", "", "import (", '\t"fmt"', '\t"os"', ")", "", "func main() {}", ""].join("\n"),
|
|
);
|
|
expect(result.added).toEqual(['"os"']);
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("groups stdlib imports before third-party imports in a new block", () => {
|
|
const source = ["package main", "", "func main() {}", ""].join("\n");
|
|
|
|
const result = apply(source, [{ from: "github.com/acme/project" }, { from: "fmt" }]);
|
|
|
|
expect(result.content).toBe(
|
|
[
|
|
"package main",
|
|
"",
|
|
"import (",
|
|
'\t"fmt"',
|
|
"",
|
|
'\t"github.com/acme/project"',
|
|
")",
|
|
"",
|
|
"func main() {}",
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("deduplicates repeated requested imports", () => {
|
|
const source = ["package main", "", "func main() {}", ""].join("\n");
|
|
|
|
const result = apply(source, [
|
|
{ from: "fmt" },
|
|
{ from: "fmt" },
|
|
{ from: "github.com/acme/project" },
|
|
{ from: "github.com/acme/project" },
|
|
]);
|
|
|
|
expect(result.content).toBe(
|
|
[
|
|
"package main",
|
|
"",
|
|
"import (",
|
|
'\t"fmt"',
|
|
"",
|
|
'\t"github.com/acme/project"',
|
|
")",
|
|
"",
|
|
"func main() {}",
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
expect(result.added).toEqual(['"fmt"', '"github.com/acme/project"']);
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
});
|