最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to lint Swift package during build without external libraries? - Stack Overflow

programmeradmin1浏览0评论

I'm working on a Swift package and want to lint its build in Xcode without relying on external tools. For Xcode projects, it's possible to integrate swift-format as a build phase to enforce code style and show warnings.

Is there a way to achieve the same for Swift packages using only Xcode or Swift Package Manager, without external dependencies?

Package.swift

import PackageDescription

let package = Package(
    name: "MyPackage",
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]
        )
    ],
    targets: [
        .target(
            name: "MyPackage"
        ),
    ]
)

MyPackage.swift

struct MyPackage {
    var a: Int
       var b: Int
    var c: Int
}

The goal is to get a warning when the package is build because of too many spaces before var b: Int line.

I'm working on a Swift package and want to lint its build in Xcode without relying on external tools. For Xcode projects, it's possible to integrate swift-format as a build phase to enforce code style and show warnings.

Is there a way to achieve the same for Swift packages using only Xcode or Swift Package Manager, without external dependencies?

Package.swift

import PackageDescription

let package = Package(
    name: "MyPackage",
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]
        )
    ],
    targets: [
        .target(
            name: "MyPackage"
        ),
    ]
)

MyPackage.swift

struct MyPackage {
    var a: Int
       var b: Int
    var c: Int
}

The goal is to get a warning when the package is build because of too many spaces before var b: Int line.

Share Improve this question edited Feb 14 at 21:07 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Feb 10 at 17:20 Oleksii KolomiietsOleksii Kolomiiets 513 bronze badges 4
  • 1 There's no built-in linter/formatter either in Xcode or SPM. However, there's a 1st party Apple SwiftFormat package that you can use if you want to avoid external dependencies. – David Pasztor Commented Feb 10 at 17:27
  • You can add swiftlint as plugin: swiftpackageindex/realm/… – Cy-4AH Commented Feb 11 at 10:52
  • Thanks. I wanted to avoid using any external libraries. Seems like swift-format something that can be explored. I'll post my findings. – Oleksii Kolomiiets Commented Feb 11 at 14:39
  • 1 Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Feb 12 at 0:48
Add a comment  | 

1 Answer 1

Reset to default 0

I found a solution to this issue and am sharing my findings:

Linting Swift Packages in Xcode 16 Without External Dependencies

With Xcode 16, swift-format is included as part of the Xcode toolchain, eliminating the need for external libraries and making Swift file formatting more convenient.

Solution Summary

You can lint your Swift package without third-party dependencies by:

  1. Creating a build tool plugin in your Package.swift file
  2. Implementing the plugin to use the built-in swift-format from Xcode 16
  3. Adding the plugin to your target

When your package builds, Xcode will show warnings for formatting issues like the inconsistent indentation in my example:

struct MyPackage {
    var a: Int
       var b: Int  // This will show a warning in Xcode
    var c: Int
}

The warnings are displayed directly in Xcode’s issue navigator during the build process.

For a comprehensive guide with step-by-step instructions and sample code, refer to this article: Linting a Swift Package with swift-format

Note that while the article references adding swift-format as a dependency, with Xcode 16 you can simplify this approach by using the built-in toolchain version instead.

(Full disclosure: I contributed to this article at SnappMobile.)

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>