tsfmt 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -0
- package/bin/ci_build.sh +50 -0
- package/bin/cli.js +20 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# tsfmt
|
|
2
|
+
|
|
3
|
+
**Opinionated TypeScript and JavaScript code formatter with AST-based transformations**
|
|
4
|
+
|
|
5
|
+
An advanced code formatting tool that goes beyond traditional pretty-printing to enforce structural consistency across TypeScript and JavaScript codebases. Built by
|
|
6
|
+
Encore Digital Group, tsfmt combines configurable formatting rules with intelligent AST analysis to automatically organize imports, sort class members, arrange file
|
|
7
|
+
declarations, and apply consistent code style patterns.
|
|
8
|
+
|
|
9
|
+
## What tsfmt Does
|
|
10
|
+
|
|
11
|
+
tsfmt is a comprehensive code formatter that operates on multiple levels:
|
|
12
|
+
|
|
13
|
+
**AST-Based Sorting & Organization**
|
|
14
|
+
|
|
15
|
+
- Intelligently sorts class members (properties, constructors, methods, accessors) with dependency awareness
|
|
16
|
+
- Organizes file-level declarations (interfaces, types, enums, functions, classes) in logical order
|
|
17
|
+
- Handles React component lifecycle methods with specialized sorting rules
|
|
18
|
+
- Respects code dependencies to prevent breaking changes during reorganization
|
|
19
|
+
|
|
20
|
+
**Import Management**
|
|
21
|
+
|
|
22
|
+
- Automatically organizes and groups imports (external, internal, relative)
|
|
23
|
+
- Removes unused imports while preserving side-effect imports
|
|
24
|
+
- Sorts import statements alphabetically within groups
|
|
25
|
+
- Configurable import grouping and separation
|
|
26
|
+
|
|
27
|
+
**Code Style Formatting**
|
|
28
|
+
|
|
29
|
+
- Enforces consistent quote styles, semicolon usage, and bracket spacing
|
|
30
|
+
- Manages indentation (spaces vs tabs) and line width constraints
|
|
31
|
+
- Controls trailing comma placement and arrow function parentheses
|
|
32
|
+
- Applies spacing rules for blank lines between declarations and before returns
|
|
33
|
+
|
|
34
|
+
**Configuration File Formatting**
|
|
35
|
+
|
|
36
|
+
- Sorts `package.json` fields according to company standards
|
|
37
|
+
- Alphabetically sorts all keys in `tsconfig.json` files
|
|
38
|
+
- Maintains consistent JSON indentation and structure
|
|
39
|
+
|
|
40
|
+
## Core Formatting Opinions
|
|
41
|
+
|
|
42
|
+
tsfmt enforces these opinionated defaults designed for enterprise-grade codebases:
|
|
43
|
+
|
|
44
|
+
**Code Style Standards**
|
|
45
|
+
|
|
46
|
+
- Double quotes for all string literals
|
|
47
|
+
- Semicolons always required
|
|
48
|
+
- No bracket spacing in object literals (`{key: value}` not `{ key: value }`)
|
|
49
|
+
- 4-space indentation (no tabs)
|
|
50
|
+
- 120-character line width limit
|
|
51
|
+
- Trailing commas everywhere possible
|
|
52
|
+
- Arrow function parentheses omitted when possible (`x => x` not `(x) => x`)
|
|
53
|
+
|
|
54
|
+
**Structural Organization**
|
|
55
|
+
|
|
56
|
+
- Class members ordered by type: static properties, instance properties, constructor, accessors, static methods, instance methods
|
|
57
|
+
- File declarations ordered by importance: interfaces, types, enums, helper functions, exported functions, classes, default exports
|
|
58
|
+
- Import groups separated by origin: external packages, internal modules, relative imports
|
|
59
|
+
- Blank lines enforced between different declaration types and before return statements
|
|
60
|
+
|
|
61
|
+
**Package & Config Files**
|
|
62
|
+
|
|
63
|
+
- `package.json` fields ordered by company standard: name, type, author, version, description, publishConfig, keywords, homepage, engines, dependencies, devDependencies,
|
|
64
|
+
scripts, types, main, module, exports, files, repository, bugs
|
|
65
|
+
- `tsconfig.json` keys sorted alphabetically at all nesting levels
|
|
66
|
+
- Consistent 4-space JSON indentation throughout
|
|
67
|
+
|
|
68
|
+
## Architecture
|
|
69
|
+
|
|
70
|
+
tsfmt uses a sophisticated pipeline-based architecture:
|
|
71
|
+
|
|
72
|
+
**Formatter Pipeline**
|
|
73
|
+
|
|
74
|
+
- Executes formatters in a specific order: CodeStyle → ImportOrganization → ASTTransformation → Spacing
|
|
75
|
+
- Each formatter can be independently enabled/disabled
|
|
76
|
+
- Pipeline maintains context and tracks changes across transformations
|
|
77
|
+
|
|
78
|
+
**AST Analysis Engine**
|
|
79
|
+
|
|
80
|
+
- Built on TypeScript compiler API for accurate parsing
|
|
81
|
+
- Dependency resolution prevents breaking member/declaration relationships
|
|
82
|
+
- Handles complex scenarios like method dependencies and forward references
|
|
83
|
+
|
|
84
|
+
**Configuration System**
|
|
85
|
+
|
|
86
|
+
- Zero-configuration by default with sensible opinions
|
|
87
|
+
- Optional `core.config.ts` file for project-specific customization
|
|
88
|
+
- Deep merging of user configuration with defaults
|
|
89
|
+
|
|
90
|
+
## Key Features
|
|
91
|
+
|
|
92
|
+
- **Dependency-Aware Sorting**: Analyzes code relationships to prevent breaking changes during reorganization
|
|
93
|
+
- **React-Specific Rules**: Specialized handling for React component lifecycle methods and patterns
|
|
94
|
+
- **Configurable Pipeline**: Modular formatter system allows granular control over formatting operations
|
|
95
|
+
- **Incremental Processing**: Only modifies files that need changes, preserving unchanged content
|
|
96
|
+
- **TypeScript-Native**: Built on TypeScript compiler API for maximum compatibility and accuracy
|
|
97
|
+
- **Enterprise-Ready**: Designed for large codebases with consistent, non-negotiable formatting standards
|
|
98
|
+
|
|
99
|
+
## Philosophy
|
|
100
|
+
|
|
101
|
+
tsfmt is built on the principle that code formatting should not just make code look consistent, but should also impose logical structure that improves maintainability. By
|
|
102
|
+
combining traditional pretty-printing with intelligent AST transformations, tsfmt ensures that codebases follow not just visual consistency, but also structural patterns
|
|
103
|
+
that make code easier to navigate, understand, and modify.
|
|
104
|
+
|
|
105
|
+
The tool is intentionally opinionated to eliminate formatting debates and establish company-wide standards that prioritize readability, consistency, and maintainability
|
|
106
|
+
over individual preferences.
|
package/bin/ci_build.sh
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2025. Encore Digital Group.
|
|
4
|
+
# All Rights Reserved.
|
|
5
|
+
#
|
|
6
|
+
|
|
7
|
+
git config --global user.name "EncoreBot"
|
|
8
|
+
git config --global user.email "ghbot@encoredigitalgroup.com"
|
|
9
|
+
|
|
10
|
+
# Change to workspace directory
|
|
11
|
+
cd "$GITHUB_WORKSPACE" || {
|
|
12
|
+
echo "Error: Failed to change directory to $GITHUB_WORKSPACE"
|
|
13
|
+
exit 1
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
# Run build and capture exit code
|
|
17
|
+
npm run format
|
|
18
|
+
BUILD_EXIT_CODE=$?
|
|
19
|
+
|
|
20
|
+
# If build failed, exit with the same code
|
|
21
|
+
if [ $BUILD_EXIT_CODE -ne 0 ]; then
|
|
22
|
+
echo "Build failed with exit code $BUILD_EXIT_CODE"
|
|
23
|
+
exit $BUILD_EXIT_CODE
|
|
24
|
+
fi
|
|
25
|
+
|
|
26
|
+
# Run format and capture exit code
|
|
27
|
+
npm run format
|
|
28
|
+
FORMAT_EXIT_CODE=$?
|
|
29
|
+
|
|
30
|
+
# If format failed, exit with the same code
|
|
31
|
+
if [ $FORMAT_EXIT_CODE -ne 0 ]; then
|
|
32
|
+
echo "Format failed with exit code $FORMAT_EXIT_CODE"
|
|
33
|
+
exit $FORMAT_EXIT_CODE
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
# Build and format succeeded, check for changes
|
|
37
|
+
if [ -z "$(git status --porcelain)" ]; then
|
|
38
|
+
# Working directory clean
|
|
39
|
+
echo "Working Tree is Clean! Nothing to commit."
|
|
40
|
+
else
|
|
41
|
+
# Add all changes to staging
|
|
42
|
+
git add .
|
|
43
|
+
|
|
44
|
+
# Commit changes
|
|
45
|
+
commit_message="Apply Formatting"
|
|
46
|
+
git commit -m "$commit_message"
|
|
47
|
+
|
|
48
|
+
# Push changes to origin
|
|
49
|
+
git push origin --force
|
|
50
|
+
fi
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2026. Encore Digital Group.
|
|
4
|
+
* All Rights Reserved.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// This is the entry point for consumers of the package
|
|
8
|
+
// It uses tsx to run the TypeScript source directly without requiring a build step
|
|
9
|
+
|
|
10
|
+
const { register } = require('tsx/cjs/api');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
|
|
13
|
+
// Register tsx to handle TypeScript files
|
|
14
|
+
const unregister = register();
|
|
15
|
+
|
|
16
|
+
// Load and run the CLI
|
|
17
|
+
require(path.join(__dirname, '../src/cli.ts'));
|
|
18
|
+
|
|
19
|
+
// Cleanup
|
|
20
|
+
process.on('exit', () => unregister());
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tsfmt",
|
|
3
|
+
"author": "Encore Digital Group",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "An opinionated TypeScript code formatter",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public",
|
|
8
|
+
"registry": "https://registry.npmjs.org"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=20.0.0"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"glob": "^13.0.0",
|
|
15
|
+
"json-sort-cli": "^4.0.9",
|
|
16
|
+
"sort-package-json": "^3.0.0",
|
|
17
|
+
"tsx": "^4.21.0",
|
|
18
|
+
"typescript": "^5.0.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/jest": "^30.0.0",
|
|
22
|
+
"@types/node": "^25.0.3",
|
|
23
|
+
"jest": "^30.1.1",
|
|
24
|
+
"ts-jest": "^29.0.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"format": "node bin/cli.js",
|
|
28
|
+
"test": "jest"
|
|
29
|
+
},
|
|
30
|
+
"main": "bin/cli.js",
|
|
31
|
+
"files": [
|
|
32
|
+
"bin",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/EncoreDigitalGroup/tsfmt.git"
|
|
38
|
+
},
|
|
39
|
+
"license": "BSD-3-Clause",
|
|
40
|
+
"bin": {
|
|
41
|
+
"tsfmt": "./bin/cli.js"
|
|
42
|
+
}
|
|
43
|
+
}
|