From 367be052ab4d13b21789b922b70387cbd05a31cb Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 14 Feb 2026 22:10:28 +0400 Subject: [PATCH] Fix some eslint js issues (#33856) The PR fixes eslint issues in *.js files and removes the related rules from the backlist. --- .prettierignore | 1 - build/commands/lib/branding.js | 5 +++- build/commands/lib/buildChromiumRelease.js | 4 +-- build/commands/lib/config.js | 2 +- build/commands/lib/genGradle.js | 2 +- build/commands/lib/perfTests.js | 4 +-- build/commands/lib/util.js | 3 +- .../commands/scripts/buildRustToolchainAux.js | 2 +- build/commands/scripts/format.js | 2 +- .../scripts/updateBraveToolsCrates.js | 2 +- build/commands/scripts/updatePatches.js | 2 +- build/commands/scripts/updateWasmResources.js | 2 +- .../resources/domain_block_interstitial.js | 2 ++ .../resources/ethereum_provider.js | 2 ++ .../brave_wallet/resources/solana_provider.js | 2 ++ .../resources/data/content_cosmetic_ios.js | 2 ++ .../resources/speedreader-desktop.js | 6 ++-- .../core/browser/resources/brave_translate.js | 2 ++ components/webpack/gen-webpack-grd.js | 2 +- eslint.config.mjs | 28 +++++++++++-------- .../scripts/cosmetic-filter-tests.js | 2 ++ .../Resources/scripts/farbling-tests.js | 2 ++ .../scripts/request-blocking-tests.js | 2 ++ package-lock.json | 14 ++++++++++ package.json | 1 + .../static/js/frames/ephemeral-storage.js | 7 ++++- .../api_test/ipfsCompanion/background.js | 9 +++--- .../jest_teamcity_reporter.js | 3 +- 28 files changed, 83 insertions(+), 34 deletions(-) diff --git a/.prettierignore b/.prettierignore index eb69022d85d..3f2b4104b25 100644 --- a/.prettierignore +++ b/.prettierignore @@ -35,7 +35,6 @@ components/ai_chat/core/browser/tools/tools.md chromium_presubmit_config.json5 jest.config.js snapcraft.yaml -.eslintrc.js # Needs to be refactored before being formatted /build/commands/lib/util.js diff --git a/build/commands/lib/branding.js b/build/commands/lib/branding.js index eab8e684d15..69eeb1a4365 100644 --- a/build/commands/lib/branding.js +++ b/build/commands/lib/branding.js @@ -356,7 +356,10 @@ exports.update = () => { .split('\n') let untrackedChromiumPaths = [] for (const untrackedChromiumFile of untrackedChromiumFiles) { - untrackedChromiumPath = path.join(suspectedDir, untrackedChromiumFile) + const untrackedChromiumPath = path.join( + suspectedDir, + untrackedChromiumFile, + ) if (!fs.statSync(untrackedChromiumPath).isDirectory()) { untrackedChromiumPaths.push(untrackedChromiumPath) diff --git a/build/commands/lib/buildChromiumRelease.js b/build/commands/lib/buildChromiumRelease.js index f997a1d3aa9..69582cf7b78 100644 --- a/build/commands/lib/buildChromiumRelease.js +++ b/build/commands/lib/buildChromiumRelease.js @@ -35,8 +35,8 @@ const chromiumConfigs = { buildTargets: ['mini_installer'], processArtifacts: () => { // Repack it to reduce the size and use .zip instead of .7z. - input = path.join(config.outputDir, 'chrome.7z') - output = path.join(config.outputDir, `${getOutputFilename()}.zip`) + const input = path.join(config.outputDir, 'chrome.7z') + const output = path.join(config.outputDir, `${getOutputFilename()}.zip`) util.run( 'python3', [ diff --git a/build/commands/lib/config.js b/build/commands/lib/config.js index ea105cd1946..5005a9aee29 100644 --- a/build/commands/lib/config.js +++ b/build/commands/lib/config.js @@ -1244,7 +1244,7 @@ Object.defineProperty(Config.prototype, 'outputDir', { return path.join(baseDir, buildConfigDir) }, set: function (outputDir) { - return (this.__outputDir = outputDir) + this.__outputDir = outputDir }, }) diff --git a/build/commands/lib/genGradle.js b/build/commands/lib/genGradle.js index ace38ebb65f..22d1a8706ec 100644 --- a/build/commands/lib/genGradle.js +++ b/build/commands/lib/genGradle.js @@ -17,7 +17,7 @@ const genGradle = ( config.buildConfig = buildConfig config.update(options) Log.progressScope('Generating Gradle files', () => { - braveArgs = [ + let braveArgs = [ 'build/android/gradle/generate_gradle.py', '--output-directory', config.outputDir, diff --git a/build/commands/lib/perfTests.js b/build/commands/lib/perfTests.js index ae9822bb2a0..e7c2ae395d4 100644 --- a/build/commands/lib/perfTests.js +++ b/build/commands/lib/perfTests.js @@ -14,7 +14,7 @@ const runPerfTests = ( targetBuildConfig, options, ) => { - args = [ + const args = [ path.join(config.braveCoreDir, 'tools', 'perf', 'run_perftests.py'), perfConfig, ] @@ -32,7 +32,7 @@ const runPerfTests = ( config.buildConfig = targetBuildConfig config.update(options) - binaryPath = path.join(config.outputDir, 'brave') + let binaryPath = path.join(config.outputDir, 'brave') if (process.platform === 'win32') { binaryPath += '.exe' } else if (process.platform === 'darwin') { diff --git a/build/commands/lib/util.js b/build/commands/lib/util.js index eefd3657eaf..596419b4d52 100644 --- a/build/commands/lib/util.js +++ b/build/commands/lib/util.js @@ -890,7 +890,8 @@ const util = { return dotGitPath } // Returns the actual .git dir in case a worktree is used. - gitDir = util.runGit(repoDir, ['rev-parse', '--git-common-dir'], false) + const gitDir = util.runGit(repoDir, ['rev-parse', '--git-common-dir'], + false) if (!path.isAbsolute(gitDir)) { return path.join(repoDir, gitDir) } diff --git a/build/commands/scripts/buildRustToolchainAux.js b/build/commands/scripts/buildRustToolchainAux.js index e09e3419942..8a835b970e4 100644 --- a/build/commands/scripts/buildRustToolchainAux.js +++ b/build/commands/scripts/buildRustToolchainAux.js @@ -35,7 +35,7 @@ program 'src@refs/tags/' + options.tag, ]) } - args = [ + const args = [ path.join( config.srcDir, 'brave', diff --git a/build/commands/scripts/format.js b/build/commands/scripts/format.js index 60ff6426669..0bac8513b85 100644 --- a/build/commands/scripts/format.js +++ b/build/commands/scripts/format.js @@ -190,7 +190,7 @@ const runPrettier = async (files, dryRun) => { const options = require(path.join(config.braveCoreDir, '.prettierrc')) const ignorePath = path.join(config.braveCoreDir, '.prettierignore') if (!fs.existsSync(ignorePath)) { - throw new RuntimeError(`${ignorePath} file not found`) + throw new Error(`${ignorePath} file not found`) } const prettierIssues = [] diff --git a/build/commands/scripts/updateBraveToolsCrates.js b/build/commands/scripts/updateBraveToolsCrates.js index 916eaee2ba2..56cae408ee4 100644 --- a/build/commands/scripts/updateBraveToolsCrates.js +++ b/build/commands/scripts/updateBraveToolsCrates.js @@ -7,5 +7,5 @@ const config = require('../lib/config') const path = require('path') const util = require('../lib/util') -args = [path.join(config.srcDir, 'brave', 'tools', 'crates', 'update.py')] +const args = [path.join(config.srcDir, 'brave', 'tools', 'crates', 'update.py')] util.run('vpython3', args, config.defaultOptions) diff --git a/build/commands/scripts/updatePatches.js b/build/commands/scripts/updatePatches.js index ef71750902f..9c2bb362507 100644 --- a/build/commands/scripts/updatePatches.js +++ b/build/commands/scripts/updatePatches.js @@ -49,7 +49,7 @@ function loadChromiumPathFilter(filePath) { } } -chromiumPathFilter = loadChromiumPathFilter( +const chromiumPathFilter = loadChromiumPathFilter( path.join(config.braveCoreDir, 'build', 'update_patches_exclusions.cfg'), ) diff --git a/build/commands/scripts/updateWasmResources.js b/build/commands/scripts/updateWasmResources.js index 1b46162e6a9..a93ed530ad4 100644 --- a/build/commands/scripts/updateWasmResources.js +++ b/build/commands/scripts/updateWasmResources.js @@ -7,7 +7,7 @@ const config = require('../lib/config') const path = require('path') const util = require('../lib/util') -args = [ +const args = [ path.join( config.srcDir, 'brave', diff --git a/components/brave_shields/resources/domain_block_interstitial.js b/components/brave_shields/resources/domain_block_interstitial.js index 917fce129c8..c00e8a8e7ff 100644 --- a/components/brave_shields/resources/domain_block_interstitial.js +++ b/components/brave_shields/resources/domain_block_interstitial.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* global loadTimeDataRaw */ + import { SecurityInterstitialCommandId, sendCommand } from 'chrome://interstitials/common/resources/interstitial_common.js'; function setupEvents() { diff --git a/components/brave_wallet/resources/ethereum_provider.js b/components/brave_wallet/resources/ethereum_provider.js index c7b93521ae9..88bc2c9c153 100644 --- a/components/brave_wallet/resources/ethereum_provider.js +++ b/components/brave_wallet/resources/ethereum_provider.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // you can obtain one at https://mozilla.org/MPL/2.0/. +/* global $Object */ + ;(function () { if (!window.braveEthereum) { return diff --git a/components/brave_wallet/resources/solana_provider.js b/components/brave_wallet/resources/solana_provider.js index 4e864251292..2339926b433 100644 --- a/components/brave_wallet/resources/solana_provider.js +++ b/components/brave_wallet/resources/solana_provider.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // you can obtain one at https://mozilla.org/MPL/2.0/. +/* global $Object */ + ;(function () { if (!window.braveSolana) { return diff --git a/components/cosmetic_filters/resources/data/content_cosmetic_ios.js b/components/cosmetic_filters/resources/data/content_cosmetic_ios.js index 3b72606f7d2..2f7321f965d 100644 --- a/components/cosmetic_filters/resources/data/content_cosmetic_ios.js +++ b/components/cosmetic_filters/resources/data/content_cosmetic_ios.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* eslint-disable no-undef */ + import { applyCompiledSelector, compileProceduralSelector, diff --git a/components/speedreader/resources/speedreader-desktop.js b/components/speedreader/resources/speedreader-desktop.js index 7496059f6b3..9c5143b4d39 100644 --- a/components/speedreader/resources/speedreader-desktop.js +++ b/components/speedreader/resources/speedreader-desktop.js @@ -68,7 +68,7 @@ class speedreaderUtils { const wpm = this.speedreaderData.averageWordsPerMinute const minutes = Math.ceil(words / wpm) - readTimeDiv.innerText = minutes + ' ' + speedreaderData.minutesText + readTimeDiv.innerText = minutes + ' ' + this.speedreaderData.minutesText } static getTextContent = (element) => { @@ -126,7 +126,7 @@ class speedreaderUtils { button.classList.add('tts-circle') const playButton = document.createElement('span') playButton.classList.add('tts-paragraph-player-button', 'tts-play-icon') - playButton.title = speedreaderData.playButtonTitle + playButton.title = this.speedreaderData.playButtonTitle playButton.onclick = button.onclick = (ev) => { window.speedreader.ttsPlayPause(parseInt(p.getAttribute('tts-paragraph-index'))) } @@ -157,6 +157,7 @@ class speedreaderUtils { let currentParent = null let paragraphs = [] let currentNode = null; + // eslint-disable-next-line no-cond-assign while (currentNode = nodes.nextNode()) { if (currentNode === nodes.root || isChildOf(currentNode, currentParent) || @@ -219,6 +220,7 @@ class speedreaderUtils { let startNode = null let endNode = null let node = null + // eslint-disable-next-line no-cond-assign while (node = nodes.nextNode()) { if (!startNode) { if (start < node.textContent.length) { diff --git a/components/translate/core/browser/resources/brave_translate.js b/components/translate/core/browser/resources/brave_translate.js index c8eb9fdbd6a..e35898dafb2 100644 --- a/components/translate/core/browser/resources/brave_translate.js +++ b/components/translate/core/browser/resources/brave_translate.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* eslint-disable no-undef */ + // securityOrigin is predefined by translate_script.cc. const securityOriginHost = new URL(securityOrigin).host diff --git a/components/webpack/gen-webpack-grd.js b/components/webpack/gen-webpack-grd.js index a8242d592ea..514f743f16e 100644 --- a/components/webpack/gen-webpack-grd.js +++ b/components/webpack/gen-webpack-grd.js @@ -91,7 +91,7 @@ async function createDynamicGDR () { // remove previously generated file try { await fs.unlink(gdrPath) - } catch (e) {} + } catch (e) {} // eslint-disable-line no-empty // build file list from target dir const filePaths = await getFileListDeep(targetDir) const contents = gdrPath.endsWith('.grdp') diff --git a/eslint.config.mjs b/eslint.config.mjs index 42145de1551..e96d941282a 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -13,6 +13,7 @@ import love from 'eslint-config-love' import prettier from 'eslint-config-prettier/flat' import tslint from 'typescript-eslint' import eslintJs from '@eslint/js' +import globals from 'globals' // Config layout: each plugin or override is a separate array entry. // Guidelines: @@ -22,6 +23,18 @@ import eslintJs from '@eslint/js' // it targets. // Verify changes with: npm run eslint (about 1 min). export default defineConfig([ + // Setup project-wide language options. + { + languageOptions: { + globals: { + ...globals.browser, + ...globals.jest, + ...globals.node, + chrome: 'readonly', + }, + }, + }, + // Turn off rules that conflict with Prettier formatting (all files). prettier, @@ -100,7 +113,6 @@ export default defineConfig([ '.storybook/*', 'browser/*', 'ui/webui/resources/*', - '**/*.js', '**/*.d.ts', 'tools/chromium_src/lit_mangler/*.ts', @@ -116,6 +128,7 @@ export default defineConfig([ // Generated by scripts, skip linting 'ios/brave-ios/Sources/AIChat/Components/Markdown/CodeHighlight/Scripts/highlight.min.js', 'components/brave_wallet/resources/solana_web3_script.js', + 'test/data/ephemeral-storage/static/js/libs/js.cookie.min.js', ]), { @@ -278,25 +291,18 @@ export default defineConfig([ }, }, { - // Rules disabled for plain JavaScript. + // Brave JavaScript files uses require() to load modules. files: ['**/*.{js,jsx,mjs,cjs}'], rules: { '@typescript-eslint/no-require-imports': 'off', - - 'no-cond-assign': 'off', - 'no-control-regex': 'off', - 'no-empty': 'off', - 'no-redeclare': 'off', - 'no-setter-return': 'off', - 'no-undef': 'off', - 'no-unused-vars': 'off', }, }, { - // Test data files; skip license header requirement. + // Test data files; skips some non-important rules. files: ['test/data/**/*.js'], rules: { 'licenses/header': 'off', + 'no-undef': 'off', }, }, ]) diff --git a/ios/brave-ios/Tests/ClientTests/Resources/scripts/cosmetic-filter-tests.js b/ios/brave-ios/Tests/ClientTests/Resources/scripts/cosmetic-filter-tests.js index 80d9e3297ab..318dd7ca81b 100644 --- a/ios/brave-ios/Tests/ClientTests/Resources/scripts/cosmetic-filter-tests.js +++ b/ios/brave-ios/Tests/ClientTests/Resources/scripts/cosmetic-filter-tests.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* global webkit */ + (() => { "use strict" diff --git a/ios/brave-ios/Tests/ClientTests/Resources/scripts/farbling-tests.js b/ios/brave-ios/Tests/ClientTests/Resources/scripts/farbling-tests.js index d9b61bf218b..597e8a9bfd3 100644 --- a/ios/brave-ios/Tests/ClientTests/Resources/scripts/farbling-tests.js +++ b/ios/brave-ios/Tests/ClientTests/Resources/scripts/farbling-tests.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* global webkit */ + (() => { "use strict" diff --git a/ios/brave-ios/Tests/ClientTests/Resources/scripts/request-blocking-tests.js b/ios/brave-ios/Tests/ClientTests/Resources/scripts/request-blocking-tests.js index 0ce0b9d217c..22230e9a284 100644 --- a/ios/brave-ios/Tests/ClientTests/Resources/scripts/request-blocking-tests.js +++ b/ios/brave-ios/Tests/ClientTests/Resources/scripts/request-blocking-tests.js @@ -3,6 +3,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. +/* global webkit */ + (() => { "use strict" /** diff --git a/package-lock.json b/package-lock.json index 02a252c8f26..9f3b45b22d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -108,6 +108,7 @@ "font-awesome": "4.7.0", "fork-ts-checker-webpack-plugin": "9.0.2", "fs-extra": "8.1.0", + "globals": "17.3.0", "https-browserify": "1.0.0", "identity-obj-proxy": "3.0.0", "jest": "28.1.3", @@ -12415,6 +12416,19 @@ "process": "^0.11.10" } }, + "node_modules/globals": { + "version": "17.3.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-17.3.0.tgz", + "integrity": "sha512-yMqGUQVVCkD4tqjOJf3TnrvaaHDMYp4VlUSObbkIiuCPe/ofdMBFIAcBbCSRFWOnos6qRiTVStDwqPLUclaxIw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/globalthis": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/globalthis/-/globalthis-1.0.4.tgz", diff --git a/package.json b/package.json index fd6afc24ecc..5e5959ed3d6 100644 --- a/package.json +++ b/package.json @@ -156,6 +156,7 @@ "font-awesome": "4.7.0", "fork-ts-checker-webpack-plugin": "9.0.2", "fs-extra": "8.1.0", + "globals": "17.3.0", "https-browserify": "1.0.0", "identity-obj-proxy": "3.0.0", "jest": "28.1.3", diff --git a/test/data/ephemeral-storage/static/js/frames/ephemeral-storage.js b/test/data/ephemeral-storage/static/js/frames/ephemeral-storage.js index ad6c680e74c..e6f3ca48724 100644 --- a/test/data/ephemeral-storage/static/js/frames/ephemeral-storage.js +++ b/test/data/ephemeral-storage/static/js/frames/ephemeral-storage.js @@ -1,3 +1,8 @@ +// Copyright (c) 2021 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + (_ => { const W = window const C = W.Cookies @@ -19,7 +24,7 @@ // Last, see if we're remote by seeing if we trigger a SOP violation // by reading the location of the parent. try { - if (window.parent.location.href) {} + console.log(window.parent.location.href) return false } catch (_) { return true diff --git a/test/data/extensions/api_test/ipfsCompanion/background.js b/test/data/extensions/api_test/ipfsCompanion/background.js index d04d6b71d1c..2c67d02f261 100644 --- a/test/data/extensions/api_test/ipfsCompanion/background.js +++ b/test/data/extensions/api_test/ipfsCompanion/background.js @@ -1,6 +1,7 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ +// Copyright (c) 2020 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. function getConfig(expectedSuccess, expectedConfig) { chrome.ipfs.getConfig((success, config) => { @@ -80,7 +81,7 @@ function compareObjects(o1, o2) { } } } - for(var p in o2) { + for(p in o2) { if (o2.hasOwnProperty(p)) { if (o1[p] !== o2[p]) { return false; diff --git a/tools/jest_teamcity_reporter/jest_teamcity_reporter.js b/tools/jest_teamcity_reporter/jest_teamcity_reporter.js index 32ee173bcf0..4c7ee19350b 100644 --- a/tools/jest_teamcity_reporter/jest_teamcity_reporter.js +++ b/tools/jest_teamcity_reporter/jest_teamcity_reporter.js @@ -276,7 +276,8 @@ class TeamcityReporter { // https://www.jetbrains.com/help/teamcity/service-messages.html#Escaped+Values const escapedParamValue = strParamValue - // Remove ANSI escape sequences (colors, styles, etc.). + // Remove ANSI escape sequences (colors, styles, etc.) + // eslint-disable-next-line no-control-regex .replace(/\u001B\[\d+(;\d+)*m/g, '') // Replace Teamcity Service Messages special characters. .replace(/[\n\r'|[\]]/g, (match) => {