Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reuse_identifier rule and test. #137

Merged

Conversation

r-plus
Copy link
Contributor

@r-plus r-plus commented Jan 20, 2020

Hi

I just try to implement reuse_identifierrule, how about this rule?
If acceptable, I'll continue to add xib support and docs for it.

Closes #136

// Currently only supported TableViewCell and CollectionViewCell (contains UICollectionReusableView)
if let tableView = view as? TableView {
return tableView.prototypeCells?.compactMap {
guard let customClass = $0.customClass, let reuseIdentifier = $0.reuseIdentifier else {
Copy link
Member

@phimage phimage Jan 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a IBReusable protocol implemented by the two type of cells
maybe it could help to factorise some code

So you can extract a function or append list of cells into one before using compactMap
Or we could also add a protocol on TableView and CollectionView to provide the reusableCells of type IBReusable, so only one if let parentResuable = view as? IBReusableParentView { is necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phimage Thanks for your feedback. I'll try that pattern to aggregate logic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, that view types are defined in IBDecodable. Should I modify that project first or add extension it from IBLinter project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, firstly I try to append list of cell pattern without modify IBDecodable project.

@r-plus r-plus changed the title WIP: add reuse_identifier rule and test. Add reuse_identifier rule and test. Jan 21, 2020
@r-plus
Copy link
Contributor Author

r-plus commented Jan 21, 2020

@kateinoigakukun @phimage okey, i finish up implementation for reuse_identifier rule and test.
Could you review this?

NOTE: I added some mess commits for this PR, so please squash merge to clean up git history when you merge or I'll rebase before or after review if you want.

looks like CI is not running, this is test result in my local.

`swift test` result
[332/332] Linking IBLinterPackageTests
Test Suite 'All tests' started at 2020-01-21 12:48:45.209
Test Suite 'IBLinterPackageTests.xctest' started at 2020-01-21 12:48:45.209
Test Suite 'AmbiguousViewRuleTests' started at 2020-01-21 12:48:45.209
Test Case '-[IBLinterKitTest.AmbiguousViewRuleTests testAmbiguous]' started.
Test Case '-[IBLinterKitTest.AmbiguousViewRuleTests testAmbiguous]' passed (0.173 seconds).
Test Suite 'AmbiguousViewRuleTests' passed at 2020-01-21 12:48:45.382.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.173 (0.173) seconds
Test Suite 'AssetsCatalogTest' started at 2020-01-21 12:48:45.382
Test Case '-[IBLinterKitTest.AssetsCatalogTest testImageAssets]' started.
Test Case '-[IBLinterKitTest.AssetsCatalogTest testImageAssets]' passed (0.004 seconds).
Test Suite 'AssetsCatalogTest' passed at 2020-01-21 12:48:45.387.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.004 (0.004) seconds
Test Suite 'ConfigTest' started at 2020-01-21 12:48:45.387
Test Case '-[IBLinterKitTest.ConfigTest testConfigFile]' started.
Test Case '-[IBLinterKitTest.ConfigTest testConfigFile]' passed (0.004 seconds).
Test Case '-[IBLinterKitTest.ConfigTest testNullableConfigFile]' started.
Test Case '-[IBLinterKitTest.ConfigTest testNullableConfigFile]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.ConfigTest testViewAsDeviceConfigFile]' started.
Test Case '-[IBLinterKitTest.ConfigTest testViewAsDeviceConfigFile]' passed (0.001 seconds).
Test Suite 'ConfigTest' passed at 2020-01-21 12:48:45.393.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.006 (0.006) seconds
Test Suite 'CustomClassNameRuleTests' started at 2020-01-21 12:48:45.393
Test Case '-[IBLinterKitTest.CustomClassNameRuleTests testCustomClassName]' started.
Test Case '-[IBLinterKitTest.CustomClassNameRuleTests testCustomClassName]' passed (0.004 seconds).
Test Suite 'CustomClassNameRuleTests' passed at 2020-01-21 12:48:45.397.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.004 (0.004) seconds
Test Suite 'CustomModuleRuleTests' started at 2020-01-21 12:48:45.397
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testCustomModule]' started.
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testCustomModule]' passed (0.051 seconds).
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testCustomModuleWithRelativePath]' started.
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testCustomModuleWithRelativePath]' passed (0.006 seconds).
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testSameNameClass]' started.
Test Case '-[IBLinterKitTest.CustomModuleRuleTests testSameNameClass]' passed (0.005 seconds).
Test Suite 'CustomModuleRuleTests' passed at 2020-01-21 12:48:45.459.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.062 (0.062) seconds
Test Suite 'DuplicateConstraintRuleTests' started at 2020-01-21 12:48:45.459
Test Case '-[IBLinterKitTest.DuplicateConstraintRuleTests testDuplicateConstraint]' started.
Test Case '-[IBLinterKitTest.DuplicateConstraintRuleTests testDuplicateConstraint]' passed (0.002 seconds).
Test Suite 'DuplicateConstraintRuleTests' passed at 2020-01-21 12:48:45.461.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.002 (0.003) seconds
Test Suite 'DuplicateIDRuleTests' started at 2020-01-21 12:48:45.461
Test Case '-[IBLinterKitTest.DuplicateIDRuleTests testDuplicateId]' started.
Test Case '-[IBLinterKitTest.DuplicateIDRuleTests testDuplicateId]' passed (0.009 seconds).
Test Suite 'DuplicateIDRuleTests' passed at 2020-01-21 12:48:45.470.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.009 (0.009) seconds
Test Suite 'GlobTests' started at 2020-01-21 12:48:45.470
Test Case '-[IBLinterKitTest.GlobTests testExpandGlobstar]' started.
Test Case '-[IBLinterKitTest.GlobTests testExpandGlobstar]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.GlobTests testMultiRecursivePaths]' started.
Test Case '-[IBLinterKitTest.GlobTests testMultiRecursivePaths]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.GlobTests testMultiRecursiveSubPaths]' started.
Test Case '-[IBLinterKitTest.GlobTests testMultiRecursiveSubPaths]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.GlobTests testRecursiveSubPaths]' started.
Test Case '-[IBLinterKitTest.GlobTests testRecursiveSubPaths]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.GlobTests testSimpleFile]' started.
Test Case '-[IBLinterKitTest.GlobTests testSimpleFile]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.GlobTests testSimplePath]' started.
Test Case '-[IBLinterKitTest.GlobTests testSimplePath]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.GlobTests testSubPaths]' started.
Test Case '-[IBLinterKitTest.GlobTests testSubPaths]' passed (0.000 seconds).
Test Suite 'GlobTests' passed at 2020-01-21 12:48:45.474.
	 Executed 7 tests, with 0 failures (0 unexpected) in 0.004 (0.004) seconds
