.pre-commit-config.yaml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. # Update versions with:
  5. # pre-commit autoupdate --freeze && pre-commit run -a
  6. # See https://pre-commit.com for more information
  7. # See https://pre-commit.com/hooks.html for more hooks
  8. default_language_version:
  9. python: python3 # Defaults to python2, so override it.
  10. repos:
  11. - repo: https://github.com/pre-commit/pre-commit-hooks
  12. rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
  13. hooks:
  14. - id: check-added-large-files
  15. - id: check-case-conflict
  16. - id: check-executables-have-shebangs
  17. - id: check-merge-conflict
  18. - id: check-symlinks
  19. exclude: ^bazel-execroot$
  20. - id: check-yaml
  21. - id: detect-private-key
  22. - id: end-of-file-fixer
  23. exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
  24. - id: mixed-line-ending
  25. args: ['--fix=lf']
  26. exclude: '^(.*/fuzzer_corpus/.*|.*\.svg)$'
  27. - id: trailing-whitespace
  28. exclude: '^(.*/fuzzer_corpus/.*|.*/testdata/.*\.golden|.*\.svg)$'
  29. - repo: https://github.com/google/pre-commit-tool-hooks
  30. rev: efaea7c61c774c0b1a9805fd999e754a2d19dbd1 # frozen: v1.2.5
  31. hooks:
  32. - id: check-google-doc-style
  33. - id: markdown-toc
  34. - repo: local
  35. hooks:
  36. - id: fix-cc-deps
  37. name: Fix missing C++ deps
  38. entry: scripts/fix_cc_deps.py
  39. language: python
  40. files: ^.*/(BUILD|[^/]+\.(h|cpp))$
  41. pass_filenames: false
  42. # Formatters should be run late so that they can re-format any prior changes.
  43. - repo: https://github.com/psf/black
  44. rev: 8a737e727ac5ab2f1d4cf5876720ed276dc8dc4b # frozen: 25.1.0
  45. hooks:
  46. - id: black
  47. - repo: local
  48. hooks:
  49. - id: prettier
  50. name: prettier
  51. language: node
  52. additional_dependencies: ['prettier@3.3.3']
  53. types_or: [html, javascript, json, markdown, yaml]
  54. entry: npx prettier --write --log-level=warn
  55. - repo: local
  56. hooks:
  57. - id: buildifier
  58. name: Bazel buildifier
  59. entry: scripts/run_buildifier.py
  60. # Beyond just formatting, explicitly fix lint warnings.
  61. args: ['--lint=fix', '--warnings=all', '-r', '.']
  62. language: python
  63. files: |
  64. (?x)^(
  65. .*BUILD.*|
  66. .*MODULE.bazel.*|
  67. .*WORKSPACE.*|
  68. .*\.bzl
  69. )$
  70. - id: check-bazel-mod-deps
  71. # Check this after buildifier because buildifier may modify inputs, and
  72. # MODULE.bazel.lock includes line/column details.
  73. name: Check bazel mod deps
  74. entry: scripts/bazel_mod_deps.py
  75. language: python
  76. files: |
  77. (?x)^(
  78. .*MODULE.bazel.*|
  79. .*WORKSPACE.*
  80. )$
  81. - id: clang-format
  82. name: clang-format
  83. entry: clang-format
  84. types_or: [c++, def]
  85. language: python
  86. args: ['-i']
  87. additional_dependencies: ['clang-format==17.0.1']
  88. - id: explorer-format-grammar
  89. name: Format the explorer grammar file
  90. entry: explorer/syntax/format_grammar.py
  91. language: python
  92. files: ^explorer/syntax/(lexer.lpp|parser.ypp)$
  93. pass_filenames: false
  94. additional_dependencies: ['clang-format==17.0.1']
  95. - repo: local
  96. hooks:
  97. - id: check-header-guards
  98. # This should run after clang-format, which may reformat a guard.
  99. name: Check header guards
  100. entry: scripts/check_header_guards.py
  101. language: python
  102. files: ^.*\.h$
  103. - id: check-sha-filenames
  104. # This may rename files, so it's deliberately between formatters and
  105. # linters.
  106. name: Check fuzzer SHA filenames
  107. entry: scripts/check_sha_filenames.py
  108. language: python
  109. files: ^.*/fuzzer_corpus/.*$
  110. - id: check-toolchain-diagnostics
  111. name: Check toolchain diagnostics
  112. entry: toolchain/diagnostics/check_diagnostics.py
  113. language: python
  114. files: |
  115. (?x)^(
  116. toolchain/.*\.cpp|
  117. toolchain/.*\.h|
  118. toolchain/diagnostics/check_diagnostics\.py|
  119. toolchain/diagnostics/diagnostic_kind\.def
  120. )$
  121. pass_filenames: false
  122. # Run linters last, as formatters and other checks may fix issues.
  123. - repo: local
  124. hooks:
  125. - id: forbid-llvm-googletest
  126. name: Checks for deps on LLVM's version of GoogleTest
  127. entry: scripts/forbid_llvm_googletest.py
  128. language: python
  129. files: ^.*/BUILD$
  130. pass_filenames: false
  131. - repo: https://github.com/PyCQA/flake8
  132. rev: bddd87797f8dfc07d2a10c894776018d9bec590b # frozen: 7.1.2
  133. hooks:
  134. - id: flake8
  135. - repo: https://github.com/pre-commit/mirrors-mypy
  136. rev: 'f40886d54c729f533f864ed6ce584e920feb0af7' # frozen: v1.15.0
  137. hooks:
  138. - id: mypy
  139. # Use setup.cfg to match the command line.
  140. args:
  141. - --config-file=setup.cfg
  142. # This should match the requirements added in the WORKSPACE pip_install.
  143. additional_dependencies:
  144. - gql >= 2.0.0, < 3.0.0
  145. - PyGitHub
  146. # Exclusions are:
  147. # - p#### scripts because they're not tested or maintained.
  148. # - lit.cfg.py because it has multiple copies, breaking mypy.
  149. # - Unit tests because they sometimes violate typing, such as by
  150. # assigning a mock to a function.
  151. exclude: |
  152. (?x)^(
  153. proposals/(?!scripts/).*|
  154. .*/lit\.cfg\.py|
  155. .*_test\.py
  156. )$
  157. - repo: https://github.com/codespell-project/codespell
  158. rev: 63c8f8312b7559622c0d82815639671ae42132ac # frozen: v2.4.1
  159. hooks:
  160. - id: codespell
  161. args: ['-I', '.codespell_ignore', '--uri-ignore-words-list', '*']
  162. # Test data may contain intentional misspellings, as well as short,
  163. # meaningless identifiers that codespell incorrectly identifies as
  164. # typos but that we would want to detect in other contexts.
  165. exclude: |
  166. (?x)^(
  167. .*/testdata/.*|
  168. .*/fuzzer_corpus/.*
  169. )$
  170. - repo: https://github.com/google/pre-commit-tool-hooks
  171. rev: efaea7c61c774c0b1a9805fd999e754a2d19dbd1 # frozen: v1.2.5
  172. hooks:
  173. - id: check-copyright
  174. args:
  175. - --copyright
  176. - |+
  177. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  178. Exceptions. See /LICENSE for license information.
  179. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  180. - --custom_format
  181. - '\.(carbon|c|json|scss|ypp)(\.tmpl)?$'
  182. - ''
  183. - '// '
  184. - ''
  185. - --custom_format
  186. - '\.(js|ts|mjs)$'
  187. - '/*'
  188. - ' * '
  189. - ' */'
  190. - --custom_format
  191. - '\.(l|lpp|y)$'
  192. - '/*'
  193. - ''
  194. - '*/'
  195. - --custom_format
  196. - '\.(plist)$'
  197. - '<!--'
  198. - ''
  199. - '\-->'
  200. - --custom_format
  201. - '\.vim$'
  202. - ''
  203. - '" '
  204. - ''
  205. - --custom_format
  206. - '\.scm$'
  207. - ''
  208. - '; '
  209. - ''
  210. - --custom_format
  211. - '\.lua$'
  212. - ''
  213. - '-- '
  214. - ''
  215. exclude: |
  216. (?x)^(
  217. .bazelversion|
  218. .github/pull_request_template.md|
  219. .python-version|
  220. compile_flags.txt|
  221. github_tools/requirements.txt|
  222. third_party/.*|
  223. utils/vscode/esbuild.js|
  224. website/.ruby-version|
  225. website/Gemfile.lock|
  226. .*\.def|
  227. .*\.png|
  228. .*\.svg|
  229. .*/fuzzer_corpus/.*|
  230. .*/testdata/.*\.golden
  231. )$
  232. - id: check-links
  233. # This excludes third-party code, and patches to third-party code.
  234. exclude: |
  235. (?x)^(
  236. MODULE.bazel.lock|
  237. bazel/bazel_clang_tidy/.*\.patch|
  238. bazel/google_benchmark/.*\.patch|
  239. bazel/libpfm/.*\.patch|
  240. bazel/llvm_project/.*\.patch|
  241. third_party/examples/.*/carbon/.*|
  242. )$