Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 2bb1452

Browse files
authored
Merge pull request #22 from flyingrobots/chore/cjs-to-esm
fix: Convert all CommonJS to ESM
2 parents 7ca05c8 + 9bb1d62 commit 2bb1452

File tree

177 files changed

+7087
-10493
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+7087
-10493
lines changed

.eslintignore

Lines changed: 0 additions & 31 deletions
This file was deleted.

.prettierignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Dependencies
2+
node_modules/
3+
package-lock.json
4+
pnpm-lock.yaml
5+
yarn.lock
6+
7+
# Build outputs
8+
dist/
9+
build/
10+
coverage/
11+
.vitest/
12+
.nyc_output/
13+
14+
# IDE
15+
.obsidian/
16+
.vscode/
17+
.idea/
18+
19+
# Git
20+
.git/
21+
22+
# Misc
23+
*.min.js
24+
*.min.css
25+
test-results/
26+
junit.xml
27+
*.tap
28+
*.log
29+
30+
# Generated files
31+
migrations/
32+
.migration_archive/

.prettierrc.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "none",
4+
"singleQuote": true,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"useTabs": false,
8+
"arrowParens": "always",
9+
"endOfLine": "lf",
10+
"bracketSpacing": true,
11+
"bracketSameLine": false,
12+
"proseWrap": "preserve",
13+
"htmlWhitespaceSensitivity": "css",
14+
"embeddedLanguageFormatting": "auto"
15+
}

bin/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ config();
2121
import { cli } from '../src/index.js';
2222

