codemod 1.0.9 → 1.0.10
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 +237 -13
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -80,12 +80,12 @@ The init command will prompt you with several questions to customize your projec
|
|
|
80
80
|
1. **Project Type Selection:**
|
|
81
81
|
```
|
|
82
82
|
? What type of workflow would you like to create?
|
|
83
|
-
❯
|
|
84
|
-
|
|
83
|
+
❯ ast-grep with JavaScript/TypeScript rules
|
|
84
|
+
ast-grep with YAML rules
|
|
85
85
|
Blank workflow (custom commands)
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
2. **Language Selection** (if
|
|
88
|
+
2. **Language Selection** (if ast-grep is chosen):
|
|
89
89
|
```
|
|
90
90
|
? Which language would you like to target?
|
|
91
91
|
❯ JavaScript/TypeScript
|
|
@@ -108,7 +108,7 @@ The init command will prompt you with several questions to customize your projec
|
|
|
108
108
|
|
|
109
109
|
Depending on your selections, the init command creates different project templates:
|
|
110
110
|
|
|
111
|
-
####
|
|
111
|
+
#### ast-grep JavaScript Project
|
|
112
112
|
```
|
|
113
113
|
my-codemod/
|
|
114
114
|
├── workflow.yaml # Main workflow definition
|
|
@@ -123,7 +123,7 @@ my-codemod/
|
|
|
123
123
|
└── README.md # Project documentation
|
|
124
124
|
```
|
|
125
125
|
|
|
126
|
-
####
|
|
126
|
+
#### ast-grep YAML Project
|
|
127
127
|
```
|
|
128
128
|
my-codemod/
|
|
129
129
|
├── workflow.yaml # Main workflow definition
|
|
@@ -149,9 +149,9 @@ my-workflow/
|
|
|
149
149
|
**Example Usage:**
|
|
150
150
|
|
|
151
151
|
```bash
|
|
152
|
-
# Create a new
|
|
152
|
+
# Create a new ast-grep JavaScript project
|
|
153
153
|
$ codemod init my-js-codemod
|
|
154
|
-
? What type of workflow would you like to create?
|
|
154
|
+
? What type of workflow would you like to create? ast-grep with JavaScript/TypeScript rules
|
|
155
155
|
? Which language would you like to target? JavaScript/TypeScript
|
|
156
156
|
? Project name: my-js-codemod
|
|
157
157
|
? Description: Migrate from React class components to hooks
|
|
@@ -642,9 +642,9 @@ For detailed help on any command, use:
|
|
|
642
642
|
codemod <command> --help
|
|
643
643
|
```
|
|
644
644
|
|
|
645
|
-
##
|
|
645
|
+
## jssg (JavaScript ast-grep)
|
|
646
646
|
|
|
647
|
-
|
|
647
|
+
jssg is a JavaScript/TypeScript codemod runner and testing framework inspired by [ast-grep](https://ast-grep.github.io/). It enables you to write codemods in JavaScript and apply them to codebases with powerful CLI and test automation support.
|
|
648
648
|
|
|
649
649
|
### Running a Codemod
|
|
650
650
|
|
|
@@ -672,11 +672,11 @@ codemod jssg run my-codemod.js ./src --language javascript --dry-run
|
|
|
672
672
|
|
|
673
673
|
---
|
|
674
674
|
|
|
675
|
-
#
|
|
675
|
+
# jssg Testing Framework Usage Guide
|
|
676
676
|
|
|
677
677
|
## Overview
|
|
678
678
|
|
|
679
|
-
The
|
|
679
|
+
The jssg Testing Framework provides comprehensive testing capabilities for JavaScript codemods with before/after fixture files. It integrates with the existing ExecutionEngine and provides a familiar test runner interface.
|
|
680
680
|
|
|
681
681
|
## Quick Start
|
|
682
682
|
|
|
@@ -704,7 +704,7 @@ tests/
|
|
|
704
704
|
### 2. Running Tests
|
|
705
705
|
|
|
706
706
|
```bash
|
|
707
|
-
# Basic test run
|
|
707
|
+
# Basic test run (language can be specified in config file)
|
|
708
708
|
codemod jssg test my-codemod.js --language javascript
|
|
709
709
|
|
|
710
710
|
# With custom test directory
|
|
@@ -718,13 +718,18 @@ codemod jssg test my-codemod.js --language javascript --verbose
|
|
|
718
718
|
|
|
719
719
|
# Watch mode (re-run tests on file changes)
|
|
720
720
|
codemod jssg test my-codemod.js --language javascript --watch
|
|
721
|
+
|
|
722
|
+
# Using configuration files (no CLI args needed)
|
|
723
|
+
codemod jssg test my-codemod.js
|
|
721
724
|
```
|
|
722
725
|
|
|
723
726
|
## CLI Options
|
|
724
727
|
|
|
725
728
|
### Required Arguments
|
|
726
729
|
- `codemod_file`: Path to the codemod JavaScript file
|
|
727
|
-
|
|
730
|
+
|
|
731
|
+
### Optional Arguments
|
|
732
|
+
- `--language`: Target language (javascript, typescript, etc.) - can be specified in config file
|
|
728
733
|
|
|
729
734
|
### Test Discovery
|
|
730
735
|
- `--test-directory`: Test directory (default: "tests")
|
|
@@ -944,3 +949,222 @@ codemod jssg test my-codemod.js --language javascript --verbose --sequential
|
|
|
944
949
|
```
|
|
945
950
|
|
|
946
951
|
This framework provides a robust foundation for testing JavaScript codemods with familiar tooling and comprehensive features.
|
|
952
|
+
|
|
953
|
+
## Configuration System
|
|
954
|
+
|
|
955
|
+
jssg supports a hierarchical configuration system using configuration files, allowing you to specify test settings globally and override them per test case. Multiple file formats and aliases are supported.
|
|
956
|
+
|
|
957
|
+
### Configuration Files
|
|
958
|
+
|
|
959
|
+
#### Supported Formats and Names
|
|
960
|
+
|
|
961
|
+
Configuration files are searched in the following order of precedence (first found wins):
|
|
962
|
+
|
|
963
|
+
1. `test.config.json` - JSON format
|
|
964
|
+
2. `test.config.yaml` - YAML format
|
|
965
|
+
3. `codemod-test.config.json` - JSON format with codemod prefix
|
|
966
|
+
4. `codemod-test.config.yaml` - YAML format with codemod prefix
|
|
967
|
+
|
|
968
|
+
#### Global Configuration
|
|
969
|
+
|
|
970
|
+
Create a configuration file in your project root to set default configurations:
|
|
971
|
+
|
|
972
|
+
**JSON format (`test.config.json`):**
|
|
973
|
+
```json
|
|
974
|
+
{
|
|
975
|
+
"language": "typescript",
|
|
976
|
+
"timeout": 30,
|
|
977
|
+
"ignore_whitespace": false
|
|
978
|
+
}
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
**YAML format (`test.config.yaml`):**
|
|
982
|
+
```yaml
|
|
983
|
+
language: typescript
|
|
984
|
+
timeout: 30
|
|
985
|
+
ignore_whitespace: false
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
#### Test Case Configuration
|
|
989
|
+
|
|
990
|
+
Create configuration files in individual test case directories to override global settings:
|
|
991
|
+
|
|
992
|
+
```
|
|
993
|
+
tests/
|
|
994
|
+
├── test.config.yaml # Global config (YAML)
|
|
995
|
+
├── simple-transform/
|
|
996
|
+
│ ├── input.js
|
|
997
|
+
│ ├── expected.js
|
|
998
|
+
│ └── test.config.json # Test-specific config (JSON)
|
|
999
|
+
└── complex-case/
|
|
1000
|
+
├── input.ts
|
|
1001
|
+
├── expected.ts
|
|
1002
|
+
└── codemod-test.config.yaml # Different test-specific config (YAML with prefix)
|
|
1003
|
+
```
|
|
1004
|
+
|
|
1005
|
+
**Example test-specific configs:**
|
|
1006
|
+
|
|
1007
|
+
JSON format (`test.config.json`):
|
|
1008
|
+
```json
|
|
1009
|
+
{
|
|
1010
|
+
"language": "javascript",
|
|
1011
|
+
"expect_errors": ["syntax-error"],
|
|
1012
|
+
"timeout": 60
|
|
1013
|
+
}
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
YAML format (`test.config.yaml`):
|
|
1017
|
+
```yaml
|
|
1018
|
+
language: javascript
|
|
1019
|
+
expect_errors:
|
|
1020
|
+
- syntax-error
|
|
1021
|
+
timeout: 60
|
|
1022
|
+
```
|
|
1023
|
+
|
|
1024
|
+
### Configuration Schema
|
|
1025
|
+
|
|
1026
|
+
Configuration options that can be specified in configuration files:
|
|
1027
|
+
|
|
1028
|
+
**Per-Test Configurable Options:**
|
|
1029
|
+
| Option | Type | Description |
|
|
1030
|
+
|--------|------|-------------|
|
|
1031
|
+
| `language` | string | Target language (javascript, typescript, etc.) |
|
|
1032
|
+
| `timeout` | number | Test timeout in seconds |
|
|
1033
|
+
| `ignore_whitespace` | boolean | Ignore whitespace differences in comparisons |
|
|
1034
|
+
| `expect_errors` | string[] | Test patterns expected to produce errors |
|
|
1035
|
+
|
|
1036
|
+
**Global-Only Options (CLI arguments only):**
|
|
1037
|
+
These options control test execution behavior and cannot be configured per-test:
|
|
1038
|
+
- `test_directory` - Directory containing test fixtures
|
|
1039
|
+
- `filter` - Run only tests matching this pattern
|
|
1040
|
+
- `update_snapshots` - Update expected outputs with actual results
|
|
1041
|
+
- `verbose` - Show detailed output for each test
|
|
1042
|
+
- `sequential` - Run tests sequentially instead of in parallel
|
|
1043
|
+
- `max_threads` - Maximum number of concurrent test threads
|
|
1044
|
+
- `fail_fast` - Stop on first test failure
|
|
1045
|
+
- `watch` - Watch for file changes and re-run tests
|
|
1046
|
+
- `reporter` - Output format (console, json, terse)
|
|
1047
|
+
- `context_lines` - Number of context lines in diff output
|
|
1048
|
+
|
|
1049
|
+
### Configuration Precedence
|
|
1050
|
+
|
|
1051
|
+
Configuration is resolved differently for per-test and global options:
|
|
1052
|
+
|
|
1053
|
+
**For Per-Test Options** (language, timeout, ignore_whitespace, expect_errors):
|
|
1054
|
+
1. Default values
|
|
1055
|
+
2. Root `test.config.json` (walking up directory tree)
|
|
1056
|
+
3. Parent directory configs (inheriting from root to test case)
|
|
1057
|
+
4. Test case `test.config.json` (highest priority for per-test options)
|
|
1058
|
+
5. CLI arguments (override everything)
|
|
1059
|
+
|
|
1060
|
+
**For Global Options** (all execution behavior settings):
|
|
1061
|
+
- Only CLI arguments are considered - config files are ignored for these options
|
|
1062
|
+
|
|
1063
|
+
### Usage Examples
|
|
1064
|
+
|
|
1065
|
+
#### Basic Global Configuration
|
|
1066
|
+
|
|
1067
|
+
**Using JSON:**
|
|
1068
|
+
```bash
|
|
1069
|
+
# Create global config (only per-test options)
|
|
1070
|
+
cat > test.config.json << EOF
|
|
1071
|
+
{
|
|
1072
|
+
"language": "typescript",
|
|
1073
|
+
"timeout": 60
|
|
1074
|
+
}
|
|
1075
|
+
EOF
|
|
1076
|
+
```
|
|
1077
|
+
|
|
1078
|
+
**Using YAML:**
|
|
1079
|
+
```bash
|
|
1080
|
+
# Create global config (only per-test options)
|
|
1081
|
+
cat > test.config.yaml << EOF
|
|
1082
|
+
language: typescript
|
|
1083
|
+
timeout: 60
|
|
1084
|
+
EOF
|
|
1085
|
+
```
|
|
1086
|
+
|
|
1087
|
+
**Running tests:**
|
|
1088
|
+
```bash
|
|
1089
|
+
# Run tests (use CLI for global execution options)
|
|
1090
|
+
codemod jssg test my-codemod.js --reporter json --max-threads 2
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
#### Per-Test-Case Language Support
|
|
1094
|
+
|
|
1095
|
+
```bash
|
|
1096
|
+
# Global config (YAML)
|
|
1097
|
+
cat > test.config.yaml << EOF
|
|
1098
|
+
language: javascript
|
|
1099
|
+
EOF
|
|
1100
|
+
|
|
1101
|
+
# TypeScript-specific test (JSON)
|
|
1102
|
+
mkdir tests/typescript-test
|
|
1103
|
+
cat > tests/typescript-test/test.config.json << EOF
|
|
1104
|
+
{
|
|
1105
|
+
"language": "typescript"
|
|
1106
|
+
}
|
|
1107
|
+
EOF
|
|
1108
|
+
|
|
1109
|
+
# Another test with codemod prefix (YAML)
|
|
1110
|
+
mkdir tests/special-test
|
|
1111
|
+
cat > tests/special-test/codemod-test.config.yaml << EOF
|
|
1112
|
+
language: typescript
|
|
1113
|
+
timeout: 120
|
|
1114
|
+
expect_errors:
|
|
1115
|
+
- compile-error
|
|
1116
|
+
EOF
|
|
1117
|
+
|
|
1118
|
+
# Mixed language and format testing works automatically
|
|
1119
|
+
codemod jssg test my-codemod.js
|
|
1120
|
+
```
|
|
1121
|
+
|
|
1122
|
+
#### Development Workflow
|
|
1123
|
+
|
|
1124
|
+
```bash
|
|
1125
|
+
# Set up development-friendly defaults (per-test options only)
|
|
1126
|
+
cat > test.config.json << EOF
|
|
1127
|
+
{
|
|
1128
|
+
"language": "typescript",
|
|
1129
|
+
"timeout": 120
|
|
1130
|
+
}
|
|
1131
|
+
EOF
|
|
1132
|
+
|
|
1133
|
+
# Use CLI args for execution behavior
|
|
1134
|
+
# Development
|
|
1135
|
+
codemod jssg test my-codemod.js --verbose --fail-fast --context-lines 5
|
|
1136
|
+
|
|
1137
|
+
# CI
|
|
1138
|
+
codemod jssg test my-codemod.js --reporter json --max-threads 1 --sequential
|
|
1139
|
+
```
|
|
1140
|
+
|
|
1141
|
+
### Configuration Discovery
|
|
1142
|
+
|
|
1143
|
+
The system automatically discovers per-test configuration files by:
|
|
1144
|
+
|
|
1145
|
+
1. Starting from the current working directory
|
|
1146
|
+
2. Looking for configuration files in the supported formats and names
|
|
1147
|
+
3. Walking up the directory tree to find parent configs
|
|
1148
|
+
4. For each test case, checking for test-specific configuration files
|
|
1149
|
+
5. Merging configurations with proper inheritance (child overrides parent)
|
|
1150
|
+
6. Applying CLI argument overrides (for both global and per-test options)
|
|
1151
|
+
|
|
1152
|
+
**Configuration File Discovery Order:**
|
|
1153
|
+
At each directory level, the system checks for files in this order:
|
|
1154
|
+
1. `test.config.json`
|
|
1155
|
+
2. `test.config.yaml`
|
|
1156
|
+
3. `codemod-test.config.json`
|
|
1157
|
+
4. `codemod-test.config.yaml`
|
|
1158
|
+
|
|
1159
|
+
The first file found is used for that directory level.
|
|
1160
|
+
|
|
1161
|
+
This allows for flexible project organization where you can have:
|
|
1162
|
+
- Repository-wide defaults in the root configuration file
|
|
1163
|
+
- Module-specific configs in subdirectories (any supported format)
|
|
1164
|
+
- Test-case specific overrides in individual test folders (any supported format)
|
|
1165
|
+
|
|
1166
|
+
**Important:** Only per-test options (language, timeout, ignore_whitespace, expect_errors) are inherited from config files. Global execution options must be specified via CLI arguments.
|
|
1167
|
+
|
|
1168
|
+
**Current Limitations:**
|
|
1169
|
+
- Per-test timeout and ignore_whitespace settings are applied during codemod execution but not yet integrated with the test runner's timeout mechanism
|
|
1170
|
+
- Per-test expect_errors patterns work correctly for individual test cases
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codemod",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"postinstall": "node postinstall.js"
|
|
30
30
|
},
|
|
31
31
|
"optionalDependencies": {
|
|
32
|
-
"@codemod.com/cli-darwin-arm64": "1.0.
|
|
33
|
-
"@codemod.com/cli-darwin-x64": "1.0.
|
|
34
|
-
"@codemod.com/cli-linux-arm64-gnu": "1.0.
|
|
35
|
-
"@codemod.com/cli-linux-x64-gnu": "1.0.
|
|
36
|
-
"@codemod.com/cli-win32-x64-msvc": "1.0.
|
|
32
|
+
"@codemod.com/cli-darwin-arm64": "1.0.10",
|
|
33
|
+
"@codemod.com/cli-darwin-x64": "1.0.10",
|
|
34
|
+
"@codemod.com/cli-linux-arm64-gnu": "1.0.10",
|
|
35
|
+
"@codemod.com/cli-linux-x64-gnu": "1.0.10",
|
|
36
|
+
"@codemod.com/cli-win32-x64-msvc": "1.0.10"
|
|
37
37
|
},
|
|
38
38
|
"bin": {
|
|
39
39
|
"codemod": "codemod"
|