1 | 'use strict';
|
---|
2 |
|
---|
3 | var path = require('path');
|
---|
4 | var fs = require('fs');
|
---|
5 | var acorn = require('./acorn.js');
|
---|
6 |
|
---|
7 | function _interopNamespace(e) {
|
---|
8 | if (e && e.__esModule) return e;
|
---|
9 | var n = Object.create(null);
|
---|
10 | if (e) {
|
---|
11 | Object.keys(e).forEach(function (k) {
|
---|
12 | if (k !== 'default') {
|
---|
13 | var d = Object.getOwnPropertyDescriptor(e, k);
|
---|
14 | Object.defineProperty(n, k, d.get ? d : {
|
---|
15 | enumerable: true,
|
---|
16 | get: function () { return e[k]; }
|
---|
17 | });
|
---|
18 | }
|
---|
19 | });
|
---|
20 | }
|
---|
21 | n["default"] = e;
|
---|
22 | return Object.freeze(n);
|
---|
23 | }
|
---|
24 |
|
---|
25 | var acorn__namespace = /*#__PURE__*/_interopNamespace(acorn);
|
---|
26 |
|
---|
27 | var inputFilePaths = [], forceFileName = false, fileMode = false, silent = false, compact = false, tokenize = false;
|
---|
28 | var options = {};
|
---|
29 |
|
---|
30 | function help(status) {
|
---|
31 | var print = (status === 0) ? console.log : console.error;
|
---|
32 | print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|...|--ecma2015|--ecma2016|--ecma2017|--ecma2018|...]");
|
---|
33 | print(" [--tokenize] [--locations] [---allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [<infile>...]");
|
---|
34 | process.exit(status);
|
---|
35 | }
|
---|
36 |
|
---|
37 | for (var i = 2; i < process.argv.length; ++i) {
|
---|
38 | var arg = process.argv[i];
|
---|
39 | if (arg[0] !== "-" || arg === "-") { inputFilePaths.push(arg); }
|
---|
40 | else if (arg === "--") {
|
---|
41 | inputFilePaths.push.apply(inputFilePaths, process.argv.slice(i + 1));
|
---|
42 | forceFileName = true;
|
---|
43 | break
|
---|
44 | } else if (arg === "--locations") { options.locations = true; }
|
---|
45 | else if (arg === "--allow-hash-bang") { options.allowHashBang = true; }
|
---|
46 | else if (arg === "--allow-await-outside-function") { options.allowAwaitOutsideFunction = true; }
|
---|
47 | else if (arg === "--silent") { silent = true; }
|
---|
48 | else if (arg === "--compact") { compact = true; }
|
---|
49 | else if (arg === "--help") { help(0); }
|
---|
50 | else if (arg === "--tokenize") { tokenize = true; }
|
---|
51 | else if (arg === "--module") { options.sourceType = "module"; }
|
---|
52 | else {
|
---|
53 | var match = arg.match(/^--ecma(\d+)$/);
|
---|
54 | if (match)
|
---|
55 | { options.ecmaVersion = +match[1]; }
|
---|
56 | else
|
---|
57 | { help(1); }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | function run(codeList) {
|
---|
62 | var result = [], fileIdx = 0;
|
---|
63 | try {
|
---|
64 | codeList.forEach(function (code, idx) {
|
---|
65 | fileIdx = idx;
|
---|
66 | if (!tokenize) {
|
---|
67 | result = acorn__namespace.parse(code, options);
|
---|
68 | options.program = result;
|
---|
69 | } else {
|
---|
70 | var tokenizer = acorn__namespace.tokenizer(code, options), token;
|
---|
71 | do {
|
---|
72 | token = tokenizer.getToken();
|
---|
73 | result.push(token);
|
---|
74 | } while (token.type !== acorn__namespace.tokTypes.eof)
|
---|
75 | }
|
---|
76 | });
|
---|
77 | } catch (e) {
|
---|
78 | console.error(fileMode ? e.message.replace(/\(\d+:\d+\)$/, function (m) { return m.slice(0, 1) + inputFilePaths[fileIdx] + " " + m.slice(1); }) : e.message);
|
---|
79 | process.exit(1);
|
---|
80 | }
|
---|
81 | if (!silent) { console.log(JSON.stringify(result, null, compact ? null : 2)); }
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (fileMode = inputFilePaths.length && (forceFileName || !inputFilePaths.includes("-") || inputFilePaths.length !== 1)) {
|
---|
85 | run(inputFilePaths.map(function (path) { return fs.readFileSync(path, "utf8"); }));
|
---|
86 | } else {
|
---|
87 | var code = "";
|
---|
88 | process.stdin.resume();
|
---|
89 | process.stdin.on("data", function (chunk) { return code += chunk; });
|
---|
90 | process.stdin.on("end", function () { return run([code]); });
|
---|
91 | }
|
---|