UNPKG

6.12 kBJavaScriptView Raw
1import path from 'path';
2import { fileURLToPath } from 'url';
3import { createSyncFn } from 'synckit';
4import { languages } from './languages.js';
5const _dirname = typeof __dirname === 'undefined'
6 ? path.dirname(fileURLToPath(import.meta.url))
7 : __dirname;
8const workerPath = path.resolve(_dirname, 'worker.js');
9const printSync = createSyncFn(workerPath);
10const ShPlugin = {
11 languages,
12 parsers: {
13 sh: {
14 parse: (_text, _parsers, options) => options,
15 astFormat: 'sh',
16 locStart: () => 0,
17 locEnd: () => 0,
18 },
19 },
20 printers: {
21 sh: {
22 print: (_path, { originalText, filepath, useTabs, tabWidth, keepComments, stopAt, variant, indent, binaryNextLine, switchCaseIndent, spaceRedirects, keepPadding, minify, functionNextLine, }) => {
23 try {
24 return printSync(originalText, {
25 filepath,
26 useTabs,
27 tabWidth,
28 keepComments,
29 stopAt,
30 variant,
31 indent,
32 binaryNextLine,
33 switchCaseIndent,
34 spaceRedirects,
35 keepPadding,
36 minify,
37 functionNextLine,
38 });
39 }
40 catch (err) {
41 const error = err;
42 if (typeof error === 'string') {
43 throw new SyntaxError(error);
44 }
45 throw Object.assign(error, {
46 loc: {
47 start: {
48 column: error.pos.col,
49 line: error.pos.line,
50 },
51 },
52 });
53 }
54 },
55 },
56 },
57 options: {
58 keepComments: {
59 since: '0.1.0',
60 category: 'Output',
61 type: 'boolean',
62 default: true,
63 description: 'KeepComments makes the parser parse comments and attach them to nodes, as opposed to discarding them.',
64 },
65 stopAt: {
66 since: '0.1.0',
67 category: 'Config',
68 type: 'path',
69 description: [
70 'StopAt configures the lexer to stop at an arbitrary word, treating it as if it were the end of the input. It can contain any characters except whitespace, and cannot be over four bytes in size.',
71 'This can be useful to embed shell code within another language, as one can use a special word to mark the delimiters between the two.',
72 'As a word, it will only apply when following whitespace or a separating token. For example, StopAt("$$") will act on the inputs "foo $$" and "foo;$$", but not on "foo \'$$\'".',
73 'The match is done by prefix, so the example above will also act on "foo $$bar".',
74 ].join('\n'),
75 },
76 variant: {
77 since: '0.1.0',
78 category: 'Config',
79 type: 'choice',
80 default: undefined,
81 choices: [
82 {
83 value: 0,
84 description: 'Bash',
85 },
86 {
87 value: 1,
88 description: 'POSIX',
89 },
90 {
91 value: 2,
92 description: 'MirBSDKorn',
93 },
94 ],
95 description: 'Variant changes the shell language variant that the parser will accept.',
96 },
97 indent: {
98 since: '0.1.0',
99 category: 'Format',
100 type: 'int',
101 description: 'Indent sets the number of spaces used for indentation. If set to 0, tabs will be used instead.',
102 },
103 binaryNextLine: {
104 since: '0.1.0',
105 category: 'Output',
106 type: 'boolean',
107 default: true,
108 description: 'BinaryNextLine will make binary operators appear on the next line when a binary command, such as a pipe, spans multiple lines. A backslash will be used.',
109 },
110 switchCaseIndent: {
111 since: '0.1.0',
112 category: 'Format',
113 type: 'boolean',
114 default: true,
115 description: 'SwitchCaseIndent will make switch cases be indented. As such, switch case bodies will be two levels deeper than the switch itself.',
116 },
117 spaceRedirects: {
118 since: '0.1.0',
119 category: 'Format',
120 type: 'boolean',
121 default: true,
122 description: "SpaceRedirects will put a space after most redirection operators. The exceptions are '>&', '<&', '>(', and '<('.",
123 },
124 keepPadding: {
125 since: '0.1.0',
126 category: 'Format',
127 type: 'boolean',
128 default: false,
129 description: [
130 'KeepPadding will keep most nodes and tokens in the same column that they were in the original source. This allows the user to decide how to align and pad their code with spaces.',
131 'Note that this feature is best-effort and will only keep the alignment stable, so it may need some human help the first time it is run.',
132 ].join('\n'),
133 },
134 minify: {
135 since: '0.1.0',
136 category: 'Output',
137 type: 'boolean',
138 default: false,
139 description: 'Minify will print programs in a way to save the most bytes possible. For example, indentation and comments are skipped, and extra whitespace is avoided when possible.',
140 },
141 functionNextLine: {
142 since: '0.1.0',
143 category: 'Format',
144 type: 'boolean',
145 default: false,
146 description: "FunctionNextLine will place a function's opening braces on the next line.",
147 },
148 },
149};
150export default ShPlugin;
151//# sourceMappingURL=index.js.map
\No newline at end of file