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.
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { pythonImportHandler } from "../../../src/patch/imports/python";
|
|
|
|
describe("pythonImportHandler.apply", () => {
|
|
test("inserts after shebang, encoding, and module docstring", () => {
|
|
const content = '#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\n"""module docs"""\n\nvalue = 1\n';
|
|
|
|
const result = pythonImportHandler.apply(content, [{ from: "os", imports: ["path"] }]);
|
|
|
|
expect(result).toEqual({
|
|
content:
|
|
'#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\n"""module docs"""\n\nfrom os import path\n\nvalue = 1\n',
|
|
added: ["from os import path"],
|
|
warnings: [],
|
|
});
|
|
});
|
|
|
|
test("merges names into an existing from-import", () => {
|
|
const content = "from pathlib import Path\n\nvalue = 1\n";
|
|
|
|
const result = pythonImportHandler.apply(content, [{ from: "pathlib", imports: ["PurePath"] }]);
|
|
|
|
expect(result.content).toBe("from pathlib import Path, PurePath\n\nvalue = 1\n");
|
|
expect(result.added).toEqual(["from pathlib import PurePath"]);
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
test("preserves existing import grouping when adding to an existing group", () => {
|
|
const content = "import os\n\nimport requests\n\nfrom .local import thing\n\nvalue = 1\n";
|
|
|
|
const result = pythonImportHandler.apply(content, [{ from: "sys" }]);
|
|
|
|
expect(result.content).toBe(
|
|
"import os\nimport sys\n\nimport requests\n\nfrom .local import thing\n\nvalue = 1\n",
|
|
);
|
|
expect(result.added).toEqual(["import sys"]);
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
test("dedupes an already present import", () => {
|
|
const content = "from pathlib import Path\n";
|
|
|
|
const result = pythonImportHandler.apply(content, [{ from: "pathlib", imports: ["Path"] }]);
|
|
|
|
expect(result).toEqual({
|
|
content,
|
|
added: [],
|
|
warnings: [],
|
|
});
|
|
});
|
|
});
|