Skip to content
Open
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
3 changes: 3 additions & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**/Pods/**
**/build/**
**/node_modules/**
packages/react-native/React/I18n/FBXXHashUtils.h
packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/**
packages/react-native/ReactAndroid/src/main/jni/third-party/**
Expand Down
40 changes: 0 additions & 40 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.binary.compatibility.validator) apply true
alias(libs.plugins.android.test) apply false
alias(libs.plugins.ktfmt) apply true
}

val reactAndroidProperties = java.util.Properties()
Expand Down Expand Up @@ -176,42 +175,3 @@ if (hermesSubstitution != null) {
}
}
}

ktfmt {
blockIndent.set(2)
continuationIndent.set(4)
maxWidth.set(100)
removeUnusedImports.set(false)
manageTrailingCommas.set(false)
}

// Configure ktfmt tasks to include gradle-plugin
listOf("ktfmtCheck", "ktfmtFormat").forEach { taskName ->
tasks.named(taskName) { dependsOn(gradle.includedBuild("gradle-plugin").task(":$taskName")) }
}

allprojects {
// Apply exclusions for specific files that should not be formatted
val excludePatterns = listOf(
"**/build/**",
"**/hermes-engine/**",
"**/internal/featureflags/**",
"**/systeminfo/ReactNativeVersion.kt",
)
listOf(
com.ncorti.ktfmt.gradle.tasks.KtfmtCheckTask::class,
com.ncorti.ktfmt.gradle.tasks.KtfmtFormatTask::class,
)
.forEach { tasks.withType(it) { exclude(excludePatterns) } }

// Disable the problematic ktfmt script tasks due to symbolic link issues in subprojects
afterEvaluate {
listOf("ktfmtCheckScripts", "ktfmtFormatScripts").forEach {
tasks.findByName(it)?.enabled = false
}
}
}

// We intentionally disable the `ktfmtCheck` tasks as the formatting is primarly handled inside
// fbsource
allprojects { tasks.withType<com.ncorti.ktfmt.gradle.tasks.KtfmtCheckTask>() { enabled = false } }
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@
"build-android": "./gradlew :packages:react-native:ReactAndroid:build",
"build": "node ./scripts/build/build.js",
"build-types": "node ./scripts/js-api/build-types",
"clang-format": "node ./scripts/clang-format.js",
"clean": "node ./scripts/build/clean.js",
"cxx-api-build": "python -m scripts.cxx-api.parser",
"cxx-api-validate": "python -m scripts.cxx-api.parser --validate",
"flow-check": "flow full-check",
"flow": "flow",
"format-check": "prettier --list-different \"./**/*.{js,md,yml,ts,tsx}\"",
"format": "npm run prettier && npm run clang-format",
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin && yarn format-check-java",
"format-check-cpp": "node ./scripts/clang-format.js --check",
"format-check-java": "node ./scripts/format-java.js --check",
"format-check-javascript": "prettier --check \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-check-kotlin": "node ./scripts/format-kotlin.js --check",
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin && yarn format-java",
"format-cpp": "node ./scripts/clang-format.js",
"format-java": "node ./scripts/format-java.js",
"format-javascript": "prettier --write \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-kotlin": "node ./scripts/format-kotlin.js",
"featureflags": "yarn --cwd packages/react-native featureflags",
"js-api-diff": "node ./scripts/js-api/diff-api-snapshot",
"lint-kotlin-check": "./gradlew ktfmtCheck",
"lint-kotlin": "./gradlew ktfmtFormat",
"lint-markdown": "markdownlint-cli2 2>&1",
"lint": "eslint --max-warnings 0 .",
"preinstall": "node ./scripts/try-set-hermes-compiler-prebuilt.js",
"prettier": "prettier --write \"./**/*.{js,md,yml,ts,tsx}\"",
"shellcheck": "./.github/workflow-scripts/analyze_scripts.sh",
"start": "yarn --cwd packages/rn-tester start",
"set-version": "node ./scripts/releases/set-version.js",
Expand Down Expand Up @@ -91,6 +95,7 @@
"flow-eslint": "0.331.0",
"flow-parser": "0.331.0",
"flow-transform": "0.331.0",
"google-java-format": "1.4.0",
"ini": "^5.0.0",
"inquirer": "^7.1.0",
"jest": "^29.7.0",
Expand All @@ -99,6 +104,7 @@
"jest-junit": "^16.0.0",
"jest-snapshot": "^29.7.0",
"jsonc-parser": "2.2.1",
"ktfmt": "0.59.0",
"markdownlint-cli2": "^0.17.2",
"markdownlint-rule-relative-links": "^3.0.0",
"memfs": "^4.38.2",
Expand Down
23 changes: 0 additions & 23 deletions packages/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

plugins {
alias(libs.plugins.kotlin.jvm).apply(false)
alias(libs.plugins.ktfmt).apply(true)
}

tasks.register("build") {
Expand All @@ -27,25 +26,3 @@ tasks.register("clean") {
":shared:clean",
)
}

tasks.named("ktfmtCheck") {
dependsOn(
":react-native-gradle-plugin:ktfmtCheck",
":settings-plugin:ktfmtCheck",
":shared-testutil:ktfmtCheck",
":shared:ktfmtCheck",
)
}

tasks.named("ktfmtFormat") {
dependsOn(
":react-native-gradle-plugin:ktfmtFormat",
":settings-plugin:ktfmtFormat",
":shared-testutil:ktfmtFormat",
":shared:ktfmtFormat",
)
}