2323
// Run the CLI with process arguments
24-
cli(process.argv).catch(error => {
24+
cli(process.argv).catch((error) => {
2525
console.error('Fatal error:', error.message);
2626
if (process.env.DEBUG) {
2727
console.error(error.stack);

eslint.config.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import promisePlugin from 'eslint-plugin-promise';
33

44
export default [
55
js.configs.recommended,
6+
{
7+
ignores: [
8+
'node_modules/**',
9+
'.obsidian/**',
10+
'dist/**',
11+
'build/**',
12+
'coverage/**',
13+
'*.min.js',
14+
'.git/**',
15+
'.vitest/**',
16+
'.nyc_output/**',
17+
'test-results/**'
18+
]
19+
},
620
{
721
files: ['**/*.js'],
822
languageOptions: {
@@ -21,43 +35,46 @@ export default [
2135
}
2236
},
2337
plugins: {
24-
'promise': promisePlugin
38+
promise: promisePlugin
2539
},
2640
rules: {
2741
// Promise-specific rules for proper async handling
2842
'promise/catch-or-return': 'error',
2943
'promise/always-return': 'error',
3044
'promise/no-return-wrap': 'error',
3145
'promise/param-names': 'error',
32-
46+
3347
// Require await in async functions
3448
'require-await': 'error',
35-
49+
3650
// Other async best practices
3751
'no-async-promise-executor': 'error',
3852
'no-await-in-loop': 'warn',
3953
'no-return-await': 'error',
4054
'prefer-promise-reject-errors': 'error',
41-
55+
4256
// ESM-specific rules
4357
'no-console': 'off',
4458
'no-undef': 'error',
45-
'no-unused-vars': ['error', {
46-
'argsIgnorePattern': '^_',
47-
'varsIgnorePattern': '^_'
48-
}],
49-
59+
'no-unused-vars': [
60+
'error',
61+
{
62+
argsIgnorePattern: '^_',
63+
varsIgnorePattern: '^_'
64+
}
65+
],
66+
5067
// Modern JavaScript best practices
5168
'prefer-const': 'error',
5269
'prefer-arrow-callback': 'error',
5370
'no-var': 'error',
5471
'object-shorthand': 'error',
55-
'semi': ['error', 'always'],
56-
'quotes': ['error', 'single', { 'avoidEscape': true }],
72+
semi: ['error', 'always'],
73+
quotes: ['error', 'single', { avoidEscape: true }],
5774
'comma-dangle': ['error', 'never'],
58-
'indent': ['error', 2],
75+
indent: ['error', 2],
5976
'no-trailing-spaces': 'error',
6077
'eol-last': 'error'
6178
}
6279
}
63-
];
80+
];

fix-commonjs.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
# Script to systematically convert CommonJS to ESM
3+
4+
set -e
5+
6+
echo "🔧 Converting CommonJS to ESM..."
7+
8+
# Function to convert a single file
9+
convert_file() {
10+
local file=$1
11+
echo "Converting: $file"
12+
13+
# Create backup
14+
cp "$file" "$file.bak"
15+
16+
# Convert require statements
17+
# const x = require('y') -> import x from 'y'
18+
sed -i '' "s/const \([a-zA-Z_][a-zA-Z0-9_]*\) = require(\(.*\))/import \1 from \2/g" "$file"
19+
20+
# const { x, y } = require('z') -> import { x, y } from 'z'
21+
sed -i '' "s/const { \(.*\) } = require(\(.*\))/import { \1 } from \2/g" "$file"
22+
23+
# Fix relative imports - add .js extension
24+
sed -i '' "s/from '\(\.\.[^']*\)'/from '\1.js'/g" "$file"
25+
sed -i '' 's/from "\(\.\.[^"]*\)"/from "\1.js"/g' "$file"
26+
27+
# Fix double .js.js
28+
sed -i '' "s/\.js\.js'/.js'/g" "$file"
29+
sed -i '' 's/\.js\.js"/.js"/g' "$file"
30+
31+
# module.exports = x -> export default x
32+
sed -i '' 's/^module\.exports = \(.*\);$/export default \1;/g' "$file"
33+
34+
# module.exports = { -> export {
35+
sed -i '' 's/^module\.exports = {$/export {/g' "$file"
36+
37+
# exports.x = y -> export const x = y
38+
sed -i '' 's/^exports\.\([a-zA-Z_][a-zA-Z0-9_]*\) = \(.*\);$/export const \1 = \2;/g' "$file"
39+
40+
echo "✓ Converted $file"
41+
}
42+
43+
# Convert each file
44+
for file in $(cat /tmp/all-commonjs-files.txt); do
45+
if [[ "$file" == *"codemods"* ]]; then
46+
echo "Skipping codemod file: $file"
47+
continue
48+
fi
49+
convert_file "$file"
50+
done
51+
52+
echo "✅ Conversion complete!"
53+
echo ""
54+
echo "🔍 Checking for remaining CommonJS patterns..."
55+
grep -r "require(" . --include="*.js" --exclude-dir=node_modules --exclude-dir=.obsidian --exclude="*.bak" | grep -v "codemods" | wc -l

fix-eslint.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# Fix ESLint errors systematically
4+
5+
echo "🔧 Fixing ESLint errors..."
6+
7+
# Fix unused variables by prefixing with underscore
8+
echo "📝 Fixing unused variables..."
9+
find ./starfleet ./src ./test -name "*.js" -type f | while read file; do
10+
# Fix unused error variables
11+
sed -i '' 's/catch (error)/catch (_error)/g' "$file"
12+
sed -i '' 's/\.catch(error/\.catch(_error/g' "$file"
13+
14+
# Fix unused function parameters
15+
sed -i '' 's/function([^)]*\boptions\b/function(_options/g' "$file"
16+
sed -i '' 's/(\([^)]*\), reject)/(\1, _reject)/g' "$file"
17+
done
18+
19+
# Remove redundant await on return
20+
echo "📝 Removing redundant await on return..."
21+
find ./starfleet ./src ./test -name "*.js" -type f | while read file; do
22+
sed -i '' 's/return await /return /g' "$file"
23+
done
24+
25+
# Fix async functions with no await by removing async
26+
echo "📝 Fixing async functions with no await..."
27+
find ./starfleet ./src ./test -name "*.js" -type f | while read file; do
28+
# This is more complex, so we'll just flag them for now
29+
grep -n "Async.*has no 'await'" "$file" 2>/dev/null && echo " ⚠️ $file has async functions without await"
30+
done
31+
32+
echo "✅ Basic fixes complete!"
33+
echo "🔍 Running ESLint again..."
34+
pnpm eslint src starfleet test 2>&1 | tail -5

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
],
4646
"author": "Flyingrobots Development Team",
4747
"license": "MIT",
48-
"dependencies": {},
4948
"devDependencies": {
5049
"@eslint/js": "^9.34.0",
5150
"@typescript-eslint/eslint-plugin": "^8.41.0",
@@ -54,6 +53,9 @@
5453
"eslint": "^9.34.0",
5554
"eslint-plugin-promise": "^7.2.1",
5655
"husky": "^9.1.7",
56+
"jscodeshift": "^17.3.0",
57+
"prettier": "^3.6.2",
58+
"recast": "^0.23.11",
5759
"vitest": "^2.0.0"
5860
},
5961
"engines": {

0 commit comments

Comments
 (0)