/
NonCommentingSourceStatements.swift
288 lines (255 loc) · 8.39 KB
/
NonCommentingSourceStatements.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2017 Ryuichi Laboratories and the Yanagiba project contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
import AST
public class NonCommentingSourceStatements {
public func calculate(for topLevelDecl: TopLevelDeclaration) -> Int {
return ncss(topLevelDecl.statements)
}
public func calculate(for codeBlock: CodeBlock) -> Int {
return ncss(codeBlock)
}
public func calculate(for stmt: Statement) -> Int {
return ncss(stmt)
}
private func ncss(_ codeBlock: CodeBlock) -> Int {
return ncss(codeBlock.statements)
}
private func ncss(_ stmts: [Statement]) -> Int {
return stmts.reduce(0) { $0 + ncss($1) }
}
private func ncss(_ stmt: Statement) -> Int { // swift-lint:suppress(high_cyclomatic_complexity,high_ncss)
switch stmt {
case let deferStmt as DeferStatement:
return 1 + deferStmt.codeBlock.ncssCount
case let doStmt as DoStatement:
return ncss(doStmt)
case let forStmt as ForInStatement:
return 1 + forStmt.codeBlock.ncssCount
case let guardStmt as GuardStatement:
return 1 + guardStmt.codeBlock.ncssCount
case let ifStmt as IfStatement:
return ncss(ifStmt)
case let labeledStmt as LabeledStatement:
return ncss(labeledStmt.statement)
case let repeatWhileStmt as RepeatWhileStatement:
return 2 + repeatWhileStmt.codeBlock.ncssCount
case let switchStmt as SwitchStatement:
return ncss(switchStmt)
case let whileStmt as WhileStatement:
return 1 + whileStmt.codeBlock.ncssCount
case let returnStmt as ReturnStatement:
return returnStmt.expression.map({ ncss($0) }) ?? 1
case let throwStmt as ThrowStatement:
return ncss(throwStmt.expression)
case let classDecl as ClassDeclaration:
return ncss(classDecl)
case let initDecl as InitializerDeclaration:
return 1 + initDecl.body.ncssCount
case let deinitDecl as DeinitializerDeclaration:
return 1 + deinitDecl.body.ncssCount
case let enumDecl as EnumDeclaration:
return ncss(enumDecl)
case let extensionDecl as ExtensionDeclaration:
return ncss(extensionDecl)
case let funcDecl as FunctionDeclaration:
return ncss(funcDecl)
case let precedenceGroupDecl as PrecedenceGroupDeclaration:
return 1 + precedenceGroupDecl.attributes.count
case let protocolDecl as ProtocolDeclaration:
return ncss(protocolDecl)
case let structDecl as StructDeclaration:
return ncss(structDecl)
case let subscriptDecl as SubscriptDeclaration:
return ncss(subscriptDecl)
case let constantDecl as ConstantDeclaration:
return ncss(constantDecl)
case let variableDecl as VariableDeclaration:
return ncss(variableDecl)
case let expr as Expression:
return ncss(expr)
default:
return 1
}
}
private func ncss(_ doStmt: DoStatement) -> Int {
return 1 +
doStmt.codeBlock.ncssCount +
doStmt.catchClauses.reduce(0) { $0 + 1 + $1.codeBlock.ncssCount }
}
private func ncss(_ ifStmt: IfStatement) -> Int {
return 1 +
ifStmt.codeBlock.ncssCount +
(
ifStmt.elseClause.map({
switch $0 {
case .else(let codeBlock):
return 1 + codeBlock.ncssCount
case .elseif(let ifStmt):
return ncss(ifStmt)
}
}) ?? 0
)
}
private func ncss(_ switchStmt: SwitchStatement) -> Int {
return 1 +
switchStmt.cases.reduce(0) {
switch $1 {
case .case(_, let stmts):
return 1 + ncss(stmts) + $0
case .default(let stmts):
return 1 + ncss(stmts) + $0
}
}
}
private func ncss(_ classDecl: ClassDeclaration) -> Int {
return 1 + classDecl.members.flatMap { member -> Int? in
switch member {
case .declaration(let decl):
return decl.ncssCount
default:
return nil
}
}.reduce(0, +)
}
private func ncss(_ enumDecl: EnumDeclaration) -> Int {
return 1 + enumDecl.members.flatMap { member -> Int? in
switch member {
case .declaration(let decl):
return decl.ncssCount
case .union, .rawValue:
return 1
default:
return nil
}
}.reduce(0, +)
}
private func ncss(_ extDecl: ExtensionDeclaration) -> Int {
return 1 + extDecl.members.flatMap { member -> Int? in
switch member {
case .declaration(let decl):
return decl.ncssCount
default:
return nil
}
}.reduce(0, +)
}
private func ncss(_ funcDecl: FunctionDeclaration) -> Int {
guard let body = funcDecl.body else {
return 1
}
return 1 + body.ncssCount
}
private func ncss(_ protocolDecl: ProtocolDeclaration) -> Int {
return 1 + protocolDecl.members.filter {
switch $0 {
case .compilerControl:
return false
default:
return true
}
}.count
}
private func ncss(_ structDecl: StructDeclaration) -> Int {
return 1 + structDecl.members.flatMap { member -> Int? in
switch member {
case .declaration(let decl):
return decl.ncssCount
default:
return nil
}
}.reduce(0, +)
}
private func ncss(_ subscriptDecl: SubscriptDeclaration) -> Int {
let ncssBody: Int
switch subscriptDecl.body {
case .codeBlock(let codeBlock):
ncssBody = codeBlock.ncssCount
case .getterSetterBlock(let block):
ncssBody = ncss(block)
case .getterSetterKeywordBlock(let block):
ncssBody = ncss(block)
}
return 1 + ncssBody
}
private func ncss(_ constDecl: ConstantDeclaration) -> Int {
return 1 + ncss(constDecl.initializerList)
}
private func ncss(_ varDecl: VariableDeclaration) -> Int {
switch varDecl.body {
case .initializerList(let list):
return 1 + ncss(list)
case .codeBlock(_, _, let block):
return 1 + block.ncssCount
case .getterSetterBlock(_, _, let block):
return 1 + ncss(block)
case .getterSetterKeywordBlock(_, _, let block):
return 1 + ncss(block)
case let .willSetDidSetBlock(_, _, expr, block):
let ncssExpr = expr.map({ ncss($0) - 1 }) ?? 0
return 1 + ncssExpr + ncss(block)
}
}
private func ncss(_ initList: [PatternInitializer]) -> Int {
return initList.flatMap({ $0.initializerExpression })
.map({ ncss($0) - 1 })
.reduce(0, +)
}
private func ncss(_ block: GetterSetterBlock) -> Int {
let ncssGetter = block.getter.codeBlock.ncssCount + 1
let ncssSetter = block.setter.map({ $0.codeBlock.ncssCount + 1 }) ?? 0
return ncssGetter + ncssSetter
}
private func ncss(_ block: GetterSetterKeywordBlock) -> Int {
return 1 + (block.setter == nil ? 0 : 1)
}
private func ncss(_ block: WillSetDidSetBlock) -> Int {
let ncssWillSet = block.willSetClause.map({ $0.codeBlock.ncssCount + 1 }) ?? 0
let ncssDidSet = block.didSetClause.map({ $0.codeBlock.ncssCount + 1 }) ?? 0
return ncssWillSet + ncssDidSet
}
private func ncss(_ expr: Expression) -> Int { // swift-lint:suppress(nested_code_block_depth)
class NPathExpressionVisitor : ASTVisitor {
var _count = 0
var _isTrailingClosure = false
func cal(_ expr: Expression) -> Int {
_count = 0
_isTrailingClosure = false
do {
_ = try traverse(expr)
if _count == 0 {
return 1
}
return _count
} catch {
return 0
}
}
func visit(_ funcCallExpr: FunctionCallExpression) throws -> Bool {
if funcCallExpr.trailingClosure != nil {
_isTrailingClosure = true
}
return true
}
func visit(_ closureExpr: ClosureExpression) throws -> Bool {
if closureExpr.signature != nil || _isTrailingClosure {
_count += 1
}
_count += CodeBlock(statements: closureExpr.statements ?? []).ncssCount
_isTrailingClosure = false
return true
}
}
return NPathExpressionVisitor().cal(expr)
}
}