Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift6のFoundationのバグを回避する #43

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: swift-actions/setup-swift@v1
- uses: swift-actions/setup-swift@v2
with:
swift-version: "5.7"
- uses: actions/checkout@v3
swift-version: "5.10.1"
- uses: actions/checkout@v4
- run: swift package resolve
- run: swift build
- run: swift test
10 changes: 5 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"identity" : "codegenkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/omochi/CodegenKit",
"location" : "https://github.com/omochi/CodegenKit.git",
"state" : {
"revision" : "ab5ad508aab30b1572caa6e73ac0206611db0b0d",
"version" : "2.0.0"
Expand All @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-cmark.git",
"state" : {
"revision" : "3bc2f3e25df0cecc5dc269f7ccae65d0f386f06a",
"version" : "0.4.0"
"revision" : "3ccff77b2dc5b96b77db3da0d68d28068593fa53",
"version" : "0.5.0"
}
},
{
Expand All @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-markdown.git",
"state" : {
"revision" : "4aae40bf6fff5286e0e1672329d17824ce16e081",
"version" : "0.4.0"
"revision" : "8f79cb175981458a0a27e76cb42fee8e17b1a993",
"version" : "0.5.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.8

import PackageDescription

Expand Down
27 changes: 20 additions & 7 deletions Sources/TypeScriptAST/Basic/URLs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ public enum URLs {
Modified implementation of
https://github.com/neoneye/SwiftyRelativePath
*/
public static func relativePath(to dest: URL, from originalFrom: URL) -> URL {
let dest = dest.absoluteURL.standardized.pathComponents
let from = originalFrom.absoluteURL.standardized.pathComponents
public static func relativePath(to destURL: URL, from fromURL: URL) -> URL {
let dest = destURL.absoluteURL.standardized.pathComponents
let from = fromURL.absoluteURL.standardized.pathComponents

var i = 0
while i < dest.count,
Expand All @@ -21,23 +21,36 @@ public enum URLs {
rel.append(contentsOf: dest[i...])
return URL(
fileURLWithPath: rel.joined(separator: "/"),
relativeTo: originalFrom
relativeTo: fromURL
)
}

public static func replacingPathExtension(of url: URL, to ext: String) -> URL {
let dir = url.deletingLastPathComponent()
var dir = url.deletingLastPathComponent()
if dir.relativePath == "" {
// swift-foundation のバグを回避する
dir = URL(fileURLWithPath: ".", isDirectory: true, relativeTo: dir.baseURL)
}

var base = url.lastPathComponent
let stem = (base as NSString).deletingPathExtension
base = stem
if !ext.isEmpty {
base += "." + ext
}
if dir.relativePath == "." {
/*
元の relativePath が foo.ts のような単体ファイル名だった場合、
appendすると ./foo.ts に変化してしまう。
これを防ぐために init を使う。
*/
return URL(fileURLWithPath: base, relativeTo: url.baseURL)
} else {
return dir.appendingPathComponent(base)
}

/*
元の relativePath が絶対パスでも相対パスでもそれを維持するために、init ではなく append を使う.
*/
return dir.appendingPathComponent(base)
}
}

43 changes: 43 additions & 0 deletions Tests/TypeScriptASTTests/UtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import XCTest
import TypeScriptAST

final class UtilsTests: XCTestCase {
func testReplaceExtension() throws {
let work = URL(fileURLWithPath: "/work", isDirectory: true)

do {
let u = URLs.replacingPathExtension(of: URL(fileURLWithPath: "a.swift", relativeTo: work), to: "ts")
XCTAssertEqual(u.relativePath, "a.ts")
XCTAssertEqual(u.path, "/work/a.ts")
XCTAssertEqual(u.baseURL?.path, "/work")
}

do {
let u = URLs.replacingPathExtension(of: URL(fileURLWithPath: "dir/a.swift", relativeTo: work), to: "ts")
XCTAssertEqual(u.relativePath, "dir/a.ts")
XCTAssertEqual(u.path, "/work/dir/a.ts")
XCTAssertEqual(u.baseURL?.path, "/work")
}

do {
let u = URLs.replacingPathExtension(of: URL(fileURLWithPath: "../a.swift", relativeTo: work), to: "ts")
XCTAssertEqual(u.relativePath, "../a.ts")
XCTAssertEqual(u.path, "/a.ts")
XCTAssertEqual(u.baseURL?.path, "/work")
}

do {
let u = URLs.replacingPathExtension(of: URL(fileURLWithPath: "/work/a.swift"), to: "ts")
XCTAssertEqual(u.relativePath, "/work/a.ts")
XCTAssertEqual(u.path, "/work/a.ts")
XCTAssertNil(u.baseURL)
}

do {
let u = URLs.replacingPathExtension(of: URL(fileURLWithPath: "/work/dir/a.swift"), to: "ts")
XCTAssertEqual(u.relativePath, "/work/dir/a.ts")
XCTAssertEqual(u.path, "/work/dir/a.ts")
XCTAssertNil(u.baseURL)
}
}
}