|
| 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 | + |
1 | 34 | # 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 |
3 | 66 |
|
4 | | -__doc__ = _formate_js.__doc__ |
5 | | -if hasattr(_formate_js, "__all__"): |
6 | | - __all__ = _formate_js.__all__ |
| 67 | + return result |
0 commit comments