Skip to content

Commit

Permalink
Merge pull request #1707 from krantheman/update-formatting
Browse files Browse the repository at this point in the history
ci: use prettier for js formatting and ruff for py formatting/linting
  • Loading branch information
ruchamahabal committed May 1, 2024
2 parents f3961b7 + f779f00 commit ff4583e
Show file tree
Hide file tree
Showing 304 changed files with 4,608 additions and 4,575 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Root editor config file
root = true

# Common settings
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

# js indentation settings
[{*.js,*.vue,*.css,*.scss,*.html}]
indent_style = tab
indent_size = 4
max_line_length = 99
37 changes: 0 additions & 37 deletions .flake8

This file was deleted.

8 changes: 7 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ b55d6e27af6bd274dfa47e66a3012ddec68ce798
f37f15b2b5329e3b0b35891e1c4fd82f48562c6d

# bulk formatting PWA frontend code
920daa1a3ddccaefaf7b9348f850831d6e0a0e6b
920daa1a3ddccaefaf7b9348f850831d6e0a0e6b

# js, scss prettier formatting
29a18f44ed7e1f1c183a6d180bb76cdb3ba484f1

# python ruff formatting
16c75305e5fd25cb4a25d6232a7ad6a8814be11d
73 changes: 0 additions & 73 deletions .github/helper/.flake8_strict

This file was deleted.

11 changes: 4 additions & 7 deletions .github/helper/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ def uri_validator(x):
result = urlparse(x)
return all([result.scheme, result.netloc, result.path])


def docs_link_exists(body):
for line in body.splitlines():
for word in line.split():
if word.startswith('http') and uri_validator(word):
if word.startswith("http") and uri_validator(word):
parsed_url = urlparse(word)
if parsed_url.netloc == "github.com":
parts = parsed_url.path.split('/')
parts = parsed_url.path.split("/")
if len(parts) == 5 and parts[1] == "frappe" and parts[2] == "hrms":
return True
elif parsed_url.netloc == "frappehr.com":
Expand All @@ -30,11 +31,7 @@ def docs_link_exists(body):
head_sha = (payload.get("head") or {}).get("sha")
body = (payload.get("body") or "").lower()

if (title.startswith("feat")
and head_sha
and "no-docs" not in body
and "backport" not in body
):
if title.startswith("feat") and head_sha and "no-docs" not in body and "backport" not in body:
if docs_link_exists(body):
print("Documentation Link Found. You're Awesome! 🎉")

Expand Down
34 changes: 21 additions & 13 deletions .github/helper/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
import sys

errors_encounter = 0
pattern = re.compile(r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,(\s*?.*?\n*?)*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)")
pattern = re.compile(
r"_\(([\"']{,3})(?P<message>((?!\1).)*)\1(\s*,\s*context\s*=\s*([\"'])(?P<py_context>((?!\5).)*)\5)*(\s*,(\s*?.*?\n*?)*(,\s*([\"'])(?P<js_context>((?!\11).)*)\11)*)*\)"
)
words_pattern = re.compile(r"_{1,2}\([\"'`]{1,3}.*?[a-zA-Z]")
start_pattern = re.compile(r"_{1,2}\([f\"'`]{1,3}")
f_string_pattern = re.compile(r"_\(f[\"']")
starts_with_f_pattern = re.compile(r"_\(f")

# skip first argument
files = sys.argv[1:]
files_to_scan = [_file for _file in files if _file.endswith(('.py', '.js'))]
files_to_scan = [_file for _file in files if _file.endswith((".py", ".js"))]

for _file in files_to_scan:
with open(_file, 'r') as f:
print(f'Checking: {_file}')
with open(_file, "r") as f:
print(f"Checking: {_file}")
file_lines = f.readlines()
for line_number, line in enumerate(file_lines, 1):
if 'frappe-lint: disable-translate' in line:
if "frappe-lint: disable-translate" in line:
continue

start_matches = start_pattern.search(line)
Expand All @@ -28,33 +30,39 @@
has_f_string = f_string_pattern.search(line)
if has_f_string:
errors_encounter += 1
print(f'\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}')
print(
f"\nF-strings are not supported for translations at line number {line_number}\n{line.strip()[:100]}"
)
continue
else:
continue

match = pattern.search(line)
error_found = False

if not match and line.endswith((',\n', '[\n')):
if not match and line.endswith((",\n", "[\n")):
# concat remaining text to validate multiline pattern
line = "".join(file_lines[line_number - 1:])
line = line[start_matches.start() + 1:]
line = "".join(file_lines[line_number - 1 :])
line = line[start_matches.start() + 1 :]
match = pattern.match(line)

if not match:
error_found = True
print(f'\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}')
print(f"\nTranslation syntax error at line number {line_number}\n{line.strip()[:100]}")

if not error_found and not words_pattern.search(line):
error_found = True
print(f'\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}')
print(
f"\nTranslation is useless because it has no words at line number {line_number}\n{line.strip()[:100]}"
)

if error_found:
errors_encounter += 1

if errors_encounter > 0:
print('\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.')
print(
'\nVisit "https://frappeframework.com/docs/user/en/translations" to learn about valid translation strings.'
)
sys.exit(1)
else:
print('\nGood To Go!')
print("\nGood To Go!")
39 changes: 22 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@ repos:
- id: check-merge-conflict
- id: check-ast

- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: flake8
additional_dependencies: [
'flake8-bugbear',
]
args: ['--config', '.github/helper/.flake8_strict']
exclude: ".*setup.py$"
- id: prettier
types_or: [javascript, vue, css, scss]
# Ignore fronetend folder and any files that might contain jinja / bundles
exclude: |
(?x)^(
frontend/.*|
hrms/public/dist/.*|
.*node_modules.*|
.*boilerplate.*|
hrms/templates/includes/.*|
hrms/hr/doctype/employee_promotion/employee_promotion.js|
hrms/hr/doctype/employee_transfer/employee_transfer.js|
)$
- repo: https://github.com/adityahase/black
rev: 9cb0a69f4d0030cdf687eddf314468b39ed54119
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.7
hooks:
- id: black
additional_dependencies: ['click==8.0.4']
- id: ruff
name: "Run ruff linter and apply fixes"
args: ["--fix"]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
exclude: ".*setup.py$"
- id: ruff-format
name: "Format Python code"

ci:
autoupdate_schedule: weekly
Expand Down
36 changes: 18 additions & 18 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module.exports = {
parserPreset: 'conventional-changelog-conventionalcommits',
parserPreset: "conventional-changelog-conventionalcommits",
rules: {
'subject-empty': [2, 'never'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'type-enum': [
"subject-empty": [2, "never"],
"type-case": [2, "always", "lower-case"],
"type-empty": [2, "never"],
"type-enum": [
2,
'always',
"always",
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
'patch',
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
"patch",
],
],
},
Expand Down
Loading

0 comments on commit ff4583e

Please sign in to comment.