diff --git a/.eslintrc.js b/.eslintrc.js index 6bef956dfd20..3cc706c07a0b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -4,8 +4,8 @@ // You can obtain one at https://mozilla.org/MPL/2.0/. module.exports = { - 'root': true, /* Don't read chromium's eslint config (https://eslint.org/docs/user-guide/configuring/configuration-files#cascading-and-hierarchy) */ - 'extends': 'standard-with-typescript', + 'root': true /* Don't read chromium's eslint config (https://eslint.org/docs/user-guide/configuring/configuration-files#cascading-and-hierarchy) */, + 'extends': ['standard-with-typescript', 'prettier'], 'ignorePatterns': [ '.storybook/*', 'build/*', @@ -21,10 +21,7 @@ module.exports = { 'es6': true, 'jest/globals': true }, - 'plugins': [ - 'jest', - 'licenses' - ], + 'plugins': ['jest', 'licenses'], 'globals': { 'chrome': 'readonly' }, @@ -67,10 +64,6 @@ module.exports = { ] } ], - 'quote-props': [ - 'error', - 'consistent' - ], 'object-shorthand': 0, 'n/no-callback-literal': 0, '@typescript-eslint/await-thenable': 0, diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 000000000000..f13268c1e673 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,13 @@ +// Copyright (c) 2022 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/. + +module.exports = { + 'trailingComma': 'none', + 'tabWidth': 2, + 'semi': false, + 'singleQuote': true, + 'bracketSameLine': false, + 'quoteProps': 'preserve' +} diff --git a/PRESUBMIT.py b/PRESUBMIT.py index a48227388caf..bd4be697f715 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -7,9 +7,10 @@ import os import sys -import import_inline +import brave_node import chromium_presubmit_overrides import git_cl +import import_inline import override_utils USE_PYTHON3 = True @@ -54,6 +55,39 @@ def CheckPatchFormatted(input_api, output_api): is_format_required = git_cl_format_code == 2 + if not is_format_required or input_api.PRESUBMIT_FIX: + # Use Prettier to format other file types. + files_to_check = ( + # Enable when files will be formatted. + # r'.+\.js$', + # r'.+\.ts$', + # r'.+\.tsx$', + ) + files_to_skip = input_api.DEFAULT_FILES_TO_SKIP + + file_filter = lambda f: input_api.FilterSourceFile( + f, files_to_check=files_to_check, files_to_skip=files_to_skip) + affected_files = input_api.AffectedFiles(file_filter=file_filter, + include_deletes=False) + files_to_format = [f.AbsoluteLocalPath() for f in affected_files] + + node_args = [ + brave_node.PathInNodeModules('prettier', 'bin-prettier'), + '--write' if input_api.PRESUBMIT_FIX else '--check', + ] + + files_per_command = 25 if input_api.is_windows else 1000 + for i in range(0, len(files_to_format), files_per_command): + args = node_args + files_to_format[i:i + files_per_command] + try: + brave_node.RunNode(args) + except RuntimeError as err: + if 'Forgot to run Prettier?' in str(err): + is_format_required = True + break + # Raise on unexpected output. Could be node or prettier issues. + raise + if is_format_required: if input_api.PRESUBMIT_FIX: raise RuntimeError('--fix was passed, but format has failed') diff --git a/package-lock.json b/package-lock.json index 0124e6935cb2..5eb038647ea7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -77,6 +77,7 @@ "enzyme": "3.11.0", "enzyme-adapter-react-16": "1.15.6", "eslint": "8.33.0", + "eslint-config-prettier": "8.6.0", "eslint-config-standard-with-typescript": "34.0.0", "eslint-plugin-import": "2.27.5", "eslint-plugin-jest": "27.2.1", @@ -97,6 +98,7 @@ "log-update": "3.4.0", "mkdirp": "0.5.5", "mz": "2.7.0", + "prettier": "2.8.4", "react": "16.14.0", "react-dom": "16.14.0", "react-redux": "7.2.2", @@ -6043,6 +6045,18 @@ "node": ">=0.4.0" } }, + "node_modules/@storybook/addon-docs/node_modules/prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/@storybook/addon-essentials": { "version": "6.4.13", "resolved": "https://registry.npmjs.org/@storybook/addon-essentials/-/addon-essentials-6.4.13.tgz", @@ -7259,6 +7273,18 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/@storybook/csf-tools/node_modules/prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/@storybook/csf-tools/node_modules/universalify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", @@ -7878,6 +7904,18 @@ "react-dom": "^16.8.0 || ^17.0.0" } }, + "node_modules/@storybook/source-loader/node_modules/prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/@storybook/store": { "version": "6.4.13", "resolved": "https://registry.npmjs.org/@storybook/store/-/store-6.4.13.tgz", @@ -14583,6 +14621,18 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint-config-prettier": { + "version": "8.6.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.6.0.tgz", + "integrity": "sha512-bAF0eLpLVqP5oEVUFKpMA+NnRFICwn9X8B5jrR9FcqnYBuPbqWEjTEspPWMj5ye6czoSLDweCzSo3Ko7gGrZaA==", + "dev": true, + "bin": { + "eslint-config-prettier": "bin/cli.js" + }, + "peerDependencies": { + "eslint": ">=7.0.0" + } + }, "node_modules/eslint-config-standard": { "version": "17.0.0", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-17.0.0.tgz", @@ -23299,15 +23349,18 @@ } }, "node_modules/prettier": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", - "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.4.tgz", + "integrity": "sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw==", "dev": true, "bin": { "prettier": "bin-prettier.js" }, "engines": { "node": ">=10.13.0" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" } }, "node_modules/prettier-bytes": { @@ -35919,6 +35972,12 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", "integrity": "sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==", "dev": true + }, + "prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true } } }, @@ -36757,6 +36816,12 @@ "universalify": "^2.0.0" } }, + "prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true + }, "universalify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", @@ -37199,6 +37264,14 @@ "lodash": "^4.17.21", "prettier": "<=2.3.0", "regenerator-runtime": "^0.13.7" + }, + "dependencies": { + "prettier": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", + "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "dev": true + } } }, "@storybook/store": { @@ -42675,6 +42748,13 @@ } } }, + "eslint-config-prettier": { + "version": "8.6.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.6.0.tgz", + "integrity": "sha512-bAF0eLpLVqP5oEVUFKpMA+NnRFICwn9X8B5jrR9FcqnYBuPbqWEjTEspPWMj5ye6czoSLDweCzSo3Ko7gGrZaA==", + "dev": true, + "requires": {} + }, "eslint-config-standard": { "version": "17.0.0", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-17.0.0.tgz", @@ -49307,9 +49387,9 @@ "dev": true }, "prettier": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.0.tgz", - "integrity": "sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==", + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.4.tgz", + "integrity": "sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw==", "dev": true }, "prettier-bytes": { diff --git a/package.json b/package.json index b7cb70ce471e..913a273500dc 100644 --- a/package.json +++ b/package.json @@ -306,6 +306,7 @@ "enzyme": "3.11.0", "enzyme-adapter-react-16": "1.15.6", "eslint": "8.33.0", + "eslint-config-prettier": "8.6.0", "eslint-config-standard-with-typescript": "34.0.0", "eslint-plugin-import": "2.27.5", "eslint-plugin-jest": "27.2.1", @@ -326,6 +327,7 @@ "log-update": "3.4.0", "mkdirp": "0.5.5", "mz": "2.7.0", + "prettier": "2.8.4", "react": "16.14.0", "react-dom": "16.14.0", "react-redux": "7.2.2",