requirement: TypeScript package should be installed
Step 1: Create a new plugin (Tools > Developer > new plugin) with the following, save it as format-on-save.py
for example:
import sublime
import sublime_plugin
class FormatTypescriptOnSave(sublime_plugin.EventListener):
def on_pre_save(self, view):
if "TypeScript" in view.settings().get("syntax") or "JavaScript" in view.settings().get("syntax"):
view.run_command("typescript_format_document")
source: https://stackoverflow.com/a/42237769/3183756
Step 2: Customize additional formatting settings in ~/.config/sublime-text-3/Packages/TypeScript/typescript/libs/editor_client.py
, here I added "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False
def set_features(self):
host_info = "Sublime Text version " + str(sublime.version())
# Preferences Settings
format_options = {
"tabSize": self.tab_size,
"indentSize": self.indent_size,
"convertTabsToSpaces": self.translate_tab_to_spaces,
"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False,
"insertSpaceAfterFunctionKeywordForAnonymousFunctions": True, # False by default on SublimeText TypeScript
}
self.service.configure(host_info, None, format_options)