slackhq/compose-lints

Checks don't consider custom Preview annotations

inktomi opened this issue · 3 comments

We can simplify the creation of Preview variants by making an annotation like this:

@Preview(name = "Day",
    uiMode = Configuration.UI_MODE_NIGHT_NO or Configuration.UI_MODE_TYPE_NORMAL,
    backgroundColor = 0xFFFFFFFF,
    device = "id:pixel_2"
    )
@Preview(name = "Night",
    uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL,
    backgroundColor = 0xFF111111,
    device = "id:pixel_2"
    )
annotation class DayNightPreviews

Then in code we can just use @DayNightPreviews. The issue is that the lint checks do not see @DayNightPreviews as a preview, and so don't apply checks related to previews to these functions.

We do check this case, can you make a failing test case in this repo's tests?

I am working on getting the tests to run locally. It's having issues finding my ANDROID_HOME - I do see the tests related to exactly this case though. The only thing I can think of is that we define DayNightPreviews in a different gradle module than where it's being used.

Interestingly, in testing.. if I set it up all in one file, I can get the PreviewNamingDetector to trigger, but I don't get the PreviewPublicDetector to trigger:

@Preview(name = "Day",
    uiMode = Configuration.UI_MODE_NIGHT_NO or Configuration.UI_MODE_TYPE_NORMAL,
    backgroundColor = 0xFFFFFFFF,
    device = "id:pixel_2"
)
@Preview(name = "Night",
    uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL,
    backgroundColor = 0xFF111111,
    device = "id:pixel_2"
)
annotation class TestDayNightPreview
// This annotation class triggers `Preview annotations with 2 preview annotations should end with the Previews suffix.`

@TestDayNightPreview
@Composable
fun InitializeFeaturePreview() { // No error triggered on this public method. 

I'm seeing the lint errors in the editor in Android Studio Jellyfish Preview 13. I see you have a test using @CombinedPreviews in the PreviewPublicDetectorTest so it seems like this should work 🤔

I'll try to get tests running locally. I also verified we don't turn off these detectors, and have not baselined the lint issue.

I'm going to close this because we're seeing inconsistent behavior locally - it is probably environmental on our end.