Fix throwing function braces mistaken for closure

This commit is contained in:
Nick Lockwood
2020-08-08 18:43:08 +01:00
parent 909cbea001
commit 445abe35fc
4 changed files with 41 additions and 17 deletions
+16 -16
View File
@@ -391,25 +391,25 @@ private struct _FirestoreKeyedDecodingContainer<K: CodingKey>: KeyedDecodingCont
public func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type,
forKey key: Key) throws
-> KeyedDecodingContainer<NestedKey> {
decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }
decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }
guard let value = self.container[key.stringValue] else {
throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,
DecodingError.Context(codingPath: codingPath,
debugDescription: "Cannot get nested keyed container -- no value found for key \"\(key.stringValue)\""))
}
guard let dictionary = value as? [String: Any] else {
throw DecodingError
._typeMismatch(at: codingPath, expectation: [String: Any].self, reality: value)
}
let container = _FirestoreKeyedDecodingContainer<NestedKey>(referencing: decoder,
wrapping: dictionary)
return KeyedDecodingContainer(container)
guard let value = self.container[key.stringValue] else {
throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,
DecodingError.Context(codingPath: codingPath,
debugDescription: "Cannot get nested keyed container -- no value found for key \"\(key.stringValue)\""))
}
guard let dictionary = value as? [String: Any] else {
throw DecodingError
._typeMismatch(at: codingPath, expectation: [String: Any].self, reality: value)
}
let container = _FirestoreKeyedDecodingContainer<NestedKey>(referencing: decoder,
wrapping: dictionary)
return KeyedDecodingContainer(container)
}
public func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }
+1 -1
View File
@@ -392,7 +392,7 @@ extension Formatter {
}
}
return false
case "func", "subscript", "class", "struct", "protocol", "enum", "extension":
case "func", "subscript", "class", "struct", "protocol", "enum", "extension", "throws":
return false
default:
return true
+20
View File
@@ -157,11 +157,31 @@ class ParsingHelpersTests: XCTestCase {
XCTAssertFalse(formatter.isStartOfClosure(at: 8))
}
func testThrowingFunctionWithReturnTypeNotTreatedAsClosure() {
let formatter = Formatter(tokenize("func foo() throws -> Bar {}"))
XCTAssertFalse(formatter.isStartOfClosure(at: 12))
}
func testThrowingFunctionWithGenericReturnTypeNotTreatedAsClosure() {
let formatter = Formatter(tokenize("func foo<Baz>() throws -> Bar<Baz> {}"))
XCTAssertFalse(formatter.isStartOfClosure(at: 18))
}
func testFunctionAllmanBracesNotTreatedAsClosure() {
let formatter = Formatter(tokenize("func foo()\n{\n bar = 5\n}"))
XCTAssertFalse(formatter.isStartOfClosure(at: 6))
}
func testFunctionWithWhereClauseBracesNotTreatedAsClosure() {
let formatter = Formatter(tokenize("func foo<U, V>() where T == Result<U, V> {}"))
XCTAssertFalse(formatter.isStartOfClosure(at: 26))
}
func testThrowingFunctionWithWhereClauseBracesNotTreatedAsClosure() {
let formatter = Formatter(tokenize("func foo<U, V>() throws where T == Result<U, V> {}"))
XCTAssertFalse(formatter.isStartOfClosure(at: 28))
}
func testInitBracesNotTreatedAsClosure() {
let formatter = Formatter(tokenize("init() { foo = 5 }"))
XCTAssertFalse(formatter.isStartOfClosure(at: 4))
+4
View File
@@ -343,6 +343,7 @@ extension ParsingHelpersTests {
("testFunctionAllmanBracesNotTreatedAsClosure", testFunctionAllmanBracesNotTreatedAsClosure),
("testFunctionBracesNotTreatedAsClosure", testFunctionBracesNotTreatedAsClosure),
("testFunctionInGetterPosition", testFunctionInGetterPosition),
("testFunctionWithWhereClauseBracesNotTreatedAsClosure", testFunctionWithWhereClauseBracesNotTreatedAsClosure),
("testGenericFunctionNotTreatedAsClosure", testGenericFunctionNotTreatedAsClosure),
("testGenericSubscriptSetGet", testGenericSubscriptSetGet),
("testGetSet", testGetSet),
@@ -382,6 +383,9 @@ extension ParsingHelpersTests {
("testSubscriptAllmanBracesNotTreatedAsClosure", testSubscriptAllmanBracesNotTreatedAsClosure),
("testSubscriptBracesNotTreatedAsClosure", testSubscriptBracesNotTreatedAsClosure),
("testThrowingFunctionBracesNotTreatedAsClosure", testThrowingFunctionBracesNotTreatedAsClosure),
("testThrowingFunctionWithGenericReturnTypeNotTreatedAsClosure", testThrowingFunctionWithGenericReturnTypeNotTreatedAsClosure),
("testThrowingFunctionWithReturnTypeNotTreatedAsClosure", testThrowingFunctionWithReturnTypeNotTreatedAsClosure),
("testThrowingFunctionWithWhereClauseBracesNotTreatedAsClosure", testThrowingFunctionWithWhereClauseBracesNotTreatedAsClosure),
("testTupleReturningFunctionBracesNotTreatedAsClosure", testTupleReturningFunctionBracesNotTreatedAsClosure),
("testTypedVarAssignmentBracesTreatedAsClosure", testTypedVarAssignmentBracesTreatedAsClosure),
("testVarAssignmentBracesTreatedAsClosure", testVarAssignmentBracesTreatedAsClosure),