From ccc8ce0d01d5c11fd3f918c0ed7c9ff32b497a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Guzm=C3=A1n?= Date: Wed, 18 Jun 2025 08:22:04 -0700 Subject: [PATCH] Remove lint-java (#52092) Summary: Following up from https://github.com/facebook/react-native/pull/52064#discussion_r2151906096, this PR removes lint-java and its related files. The codebase is moving entirely to Kotlin and a Kotlin linter is being setup as well, the usage of the Java linter will become unnecessary. ## Changelog: [INTERNAL] - Remove lint-java Pull Request resolved: https://github.com/facebook/react-native/pull/52092 Test Plan: Relying on CI here to be green. Reviewed By: cortinico Differential Revision: D76880712 Pulled By: sbuggay fbshipit-source-id: 2736772e7347f435b17d007e0322e1afc2fb2d7b --- .github/actions/lint/action.yml | 3 - .github/workflow-scripts/analyze_code.sh | 3 - package.json | 1 - scripts/lint-java.js | 206 ----------------------- 4 files changed, 213 deletions(-) delete mode 100644 scripts/lint-java.js diff --git a/.github/actions/lint/action.yml b/.github/actions/lint/action.yml index 1b763e19359..9758f993668 100644 --- a/.github/actions/lint/action.yml +++ b/.github/actions/lint/action.yml @@ -26,9 +26,6 @@ runs: - name: Lint code shell: bash run: ./.github/workflow-scripts/exec_swallow_error.sh yarn lint --format junit -o ./reports/junit/eslint/results.xml - - name: Lint java - shell: bash - run: ./.github/workflow-scripts/exec_swallow_error.sh yarn lint-java --check - name: Lint file structure shell: bash run: ./.github/workflow-scripts/lint_files.sh diff --git a/.github/workflow-scripts/analyze_code.sh b/.github/workflow-scripts/analyze_code.sh index 26323a44032..8ea078174c1 100755 --- a/.github/workflow-scripts/analyze_code.sh +++ b/.github/workflow-scripts/analyze_code.sh @@ -13,9 +13,6 @@ export GITHUB_REPO=-react-native echo flow npm run flow-check --silent --json - - echo google-java-format - node scripts/lint-java.js --diff } | node private/react-native-bots/code-analysis-bot.js STATUS=$? diff --git a/package.json b/package.json index 13713e22fb9..26fd1094f0d 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "format": "npm run prettier && npm run clang-format", "featureflags": "yarn --cwd packages/react-native featureflags", "lint-ci": "./.github/workflow-scripts/analyze_code.sh && yarn shellcheck", - "lint-java": "node ./scripts/lint-java.js", "lint-markdown": "markdownlint-cli2 2>&1", "lint": "eslint --max-warnings 0 .", "prettier": "prettier --write \"./**/*.{js,md,yml,ts,tsx}\"", diff --git a/scripts/lint-java.js b/scripts/lint-java.js deleted file mode 100644 index d532503af19..00000000000 --- a/scripts/lint-java.js +++ /dev/null @@ -1,206 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @noflow - */ - -'use strict'; - -const fs = require('fs'); -const https = require('https'); -const os = require('os'); -const path = require('path'); -const {exec} = require('shelljs'); -const yargs = require('yargs'); - -const googleJavaFormatUrl = - 'https://github.com/google/google-java-format/releases/download/google-java-format-1.7/google-java-format-1.7-all-deps.jar'; -const googleJavaFormatPath = path.join( - os.tmpdir(), - 'google-java-format-all-deps.jar', -); -const javaFilesCommand = 'find ./ReactAndroid -name "*.java"'; - -function _download(url, downloadPath, resolve, reject, redirectCount) { - https.get(url, response => { - switch (response.statusCode) { - case 302: //Permanent Redirect - if (redirectCount === 0) { - throw new Error( - `Unhandled response code (HTTP${response.statusCode}) while retrieving google-java-format binary from ${url}`, - ); - } - - _download( - response.headers.location, - downloadPath, - resolve, - reject, - redirectCount - 1, - ); - break; - case 200: //OK - const file = fs.createWriteStream(downloadPath); - - response.pipe(file); - file.on('finish', () => file.close(() => resolve())); - break; - default: - reject( - `Unhandled response code (HTTP${response.statusCode}) while retrieving google-java-format binary from ${url}`, - ); - } - }); -} - -function download(url, downloadPath) { - return new Promise((resolve, reject) => { - _download(url, downloadPath, resolve, reject, 1); - }); -} - -function filesWithLintingIssues() { - const proc = exec( - `java -jar ${googleJavaFormatPath} --dry-run $(${javaFilesCommand})`, - {silent: true}, - ); - - if (proc.code !== 0) { - throw new Error(proc.stderr); - } - - return proc.stdout.split('\n').filter(x => x); -} - -function unifiedDiff(file) { - const lintedProc = exec( - `java -jar ${googleJavaFormatPath} --set-exit-if-changed ${file}`, - {silent: true}, - ); - - //Exit code 1 indicates lint violations, which is what we're expecting - if (lintedProc.code !== 1) { - throw new Error(lintedProc.stderr); - } - - const diffProc = lintedProc.exec(`diff -U 0 ${file} -`, {silent: true}); - - //Exit code 0 if inputs are the same, 1 if different, 2 if trouble. - if (diffProc.code !== 0 && diffProc.code !== 1) { - throw new Error(diffProc.stderr); - } - - return { - file, - diff: diffProc.stdout, - }; -} - -function extractRangeInformation(range) { - //eg; - // @@ -54 +54,2 @@ - // @@ -1,3 +1,9 @@ - - const regex = /^@@ [-+](\d+,?\d+) [-+](\d+,?\d+) @@$/; - const match = regex.exec(range); - - if (match) { - const original = match[1].split(','); - const updated = match[2].split(','); - - return { - original: { - line: parseInt(original[0], 10), - lineCount: parseInt(original[1], 10) || 1, - }, - updated: { - line: parseInt(updated[0], 10), - lineCount: parseInt(updated[1], 10) || 1, - }, - }; - } -} - -function parseChanges(file, diff) { - let group = null; - const groups = []; - - diff.split('\n').forEach(line => { - const range = extractRangeInformation(line); - - if (range) { - group = { - range, - description: [line], - }; - groups.push(group); - } else if (group) { - group.description.push(line); - } - }); - - return groups.map(x => ({ - file, - line: x.range.original.line, - lineCount: x.range.original.lineCount, - description: x.description.join('\n'), - })); -} - -async function main() { - const {argv} = yargs - .scriptName('lint-java') - .usage('Usage: $0 [options]') - .command( - '$0', - 'Downloads the google-java-format package and reformats Java source code to comply with Google Java Style.\n\nSee https://github.com/google/google-java-format', - ) - .option('check', { - type: 'boolean', - description: - 'Outputs a list of files with lint violations.\nExit code is set to 1 if there are violations, otherwise 0.\nDoes not reformat lint issues.', - }) - .option('diff', { - type: 'boolean', - description: - 'Outputs a diff of the lint fix changes in json format.\nDoes not reformat lint issues.', - }); - - await download(googleJavaFormatUrl, googleJavaFormatPath); - - if (argv.check) { - const files = filesWithLintingIssues(); - - files.forEach(x => console.log(x)); - - process.exit(files.length === 0 ? 0 : 1); - - return; - } - - if (argv.diff) { - const suggestions = filesWithLintingIssues() - .map(unifiedDiff) - .filter(x => x) - .map(x => parseChanges(x.file, x.diff)) - .reduce((accumulator, current) => accumulator.concat(current), []); - - console.log(JSON.stringify(suggestions)); - - return; - } - - const proc = exec( - `java -jar ${googleJavaFormatPath} --set-exit-if-changed --replace $(${javaFilesCommand})`, - ); - - process.exit(proc.code); -} - -(async () => { - await main(); -})();