UNPKG

6.27 kBJavaScriptView Raw
1import path from 'node:path';
2import { fileURLToPath } from 'node: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 processor = createSyncFn(path.resolve(_dirname, 'worker.js'));
9class ShParseError extends SyntaxError {
10 constructor(err) {
11 const error = err;
12 super(('Text' in error && error.Text) || error.message);
13 this.cause = err;
14 // FIXME: `error instanceof ParseError` is not working
15 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- just for robustness
16 if ('Pos' in error && error.Pos != null && typeof error.Pos === 'object') {
17 this.loc = { start: { column: error.Pos.Col, line: error.Pos.Line } };
18 }
19 }
20}
21const ShPlugin = {
22 languages,
23 parsers: {
24 sh: {
25 parse: (text, _parsers, { filepath, keepComments = true, stopAt, variant }) => {
26 try {
27 return processor(text, {
28 filepath,
29 keepComments,
30 stopAt,
31 variant,
32 });
33 }
34 catch (err) {
35 throw new ShParseError(err);
36 }
37 },
38 astFormat: 'sh',
39 locStart: node => node.Pos.Offset,
40 locEnd: node => node.End.Offset,
41 },
42 },
43 printers: {
44 sh: {
45 print: (path, { originalText, useTabs, tabWidth, indent = useTabs ? 0 : tabWidth, binaryNextLine = true, switchCaseIndent = true, spaceRedirects = true, keepPadding, minify, functionNextLine, }) => processor(path.getNode(), {
46 originalText,
47 useTabs,
48 tabWidth,
49 indent,
50 binaryNextLine,
51 switchCaseIndent,
52 spaceRedirects,
53 keepPadding,
54 minify,
55 functionNextLine,
56 }),
57 },
58 },
59 options: {
60 keepComments: {
61 since: '0.1.0',
62 category: 'Output',
63 type: 'boolean',
64 default: true,
65 description: 'KeepComments makes the parser parse comments and attach them to nodes, as opposed to discarding them.',
66 },
67 stopAt: {
68 since: '0.1.0',
69 category: 'Config',
70 type: 'path',
71 description: [
72 '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.',
73 '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.',
74 '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 \'$$\'".',
75 'The match is done by prefix, so the example above will also act on "foo $$bar".',
76 ].join('\n'),
77 },
78 variant: {
79 since: '0.1.0',
80 category: 'Config',
81 type: 'choice',
82 default: undefined,
83 choices: [
84 {
85 value: 0,
86 description: 'Bash',
87 },
88 {
89 value: 1,
90 description: 'POSIX',
91 },
92 {
93 value: 2,
94 description: 'MirBSDKorn',
95 },
96 ],
97 description: 'Variant changes the shell language variant that the parser will accept.',
98 },
99 indent: {
100 since: '0.1.0',
101 category: 'Format',
102 type: 'int',
103 description: 'Indent sets the number of spaces used for indentation. If set to 0, tabs will be used instead.',
104 },
105 binaryNextLine: {
106 since: '0.1.0',
107 category: 'Output',
108 type: 'boolean',
109 default: true,
110 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.',
111 },
112 switchCaseIndent: {
113 since: '0.1.0',
114 category: 'Format',
115 type: 'boolean',
116 default: true,
117 description: 'SwitchCaseIndent will make switch cases be indented. As such, switch case bodies will be two levels deeper than the switch itself.',
118 },
119 spaceRedirects: {
120 since: '0.1.0',
121 category: 'Format',
122 type: 'boolean',
123 default: true,
124 description: "SpaceRedirects will put a space after most redirection operators. The exceptions are '>&', '<&', '>(', and '<('.",
125 },
126 keepPadding: {
127 since: '0.1.0',
128 category: 'Format',
129 type: 'boolean',
130 default: false,
131 description: [
132 '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.',
133 '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.',
134 ].join('\n'),
135 },
136 minify: {
137 since: '0.1.0',
138 category: 'Output',
139 type: 'boolean',
140 default: false,
141 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.',
142 },
143 functionNextLine: {
144 since: '0.1.0',
145 category: 'Format',
146 type: 'boolean',
147 default: false,
148 description: "FunctionNextLine will place a function's opening braces on the next line.",
149 },
150 },
151};
152export default ShPlugin;
153//# sourceMappingURL=index.js.map
\No newline at end of file