@@ -64,42 +64,102 @@ public extension ParserFixIt {
64
64
65
65
// MARK: - Errors (please sort alphabetically)
66
66
67
- /// Please order the cases in this enum alphabetically by case name.
68
- public enum StaticParserError : String , DiagnosticMessage {
69
- case allStatmentsInSwitchMustBeCoveredByCase = " all statements inside a switch must be covered by a 'case' or 'default' label "
70
- case caseOutsideOfSwitchOrEnum = " 'case' can only appear inside a 'switch' statement or 'enum' declaration "
71
- case classConstraintCanOnlyBeUsedInProtocol = " 'class' constraint can only appear on protocol declarations "
72
- case consecutiveDeclarationsOnSameLine = " consecutive declarations on a line must be separated by ';' "
73
- case consecutiveStatementsOnSameLine = " consecutive statements on a line must be separated by ';' "
74
- case cStyleForLoop = " C-style for statement has been removed in Swift 3 "
75
- case defaultCannotBeUsedWithWhere = " 'default' cannot be used with a 'where' guard expression "
76
- case defaultOutsideOfSwitch = " 'default' label can only appear inside a 'switch' statement "
77
- case deinitCannotHaveName = " deinitializers cannot have a name "
78
- case deinitCannotHaveParameters = " deinitializers cannot have parameters "
79
- case editorPlaceholderInSourceFile = " editor placeholder in source file "
80
- case expectedExpressionAfterTry = " expected expression after 'try' "
81
- case invalidFlagAfterPrecedenceGroupAssignment = " expected 'true' or 'false' after 'assignment' "
82
- case missingColonAndExprInTernaryExpr = " expected ':' and expression after '? ...' in ternary expression "
83
- case missingColonInTernaryExpr = " expected ':' after '? ...' in ternary expression "
84
- case operatorShouldBeDeclaredWithoutBody = " operator should not be declared with body "
85
- case standaloneSemicolonStatement = " standalone ';' statements are not allowed "
86
- case subscriptsCannotHaveNames = " subscripts cannot have a name "
87
- case throwsInReturnPosition = " 'throws' may only occur before '->' "
88
- case tryMustBePlacedOnReturnedExpr = " 'try' must be placed on the returned expression "
89
- case tryMustBePlacedOnThrownExpr = " 'try' must be placed on the thrown expression "
90
- case tryOnInitialValueExpression = " 'try' must be placed on the initial value expression "
91
- case unexpectedSemicolon = " unexpected ';' separator "
92
- case associatedTypeCannotUsePack = " associated types cannot be variadic "
93
-
94
- public var message : String { self . rawValue }
67
+ /// A parser error with a static message.
68
+ public struct StaticParserError : DiagnosticMessage {
69
+ public let message : String
70
+ private let messageID : String
71
+
72
+ /// This should only be called within a static var on DiagnosticMessage, such
73
+ /// as the examples below. This allows us to pick up the messageID from the
74
+ /// var name.
75
+ fileprivate init ( _ message: String , messageID: String = #function) {
76
+ self . message = message
77
+ self . messageID = messageID
78
+ }
95
79
96
80
public var diagnosticID : MessageID {
97
- MessageID ( domain: diagnosticDomain, id: " \( type ( of: self ) ) . \( self ) " )
81
+ MessageID ( domain: diagnosticDomain, id: " \( type ( of: self ) ) . \( messageID ) " )
98
82
}
99
83
100
84
public var severity : DiagnosticSeverity { . error }
101
85
}
102
86
87
+ extension DiagnosticMessage where Self == StaticParserError {
88
+ /// Please order the diagnostics alphabetically by property name.
89
+ public static var allStatmentsInSwitchMustBeCoveredByCase : Self {
90
+ . init( " all statements inside a switch must be covered by a 'case' or 'default' label " )
91
+ }
92
+ public static var associatedTypeCannotUsePack : Self {
93
+ . init( " associated types cannot be variadic " )
94
+ }
95
+ public static var caseOutsideOfSwitchOrEnum : Self {
96
+ . init( " 'case' can only appear inside a 'switch' statement or 'enum' declaration " )
97
+ }
98
+ public static var classConstraintCanOnlyBeUsedInProtocol : Self {
99
+ . init( " 'class' constraint can only appear on protocol declarations " )
100
+ }
101
+ public static var consecutiveDeclarationsOnSameLine : Self {
102
+ . init( " consecutive declarations on a line must be separated by ';' " )
103
+ }
104
+ public static var consecutiveStatementsOnSameLine : Self {
105
+ . init( " consecutive statements on a line must be separated by ';' " )
106
+ }
107
+ public static var cStyleForLoop : Self {
108
+ . init( " C-style for statement has been removed in Swift 3 " )
109
+ }
110
+ public static var defaultCannotBeUsedWithWhere : Self {
111
+ . init( " 'default' cannot be used with a 'where' guard expression " )
112
+ }
113
+ public static var defaultOutsideOfSwitch : Self {
114
+ . init( " 'default' label can only appear inside a 'switch' statement " )
115
+ }
116
+ public static var deinitCannotHaveName : Self {
117
+ . init( " deinitializers cannot have a name " )
118
+ }
119
+ public static var deinitCannotHaveParameters : Self {
120
+ . init( " deinitializers cannot have parameters " )
121
+ }
122
+ public static var editorPlaceholderInSourceFile : Self {
123
+ . init( " editor placeholder in source file " )
124
+ }
125
+ public static var expectedExpressionAfterTry : Self {
126
+ . init( " expected expression after 'try' " )
127
+ }
128
+ public static var invalidFlagAfterPrecedenceGroupAssignment : Self {
129
+ . init( " expected 'true' or 'false' after 'assignment' " )
130
+ }
131
+ public static var missingColonAndExprInTernaryExpr : Self {
132
+ . init( " expected ':' and expression after '? ...' in ternary expression " )
133
+ }
134
+ public static var missingColonInTernaryExpr : Self {
135
+ . init( " expected ':' after '? ...' in ternary expression " )
136
+ }
137
+ public static var operatorShouldBeDeclaredWithoutBody : Self {
138
+ . init( " operator should not be declared with body " )
139
+ }
140
+ public static var standaloneSemicolonStatement : Self {
141
+ . init( " standalone ';' statements are not allowed " )
142
+ }
143
+ public static var subscriptsCannotHaveNames : Self {
144
+ . init( " subscripts cannot have a name " )
145
+ }
146
+ public static var throwsInReturnPosition : Self {
147
+ . init( " 'throws' may only occur before '->' " )
148
+ }
149
+ public static var tryMustBePlacedOnReturnedExpr : Self {
150
+ . init( " 'try' must be placed on the returned expression " )
151
+ }
152
+ public static var tryMustBePlacedOnThrownExpr : Self {
153
+ . init( " 'try' must be placed on the thrown expression " )
154
+ }
155
+ public static var tryOnInitialValueExpression : Self {
156
+ . init( " 'try' must be placed on the initial value expression " )
157
+ }
158
+ public static var unexpectedSemicolon : Self {
159
+ . init( " unexpected ';' separator " )
160
+ }
161
+ }
162
+
103
163
// MARK: - Diagnostics (please sort alphabetically)
104
164
105
165
public struct EffectsSpecifierAfterArrow : ParserError {
@@ -221,18 +281,43 @@ public struct UnknownDirectiveError: ParserError {
221
281
222
282
// MARK: - Fix-Its (please sort alphabetically)
223
283
224
- public enum StaticParserFixIt : String , FixItMessage {
225
- case insertSemicolon = " insert ';' "
226
- case insertAttributeArguments = " insert attribute argument "
227
- case joinIdentifiers = " join the identifiers together "
228
- case joinIdentifiersWithCamelCase = " join the identifiers together with camel-case "
229
- case removeOperatorBody = " remove operator body "
230
- case wrapKeywordInBackticks = " if this name is unavoidable, use backticks to escape it "
231
-
232
- public var message : String { self . rawValue }
284
+ /// A parser fix-it with a static message.
285
+ public struct StaticParserFixIt : FixItMessage {
286
+ public let message : String
287
+ private let messageID : String
288
+
289
+ /// This should only be called within a static var on FixItMessage, such
290
+ /// as the examples below. This allows us to pick up the messageID from the
291
+ /// var name.
292
+ fileprivate init ( _ message: String , messageID: String = #function) {
293
+ self . message = message
294
+ self . messageID = messageID
295
+ }
233
296
234
297
public var fixItID : MessageID {
235
- MessageID ( domain: diagnosticDomain, id: " \( type ( of: self ) ) . \( self ) " )
298
+ MessageID ( domain: diagnosticDomain, id: " \( type ( of: self ) ) . \( messageID) " )
299
+ }
300
+ }
301
+
302
+ extension FixItMessage where Self == StaticParserFixIt {
303
+ /// Please order alphabetically by property name.
304
+ public static var insertSemicolon : Self {
305
+ . init( " insert ';' " )
306
+ }
307
+ public static var insertAttributeArguments : Self {
308
+ . init( " insert attribute argument " )
309
+ }
310
+ public static var joinIdentifiers : Self {
311
+ . init( " join the identifiers together " )
312
+ }
313
+ public static var joinIdentifiersWithCamelCase : Self {
314
+ . init( " join the identifiers together with camel-case " )
315
+ }
316
+ public static var removeOperatorBody : Self {
317
+ . init( " remove operator body " )
318
+ }
319
+ public static var wrapKeywordInBackticks : Self {
320
+ . init( " if this name is unavoidable, use backticks to escape it " )
236
321
}
237
322
}
238
323
0 commit comments