Test Suite 'ImageResourcesRuleTests' started at 2020-01-21 12:48:45.474
Test Case '-[IBLinterKitTest.ImageResourcesRuleTests testImageResources]' started.
Test Case '-[IBLinterKitTest.ImageResourcesRuleTests testImageResources]' passed (0.036 seconds).
Test Suite 'ImageResourcesRuleTests' passed at 2020-01-21 12:48:45.510.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.036 (0.036) seconds
Test Suite 'LintCacheTests' started at 2020-01-21 12:48:45.510
Test Case '-[IBLinterKitTest.LintCacheTests testLoadDiskCache]' started.
Test Case '-[IBLinterKitTest.LintCacheTests testLoadDiskCache]' passed (0.003 seconds).
Test Suite 'LintCacheTests' passed at 2020-01-21 12:48:45.513.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.003 (0.003) seconds
Test Suite 'LintablePathsTests' started at 2020-01-21 12:48:45.513
Test Case '-[IBLinterKitTest.LintablePathsTests testExcluded]' started.
Test Case '-[IBLinterKitTest.LintablePathsTests testExcluded]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.LintablePathsTests testIncluded]' started.
Test Case '-[IBLinterKitTest.LintablePathsTests testIncluded]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.LintablePathsTests testIncludedAndExcluded]' started.
Test Case '-[IBLinterKitTest.LintablePathsTests testIncludedAndExcluded]' passed (0.000 seconds).
Test Suite 'LintablePathsTests' passed at 2020-01-21 12:48:45.515.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.001 (0.002) seconds
Test Suite 'RelativeToMarginRuleTests' started at 2020-01-21 12:48:45.515
Test Case '-[IBLinterKitTest.RelativeToMarginRuleTests testRelativeToMargin]' started.
Test Case '-[IBLinterKitTest.RelativeToMarginRuleTests testRelativeToMargin]' passed (0.002 seconds).
Test Suite 'RelativeToMarginRuleTests' passed at 2020-01-21 12:48:45.517.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.002 (0.002) seconds
Test Suite 'ReportersTest' started at 2020-01-21 12:48:45.517
Test Case '-[IBLinterKitTest.ReportersTest testJSONReporter]' started.
Test Case '-[IBLinterKitTest.ReportersTest testJSONReporter]' passed (0.000 seconds).
Test Suite 'ReportersTest' passed at 2020-01-21 12:48:45.518.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
Test Suite 'ReuseIdentifierRuleTests' started at 2020-01-21 12:48:45.518
Test Case '-[IBLinterKitTest.ReuseIdentifierRuleTests testReuseIdentifierStoryboard]' started.
Test Case '-[IBLinterKitTest.ReuseIdentifierRuleTests testReuseIdentifierStoryboard]' passed (0.005 seconds).
Test Case '-[IBLinterKitTest.ReuseIdentifierRuleTests testReuseIdentifierXib]' started.
Test Case '-[IBLinterKitTest.ReuseIdentifierRuleTests testReuseIdentifierXib]' passed (0.002 seconds).
Test Suite 'ReuseIdentifierRuleTests' passed at 2020-01-21 12:48:45.525.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.007 (0.007) seconds
Test Suite 'RuleTests' started at 2020-01-21 12:48:45.525
Test Case '-[IBLinterKitTest.RuleTests testDefaultEnabledRules]' started.
Test Case '-[IBLinterKitTest.RuleTests testDefaultEnabledRules]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.RuleTests testDisableDefaultEnabledRules]' started.
Test Case '-[IBLinterKitTest.RuleTests testDisableDefaultEnabledRules]' passed (0.000 seconds).
Test Case '-[IBLinterKitTest.RuleTests testDuplicatedEnabledRules]' started.
Test Case '-[IBLinterKitTest.RuleTests testDuplicatedEnabledRules]' passed (0.000 seconds).
Test Suite 'RuleTests' passed at 2020-01-21 12:48:45.526.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'UseBaseClassRuleTests' started at 2020-01-21 12:48:45.526
Test Case '-[IBLinterKitTest.UseBaseClassRuleTests testUseBaseClass]' started.
Test Case '-[IBLinterKitTest.UseBaseClassRuleTests testUseBaseClass]' passed (0.002 seconds).
Test Suite 'UseBaseClassRuleTests' passed at 2020-01-21 12:48:45.528.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.002 (0.002) seconds
Test Suite 'VaridatorTest' started at 2020-01-21 12:48:45.528
Test Case '-[IBLinterKitTest.VaridatorTest testValidateStoryboard]' started.
Test Case '-[IBLinterKitTest.VaridatorTest testValidateStoryboard]' passed (0.008 seconds).
Test Case '-[IBLinterKitTest.VaridatorTest testValidateXib]' started.
Test Case '-[IBLinterKitTest.VaridatorTest testValidateXib]' passed (0.003 seconds).
Test Suite 'VaridatorTest' passed at 2020-01-21 12:48:45.538.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.010 (0.011) seconds
Test Suite 'ViewAsDeviceRuleTests' started at 2020-01-21 12:48:45.538
Test Case '-[IBLinterKitTest.ViewAsDeviceRuleTests testViewAsDeviceWithConfig]' started.
Test Case '-[IBLinterKitTest.ViewAsDeviceRuleTests testViewAsDeviceWithConfig]' passed (0.001 seconds).
Test Case '-[IBLinterKitTest.ViewAsDeviceRuleTests testViewAsDeviceWithNoConfig]' started.
Test Case '-[IBLinterKitTest.ViewAsDeviceRuleTests testViewAsDeviceWithNoConfig]' passed (0.001 seconds).
Test Suite 'ViewAsDeviceRuleTests' passed at 2020-01-21 12:48:45.541.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.003 (0.003) seconds
Test Suite 'IBLinterPackageTests.xctest' passed at 2020-01-21 12:48:45.541.
	 Executed 35 tests, with 0 failures (0 unexpected) in 0.329 (0.332) seconds
