Skip to content

Commit 06a19a4

Browse files
authored
Merge pull request #1770 from whiteio/whiteio/insert-missing-equal
Suggest to add missing `equal` token to variable declarations
2 parents 208f111 + f504798 commit 06a19a4

File tree

6 files changed

+57
-101
lines changed

6 files changed

+57
-101
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,14 @@ extension Parser {
14051405
value: initExpr,
14061406
arena: self.arena
14071407
)
1408+
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.currentToken.flags.contains(.isAtStartOfLine) {
1409+
let missingEqual = RawTokenSyntax(missing: .equal, arena: self.arena)
1410+
let expr = self.parseExpression()
1411+
initializer = RawInitializerClauseSyntax(
1412+
equal: missingEqual,
1413+
value: expr,
1414+
arena: self.arena
1415+
)
14081416
} else {
14091417
initializer = nil
14101418
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,4 +2530,14 @@ final class DeclarationTests: XCTestCase {
25302530
"""
25312531
)
25322532
}
2533+
2534+
func testMissingEqualInVariableDeclaration() {
2535+
assertParse(
2536+
"let foo: [Int] 1️⃣[]",
2537+
diagnostics: [
2538+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '=' in variable", fixIts: ["insert '='"])
2539+
],
2540+
fixedSource: "let foo: [Int] = []"
2541+
)
2542+
}
25332543
}

Tests/SwiftParserTest/RegexLiteralTests.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,48 +1211,47 @@ final class RegexLiteralTests: XCTestCase {
12111211
func testPrefixOpSplitting2a() {
12121212
assertParse(
12131213
"""
1214-
let x1️⃣ .2️⃣/abc/
1214+
let x 1️⃣.2️⃣/abc/
12151215
""",
12161216
diagnostics: [
12171217
DiagnosticSpec(
12181218
locationMarker: "1️⃣",
1219-
message: "consecutive statements on a line must be separated by newline or ';'",
1220-
fixIts: ["insert newline", "insert ';'"]
1219+
message: "expected '=' in variable",
1220+
fixIts: ["insert '='"]
12211221
),
12221222
DiagnosticSpec(
12231223
locationMarker: "2️⃣",
12241224
message: "expected name in member access",
12251225
fixIts: ["insert name"]
12261226
),
12271227
],
1228-
applyFixIts: ["insert newline", "insert name"],
1228+
applyFixIts: ["insert '='", "insert name"],
12291229
fixedSource: """
1230-
let x
1231-
.<#identifier#>/abc/
1230+
let x = .<#identifier#>/abc/
12321231
"""
12331232
)
12341233
}
12351234

12361235
func testPrefixOpSplitting2b() {
12371236
assertParse(
12381237
"""
1239-
let x1️⃣ .2️⃣/abc/
1238+
let x 1️⃣.2️⃣/abc/
12401239
""",
12411240
diagnostics: [
12421241
DiagnosticSpec(
12431242
locationMarker: "1️⃣",
1244-
message: "consecutive statements on a line must be separated by newline or ';'",
1245-
fixIts: ["insert newline", "insert ';'"]
1243+
message: "expected '=' in variable",
1244+
fixIts: ["insert '='"]
12461245
),
12471246
DiagnosticSpec(
12481247
locationMarker: "2️⃣",
12491248
message: "expected name in member access",
12501249
fixIts: ["insert name"]
12511250
),
12521251
],
1253-
applyFixIts: ["insert ';'", "insert name"],
1252+
applyFixIts: ["insert '='", "insert name"],
12541253
fixedSource: """
1255-
let x; .<#identifier#>/abc/
1254+
let x = .<#identifier#>/abc/
12561255
"""
12571256
)
12581257
}

Tests/SwiftParserTest/translated/ConsecutiveStatementsTests.swift

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
7070
if i != j { i = j }
7171
// Errors
7272
i = j1️⃣ j = i
73-
let r : Int2️⃣ i = j
73+
let r : Int 2️⃣i = j
7474
let s : Int3️⃣ let t : Int
7575
_ = r; _ = s; _ = t
7676
}
@@ -83,16 +83,16 @@ final class ConsecutiveStatementsTests: XCTestCase {
8383
),
8484
DiagnosticSpec(
8585
locationMarker: "2️⃣",
86-
message: "consecutive statements on a line must be separated by newline or ';'",
87-
fixIts: ["insert newline", "insert ';'"]
86+
message: "expected '=' in variable",
87+
fixIts: ["insert '='"]
8888
),
8989
DiagnosticSpec(
9090
locationMarker: "3️⃣",
9191
message: "consecutive statements on a line must be separated by newline or ';'",
9292
fixIts: ["insert newline", "insert ';'"]
9393
),
9494
],
95-
applyFixIts: ["insert newline"],
95+
applyFixIts: ["insert newline", "insert '='"],
9696
fixedSource: """
9797
// Within a function
9898
func test(i: inout Int, j: inout Int) {
@@ -102,8 +102,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
102102
// Errors
103103
i = j
104104
j = i
105-
let r : Int
106-
i = j
105+
let r : Int = i = j
107106
let s : Int
108107
let t : Int
109108
_ = r; _ = s; _ = t
@@ -122,7 +121,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
122121
if i != j { i = j }
123122
// Errors
124123
i = j1️⃣ j = i
125-
let r : Int2️⃣ i = j
124+
let r : Int 2️⃣i = j
126125
let s : Int3️⃣ let t : Int
127126
_ = r; _ = s; _ = t
128127
}
@@ -135,8 +134,8 @@ final class ConsecutiveStatementsTests: XCTestCase {
135134
),
136135
DiagnosticSpec(
137136
locationMarker: "2️⃣",
138-
message: "consecutive statements on a line must be separated by newline or ';'",
139-
fixIts: ["insert newline", "insert ';'"]
137+
message: "expected '=' in variable",
138+
fixIts: ["insert '='"]
140139
),
141140
DiagnosticSpec(
142141
locationMarker: "3️⃣",
@@ -153,7 +152,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
153152
if i != j { i = j }
154153
// Errors
155154
i = j; j = i
156-
let r : Int; i = j
155+
let r : Int i = j
157156
let s : Int; let t : Int
158157
_ = r; _ = s; _ = t
159158
}
@@ -468,26 +467,18 @@ final class ConsecutiveStatementsTests: XCTestCase {
468467
assertParse(
469468
"""
470469
// At the top level
471-
var i, j : Int1️⃣ i = j2️⃣ j = i
470+
var i, j : Int 1️⃣i = j2️⃣ j = i
472471
""",
473472
diagnostics: [
474473
DiagnosticSpec(
475474
locationMarker: "1️⃣",
476-
message: "consecutive statements on a line must be separated by newline or ';'",
477-
fixIts: ["insert newline", "insert ';'"]
478-
),
479-
DiagnosticSpec(
480-
locationMarker: "2️⃣",
481-
message: "consecutive statements on a line must be separated by newline or ';'",
482-
fixIts: ["insert newline", "insert ';'"]
483-
),
475+
message: "expected '=' in variable",
476+
fixIts: ["insert '='"]
477+
)
484478
],
485-
applyFixIts: ["insert newline"],
486479
fixedSource: """
487480
// At the top level
488-
var i, j : Int
489-
i = j
490-
j = i
481+
var i, j : Int = i = j j = i
491482
"""
492483
)
493484
}
@@ -496,24 +487,18 @@ final class ConsecutiveStatementsTests: XCTestCase {
496487
assertParse(
497488
"""
498489
// At the top level
499-
var i, j : Int1️⃣ i = j2️⃣ j = i
490+
var i, j : Int 1️⃣i = j j = i
500491
""",
501492
diagnostics: [
502493
DiagnosticSpec(
503494
locationMarker: "1️⃣",
504-
message: "consecutive statements on a line must be separated by newline or ';'",
505-
fixIts: ["insert newline", "insert ';'"]
506-
),
507-
DiagnosticSpec(
508-
locationMarker: "2️⃣",
509-
message: "consecutive statements on a line must be separated by newline or ';'",
510-
fixIts: ["insert newline", "insert ';'"]
511-
),
495+
message: "expected '=' in variable",
496+
fixIts: ["insert '='"]
497+
)
512498
],
513-
applyFixIts: ["insert ';'"],
514499
fixedSource: """
515500
// At the top level
516-
var i, j : Int; i = j; j = i
501+
var i, j : Int = i = j j = i
517502
"""
518503
)
519504
}

