Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/actions/fetch-tool/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Fetch a pinned tool
description: >-
Restores the tool's release file from the cache, or downloads it; checks its SHA-256 either way
(a cached copy that fails is downloaded again); installs it on PATH for the job's later steps; and
saves a new download to the cache at once, so a later failing step does not lose it. The caller
names the release URL and its SHA-256, so the pinned version stays visible in the workflow. Needs
the checkout and Node from .nvmrc (scripts/fetch-tool.ts runs with Node's TypeScript support).

inputs:
name:
description: The file name the tool is installed under (a command name, or a jar's name).
required: true
url:
description: The release asset to download (a zip, a tar archive in any compression tar detects, or the tool itself).
required: true
sha256:
description: The SHA-256 of the downloaded file, from the official release.
required: true
archive-path:
description: The tool's path inside the archive; empty when the download is the tool itself.
required: false
default: ''

outputs:
path:
description: The installed file, for tools run by path (a jar).
value: ${{ steps.fetch.outputs.path }}
downloaded:
description: "'true' when the file was downloaded (no cache entry, or a corrupted one), else 'false'."
value: ${{ steps.fetch.outputs.downloaded }}

runs:
using: composite
steps:
# The cache entry is named after the URL and the checksum together. A version bump that changes
# the URL but not the pinned checksum misses the cache, so the new file is downloaded and fails
# its check, instead of the old file being restored and passing it.
- name: Cache entry for ${{ inputs.name }}
id: entry
shell: bash
env:
NAME: ${{ inputs.name }}
URL: ${{ inputs.url }}
SHA256: ${{ inputs.sha256 }}
run: >-
node -e 'const id = require("node:crypto").createHash("sha256")
.update(process.env.URL + "\n" + process.env.SHA256).digest("hex").slice(0, 32);
console.log("id=" + process.env.NAME + "-" + id)' >> "$GITHUB_OUTPUT"

# An entry only ever holds a file that passed the check (it is saved below only after it).
- name: Restore cached ${{ inputs.name }}
id: restore
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/ci-tools-cache/${{ steps.entry.outputs.id }}
key: ci-tools-${{ runner.os }}-${{ steps.entry.outputs.id }}

- name: Download, verify and install ${{ inputs.name }}
id: fetch
shell: bash
env:
NAME: ${{ inputs.name }}
URL: ${{ inputs.url }}
SHA256: ${{ inputs.sha256 }}
ARCHIVE_PATH: ${{ inputs.archive-path }}
CACHE_FILE: ${{ runner.temp }}/ci-tools-cache/${{ steps.entry.outputs.id }}
CACHE_KEY: ci-tools-${{ runner.os }}-${{ steps.entry.outputs.id }}
run: node scripts/fetch-tool.ts

# A cache entry cannot be overwritten, and one that is restored on every run never expires.
# After a corrupted hit (downloaded again above, with a warning naming the key) every run
# downloads until someone deletes the entry with `gh cache delete <key>`.
- name: Save ${{ inputs.name }} to the cache
if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/ci-tools-cache/${{ steps.entry.outputs.id }}
key: ci-tools-${{ runner.os }}-${{ steps.entry.outputs.id }}
230 changes: 105 additions & 125 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,43 +69,44 @@ yarn lint --fix

### Static analysis

CI runs these checks and fails on any new finding, warnings included (`yarn lint` runs with
CI runs these checks and fails on any finding, warnings included (`yarn lint` runs with
`--max-warnings 0`, Android Lint with `-PremoteComposeStrictLint=true`, which turns on
`warningsAsErrors`). Without that property Android Lint fails on errors only: `android/` ships in the
npm package, and an app that lints the library with a newer lint must not fail on a new warning. Findings that predate a tool are recorded
in a baseline; fix one and shrink its baseline in the same change. What a baseline matches differs:

- ESLint's `eslint-suppressions.json` counts violations per file and rule, not per location. A fixed
violation whose count is not lowered fails (`yarn lint --prune-suppressions` lowers it), but a new
violation of the same rule in the same file passes if an old one is fixed in the same change.
- Android Lint and detekt: an entry that no longer matches a finding does not fail (Android Lint
only prints that it was not found), so remove it with the fix.
- detekt matches an entry by rule, file name (not path) and signature (the enclosing declaration or
the flagged code), not by line. A new finding with the same rule and signature in a file of the same
name, such as a second `catch (e: Exception)` in `Main.kt`, is hidden by the old entry.
npm package, and an app that lints the library with a newer lint must not fail on a new warning.

No tool has a baseline of old findings. A finding is fixed, or, when the code is right as it is,
suppressed on the narrowest element with the reason next to it:

- ESLint: `// eslint-disable-next-line <rule> -- <reason>`. A disable that no longer matches a
finding is reported, and fails with `--max-warnings 0`. No bulk suppressions
(`eslint-suppressions.json`): they count per file and rule, so a fixed violation can hide a new one.
- detekt: `@Suppress("<Rule>")` on the catch parameter, expression or declaration, with a comment.
Its baseline matched by file name and signature, so one entry hid every `catch (e: Exception)` in a
file of that name.
- Android Lint: `@SuppressLint("<Id>")` (or `@Suppress`, in code the JVM-only CLI also compiles), with
a comment.
- knip fails on an `ignoreDependencies` entry that is no longer needed. A `@knipignore` tag covers
only the export it is on; knip does not report one left on an export that became used.
- Android Lint skips the checks whose result changes with no code change (newer versions published,
Google's SDK Index, the date); see `lint` in `android/build.gradle`.

| Area | Tool (pinned in CI) | Run locally | Baseline |
| --- | --- | --- | --- |
| TypeScript | ESLint with typescript-eslint `strictTypeChecked` | `yarn lint` | `eslint-suppressions.json` (`yarn lint --prune-suppressions` after a fix) |
| TypeScript | ESLint with typescript-eslint `strictTypeChecked` | `yarn lint` | none |
| TypeScript | `tsc` (`strict`, `noUncheckedIndexedAccess`) | `yarn typecheck` | none |
| All workspaces | knip 6.45.1 (unused files, exports, dependencies) | `yarn knip` | `ignoreDependencies` in `knip.jsonc`; `@knipignore` on single exports |
| Kotlin | ktlint 1.8.0 | `ktlint "android/src/**/*.kt" "!android/src/main/java/com/remotecompose/contract/**" "tools/remote-compose-json/**/*.kt" "tools/remote-compose-json/*.kts"` | none |
| Kotlin | detekt 1.23.8 | see the `lint` job in `.github/workflows/ci.yml` | `detekt-baseline.xml` |
| Android | Android Lint | `example/android/gradlew -p example/android :react-native-remote-compose:lintDebug -PremoteComposeStrictLint=true` | `android/lint-baseline.xml` (`updateLintBaseline`) |
| Kotlin | detekt 1.23.8 | `detekt --build-upon-default-config --config detekt.yml --input android/src,tools/remote-compose-json/src --excludes '**/contract/**'` | none |
| Android | Android Lint | `example/android/gradlew -p example/android :react-native-remote-compose:lintDebug -PremoteComposeStrictLint=true` | none |
| Swift | SwiftFormat 0.64.7 | `swiftformat --lint ios` | none |
| Swift | SwiftLint 0.65.1 | `swiftlint lint --strict` | none |
| Secrets | gitleaks 8.30.1, every commit reachable from `HEAD` | `gitleaks git --config .gitleaks.toml --log-opts="--diff-merges=separate HEAD" .` | allowlist in `.gitleaks.toml` |
| Dependencies | OSV-Scanner | `osv-scanner scan --config=osv-scanner.toml --lockfile=yarn.lock --lockfile=example/Gemfile.lock --lockfile=example/android/app/gradle.lockfile --lockfile=tools/remote-compose-json/gradle.lockfile` | `osv-scanner.toml` |
| Versions | `scripts/check-versions.ts` (copies of native dependency versions agree) | `yarn lint` | none |

To regenerate the detekt baseline after a fix, run detekt as in CI with `--create-baseline`.

Use the versions in the table locally (CI downloads exactly those, checksum-verified; see the `env` of
the `lint` job in `.github/workflows/ci.yml`). CI runs SwiftLint's statically linked Linux build
Use the versions in the table locally. CI downloads exactly those through
`.github/actions/fetch-tool`, which checks each file's SHA-256 (pinned in the `env` of the `lint` and
`secrets` jobs in `.github/workflows/ci.yml`) before using it, cached or not. CI runs SwiftLint's statically linked Linux build
(`swiftlint-static`), which has no SourceKit and skips the rules that need it (such as
`statement_position`; SwiftFormat covers that one). SwiftLint on macOS runs those rules too, so it can
report findings CI does not.
Expand Down
4 changes: 1 addition & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ android {
"PlaySdkIndexGenericIssues",
"PlaySdkIndexDeprecated",
]
// Findings that predate running lint in CI; only new ones fail. Regenerate with
// `:react-native-remote-compose:updateLintBaseline` after fixing one.
baseline = file("lint-baseline.xml")
// No baseline: a finding is fixed, or suppressed where it is (@SuppressLint) with the reason.
}
}

Expand Down
26 changes: 0 additions & 26 deletions android/lint-baseline.xml

This file was deleted.

9 changes: 8 additions & 1 deletion android/src/main/java/com/remotecompose/DocumentLoader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.remotecompose

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
Expand Down Expand Up @@ -48,6 +49,9 @@ internal object DocumentLoader {
}

private suspend fun loadUri(context: Context, source: DocumentSource.Uri): ByteArray {
// UseKtx: toUri() is core-ktx, which this library does not declare. Declaring it would put a
// minimum core-ktx version on every app for a one-line wrapper around Uri.parse.
@SuppressLint("UseKtx")
val uri = Uri.parse(source.uri)
return when {
URLUtil.isNetworkUrl(source.uri) -> loadHttp(source)
Expand All @@ -68,7 +72,8 @@ internal object DocumentLoader {
val request =
Request.Builder()
.url(source.uri)
.headers(Headers.headersOf(*source.headers.toTypedArray()))
// A few header strings copied once per request: the copy the rule warns about is negligible.
.headers(@Suppress("SpreadOperator") Headers.headersOf(*source.headers.toTypedArray()))
.build()
val call = httpClient.newCall(request)
return suspendCancellableCoroutine { continuation ->
Expand All @@ -87,6 +92,8 @@ internal object DocumentLoader {
}
}

// One throw per way a response can fail, each with its own message for onError.
@Suppress("ThrowsCount")
private fun readBody(response: Response, uri: String): ByteArray {
if (!response.isSuccessful) throw IOException("HTTP ${response.code} for $uri")
val body = response.body ?: throw IOException("Empty response for $uri")
Expand Down
3 changes: 2 additions & 1 deletion android/src/main/java/com/remotecompose/PreparedDocument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private suspend fun <T> stage(code: ErrorCode, block: suspend () -> T): T = try
block()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
// Every failure of a stage, whatever AndroidX or rc-players throws, reaches JS as that stage's code.
throw RemoteComposeException(code, e.message ?: e.toString(), e)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class RemoteComposeModule(reactContext: ReactApplicationContext) : NativeRemoteC
executor.execute {
try {
promise.resolve(Base64.encodeToString(JsonDocuments.toRc(json), Base64.NO_WRAP))
} catch (e: Exception) {
// A conversion failure is a `load` failure, as the view reports it for `{ json }`.
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
// Anything AndroidX's parser throws rejects the promise (uncaught on this executor it
// would crash the app), as a `load` failure, as the view reports it for `{ json }`.
promise.reject(ErrorCode.LOAD.wireValue, e.message ?: e.toString(), e)
}
}
Expand Down
6 changes: 6 additions & 0 deletions android/src/main/java/com/remotecompose/RemoteComposeView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.remotecompose

import android.annotation.SuppressLint
import android.util.Log
import android.widget.FrameLayout
import androidx.annotation.MainThread
Expand Down Expand Up @@ -32,6 +33,11 @@ import kotlinx.coroutines.launch
* is therefore confined to the main thread, and a newer load cancels the older one before it can
* apply a stale result.
*/
// ViewConstructor: React Native creates it only through RemoteComposeViewManager, with the
// ThemedReactContext it needs; it is never inflated from XML or created by layout tools.
// TooManyFunctions: one setter per React prop (Codegen's interface), the rest is the load lifecycle.
@SuppressLint("ViewConstructor")
@Suppress("TooManyFunctions")
class RemoteComposeView(private val reactContext: ThemedReactContext) : FrameLayout(reactContext) {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private val typefaces = RcTypefaceLoader.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.remotecompose.contract.Theme

/** Maps Codegen's props interface to [RemoteComposeView]; wire values become typed here. */
@ReactModule(name = RemoteComposeViewManager.NAME)
@Suppress("TooManyFunctions") // one override per React prop and view-manager callback
class RemoteComposeViewManager :
SimpleViewManager<RemoteComposeView>(),
RemoteComposeViewManagerInterface<RemoteComposeView> {
Expand Down
15 changes: 0 additions & 15 deletions detekt-baseline.xml

This file was deleted.

3 changes: 2 additions & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# detekt: applied on top of its default configuration (--build-upon-default-config).
# Findings that predate detekt are in detekt-baseline.xml; CI fails on any new one.
# No baseline: CI fails on any finding. One that is right as it is gets @Suppress("Rule") on the
# narrowest element (a catch parameter, an expression, a function), with a comment saying why.
naming:
FunctionNaming:
# Compose's convention, as in .editorconfig for ktlint: composables are PascalCase nouns.
Expand Down
Loading
Loading