Skip to content

Commit b76426b

Browse files
authored
[acorn-opt] Use parseArgs. NFC (#24496)
1 parent eb8f3d3 commit b76426b

File tree

2 files changed

+34
-57
lines changed

2 files changed

+34
-57
lines changed

tools/acorn-optimizer.mjs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as acorn from 'acorn';
44
import * as terser from '../third_party/terser/terser.js';
55
import * as fs from 'node:fs';
66
import assert from 'node:assert';
7+
import {parseArgs} from 'node:util';
78

89
// Utilities
910

@@ -1742,16 +1743,27 @@ function reattachComments(ast, commentsMap) {
17421743

17431744
let suffix = '';
17441745

1745-
const argv = process.argv.slice(2);
1746-
1747-
function getArg(arg) {
1748-
const index = argv.indexOf(arg);
1749-
if (index == -1) {
1750-
return false;
1751-
}
1752-
argv.splice(index, 1);
1753-
return true;
1754-
}
1746+
const {
1747+
values: {
1748+
'closure-friendly': closureFriendly,
1749+
'export-es6': exportES6,
1750+
verbose,
1751+
'no-print': noPrint,
1752+
'minify-whitespace': minifyWhitespace,
1753+
outfile,
1754+
},
1755+
positionals: [infile, ...passes],
1756+
} = parseArgs({
1757+
options: {
1758+
'closure-friendly': {type: 'boolean'},
1759+
'export-es6': {type: 'boolean'},
1760+
verbose: {type: 'boolean'},
1761+
'no-print': {type: 'boolean'},
1762+
'minify-whitespace': {type: 'boolean'},
1763+
outfile: {type: 'string', short: 'o'},
1764+
},
1765+
allowPositionals: true,
1766+
});
17551767

17561768
function trace(...args) {
17571769
if (verbose) {
@@ -1761,21 +1773,6 @@ function trace(...args) {
17611773

17621774
// If enabled, output retains parentheses and comments so that the
17631775
// output can further be passed out to Closure.
1764-
const closureFriendly = getArg('--closure-friendly');
1765-
const exportES6 = getArg('--export-es6');
1766-
const verbose = getArg('--verbose');
1767-
const noPrint = getArg('--no-print');
1768-
const minifyWhitespace = getArg('--minify-whitespace');
1769-
1770-
let outfile;
1771-
const outfileIndex = argv.indexOf('-o');
1772-
if (outfileIndex != -1) {
1773-
outfile = argv[outfileIndex + 1];
1774-
argv.splice(outfileIndex, 2);
1775-
}
1776-
1777-
const infile = argv[0];
1778-
const passes = argv.slice(1);
17791776

17801777
const input = read(infile);
17811778
const extraInfoStart = input.lastIndexOf('// EXTRA_INFO:');

tools/unsafe_optimizations.mjs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as fs from 'node:fs';
77
import * as acorn from 'acorn';
88
import * as terser from '../third_party/terser/terser.js';
9+
import {parseArgs} from 'node:util';
910

1011
// Starting at the AST node 'root', calls the given callback function 'func' on all children and grandchildren of 'root'
1112
// that are of any of the type contained in array 'types'.
@@ -295,38 +296,17 @@ function runTests() {
295296
process.exit(numTestFailures);
296297
}
297298

298-
const args = process.argv.slice(2);
299-
300-
function readBool(arg) {
301-
let ret = false;
302-
for (;;) {
303-
const i = args.indexOf(arg);
304-
if (i >= 0) {
305-
args.splice(i, 1);
306-
ret = true;
307-
} else {
308-
return ret;
309-
}
310-
}
311-
}
312-
313-
function readArg(arg) {
314-
let ret = null;
315-
for (;;) {
316-
const i = args.indexOf(arg);
317-
if (i >= 0) {
318-
ret = args[i + 1];
319-
args.splice(i, 2);
320-
} else {
321-
return ret;
322-
}
323-
}
324-
}
325-
326-
const testMode = readBool('--test');
327-
const pretty = readBool('--pretty');
328-
const output = readArg('-o');
329-
const input = args[0];
299+
const {
300+
values: {test: testMode, pretty, output},
301+
positionals: [input],
302+
} = parseArgs({
303+
options: {
304+
test: {type: 'boolean'},
305+
pretty: {type: 'boolean'},
306+
output: {type: 'string', short: 'o'},
307+
},
308+
allowPositionals: true,
309+
});
330310

331311
if (testMode) {
332312
runTests();

0 commit comments

Comments
 (0)