Test Suite 'All tests' passed at 2020-01-21 12:48:45.541.
	 Executed 35 tests, with 0 failures (0 unexpected) in 0.329 (0.332) seconds

} else if let collectionView = view as? CollectionView, let cells = collectionView.cells {
reusableCells += cells
} else if let cell = view as? IBReusable & ViewProtocol {
reusableCells += [cell]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very special case :)
ok because cells are not in TableView or CollectionView subviews it seems

Copy link
Contributor Author

@r-plus r-plus Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I estimate this else if case is for XIB that defined TableViewCell or CollectionVIewCell without parent of TableView and CollectionView.

That type of xib is added as test resource.

Rules.md Outdated
| Default Rule | `false` |

Check that Reuse Identifier same as view class name.
Currently only supported TableViewCell and CollectionViewCell.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back-ticks on TableViewCell and CollectionViewCell

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

| Default Rule | `false` |

Check that Reuse Identifier same as view class name.
Currently only supported `TableViewCell` and `CollectionViewCell`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for confusion. This file is generated automatically based on Rule protocol information. Could you add second line description to ReuseIdentifierRule.description ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i'll do it 🙂

Copy link
Collaborator

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems legit except for nits point 👍


static let identifier = "reuse_identifier"
static let description = """
Check that ReuseIdentifier same as class name.\\
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing two space to line break for markdown is not good i think because Xcode have Automatically trim trailing whitespace option that trim space for line break.
So i choose back-tick style line break for markdown.

@kateinoigakukun
Copy link
Collaborator

Great thanks! LGTM

@kateinoigakukun kateinoigakukun merged commit a48d7b2 into IBDecodable:master Jan 22, 2020
@r-plus r-plus deleted the feature/reuseidentifier_rule branch January 22, 2020 05:37
@r-plus
Copy link
Contributor Author

r-plus commented Jan 22, 2020

thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How about reuseIdentifier rule?
3 participants