Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit443cbd7

Browse files
authored
Add new optionignore_typealiases_and_associatedtypes tonesting rule (realm#5433)
1 parent2be8846 commit443cbd7

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
####Enhancements
1212

13+
* Add new option`ignore_typealiases_and_associatedtypes` to
14+
`nesting` rule. It excludes`typealias` and`associatedtype`
15+
declarations from the analysis.
16+
[marunomi](https://github.com/marunomi)
17+
[#3183](https://github.com/realm/SwiftLint/issues/3183)
18+
1319
* Prevent from compiling`SwiftLint` target when only using`SwiftLintPlugin` on macOS.
1420
[Julien Baillon](https://github.com/julien-baillon)
1521
[#5372](https://github.com/realm/SwiftLint/issues/5372)

‎Source/SwiftLintBuiltInRules/Rules/Metrics/NestingRule.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ struct NestingRule: Rule {
4545
}
4646
switch structureKind{
4747
caselet.declaration(declarationKind):
48+
if configuration.ignoreTypealiasesAndAssociatedtypes,
49+
declarationKind==.associatedtype || declarationKind==.typealias{
50+
return args.violations
51+
}
4852
returnvalidate(file: file, structureKind: structureKind,
4953
declarationKind: declarationKind, dictionary: dictionary, args: args)
5054
case.expression,.statement:

‎Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NestingConfiguration.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct NestingConfiguration: RuleConfiguration {
1313
private(set)varcheckNestingInClosuresAndStatements=true
1414
@ConfigurationElement(key:"always_allow_one_type_in_functions")
1515
private(set)varalwaysAllowOneTypeInFunctions=false
16+
@ConfigurationElement(key:"ignore_typealiases_and_associatedtypes")
17+
private(set)varignoreTypealiasesAndAssociatedtypes=false
1618

1719
func severity(with config:Severity, for level:Int)->ViolationSeverity?{
1820
iflet error= config.error, level> error{

‎Tests/SwiftLintFrameworkTests/NestingRuleTests.swift‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@testableimport SwiftLintBuiltInRules
22

3+
// swiftlint:disable file_length
4+
35
classNestingRuleTests:SwiftLintTestCase{
46
// swiftlint:disable:next function_body_length
57
func testNestingWithAlwaysAllowOneTypeInFunctions(){
@@ -368,4 +370,49 @@ class NestingRuleTests: SwiftLintTestCase {
368370

369371
verifyRule(description, ruleConfiguration:["check_nesting_in_closures_and_statements":false])
370372
}
373+
374+
func testNestingWithoutTypealiasAndAssociatedtype(){
375+
varnonTriggeringExamples=NestingRule.description.nonTriggeringExamples
376+
nonTriggeringExamples.append(contentsOf:["class","struct","enum"].flatMap{ type->[Example]in
377+
[
378+
.init("""
379+
\(type) Example_0 {
380+
\(type) Example_1 {
381+
typealias Example_2_Type = Example_2.Type
382+
}
383+
\(type) Example_2 {}
384+
}
385+
"""),
386+
.init("""
387+
protocol Example_Protcol {
388+
associatedtype AssociatedType
389+
}
390+
391+
\(type) Example_1 {
392+
\(type) Example_2: Example_Protcol {
393+
typealias AssociatedType = Int
394+
}
395+
}
396+
"""),
397+
.init("""
398+
protocol Example_Protcol {
399+
associatedtype AssociatedType
400+
}
401+
402+
\(type) Example_1 {
403+
\(type) Example_2: SomeProtcol {
404+
typealias Example_2_Type = Example_2.Type
405+
}
406+
\(type) Example_3: Example_Protcol {
407+
typealias AssociatedType = Int
408+
}
409+
}
410+
""")
411+
]
412+
})
413+
414+
letdescription=NestingRule.description.with(nonTriggeringExamples: nonTriggeringExamples)
415+
416+
verifyRule(description, ruleConfiguration:["ignore_typealiases_and_associatedtypes":true])
417+
}
371418
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp