-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Compose: Add DaxTextField #7247
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
base: develop
Are you sure you want to change the base?
Changes from all commits
fce69e5
a789356
3fce1a3
2a32f29
d953884
92a662b
387b52e
32877ad
b3b471e
a01564c
9c4d4a6
a7aa6a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.duckduckgo.lint.ui | ||
|
|
||
| import com.android.tools.lint.client.api.UElementHandler | ||
| import com.android.tools.lint.detector.api.Category.Companion.CUSTOM_LINT_CHECKS | ||
| import com.android.tools.lint.detector.api.Detector | ||
| import com.android.tools.lint.detector.api.Implementation | ||
| import com.android.tools.lint.detector.api.Issue | ||
| import com.android.tools.lint.detector.api.JavaContext | ||
| import com.android.tools.lint.detector.api.Scope | ||
| import com.android.tools.lint.detector.api.Severity | ||
| import com.android.tools.lint.detector.api.SourceCodeScanner | ||
| import com.android.tools.lint.detector.api.TextFormat | ||
| import org.jetbrains.uast.UCallExpression | ||
| import org.jetbrains.uast.getParameterForArgument | ||
| import java.util.EnumSet | ||
|
|
||
| @Suppress("UnstableApiUsage") | ||
| class DaxSecureTextFieldTrailingIconDetector : Detector(), SourceCodeScanner { | ||
|
|
||
| override fun getApplicableUastTypes() = listOf(UCallExpression::class.java) | ||
|
|
||
| override fun createUastHandler(context: JavaContext): UElementHandler = DaxSecureTextFieldCallHandler(context) | ||
|
|
||
| internal class DaxSecureTextFieldCallHandler(private val context: JavaContext) : UElementHandler() { | ||
| override fun visitCallExpression(node: UCallExpression) { | ||
| val methodName = node.methodName | ||
|
|
||
| if (methodName == "DaxSecureTextField") { | ||
| checkTrailingIconParameter(node) | ||
| } | ||
| } | ||
|
|
||
| private fun checkTrailingIconParameter(node: UCallExpression) { | ||
| // Find the 'trailingIcon' parameter | ||
| val trailingIconArgument = node.valueArguments.find { arg -> | ||
| val parameterName = node.getParameterForArgument(arg)?.name | ||
| parameterName == "trailingIcon" | ||
| } ?: return // No 'trailingIcon' parameter provided which is fine | ||
|
|
||
| // Check if the trailingIcon uses an invalid composable | ||
| if (isInvalidComposable(trailingIconArgument)) { | ||
| reportInvalidComposableUsage(trailingIconArgument) | ||
| } | ||
| } | ||
|
|
||
| private fun isInvalidComposable(argument: org.jetbrains.uast.UExpression): Boolean { | ||
| val source = argument.sourcePsi?.text ?: return false | ||
|
|
||
| // Only DaxTextFieldTrailingIcon should be used in the trailingIcon parameter | ||
| // If the source doesn't contain it, then it's invalid | ||
| return !source.contains("DaxTextFieldTrailingIcon") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably get member list declared in the scope object class and check if the source contains one member of this list? This will allow us to avoid to edit this linter rule every time we need to edit the scope. |
||
| } | ||
|
|
||
| private fun reportInvalidComposableUsage(arg: org.jetbrains.uast.UExpression) { | ||
| context.report( | ||
| issue = INVALID_DAX_SECURE_TEXT_FIELD_TRAILING_ICON_USAGE, | ||
| location = context.getLocation(arg), | ||
| message = INVALID_DAX_SECURE_TEXT_FIELD_TRAILING_ICON_USAGE.getExplanation(TextFormat.RAW), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| val INVALID_DAX_SECURE_TEXT_FIELD_TRAILING_ICON_USAGE = Issue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we improve our implementation to be aware about all member in the scope class, we'll need to edit this issue |
||
| .create( | ||
| id = "InvalidDaxSecureTextFieldTrailingIconUsage", | ||
| briefDescription = "DaxSecureTextField trailingIcon parameter should use DaxTextFieldTrailingIcon", | ||
| explanation = """ | ||
| Use DaxTextFieldTrailingIcon instead of arbitrary composables for the trailingIcon parameter to maintain design system consistency. | ||
| Example: | ||
| DaxSecureTextField( | ||
| state = state, | ||
| isPasswordVisible = isPasswordVisible, | ||
| onShowHidePasswordIconClick = { /* toggle visibility */ }, | ||
| trailingIcon = { | ||
| DaxTextFieldTrailingIcon( | ||
| painter = painterResource(R.drawable.ic_copy_24), | ||
| contentDescription = stringResource(R.string.icon_description) | ||
| ) | ||
| } | ||
| ) | ||
| This ensures consistent styling, spacing, and behavior across all text field icons in the app. | ||
| """.trimIndent(), | ||
| moreInfo = "", | ||
| category = CUSTOM_LINT_CHECKS, | ||
| priority = 6, | ||
| severity = Severity.WARNING, | ||
| androidSpecific = true, | ||
| implementation = Implementation( | ||
| DaxSecureTextFieldTrailingIconDetector::class.java, | ||
| EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.duckduckgo.lint.ui | ||
|
|
||
| import com.android.tools.lint.client.api.UElementHandler | ||
| import com.android.tools.lint.detector.api.Category.Companion.CUSTOM_LINT_CHECKS | ||
| import com.android.tools.lint.detector.api.Detector | ||
| import com.android.tools.lint.detector.api.Implementation | ||
| import com.android.tools.lint.detector.api.Issue | ||
| import com.android.tools.lint.detector.api.JavaContext | ||
| import com.android.tools.lint.detector.api.Scope | ||
| import com.android.tools.lint.detector.api.Severity | ||
| import com.android.tools.lint.detector.api.SourceCodeScanner | ||
| import com.android.tools.lint.detector.api.TextFormat | ||
| import org.jetbrains.uast.UCallExpression | ||
| import org.jetbrains.uast.getParameterForArgument | ||
| import java.util.EnumSet | ||
|
|
||
| @Suppress("UnstableApiUsage") | ||
| class DaxTextFieldTrailingIconDetector : Detector(), SourceCodeScanner { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
|
||
| override fun getApplicableUastTypes() = listOf(UCallExpression::class.java) | ||
|
|
||
| override fun createUastHandler(context: JavaContext): UElementHandler = DaxTextFieldCallHandler(context) | ||
|
|
||
| internal class DaxTextFieldCallHandler(private val context: JavaContext) : UElementHandler() { | ||
| override fun visitCallExpression(node: UCallExpression) { | ||
| val methodName = node.methodName | ||
|
|
||
| if (methodName == "DaxTextField") { | ||
| checkTrailingIconParameter(node) | ||
| } | ||
landomen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private fun checkTrailingIconParameter(node: UCallExpression) { | ||
| // Find the 'trailingIcon' parameter | ||
| val trailingIconArgument = node.valueArguments.find { arg -> | ||
| val parameterName = node.getParameterForArgument(arg)?.name | ||
| parameterName == "trailingIcon" | ||
| } ?: return // No 'trailingIcon' parameter provided which is fine | ||
|
|
||
| // Check if the trailingIcon uses an invalid composable | ||
| if (isInvalidComposable(trailingIconArgument)) { | ||
| reportInvalidComposableUsage(trailingIconArgument) | ||
| } | ||
| } | ||
|
|
||
| private fun isInvalidComposable(argument: org.jetbrains.uast.UExpression): Boolean { | ||
| val source = argument.sourcePsi?.text ?: return false | ||
|
|
||
| // Only DaxTextFieldTrailingIcon should be used in the trailingIcon parameter | ||
| // If the source doesn't contain it, then it's invalid | ||
| return !source.contains("DaxTextFieldTrailingIcon") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably get member list declared in the scope object class and check if the source contains one member of this list? This will allow us to avoid to edit this linter rule every time we need to edit the scope. |
||
| } | ||
|
|
||
| private fun reportInvalidComposableUsage(arg: org.jetbrains.uast.UExpression) { | ||
| context.report( | ||
| issue = INVALID_DAX_TEXT_FIELD_TRAILING_ICON_USAGE, | ||
| location = context.getLocation(arg), | ||
| message = INVALID_DAX_TEXT_FIELD_TRAILING_ICON_USAGE.getExplanation(TextFormat.RAW), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| val INVALID_DAX_TEXT_FIELD_TRAILING_ICON_USAGE = Issue | ||
| .create( | ||
| id = "InvalidDaxTextFieldTrailingIconUsage", | ||
| briefDescription = "DaxTextField trailingIcon parameter should use DaxTextFieldTrailingIcon", | ||
| explanation = """ | ||
| Use DaxTextFieldTrailingIcon instead of arbitrary composables for the trailingIcon parameter to maintain design system consistency. | ||
| Example: | ||
| DaxTextField( | ||
| state = state, | ||
| trailingIcon = { | ||
| DaxTextFieldTrailingIcon( | ||
| painter = painterResource(R.drawable.ic_copy_24), | ||
| contentDescription = stringResource(R.string.icon_description) | ||
| ) | ||
| } | ||
| ) | ||
| This ensures consistent styling, spacing, and behavior across all text field icons in the app. | ||
| """.trimIndent(), | ||
| moreInfo = "", | ||
| category = CUSTOM_LINT_CHECKS, | ||
| priority = 6, | ||
| severity = Severity.WARNING, | ||
| androidSpecific = true, | ||
| implementation = Implementation( | ||
| DaxTextFieldTrailingIconDetector::class.java, | ||
| EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️