Tests/SwiftParserTest/translated/ForwardSlashRegexTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,15 +964,16 @@ final class ForwardSlashRegexTests: XCTestCase {
964964
assertParse(
965965
"""
966966
do {
967-
let 1️⃣/x/
967+
let 1️⃣/x/
968968
}
969969
""",
970970
diagnostics: [
971-
DiagnosticSpec(message: "expected pattern in variable", fixIts: ["insert pattern"])
971+
DiagnosticSpec(message: "expected pattern in variable", fixIts: ["insert pattern"]),
972+
DiagnosticSpec(message: "expected '=' in variable", fixIts: ["insert '='"]),
972973
],
973974
fixedSource: """
974975
do {
975-
let <#pattern#>/x/
976+
let <#pattern#> = /x/
976977
}
977978
"""
978979
)

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ final class RecoveryTests: XCTestCase {
4343
)
4444
}
4545

46-
func testRecovery7a() {
46+
func testRecovery7() {
4747
assertParse(
4848
"""
4949
func useContainer() -> () {
50-
var a : Containerℹ️<not 1️⃣a2️⃣ type [skip 3️⃣this greater: >] >4️⃣, b : Int
50+
var a : Containerℹ️<not 1️⃣2️⃣a type [skip 3️⃣this greater: >] >4️⃣, b : Int
5151
b = 5 // no-warning
5252
a.exists()
5353
}
@@ -61,55 +61,8 @@ final class RecoveryTests: XCTestCase {
6161
),
6262
DiagnosticSpec(
6363
locationMarker: "2️⃣",
64-
message: "consecutive statements on a line must be separated by newline or ';'",
65-
fixIts: ["insert newline", "insert ';'"]
66-
),
67-
DiagnosticSpec(
68-
locationMarker: "3️⃣",
69-
message: "unexpected code 'this greater: >' in subscript"
70-
),
71-
DiagnosticSpec(
72-
locationMarker: "4️⃣",
73-
message: "expected expression after operator",
74-
fixIts: ["insert expression"]
75-
),
76-
DiagnosticSpec(
77-
locationMarker: "4️⃣",
78-
message: "unexpected code in function"
79-
),
80-
],
81-
applyFixIts: ["insert '>'", "insert newline", "insert expression"],
82-
fixedSource: """
83-
func useContainer() -> () {
84-
var a : Container<not>a
85-
type [skip this greater: >] > <#expression#>, b : Int
86-
b = 5 // no-warning
87-
a.exists()
88-
}
89-
"""
90-
)
91-
}
92-
93-
func testRecovery7b() {
94-
assertParse(
95-
"""
96-
func useContainer() -> () {
97-
var a : Containerℹ️<not 1️⃣a2️⃣ type [skip 3️⃣this greater: >] >4️⃣, b : Int
98-
b = 5 // no-warning
99-
a.exists()
100-
}
101-
""",
102-
diagnostics: [
103-
DiagnosticSpec(
104-
locationMarker: "1️⃣",
105-
message: "expected '>' to end generic argument clause",
106-
notes: [NoteSpec(message: "to match this opening '<'")],
107-
fixIts: ["insert '>'"]
108-
),
109-
DiagnosticSpec(
110-
locationMarker: "2️⃣",
111-
message: "consecutive statements on a line must be separated by newline or ';'",
112-
fixIts: ["insert newline", "insert ';'"]
64+
message: "expected '=' in variable",
65+
fixIts: ["insert '='"]
11366
),
11467
DiagnosticSpec(
11568
locationMarker: "3️⃣",
@@ -125,10 +78,10 @@ final class RecoveryTests: XCTestCase {
12578
message: "unexpected code in function"
12679
),
12780
],
128-
applyFixIts: ["insert '>'", "insert ';'", "insert expression"],
81+
applyFixIts: ["insert '>'", "insert expression"],
12982
fixedSource: """
13083
func useContainer() -> () {
131-
var a : Container<not>a; type [skip this greater: >] > <#expression#>, b : Int
84+
var a : Container<not> a type [skip this greater: >] > <#expression#>, b : Int
13285
b = 5 // no-warning
13386
a.exists()
13487
}

0 commit comments

Comments
 (0)