Skip to content

Commit 8707352

Browse files
committed
Add formate hook
1 parent 302186b commit 8707352

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323
"Programming Language :: Python :: Implementation :: CPython",
2424
"Programming Language :: Rust",
2525
]
26-
dependencies = []
26+
dependencies = [ "formate>=1.1.2",]
2727
dynamic = []
2828

2929
[project.license]
@@ -38,6 +38,9 @@ Homepage = "https://github.com/python-formate/formate-js"
3838
"Issue Tracker" = "https://github.com/python-formate/formate-js/issues"
3939
"Source Code" = "https://github.com/python-formate/formate-js"
4040

41+
[project.entry-points.formate_hooks]
42+
formate_js = "formate_js:formate_js_hook"
43+
4144
[tool.whey]
4245
base-classifiers = [ "Programming Language :: Rust",]
4346
python-versions = [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13",]

python/formate_js/__init__.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
1+
#!/usr/bin/env python3
2+
#
3+
# __init__.py
4+
"""
5+
Formate plugin for reformatting JavaScript and TypeScript files with dprint.
6+
"""
7+
#
8+
# Copyright © 2026 Dominic Davis-Foster <dominic@davis-foster.co.uk>
9+
#
10+
# Permission is hereby granted, free of charge, to any person obtaining a copy
11+
# of this software and associated documentation files (the "Software"), to deal
12+
# in the Software without restriction, including without limitation the rights
13+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
# copies of the Software, and to permit persons to whom the Software is
15+
# furnished to do so, subject to the following conditions:
16+
#
17+
# The above copyright notice and this permission notice shall be included in all
18+
# copies or substantial portions of the Software.
19+
#
20+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24+
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
26+
# OR OTHER DEALINGS IN THE SOFTWARE.
27+
#
28+
29+
# 3rd party
30+
from domdf_python_tools.paths import PathPlus
31+
from domdf_python_tools.typing import PathLike
32+
from formate.config import wants_filename
33+
134
# this package
2-
from ._formate_js import *
35+
from ._formate_js import Configuration, ConfigurationBuilder, FormatTextOptions, format_text
36+
37+
__all__ = ["javascript_hook", "Configuration", "ConfigurationBuilder"]
38+
39+
40+
@wants_filename
41+
def javascript_hook(
42+
source: str,
43+
formate_filename: PathLike,
44+
**kwargs,
45+
) -> str:
46+
r"""
47+
Reformat JavaScript and TypeScript with dprint.
48+
49+
:param source: The source to reformat.
50+
:param \*\*kwargs:
51+
52+
:returns: The reformatted source.
53+
"""
54+
55+
config = Configuration(**kwargs)
56+
57+
filename_p = PathPlus(formate_filename)
58+
59+
options = FormatTextOptions(filename_p.as_posix(), filename_p.suffix, source, config)
60+
61+
result = format_text(options)
62+
63+
if result is None:
64+
# No change
65+
return source
366

4-
__doc__ = _formate_js.__doc__
5-
if hasattr(_formate_js, "__all__"):
6-
__all__ = _formate_js.__all__
67+
return result

repo_helper.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ exclude_files:
3434

3535
tox_unmanaged:
3636
- testenv:build
37+
38+
39+
entry_points:
40+
formate_hooks:
41+
- "formate_js=formate_js:formate_js_hook"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
formate>=1.1.2

0 commit comments

Comments
 (0)