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