esupgrade 2025.1.0 → 2025.2.1

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/AGENTS.md ADDED
@@ -0,0 +1,3 @@
1
+ When writing code, you MUST ALWAYS follow the [naming-things](https://raw.githubusercontent.com/codingjoe/naming-things/refs/heads/main/README.md) guidlines.
2
+
3
+ All code must be fully tested with a 100% coverage. Unreachable code must be removed.
package/README.md CHANGED
@@ -93,6 +93,21 @@ For more information about Baseline browser support, visit [web.dev/baseline][ba
93
93
  +const message = `You have ${count} items`;
94
94
  ```
95
95
 
96
+ #### Traditional `for` loops → [`for...of` loops][mdn-for-of]
97
+
98
+ ```diff
99
+ -for (let i = 0; i < items.length; i++) {
100
+ - const item = items[i];
101
+ - console.log(item);
102
+ -}
103
+ +for (const item of items) {
104
+ + console.log(item);
105
+ +}
106
+ ```
107
+
108
+ > [!NOTE]
109
+ > Transformations are limited to loops that start at 0, increment by 1, and where the index variable is not used in the loop body.
110
+
96
111
  #### `Array.from().forEach()` → [`for...of` loops][mdn-for-of]
97
112
 
98
113
  ```diff
@@ -104,6 +119,29 @@ For more information about Baseline browser support, visit [web.dev/baseline][ba
104
119
  +}
105
120
  ```
106
121
 
122
+ #### DOM `forEach()` → [`for...of` loops][mdn-for-of]
123
+
124
+ ```diff
125
+ -document.querySelectorAll('.item').forEach(item => {
126
+ - item.classList.add('active');
127
+ -});
128
+ +for (const item of document.querySelectorAll('.item')) {
129
+ + item.classList.add('active');
130
+ +}
131
+ ```
132
+
133
+ Supports:
134
+
135
+ - `document.querySelectorAll()`
136
+ - `document.getElementsByTagName()`
137
+ - `document.getElementsByClassName()`
138
+ - `document.getElementsByName()`
139
+ - `window.frames`
140
+
141
+ > [!NOTE]
142
+ > Transformations limited to inline arrow or function expressions with block statement bodies.
143
+ > Callbacks with index parameters or expression bodies are not transformed.
144
+
107
145
  #### `Array.from()` → [Array spread [...]][mdn-spread]
108
146
 
109
147
  ```diff
@@ -136,6 +174,15 @@ For more information about Baseline browser support, visit [web.dev/baseline][ba
136
174
  +const withItem = [...array, item];
137
175
  ```
138
176
 
177
+ #### `Math.pow()` → [Exponentiation operator \*\*][mdn-exponentiation]
178
+
179
+ ```diff
180
+ -const result = Math.pow(2, 3);
181
+ -const area = Math.PI * Math.pow(radius, 2);
182
+ +const result = 2 ** 3;
183
+ +const area = Math.PI * radius ** 2;
184
+ ```
185
+
139
186
  #### Function expressions → [Arrow functions][mdn-arrow-functions]
140
187
 
141
188
  ```diff
@@ -174,6 +221,7 @@ For more information about Baseline browser support, visit [web.dev/baseline][ba
174
221
  [baseline]: https://web.dev/baseline/
175
222
  [mdn-arrow-functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
176
223
  [mdn-const]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
224
+ [mdn-exponentiation]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation
177
225
  [mdn-for-of]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
178
226
  [mdn-let]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
179
227
  [mdn-promise-try]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try
package/bin/esupgrade.js CHANGED
@@ -98,50 +98,52 @@ function processFile(filePath, options) {
98
98
 
99
99
  if (result.modified) {
100
100
  if (options.check) {
101
- console.log(`✗ ${filePath}`)
101
+ // Group changes by type for summary
102
+ const changesByType = {}
102
103
  if (result.changes && result.changes.length > 0) {
103
- // Group changes by type
104
- const changesByType = {}
105
-
106
104
  for (const change of result.changes) {
107
105
  if (!changesByType[change.type]) {
108
- changesByType[change.type] = []
106
+ changesByType[change.type] = 0
109
107
  }
110
- changesByType[change.type].push(change.line)
108
+ changesByType[change.type]++
111
109
  }
110
+ }
112
111
 
113
- for (const [type, lines] of Object.entries(changesByType)) {
114
- const uniqueLines = [...new Set(lines)].sort((a, b) => a - b)
112
+ const transformations = Object.keys(changesByType)
113
+ .map((type) => {
115
114
  const displayName = type
116
115
  .replace(/([A-Z])/g, " $1")
117
116
  .trim()
118
117
  .toLowerCase()
119
- console.log(
120
- ` - ${displayName} (line${uniqueLines.length > 1 ? "s" : ""}: ${uniqueLines.join(", ")})`,
121
- )
122
- }
118
+ return displayName
119
+ })
120
+ .join(", ")
121
+
122
+ console.log(`✗ ${filePath}`)
123
+ if (transformations) {
124
+ console.log(` ${transformations}`)
123
125
  }
124
126
  }
125
127
 
126
128
  if (options.write) {
127
129
  fs.writeFileSync(filePath, result.code, "utf8")
128
130
  if (!options.check) {
129
- console.log(`✓ Upgraded: ${filePath}`)
131
+ console.log(`✓ ${filePath}`)
130
132
  } else {
131
- console.log(` ✓ Changes written to file`)
133
+ console.log(` ✓ written`)
132
134
  }
133
135
  }
134
136
 
135
- return true
137
+ return { modified: true, changes: result.changes }
136
138
  } else {
137
139
  if (!options.check) {
138
- console.log(` No changes: ${filePath}`)
140
+ console.log(` ${filePath}`)
139
141
  }
140
- return false
142
+ return { modified: false, changes: [] }
141
143
  }
142
144
  } catch (error) {
143
- console.error(`Error processing ${filePath}: ${error.message}`)
144
- return false
145
+ console.error(`✗ Error: ${filePath}: ${error.message}`)
146
+ return { modified: false, changes: [] }
145
147
  }
146
148
  }
147
149
 
@@ -153,27 +155,42 @@ function processFiles(patterns, options) {
153
155
  process.exit(0)
154
156
  }
155
157
 
156
- console.log(`Processing ${files.length} file(s) with baseline: ${options.baseline}\n`)
157
-
158
158
  let modifiedCount = 0
159
+ const allChanges = []
160
+
159
161
  for (const file of files) {
160
- if (processFile(file, options)) {
162
+ const result = processFile(file, options)
163
+ if (result.modified) {
161
164
  modifiedCount++
165
+ allChanges.push(...result.changes)
162
166
  }
163
167
  }
164
168
 
165
169
  // Summary
166
- console.log("")
167
- if (modifiedCount > 0) {
168
- if (options.write && options.check) {
169
- console.log(`Summary: ${modifiedCount} file(s) upgraded`)
170
- } else if (options.write) {
171
- console.log(`Summary: ${modifiedCount} file(s) upgraded`)
170
+ if (options.check) {
171
+ console.log("")
172
+ if (modifiedCount > 0) {
173
+ // Count unique transformation types
174
+ const transformTypes = new Set(allChanges.map((c) => c.type))
175
+ const typeCount = transformTypes.size
176
+ const totalChanges = allChanges.length
177
+
178
+ console.log(
179
+ `${modifiedCount} file(s) need upgrading (${totalChanges} change${totalChanges !== 1 ? "s" : ""}, ${typeCount} type${typeCount !== 1 ? "s" : ""})`,
180
+ )
181
+ if (options.write) {
182
+ console.log("Changes have been written")
183
+ }
172
184
  } else {
173
- console.log(`Summary: ${modifiedCount} file(s) need upgrading`)
185
+ console.log("All files are up to date")
174
186
  }
175
187
  } else {
176
- console.log("Summary: All files are already modern")
188
+ console.log("")
189
+ if (modifiedCount > 0) {
190
+ console.log(`✓ ${modifiedCount} file(s) upgraded`)
191
+ } else {
192
+ console.log("All files are up to date")
193
+ }
177
194
  }
178
195
 
179
196
  // Exit with code 1 if --check specified and there were changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esupgrade",
3
- "version": "2025.1.0",
3
+ "version": "2025.2.1",
4
4
  "description": "Auto-upgrade your JavaScript syntax",
5
5
  "type": "module",
6
6
  "main": "src/index.js",