Skip to content

Commit bcd791a

Browse files
committed
fix(lint): clear remaining socket/* rule violations in cli package
- prefer-cached-for-loop: cache array.length in for-loops; oxlint autofix where safe, inline disables on patterns the rule flags as unsafe (destructured loop variable, non-bare-identifier iterable) - no-default-export: convert ENV, meow, runNpmCli/runNpxCli/runPnpmCli/ runYarnCli, and the constants barrel to named exports; update all call sites; oxlint-disable on the three vitest configs (vitest API requires default export) - prefer-separate-type-import: split mixed value/type imports (autofix handled all 9 sites) Also fixes two pre-existing iteration bugs uncovered while triaging: - errors.mts calculateStringSimilarity iterated a Set with cached `.length` (Set has no .length / no numeric index), always returning 0 instead of the word-overlap ratio - utils/socket/package-alert.mts and utils/fs/glob.mts similarly iterated Sets with cached-length loops; converted back to for-of
1 parent ddd5c1b commit bcd791a

286 files changed

Lines changed: 1059 additions & 730 deletions

File tree

Some content is hidden

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

.config/oxlintrc.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/oxc-project/oxc/main/npm/oxlint/configuration_schema.json",
3-
"plugins": [
4-
"typescript",
5-
"unicorn",
6-
"import"
7-
],
8-
"jsPlugins": [
9-
"./oxlint-plugin/index.mts"
10-
],
3+
"plugins": ["typescript", "unicorn", "import"],
4+
"jsPlugins": ["./oxlint-plugin/index.mts"],
115
"categories": {
126
"correctness": "error",
137
"suspicious": "error"

.config/tsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"declarationMap": false,
55
"sourceMap": false
66
},
7-
"include": [
8-
"../src/**/*.mts",
9-
"../src/**/*.d.ts"
10-
],
7+
"include": ["../src/**/*.mts", "../src/**/*.d.ts"],
118
"exclude": [
129
"../src/**/*.test.mts",
1310
"../src/**/*.tsx",

packages/cli/.config/esbuild.cli.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ export function findSocketLibPath(importerPath: string) {
4040
return undefined
4141
}
4242

43-
export function resolveSocketLibExternal(socketLibPath: string, packageName: string) {
43+
export function resolveSocketLibExternal(
44+
socketLibPath: string,
45+
packageName: string,
46+
) {
4447
if (packageName.startsWith('@')) {
4548
const parts = packageName.split('/')
4649
const scope = parts[0]!

packages/cli/scripts/build-sea.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ async function main() {
137137
logger.log(
138138
`Building ${targets.length} target${targets.length > 1 ? 's' : ''}:`,
139139
)
140-
for (const target of targets) {
140+
for (let i = 0, { length } = targets; i < length; i += 1) {
141+
const target = targets[i]
141142
logger.log(` - ${target.platform}-${target.arch}`)
142143
}
143144
logger.log('')

packages/cli/scripts/build.mts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export async function fixNodeGypStrings(dir, options = {}) {
5757
// Find all .js files in build directory.
5858
const files = await fs.readdir(dir, { withFileTypes: true })
5959

60-
for (const file of files) {
60+
for (let i = 0, { length } = files; i < length; i += 1) {
61+
const file = files[i]
6162
const filePath = path.join(dir, file.name)
6263

6364
if (file.isDirectory()) {
@@ -169,7 +170,8 @@ async function main() {
169170
}).then(result => ({ name: 'Download Assets', result })),
170171
])
171172

172-
for (const settled of parallelPrep) {
173+
for (let i = 0, { length } = parallelPrep; i < length; i += 1) {
174+
const settled = parallelPrep[i]
173175
if (settled.status === 'rejected') {
174176
if (!quiet) {
175177
logger.error(`Parallel preparation failed: ${settled.reason}`)
@@ -274,7 +276,8 @@ async function main() {
274276

275277
const postFailed = postResults.filter(r => r.status === 'rejected')
276278
if (postFailed.length > 0) {
277-
for (const r of postFailed) {
279+
for (let i = 0, { length } = postFailed; i < length; i += 1) {
280+
const r = postFailed[i]
278281
logger.error(`Post-processing failed: ${r.reason?.message ?? r.reason}`)
279282
}
280283
throw new Error('Post-processing step(s) failed')

packages/cli/scripts/download-assets.mts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,17 @@ async function downloadAssets(assetNames, parallel = true) {
152152
)
153153
if (failed.length > 0) {
154154
logger.error(`\n${failed.length} asset(s) failed:`)
155-
for (const r of failed) {
155+
for (let i = 0, { length } = failed; i < length; i += 1) {
156+
const r = failed[i]
156157
logger.error(
157158
` - ${r.status === 'rejected' ? (r.reason?.message ?? r.reason) : r.value.name}`,
158159
)
159160
}
160161
process.exitCode = 1
161162
}
162163
} else {
163-
for (const name of assetNames) {
164+
for (let i = 0, { length } = assetNames; i < length; i += 1) {
165+
const name = assetNames[i]
164166
const result = await downloadAsset(ASSETS[name])
165167
if (!result.ok && !result.skipped) {
166168
process.exitCode = 1
@@ -247,7 +249,8 @@ async function main() {
247249
const assetNames = assetArgs.length > 0 ? assetArgs : Object.keys(ASSETS)
248250

249251
// Validate asset names.
250-
for (const name of assetNames) {
252+
for (let i = 0, { length } = assetNames; i < length; i += 1) {
253+
const name = assetNames[i]
251254
if (!(name in ASSETS)) {
252255
logger.error(`Unknown asset: ${name}`)
253256
logger.error(`Available assets: ${Object.keys(ASSETS).join(', ')}`)

packages/cli/scripts/environment-variables.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export class EnvironmentVariables {
189189

190190
// Convert all values to JSON-stringified format for esbuild.
191191
const defines: Record<string, string> = {}
192+
// oxlint-disable-next-line socket/prefer-cached-for-loop -- loop variable is destructured
192193
for (const [key, value] of Object.entries(envVars)) {
193194
defines[key] = JSON.stringify(value)
194195
}

packages/cli/scripts/esbuild-utils.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function createBaseConfig(
5050
*/
5151
export function createDefineEntries(envVars: Record<string, string>) {
5252
const entries: Record<string, string> = {}
53+
// oxlint-disable-next-line socket/prefer-cached-for-loop -- loop variable is destructured
5354
for (const [key, value] of Object.entries(envVars)) {
5455
// Dot notation: process.env.KEY
5556
entries[`process.env.${key}`] = value
@@ -96,11 +97,13 @@ export function envVarReplacementPlugin(envVars: Record<string, string>) {
9697
return
9798
}
9899

99-
for (const output of outputs) {
100+
for (let i = 0, { length } = outputs; i < length; i += 1) {
101+
const output = outputs[i]
100102
let content = output.text
101103

102104
// Replace all forms of process.env["KEY"] access, even with mangled identifiers.
103105
// Pattern: <anything>.env["KEY"] where <anything> could be "import_node_process21.default" etc.
106+
// oxlint-disable-next-line socket/prefer-cached-for-loop -- loop variable is destructured
104107
for (const [key, value] of Object.entries(envVars)) {
105108
// Match: <identifier>.env["KEY"] or <identifier>.env['KEY']
106109
const pattern = new RegExp(`(\\w+\\.)+env\\["${key}"\\]`, 'g')
@@ -149,6 +152,7 @@ export async function runBuild(config: BuildOptions, description = 'Build') {
149152

150153
// If write: false, manually write outputFiles.
151154
if (result.outputFiles && result.outputFiles.length > 0) {
155+
// oxlint-disable-next-line socket/prefer-cached-for-loop -- iterable is not a bare identifier (could be Map/Set/Generator/expression)
152156
for (const output of result.outputFiles) {
153157
mkdirSync(path.dirname(output.path), { recursive: true })
154158
writeFileSync(output.path, output.contents)

packages/cli/scripts/generate-packages.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const scripts = [
1919
path.join(packageBuilderScripts, 'generate-socketbin-packages.mts'),
2020
]
2121

22-
for (const script of scripts) {
22+
for (let i = 0, { length } = scripts; i < length; i += 1) {
23+
const script = scripts[i]
2324
const result = await spawn('node', [script], { stdio: 'inherit' })
2425

2526
if (!result) {

packages/cli/scripts/restore-cache.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ export async function hashFiles(globPattern, cwd) {
171171
return 'none'
172172
}
173173
const hash = createHash('sha256')
174-
for (const file of files) {
174+
for (let i = 0, { length } = files; i < length; i += 1) {
175+
const file = files[i]
175176
const content = await fs.readFile(path.join(cwd, file), 'utf8')
176177
hash.update(content)
177178
}

0 commit comments

Comments
 (0)