// We intentionally disable the `ktfmtCheck` tasks as the formatting is primarly handled inside
// fbsource
allprojects { tasks.withType<com.ncorti.ktfmt.gradle.tasks.KtfmtCheckTask>() { enabled = false } }
2 changes: 0 additions & 2 deletions packages/gradle-plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ javapoet = "1.13.0"
junit = "4.13.2"
kotlin = "2.2.0"
assertj = "3.25.1"
ktfmt = "0.22.0"

[libraries]
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
Expand All @@ -19,4 +18,3 @@ assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
id("java-gradle-plugin")
}

Expand Down
1 change: 0 additions & 1 deletion packages/gradle-plugin/settings-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
id("java-gradle-plugin")
}

Expand Down
1 change: 0 additions & 1 deletion packages/gradle-plugin/shared-testutil/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
}

repositories { mavenCentral() }
Expand Down
1 change: 0 additions & 1 deletion packages/gradle-plugin/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
}

repositories { mavenCentral() }
Expand Down
1 change: 0 additions & 1 deletion packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ plugins {
id("com.facebook.react")
alias(libs.plugins.android.library)
alias(libs.plugins.download)
alias(libs.plugins.ktfmt)
}

version = project.findProperty("VERSION_NAME")?.toString()!!
Expand Down
2 changes: 0 additions & 2 deletions packages/react-native/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jsc-android = "2026004.0.1"
jsr305 = "3.0.2"
junit = "4.13.2"
kotlin = "2.2.0"
ktfmt = "0.22.0"
mockito = "3.12.4"
mockito-kotlin = "3.2.0"
nexus-publish = "2.0.0"
Expand Down Expand Up @@ -97,7 +96,6 @@ thoughtworks = {module = "com.thoughtworks.xstream:xstream", version.ref = "xstr
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
download = { id = "de.undercouch.download", version.ref = "download" }
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
nexus-publish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexus-publish" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }
Expand Down
68 changes: 58 additions & 10 deletions scripts/clang-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,49 @@

'use strict';

const dotslash = require('fb-dotslash');
const {spawnSync} = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const {globSync} = require('tinyglobby');

const REPO_ROOT = path.resolve(__dirname, '..');
const CLANG_FORMAT = path.join(__dirname, 'clang-format');
const OSS_CLANG_FORMAT_DOTSLASH = path.join(__dirname, 'clang-format');
const GENERATED_MARKER = Buffer.from('@' + 'generated');
const IGNORE_FILE = path.join(REPO_ROOT, '.clang-format-ignore');
const MAX_HEADER_BYTES = 4096;
const MAX_FILES_PER_PROCESS = 30;

const SOURCE_GLOB = '**/*.{c,cc,cpp,cu,cuh,cxx,h,hh,hpp,hxx,m,mm,proto,tcc}';

function findClangFormat() {
if (process.env.CLANG_FORMAT != null && process.env.CLANG_FORMAT !== '') {
return {command: process.env.CLANG_FORMAT, prefixArguments: []};
}

try {
const metaClangFormat = require('./clang-format.fb').findMetaClangFormat();
if (metaClangFormat != null) {
return metaClangFormat;
}
} catch (error) {
if (
error == null ||
error.code !== 'MODULE_NOT_FOUND' ||
!String(error.message).includes("'./clang-format.fb'")
) {
throw error;
}
}

return {
command:
process.env.DOTSLASH != null && process.env.DOTSLASH !== ''
? process.env.DOTSLASH
: require('fb-dotslash'),
prefixArguments: [OSS_CLANG_FORMAT_DOTSLASH],
};
}

/** @param {string} file */
function isGenerated(file) {
let fd;
Expand All @@ -41,16 +72,33 @@ function isGenerated(file) {
}

function main() {
const arguments_ = process.argv.slice(2);
const check = arguments_.includes('--check');
const clangFormat = findClangFormat();
const positionalArguments = arguments_.filter(
argument => argument !== '--check',
);
const ignore = fs
.readFileSync(IGNORE_FILE, 'utf8')
.split('\n')
.map(line => line.trim())
.filter(line => line !== '' && !line.startsWith('#'));
const discoveredFiles = globSync(SOURCE_GLOB, {cwd: REPO_ROOT, ignore});
const files =
process.argv.length > 2
? process.argv.slice(2)
: globSync('*/**/*.{h,cpp,m,mm}', {cwd: REPO_ROOT});
positionalArguments.length > 0
? positionalArguments.filter(file => discoveredFiles.includes(file))
: discoveredFiles;
const sourceFiles = files.filter(file => !isGenerated(file));
let exitStatus = 0;

for (let i = 0; i < sourceFiles.length; i += MAX_FILES_PER_PROCESS) {
const formatterArguments = [
...(check ? ['--dry-run', '--Werror'] : ['-i']),
...sourceFiles.slice(i, i + MAX_FILES_PER_PROCESS),
];
const result = spawnSync(
dotslash,
[CLANG_FORMAT, '-i', ...sourceFiles.slice(i, i + MAX_FILES_PER_PROCESS)],
clangFormat.command,
[...clangFormat.prefixArguments, ...formatterArguments],
{
cwd: REPO_ROOT,
stdio: 'inherit',
Expand All @@ -61,13 +109,13 @@ function main() {
throw result.error;
}
if (result.signal != null) {
process.kill(process.pid, result.signal);
return;
throw new Error(`clang-format was terminated by ${result.signal}`);
}
if (result.status !== 0) {
process.exit(result.status ?? 1);
exitStatus = result.status ?? 1;
}
}
process.exitCode = exitStatus;
}

main();
Loading
Loading