From a36c693f9b5f4dd8e39eb5d3b269ce130cdd326a Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Mon, 18 Mar 2024 09:16:40 +0100 Subject: [PATCH] fmt: fix autoimport with shebang and comments above other imports (#21050) --- vlib/v/fmt/fmt.v | 3 ++- ...auto_with_shebang_and_comment_above_imports_expected.vv | 7 +++++++ ...rt_auto_with_shebang_and_comment_above_imports_input.vv | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_expected.vv create mode 100644 vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 77a97d122..219301f58 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -98,7 +98,8 @@ pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug return res } mut import_start_pos := f.import_pos - if stmt := file.stmts[1] { + if f.import_pos == 0 && file.stmts.len > 1 { + stmt := file.stmts[1] if stmt is ast.ExprStmt && stmt.expr is ast.Comment && (stmt.expr as ast.Comment).text.starts_with('#!') { import_start_pos = stmt.pos.len diff --git a/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_expected.vv b/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_expected.vv new file mode 100644 index 000000000..e57f581ef --- /dev/null +++ b/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_expected.vv @@ -0,0 +1,7 @@ +#!/usr/bin/env -S v + +// Comment +import json +import os + +os.join_path('', '') diff --git a/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_input.vv b/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_input.vv new file mode 100644 index 000000000..21cab8bf1 --- /dev/null +++ b/vlib/v/fmt/tests/import_auto_with_shebang_and_comment_above_imports_input.vv @@ -0,0 +1,5 @@ +#!/usr/bin/env -S v + +// Comment +import json +os.join_path('', '') -- 2.39.5