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 +3 -0
- package/README.md +48 -0
- package/bin/esupgrade.js +47 -30
- package/package.json +1 -1
- package/src/widelyAvailable.js +477 -16
- package/tests/cli.test.js +13 -17
- package/tests/widelyAvailable.test.js +1108 -0
package/AGENTS.md
ADDED
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
|
-
|
|
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]
|
|
108
|
+
changesByType[change.type]++
|
|
111
109
|
}
|
|
110
|
+
}
|
|
112
111
|
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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(`✓
|
|
131
|
+
console.log(`✓ ${filePath}`)
|
|
130
132
|
} else {
|
|
131
|
-
console.log(` ✓
|
|
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(`
|
|
140
|
+
console.log(` ${filePath}`)
|
|
139
141
|
}
|
|
140
|
-
return false
|
|
142
|
+
return { modified: false, changes: [] }
|
|
141
143
|
}
|
|
142
144
|
} catch (error) {
|
|
143
|
-
console.error(
|
|
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
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
if (
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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(
|
|
185
|
+
console.log("All files are up to date")
|
|
174
186
|
}
|
|
175
187
|
} else {
|
|
176
|
-
console.log("
|
|
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
|