bash-ast 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/deparser.d.ts +34 -10
- package/deparser.js +153 -88
- package/esm/deparser.js +153 -88
- package/esm/lexer.js +278 -33
- package/esm/parser.js +143 -40
- package/index.d.ts +1 -1
- package/lexer.d.ts +65 -5
- package/lexer.js +278 -33
- package/package.json +10 -12
- package/parser.d.ts +33 -2
- package/parser.js +143 -40
- package/types.d.ts +30 -3
package/README.md
CHANGED
|
@@ -24,6 +24,17 @@ const ast = parse(source);
|
|
|
24
24
|
console.log(JSON.stringify(ast, null, 2));
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
#### Options
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
parse(source, {
|
|
31
|
+
// keep comments as `Comment` nodes instead of dropping them (default: false)
|
|
32
|
+
keepComments: true,
|
|
33
|
+
// hard wall-clock budget; exceeding it throws instead of running unbounded
|
|
34
|
+
timeoutMs: 5000
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
27
38
|
### Deparsing
|
|
28
39
|
|
|
29
40
|
```typescript
|
|
@@ -70,9 +81,13 @@ console.log(JSON.stringify(clean1) === JSON.stringify(clean2)); // true
|
|
|
70
81
|
- Input (`<`)
|
|
71
82
|
- Output (`>`, `>>`)
|
|
72
83
|
- Here strings (`<<<`)
|
|
84
|
+
- Here-documents (`<<EOF`, `<<-EOF`, `<<'EOF'`) — the body is opaque text, never re-lexed as shell
|
|
73
85
|
- File descriptor redirects (`2>&1`)
|
|
74
86
|
|
|
75
87
|
### Other
|
|
88
|
+
- Asynchronous commands (`cmd &`)
|
|
89
|
+
- Comments (with `keepComments`)
|
|
90
|
+
- Process substitution (`<(cmd)`, `>(cmd)`)
|
|
76
91
|
- Variable assignments (`VAR=value`)
|
|
77
92
|
- Function definitions
|
|
78
93
|
- Quoted strings (single and double)
|
|
@@ -80,6 +95,24 @@ console.log(JSON.stringify(clean1) === JSON.stringify(clean2)); // true
|
|
|
80
95
|
- Command substitution (`$(cmd)`, `` `cmd` ``)
|
|
81
96
|
- Arithmetic expansion (`$((expr))`)
|
|
82
97
|
|
|
98
|
+
## Round-tripping
|
|
99
|
+
|
|
100
|
+
`deparse(parse(x))` is meant to be *semantically* equivalent to `x`, not
|
|
101
|
+
character-identical: layout and optional whitespace are normalized. The
|
|
102
|
+
properties the test suite enforces over a corpus
|
|
103
|
+
of real GitHub Actions `run:` blocks (`__fixtures__/workflows`) are:
|
|
104
|
+
|
|
105
|
+
- `parse(deparse(parse(x)))` equals `parse(x)` once positions are stripped
|
|
106
|
+
(`cleanTree`);
|
|
107
|
+
- `deparse` is idempotent from the second pass on;
|
|
108
|
+
- the emitted text is accepted by `bash -n`.
|
|
109
|
+
|
|
110
|
+
Grouping is what makes this safe: a brace group or subshell stays a single
|
|
111
|
+
command through pipelines and `&&`/`||`, `&` stays on the command it
|
|
112
|
+
backgrounds, and `NAME=value` is an assignment only in assignment position (a
|
|
113
|
+
command prefix, or an operand of `export`/`local`/`declare`/`readonly`/`typeset`)
|
|
114
|
+
— elsewhere it is an ordinary argument, as in `psql --set ON_ERROR_STOP=1`.
|
|
115
|
+
|
|
83
116
|
## License
|
|
84
117
|
|
|
85
118
|
MIT
|
package/deparser.d.ts
CHANGED
|
@@ -11,24 +11,33 @@ export interface DeparserOptions {
|
|
|
11
11
|
*/
|
|
12
12
|
export declare class Deparser {
|
|
13
13
|
private options;
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Here-document bodies owed by the statement currently being deparsed. They
|
|
16
|
+
* are emitted after the line that opened them.
|
|
17
|
+
*/
|
|
18
|
+
private heredocs;
|
|
15
19
|
constructor(options?: DeparserOptions);
|
|
16
20
|
/**
|
|
17
21
|
* Deparse AST to source string
|
|
18
22
|
*/
|
|
19
23
|
deparse(node: Script | Node): string;
|
|
20
|
-
/**
|
|
21
|
-
* Get current indentation
|
|
22
|
-
*/
|
|
23
|
-
private getIndent;
|
|
24
24
|
/**
|
|
25
25
|
* Deparse script
|
|
26
26
|
*/
|
|
27
27
|
private deparseScript;
|
|
28
|
+
/**
|
|
29
|
+
* Deparse one statement: the command itself, its `&`, and the bodies of any
|
|
30
|
+
* here-documents it opened.
|
|
31
|
+
*/
|
|
32
|
+
private emitStatement;
|
|
28
33
|
/**
|
|
29
34
|
* Deparse any command
|
|
30
35
|
*/
|
|
31
36
|
private deparseCommand;
|
|
37
|
+
/**
|
|
38
|
+
* Deparse a list of commands, keeping it on one line when that is safe
|
|
39
|
+
*/
|
|
40
|
+
private deparseList;
|
|
32
41
|
/**
|
|
33
42
|
* Deparse simple command
|
|
34
43
|
*/
|
|
@@ -38,9 +47,18 @@ export declare class Deparser {
|
|
|
38
47
|
*/
|
|
39
48
|
private deparseWord;
|
|
40
49
|
/**
|
|
41
|
-
* Deparse
|
|
50
|
+
* Deparse comment
|
|
51
|
+
*/
|
|
52
|
+
private deparseComment;
|
|
53
|
+
/**
|
|
54
|
+
* Deparse redirect. A here-document's body is owed to the end of the
|
|
55
|
+
* statement, so it is buffered rather than returned.
|
|
42
56
|
*/
|
|
43
57
|
private deparseRedirect;
|
|
58
|
+
/**
|
|
59
|
+
* Deparse the redirections attached to a compound command
|
|
60
|
+
*/
|
|
61
|
+
private deparseRedirects;
|
|
44
62
|
/**
|
|
45
63
|
* Deparse pipeline
|
|
46
64
|
*/
|
|
@@ -54,13 +72,15 @@ export declare class Deparser {
|
|
|
54
72
|
*/
|
|
55
73
|
private deparseSubshell;
|
|
56
74
|
/**
|
|
57
|
-
* Deparse
|
|
75
|
+
* Deparse brace group. The braces are always emitted: they are what keeps
|
|
76
|
+
* the group a single command inside a pipeline or after `&&`/`||`.
|
|
58
77
|
*/
|
|
59
|
-
private
|
|
78
|
+
private deparseBraceGroup;
|
|
60
79
|
/**
|
|
61
|
-
*
|
|
80
|
+
* Render `<keyword> <list><separator><terminator>`, breaking the line when
|
|
81
|
+
* the list cannot be inlined
|
|
62
82
|
*/
|
|
63
|
-
private
|
|
83
|
+
private clause;
|
|
64
84
|
/**
|
|
65
85
|
* Deparse if clause
|
|
66
86
|
*/
|
|
@@ -73,6 +93,10 @@ export declare class Deparser {
|
|
|
73
93
|
* Deparse until clause
|
|
74
94
|
*/
|
|
75
95
|
private deparseUntil;
|
|
96
|
+
/**
|
|
97
|
+
* Deparse `while`/`until`
|
|
98
|
+
*/
|
|
99
|
+
private deparseLoop;
|
|
76
100
|
/**
|
|
77
101
|
* Deparse for clause
|
|
78
102
|
*/
|
package/deparser.js
CHANGED
|
@@ -7,7 +7,11 @@ exports.deparse = deparse;
|
|
|
7
7
|
*/
|
|
8
8
|
class Deparser {
|
|
9
9
|
options;
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Here-document bodies owed by the statement currently being deparsed. They
|
|
12
|
+
* are emitted after the line that opened them.
|
|
13
|
+
*/
|
|
14
|
+
heredocs = [];
|
|
11
15
|
constructor(options = {}) {
|
|
12
16
|
this.options = {
|
|
13
17
|
newline: '\n',
|
|
@@ -23,54 +27,49 @@ class Deparser {
|
|
|
23
27
|
switch (node.type) {
|
|
24
28
|
case 'Script':
|
|
25
29
|
return this.deparseScript(node);
|
|
26
|
-
case 'SimpleCommand':
|
|
27
|
-
return this.deparseSimpleCommand(node);
|
|
28
|
-
case 'Pipeline':
|
|
29
|
-
return this.deparsePipeline(node);
|
|
30
|
-
case 'LogicalExpression':
|
|
31
|
-
return this.deparseLogicalExpression(node);
|
|
32
|
-
case 'Subshell':
|
|
33
|
-
return this.deparseSubshell(node);
|
|
34
|
-
case 'CompoundList':
|
|
35
|
-
return this.deparseCompoundList(node);
|
|
36
|
-
case 'IfClause':
|
|
37
|
-
return this.deparseIf(node);
|
|
38
|
-
case 'WhileClause':
|
|
39
|
-
return this.deparseWhile(node);
|
|
40
|
-
case 'UntilClause':
|
|
41
|
-
return this.deparseUntil(node);
|
|
42
|
-
case 'ForClause':
|
|
43
|
-
return this.deparseFor(node);
|
|
44
|
-
case 'CaseClause':
|
|
45
|
-
return this.deparseCase(node);
|
|
46
|
-
case 'FunctionDefinition':
|
|
47
|
-
return this.deparseFunction(node);
|
|
48
30
|
case 'Word':
|
|
49
31
|
return this.deparseWord(node);
|
|
50
32
|
case 'AssignmentWord':
|
|
51
33
|
return node.text;
|
|
52
34
|
case 'Redirect':
|
|
53
35
|
return this.deparseRedirect(node);
|
|
36
|
+
case 'CaseItem':
|
|
37
|
+
return this.deparseCaseItem(node);
|
|
38
|
+
case 'HereDoc': {
|
|
39
|
+
const heredoc = node;
|
|
40
|
+
return heredoc.content + heredoc.delimiter;
|
|
41
|
+
}
|
|
54
42
|
default:
|
|
55
|
-
|
|
43
|
+
return this.emitStatement(node);
|
|
56
44
|
}
|
|
57
45
|
}
|
|
58
46
|
throw new Error('Invalid node');
|
|
59
47
|
}
|
|
60
|
-
/**
|
|
61
|
-
* Get current indentation
|
|
62
|
-
*/
|
|
63
|
-
getIndent() {
|
|
64
|
-
return this.options.indent.repeat(this.indentLevel);
|
|
65
|
-
}
|
|
66
48
|
/**
|
|
67
49
|
* Deparse script
|
|
68
50
|
*/
|
|
69
51
|
deparseScript(script) {
|
|
70
52
|
return script.commands
|
|
71
|
-
.map(cmd => this.
|
|
53
|
+
.map(cmd => this.emitStatement(cmd))
|
|
72
54
|
.join(this.options.newline);
|
|
73
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Deparse one statement: the command itself, its `&`, and the bodies of any
|
|
58
|
+
* here-documents it opened.
|
|
59
|
+
*/
|
|
60
|
+
emitStatement(command) {
|
|
61
|
+
const owed = this.heredocs;
|
|
62
|
+
this.heredocs = [];
|
|
63
|
+
let text = this.deparseCommand(command);
|
|
64
|
+
if (command.async) {
|
|
65
|
+
text += ' &';
|
|
66
|
+
}
|
|
67
|
+
if (this.heredocs.length > 0) {
|
|
68
|
+
text += '\n' + this.heredocs.join('\n');
|
|
69
|
+
}
|
|
70
|
+
this.heredocs = owed;
|
|
71
|
+
return text;
|
|
72
|
+
}
|
|
74
73
|
/**
|
|
75
74
|
* Deparse any command
|
|
76
75
|
*/
|
|
@@ -84,8 +83,12 @@ class Deparser {
|
|
|
84
83
|
return this.deparseLogicalExpression(command);
|
|
85
84
|
case 'Subshell':
|
|
86
85
|
return this.deparseSubshell(command);
|
|
86
|
+
case 'BraceGroup':
|
|
87
|
+
return this.deparseBraceGroup(command);
|
|
87
88
|
case 'CompoundList':
|
|
88
|
-
return this.
|
|
89
|
+
return this.deparseList(command).text;
|
|
90
|
+
case 'Comment':
|
|
91
|
+
return this.deparseComment(command);
|
|
89
92
|
case 'IfClause':
|
|
90
93
|
return this.deparseIf(command);
|
|
91
94
|
case 'WhileClause':
|
|
@@ -102,6 +105,18 @@ class Deparser {
|
|
|
102
105
|
throw new Error(`Unknown command type: ${command.type}`);
|
|
103
106
|
}
|
|
104
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Deparse a list of commands, keeping it on one line when that is safe
|
|
110
|
+
*/
|
|
111
|
+
deparseList(list) {
|
|
112
|
+
const parts = list.commands.map(cmd => this.emitStatement(cmd));
|
|
113
|
+
const multiline = parts.some(part => part.includes('\n') || part.endsWith('&')) ||
|
|
114
|
+
list.commands.some(cmd => cmd.type === 'Comment');
|
|
115
|
+
return {
|
|
116
|
+
text: parts.join(multiline ? '\n' : '; '),
|
|
117
|
+
multiline
|
|
118
|
+
};
|
|
119
|
+
}
|
|
105
120
|
/**
|
|
106
121
|
* Deparse simple command
|
|
107
122
|
*/
|
|
@@ -120,8 +135,8 @@ class Deparser {
|
|
|
120
135
|
// Suffix (arguments and redirections)
|
|
121
136
|
if (cmd.suffix) {
|
|
122
137
|
for (const item of cmd.suffix) {
|
|
123
|
-
if (item.type === 'Word') {
|
|
124
|
-
parts.push(
|
|
138
|
+
if (item.type === 'Word' || item.type === 'AssignmentWord') {
|
|
139
|
+
parts.push(item.text);
|
|
125
140
|
}
|
|
126
141
|
else if (item.type === 'Redirect') {
|
|
127
142
|
parts.push(this.deparseRedirect(item));
|
|
@@ -137,7 +152,14 @@ class Deparser {
|
|
|
137
152
|
return word.text;
|
|
138
153
|
}
|
|
139
154
|
/**
|
|
140
|
-
* Deparse
|
|
155
|
+
* Deparse comment
|
|
156
|
+
*/
|
|
157
|
+
deparseComment(comment) {
|
|
158
|
+
return `#${comment.text}`;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Deparse redirect. A here-document's body is owed to the end of the
|
|
162
|
+
* statement, so it is buffered rather than returned.
|
|
141
163
|
*/
|
|
142
164
|
deparseRedirect(redirect) {
|
|
143
165
|
let result = '';
|
|
@@ -148,8 +170,25 @@ class Deparser {
|
|
|
148
170
|
if (redirect.file) {
|
|
149
171
|
result += this.deparseWord(redirect.file);
|
|
150
172
|
}
|
|
173
|
+
else if (redirect.heredoc) {
|
|
174
|
+
result += redirect.heredoc.quoted
|
|
175
|
+
? `'${redirect.heredoc.delimiter}'`
|
|
176
|
+
: redirect.heredoc.delimiter;
|
|
177
|
+
}
|
|
178
|
+
if (redirect.heredoc) {
|
|
179
|
+
this.heredocs.push(redirect.heredoc.content + redirect.heredoc.delimiter);
|
|
180
|
+
}
|
|
151
181
|
return result;
|
|
152
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Deparse the redirections attached to a compound command
|
|
185
|
+
*/
|
|
186
|
+
deparseRedirects(redirects) {
|
|
187
|
+
if (!redirects || redirects.length === 0) {
|
|
188
|
+
return '';
|
|
189
|
+
}
|
|
190
|
+
return ' ' + redirects.map(redirect => this.deparseRedirect(redirect)).join(' ');
|
|
191
|
+
}
|
|
153
192
|
/**
|
|
154
193
|
* Deparse pipeline
|
|
155
194
|
*/
|
|
@@ -175,104 +214,130 @@ class Deparser {
|
|
|
175
214
|
* Deparse subshell
|
|
176
215
|
*/
|
|
177
216
|
deparseSubshell(subshell) {
|
|
178
|
-
const list = this.
|
|
179
|
-
|
|
217
|
+
const list = this.deparseList(subshell.list);
|
|
218
|
+
const body = list.multiline ? `(\n${list.text}\n)` : `(${list.text})`;
|
|
219
|
+
return body + this.deparseRedirects(subshell.redirects);
|
|
180
220
|
}
|
|
181
221
|
/**
|
|
182
|
-
* Deparse
|
|
222
|
+
* Deparse brace group. The braces are always emitted: they are what keeps
|
|
223
|
+
* the group a single command inside a pipeline or after `&&`/`||`.
|
|
183
224
|
*/
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
225
|
+
deparseBraceGroup(group) {
|
|
226
|
+
const list = this.deparseList(group.list);
|
|
227
|
+
const body = list.multiline ? `{\n${list.text}\n}` : `{ ${list.text}; }`;
|
|
228
|
+
return body + this.deparseRedirects(group.redirects);
|
|
188
229
|
}
|
|
189
230
|
/**
|
|
190
|
-
*
|
|
231
|
+
* Render `<keyword> <list><separator><terminator>`, breaking the line when
|
|
232
|
+
* the list cannot be inlined
|
|
191
233
|
*/
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
234
|
+
clause(keyword, list, terminator) {
|
|
235
|
+
if (list.multiline) {
|
|
236
|
+
return `${keyword} ${list.text}\n${terminator}`;
|
|
237
|
+
}
|
|
238
|
+
return `${keyword} ${list.text}; ${terminator}`;
|
|
196
239
|
}
|
|
197
240
|
/**
|
|
198
241
|
* Deparse if clause
|
|
199
242
|
*/
|
|
200
|
-
deparseIf(ifClause) {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
parts.push(this.deparseCompoundListInline(ifClause.then));
|
|
243
|
+
deparseIf(ifClause, keyword = 'if') {
|
|
244
|
+
const condition = this.deparseList(ifClause.condition);
|
|
245
|
+
const then = this.deparseList(ifClause.then);
|
|
246
|
+
let elseText;
|
|
247
|
+
let elseMultiline = false;
|
|
206
248
|
if (ifClause.else) {
|
|
207
249
|
if (ifClause.else.type === 'IfClause') {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return parts.join(' ').replace('; elif', '; elif').replace('; fi; fi', '; fi');
|
|
250
|
+
elseText = this.deparseIf(ifClause.else, 'elif');
|
|
251
|
+
elseMultiline = elseText.includes('\n');
|
|
211
252
|
}
|
|
212
253
|
else {
|
|
213
|
-
|
|
214
|
-
|
|
254
|
+
const elseList = this.deparseList(ifClause.else);
|
|
255
|
+
elseText = elseList.text;
|
|
256
|
+
elseMultiline = elseList.multiline;
|
|
215
257
|
}
|
|
216
258
|
}
|
|
217
|
-
|
|
218
|
-
|
|
259
|
+
const multiline = condition.multiline || then.multiline || elseMultiline;
|
|
260
|
+
const isElif = keyword === 'elif';
|
|
261
|
+
if (!multiline) {
|
|
262
|
+
const parts = [`${this.clause(keyword, condition, 'then')} ${then.text}`];
|
|
263
|
+
if (elseText !== undefined) {
|
|
264
|
+
parts.push(ifClause.else.type === 'IfClause' ? elseText : `else ${elseText}`);
|
|
265
|
+
}
|
|
266
|
+
const body = parts.join('; ');
|
|
267
|
+
return isElif ? body : `${body}; fi`;
|
|
268
|
+
}
|
|
269
|
+
const parts = [`${this.clause(keyword, condition, 'then')}\n${then.text}`];
|
|
270
|
+
if (elseText !== undefined) {
|
|
271
|
+
parts.push(ifClause.else.type === 'IfClause' ? elseText : `else\n${elseText}`);
|
|
272
|
+
}
|
|
273
|
+
const body = parts.join('\n');
|
|
274
|
+
return isElif ? body : `${body}\nfi`;
|
|
219
275
|
}
|
|
220
276
|
/**
|
|
221
277
|
* Deparse while clause
|
|
222
278
|
*/
|
|
223
279
|
deparseWhile(whileClause) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
return `while ${condition}; do ${body}; done`;
|
|
280
|
+
return this.deparseLoop('while', whileClause.condition, whileClause.body) +
|
|
281
|
+
this.deparseRedirects(whileClause.redirects);
|
|
227
282
|
}
|
|
228
283
|
/**
|
|
229
284
|
* Deparse until clause
|
|
230
285
|
*/
|
|
231
286
|
deparseUntil(untilClause) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
287
|
+
return this.deparseLoop('until', untilClause.condition, untilClause.body) +
|
|
288
|
+
this.deparseRedirects(untilClause.redirects);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Deparse `while`/`until`
|
|
292
|
+
*/
|
|
293
|
+
deparseLoop(keyword, conditionList, bodyList) {
|
|
294
|
+
const condition = this.deparseList(conditionList);
|
|
295
|
+
const body = this.deparseList(bodyList);
|
|
296
|
+
if (!condition.multiline && !body.multiline) {
|
|
297
|
+
return `${keyword} ${condition.text}; do ${body.text}; done`;
|
|
298
|
+
}
|
|
299
|
+
return `${this.clause(keyword, condition, 'do')}\n${body.text}\ndone`;
|
|
235
300
|
}
|
|
236
301
|
/**
|
|
237
302
|
* Deparse for clause
|
|
238
303
|
*/
|
|
239
304
|
deparseFor(forClause) {
|
|
240
|
-
|
|
305
|
+
let head = `for ${forClause.name}`;
|
|
241
306
|
if (forClause.wordlist && forClause.wordlist.length > 0) {
|
|
242
|
-
|
|
243
|
-
parts.push(forClause.wordlist.map(w => this.deparseWord(w)).join(' '));
|
|
307
|
+
head += ` in ${forClause.wordlist.map(word => this.deparseWord(word)).join(' ')}`;
|
|
244
308
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
return parts.join(' ');
|
|
309
|
+
const body = this.deparseList(forClause.body);
|
|
310
|
+
const text = body.multiline
|
|
311
|
+
? `${head}; do\n${body.text}\ndone`
|
|
312
|
+
: `${head}; do ${body.text}; done`;
|
|
313
|
+
return text + this.deparseRedirects(forClause.redirects);
|
|
251
314
|
}
|
|
252
315
|
/**
|
|
253
316
|
* Deparse case clause
|
|
254
317
|
*/
|
|
255
318
|
deparseCase(caseClause) {
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
parts.push('esac');
|
|
264
|
-
return parts.join(' ');
|
|
319
|
+
const items = caseClause.cases.map(item => this.deparseCaseItem(item));
|
|
320
|
+
const multiline = items.some(item => item.includes('\n'));
|
|
321
|
+
const head = `case ${this.deparseWord(caseClause.word)} in`;
|
|
322
|
+
const text = multiline
|
|
323
|
+
? `${head}\n${items.join('\n')}\nesac`
|
|
324
|
+
: [head, ...items, 'esac'].join(' ');
|
|
325
|
+
return text + this.deparseRedirects(caseClause.redirects);
|
|
265
326
|
}
|
|
266
327
|
/**
|
|
267
328
|
* Deparse case item
|
|
268
329
|
*/
|
|
269
330
|
deparseCaseItem(caseItem) {
|
|
270
|
-
const pattern = caseItem.pattern.map(
|
|
331
|
+
const pattern = caseItem.pattern.map(word => this.deparseWord(word)).join('|');
|
|
271
332
|
let result = `${pattern})`;
|
|
272
333
|
if (caseItem.body) {
|
|
273
|
-
|
|
334
|
+
const body = this.deparseList(caseItem.body);
|
|
335
|
+
result += body.multiline ? `\n${body.text}\n` : ` ${body.text} `;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
result += ' ';
|
|
274
339
|
}
|
|
275
|
-
result += '
|
|
340
|
+
result += ';;';
|
|
276
341
|
return result;
|
|
277
342
|
}
|
|
278
343
|
/**
|
|
@@ -281,7 +346,7 @@ class Deparser {
|
|
|
281
346
|
deparseFunction(func) {
|
|
282
347
|
const body = func.body.type === 'Subshell'
|
|
283
348
|
? this.deparseSubshell(func.body)
|
|
284
|
-
:
|
|
349
|
+
: this.deparseBraceGroup(func.body);
|
|
285
350
|
return `${func.name}() ${body}`;
|
|
286
351
|
}
|
|
287
352
|
}
|