Skip to content

Commit

Permalink
Throw if a language config bundle cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Sep 11, 2024
1 parent 42ee1b3 commit d0938d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Sources/SwiftTreeSitter/LanguageConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public struct LanguageData: Sendable {
}
}

enum LanguageConfigurationError: Error {
case queryDirectoryNotFound
case queryDirectoryNotReadable(URL)
}

/// A structure that holds a language parser, name, and its assoicated queries.
public struct LanguageConfiguration: Sendable {
public let language: Language
Expand Down Expand Up @@ -60,8 +65,17 @@ extension LanguageConfiguration {
}

public init(_ language: Language, name: String, bundleName: String) throws {
let queriesURL = Self.bundleQueriesDirectoryURL(for: bundleName)
let queries = try queriesURL.flatMap { try Query.queries(for: language, in: $0) } ?? [:]
guard let queriesURL = Self.bundleQueriesDirectoryURL(for: bundleName) else {
throw LanguageConfigurationError.queryDirectoryNotFound
}

let path = queriesURL.standardizedFileURL.path

if FileManager.default.isReadableFile(atPath: path) == false {
throw LanguageConfigurationError.queryDirectoryNotReadable(queriesURL)
}

let queries = try Query.queries(for: language, in: queriesURL)

self.init(language, name: name, queries: queries)
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftTreeSitterTests/LanguageConfigurationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XCTest

import SwiftTreeSitter
import TestTreeSitterSwift

final class LanguageConfigurationTests: XCTestCase {
func testCreateLanguageWithIncorrectBundleName() throws {
let language = Language(language: tree_sitter_swift())

XCTAssertThrowsError(try LanguageConfiguration(language, name: "Swift", bundleName: "abc"))
}
}

0 comments on commit d0938d3

Please sign in to comment.