docker-parser 0.1.0
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/LICENSE +21 -0
- package/README.md +122 -0
- package/clean.d.ts +14 -0
- package/clean.js +97 -0
- package/deparser.d.ts +127 -0
- package/deparser.js +441 -0
- package/esm/clean.js +91 -0
- package/esm/deparser.js +436 -0
- package/esm/index.js +10 -0
- package/esm/lexer.js +412 -0
- package/esm/parser.js +943 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +37 -0
- package/lexer.d.ts +117 -0
- package/lexer.js +416 -0
- package/package.json +45 -0
- package/parser.d.ts +150 -0
- package/parser.js +948 -0
- package/types.d.ts +284 -0
- package/types.js +2 -0
package/deparser.js
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deparseSync = exports.Deparser = void 0;
|
|
4
|
+
exports.deparse = deparse;
|
|
5
|
+
/**
|
|
6
|
+
* Dockerfile Deparser - converts AST back to source
|
|
7
|
+
*/
|
|
8
|
+
class Deparser {
|
|
9
|
+
options;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.options = {
|
|
12
|
+
newline: '\n',
|
|
13
|
+
indent: '',
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Deparse Dockerfile AST to source string
|
|
19
|
+
*/
|
|
20
|
+
deparse(node) {
|
|
21
|
+
if ('type' in node) {
|
|
22
|
+
switch (node.type) {
|
|
23
|
+
case 'Dockerfile':
|
|
24
|
+
return this.deparseDockerfile(node);
|
|
25
|
+
case 'ParserDirective':
|
|
26
|
+
return this.deparseDirective(node);
|
|
27
|
+
case 'Stage':
|
|
28
|
+
return this.deparseStage(node);
|
|
29
|
+
case 'Comment':
|
|
30
|
+
return this.deparseComment(node);
|
|
31
|
+
case 'FromInstruction':
|
|
32
|
+
return this.deparseFrom(node);
|
|
33
|
+
case 'RunInstruction':
|
|
34
|
+
return this.deparseRun(node);
|
|
35
|
+
case 'CmdInstruction':
|
|
36
|
+
return this.deparseCmd(node);
|
|
37
|
+
case 'EntrypointInstruction':
|
|
38
|
+
return this.deparseEntrypoint(node);
|
|
39
|
+
case 'CopyInstruction':
|
|
40
|
+
return this.deparseCopy(node);
|
|
41
|
+
case 'AddInstruction':
|
|
42
|
+
return this.deparseAdd(node);
|
|
43
|
+
case 'EnvInstruction':
|
|
44
|
+
return this.deparseEnv(node);
|
|
45
|
+
case 'ArgInstruction':
|
|
46
|
+
return this.deparseArg(node);
|
|
47
|
+
case 'WorkdirInstruction':
|
|
48
|
+
return this.deparseWorkdir(node);
|
|
49
|
+
case 'UserInstruction':
|
|
50
|
+
return this.deparseUser(node);
|
|
51
|
+
case 'ExposeInstruction':
|
|
52
|
+
return this.deparseExpose(node);
|
|
53
|
+
case 'VolumeInstruction':
|
|
54
|
+
return this.deparseVolume(node);
|
|
55
|
+
case 'LabelInstruction':
|
|
56
|
+
return this.deparseLabel(node);
|
|
57
|
+
case 'ShellInstruction':
|
|
58
|
+
return this.deparseShell(node);
|
|
59
|
+
case 'HealthcheckInstruction':
|
|
60
|
+
return this.deparseHealthcheck(node);
|
|
61
|
+
case 'StopsignalInstruction':
|
|
62
|
+
return this.deparseStopsignal(node);
|
|
63
|
+
case 'OnbuildInstruction':
|
|
64
|
+
return this.deparseOnbuild(node);
|
|
65
|
+
case 'MaintainerInstruction':
|
|
66
|
+
return this.deparseMaintainer(node);
|
|
67
|
+
default:
|
|
68
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
throw new Error('Invalid node');
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Deparse entire Dockerfile
|
|
75
|
+
*/
|
|
76
|
+
deparseDockerfile(dockerfile) {
|
|
77
|
+
const lines = [];
|
|
78
|
+
// Directives first
|
|
79
|
+
for (const directive of dockerfile.directives) {
|
|
80
|
+
lines.push(this.deparseDirective(directive));
|
|
81
|
+
}
|
|
82
|
+
// Then stages
|
|
83
|
+
for (const stage of dockerfile.stages) {
|
|
84
|
+
if (lines.length > 0) {
|
|
85
|
+
lines.push('');
|
|
86
|
+
}
|
|
87
|
+
lines.push(this.deparseStage(stage));
|
|
88
|
+
}
|
|
89
|
+
return lines.join(this.options.newline);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Deparse parser directive
|
|
93
|
+
*/
|
|
94
|
+
deparseDirective(directive) {
|
|
95
|
+
return `# ${directive.directive}=${directive.value}`;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Deparse build stage
|
|
99
|
+
*/
|
|
100
|
+
deparseStage(stage) {
|
|
101
|
+
const lines = [];
|
|
102
|
+
// FROM instruction
|
|
103
|
+
lines.push(this.deparseFrom(stage.from));
|
|
104
|
+
// Other instructions
|
|
105
|
+
for (const instruction of stage.instructions) {
|
|
106
|
+
lines.push(this.deparseInstruction(instruction));
|
|
107
|
+
}
|
|
108
|
+
return lines.join(this.options.newline);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Deparse comment
|
|
112
|
+
*/
|
|
113
|
+
deparseComment(comment) {
|
|
114
|
+
return `# ${comment.value}`;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Deparse any instruction
|
|
118
|
+
*/
|
|
119
|
+
deparseInstruction(instruction) {
|
|
120
|
+
switch (instruction.type) {
|
|
121
|
+
case 'FromInstruction':
|
|
122
|
+
return this.deparseFrom(instruction);
|
|
123
|
+
case 'RunInstruction':
|
|
124
|
+
return this.deparseRun(instruction);
|
|
125
|
+
case 'CmdInstruction':
|
|
126
|
+
return this.deparseCmd(instruction);
|
|
127
|
+
case 'EntrypointInstruction':
|
|
128
|
+
return this.deparseEntrypoint(instruction);
|
|
129
|
+
case 'CopyInstruction':
|
|
130
|
+
return this.deparseCopy(instruction);
|
|
131
|
+
case 'AddInstruction':
|
|
132
|
+
return this.deparseAdd(instruction);
|
|
133
|
+
case 'EnvInstruction':
|
|
134
|
+
return this.deparseEnv(instruction);
|
|
135
|
+
case 'ArgInstruction':
|
|
136
|
+
return this.deparseArg(instruction);
|
|
137
|
+
case 'WorkdirInstruction':
|
|
138
|
+
return this.deparseWorkdir(instruction);
|
|
139
|
+
case 'UserInstruction':
|
|
140
|
+
return this.deparseUser(instruction);
|
|
141
|
+
case 'ExposeInstruction':
|
|
142
|
+
return this.deparseExpose(instruction);
|
|
143
|
+
case 'VolumeInstruction':
|
|
144
|
+
return this.deparseVolume(instruction);
|
|
145
|
+
case 'LabelInstruction':
|
|
146
|
+
return this.deparseLabel(instruction);
|
|
147
|
+
case 'ShellInstruction':
|
|
148
|
+
return this.deparseShell(instruction);
|
|
149
|
+
case 'HealthcheckInstruction':
|
|
150
|
+
return this.deparseHealthcheck(instruction);
|
|
151
|
+
case 'StopsignalInstruction':
|
|
152
|
+
return this.deparseStopsignal(instruction);
|
|
153
|
+
case 'OnbuildInstruction':
|
|
154
|
+
return this.deparseOnbuild(instruction);
|
|
155
|
+
case 'MaintainerInstruction':
|
|
156
|
+
return this.deparseMaintainer(instruction);
|
|
157
|
+
default:
|
|
158
|
+
throw new Error(`Unknown instruction type: ${instruction.type}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Deparse FROM instruction
|
|
163
|
+
*/
|
|
164
|
+
deparseFrom(from) {
|
|
165
|
+
const parts = ['FROM'];
|
|
166
|
+
if (from.platform) {
|
|
167
|
+
parts.push(`--platform=${from.platform}`);
|
|
168
|
+
}
|
|
169
|
+
let imageRef = from.image;
|
|
170
|
+
if (from.tag) {
|
|
171
|
+
imageRef += `:${from.tag}`;
|
|
172
|
+
}
|
|
173
|
+
if (from.digest) {
|
|
174
|
+
imageRef += `@${from.digest}`;
|
|
175
|
+
}
|
|
176
|
+
parts.push(imageRef);
|
|
177
|
+
if (from.name) {
|
|
178
|
+
parts.push('AS', from.name);
|
|
179
|
+
}
|
|
180
|
+
return parts.join(' ');
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Deparse RUN instruction
|
|
184
|
+
*/
|
|
185
|
+
deparseRun(run) {
|
|
186
|
+
const parts = ['RUN'];
|
|
187
|
+
// Mount flags
|
|
188
|
+
if (run.mount) {
|
|
189
|
+
for (const mount of run.mount) {
|
|
190
|
+
parts.push(this.deparseMountFlag(mount));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Network flag
|
|
194
|
+
if (run.network) {
|
|
195
|
+
parts.push(`--network=${run.network}`);
|
|
196
|
+
}
|
|
197
|
+
// Security flag
|
|
198
|
+
if (run.security) {
|
|
199
|
+
parts.push(`--security=${run.security}`);
|
|
200
|
+
}
|
|
201
|
+
// Command
|
|
202
|
+
if (run.isExec && Array.isArray(run.command)) {
|
|
203
|
+
parts.push(JSON.stringify(run.command));
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
parts.push(run.command);
|
|
207
|
+
}
|
|
208
|
+
return parts.join(' ');
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Deparse mount flag
|
|
212
|
+
*/
|
|
213
|
+
deparseMountFlag(mount) {
|
|
214
|
+
const opts = [];
|
|
215
|
+
if (mount.type)
|
|
216
|
+
opts.push(`type=${mount.type}`);
|
|
217
|
+
if (mount.target)
|
|
218
|
+
opts.push(`target=${mount.target}`);
|
|
219
|
+
if (mount.source)
|
|
220
|
+
opts.push(`source=${mount.source}`);
|
|
221
|
+
if (mount.from)
|
|
222
|
+
opts.push(`from=${mount.from}`);
|
|
223
|
+
if (mount.id)
|
|
224
|
+
opts.push(`id=${mount.id}`);
|
|
225
|
+
if (mount.sharing)
|
|
226
|
+
opts.push(`sharing=${mount.sharing}`);
|
|
227
|
+
if (mount.readonly)
|
|
228
|
+
opts.push('readonly');
|
|
229
|
+
if (mount.required)
|
|
230
|
+
opts.push('required');
|
|
231
|
+
if (mount.mode)
|
|
232
|
+
opts.push(`mode=${mount.mode}`);
|
|
233
|
+
if (mount.uid)
|
|
234
|
+
opts.push(`uid=${mount.uid}`);
|
|
235
|
+
if (mount.gid)
|
|
236
|
+
opts.push(`gid=${mount.gid}`);
|
|
237
|
+
return `--mount=${opts.join(',')}`;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Deparse CMD instruction
|
|
241
|
+
*/
|
|
242
|
+
deparseCmd(cmd) {
|
|
243
|
+
if (cmd.isExec && Array.isArray(cmd.command)) {
|
|
244
|
+
return `CMD ${JSON.stringify(cmd.command)}`;
|
|
245
|
+
}
|
|
246
|
+
return `CMD ${cmd.command}`;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Deparse ENTRYPOINT instruction
|
|
250
|
+
*/
|
|
251
|
+
deparseEntrypoint(entrypoint) {
|
|
252
|
+
if (entrypoint.isExec && Array.isArray(entrypoint.command)) {
|
|
253
|
+
return `ENTRYPOINT ${JSON.stringify(entrypoint.command)}`;
|
|
254
|
+
}
|
|
255
|
+
return `ENTRYPOINT ${entrypoint.command}`;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Deparse COPY instruction
|
|
259
|
+
*/
|
|
260
|
+
deparseCopy(copy) {
|
|
261
|
+
const parts = ['COPY'];
|
|
262
|
+
if (copy.from)
|
|
263
|
+
parts.push(`--from=${copy.from}`);
|
|
264
|
+
if (copy.chown)
|
|
265
|
+
parts.push(`--chown=${copy.chown}`);
|
|
266
|
+
if (copy.chmod)
|
|
267
|
+
parts.push(`--chmod=${copy.chmod}`);
|
|
268
|
+
if (copy.link)
|
|
269
|
+
parts.push('--link');
|
|
270
|
+
if (copy.parents)
|
|
271
|
+
parts.push('--parents');
|
|
272
|
+
if (copy.exclude) {
|
|
273
|
+
for (const exc of copy.exclude) {
|
|
274
|
+
parts.push(`--exclude=${exc}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
parts.push(...copy.sources);
|
|
278
|
+
parts.push(copy.destination);
|
|
279
|
+
return parts.join(' ');
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Deparse ADD instruction
|
|
283
|
+
*/
|
|
284
|
+
deparseAdd(add) {
|
|
285
|
+
const parts = ['ADD'];
|
|
286
|
+
if (add.chown)
|
|
287
|
+
parts.push(`--chown=${add.chown}`);
|
|
288
|
+
if (add.chmod)
|
|
289
|
+
parts.push(`--chmod=${add.chmod}`);
|
|
290
|
+
if (add.link)
|
|
291
|
+
parts.push('--link');
|
|
292
|
+
if (add.checksum)
|
|
293
|
+
parts.push(`--checksum=${add.checksum}`);
|
|
294
|
+
if (add.keepGitDir)
|
|
295
|
+
parts.push('--keep-git-dir');
|
|
296
|
+
if (add.exclude) {
|
|
297
|
+
for (const exc of add.exclude) {
|
|
298
|
+
parts.push(`--exclude=${exc}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
parts.push(...add.sources);
|
|
302
|
+
parts.push(add.destination);
|
|
303
|
+
return parts.join(' ');
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Deparse ENV instruction
|
|
307
|
+
*/
|
|
308
|
+
deparseEnv(env) {
|
|
309
|
+
const parts = ['ENV'];
|
|
310
|
+
for (const variable of env.variables) {
|
|
311
|
+
const value = this.needsQuotes(variable.value)
|
|
312
|
+
? `"${variable.value}"`
|
|
313
|
+
: variable.value;
|
|
314
|
+
parts.push(`${variable.key}=${value}`);
|
|
315
|
+
}
|
|
316
|
+
return parts.join(' ');
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Deparse ARG instruction
|
|
320
|
+
*/
|
|
321
|
+
deparseArg(arg) {
|
|
322
|
+
if (arg.defaultValue !== undefined) {
|
|
323
|
+
return `ARG ${arg.name}=${arg.defaultValue}`;
|
|
324
|
+
}
|
|
325
|
+
return `ARG ${arg.name}`;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Deparse WORKDIR instruction
|
|
329
|
+
*/
|
|
330
|
+
deparseWorkdir(workdir) {
|
|
331
|
+
return `WORKDIR ${workdir.path}`;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Deparse USER instruction
|
|
335
|
+
*/
|
|
336
|
+
deparseUser(user) {
|
|
337
|
+
if (user.group) {
|
|
338
|
+
return `USER ${user.user}:${user.group}`;
|
|
339
|
+
}
|
|
340
|
+
return `USER ${user.user}`;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Deparse EXPOSE instruction
|
|
344
|
+
*/
|
|
345
|
+
deparseExpose(expose) {
|
|
346
|
+
const ports = expose.ports.map(p => {
|
|
347
|
+
if (p.protocol) {
|
|
348
|
+
return `${p.port}/${p.protocol}`;
|
|
349
|
+
}
|
|
350
|
+
return String(p.port);
|
|
351
|
+
});
|
|
352
|
+
return `EXPOSE ${ports.join(' ')}`;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Deparse VOLUME instruction
|
|
356
|
+
*/
|
|
357
|
+
deparseVolume(volume) {
|
|
358
|
+
if (volume.paths.length === 1) {
|
|
359
|
+
return `VOLUME ${volume.paths[0]}`;
|
|
360
|
+
}
|
|
361
|
+
return `VOLUME ${JSON.stringify(volume.paths)}`;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Deparse LABEL instruction
|
|
365
|
+
*/
|
|
366
|
+
deparseLabel(label) {
|
|
367
|
+
const parts = ['LABEL'];
|
|
368
|
+
for (const entry of label.labels) {
|
|
369
|
+
const value = this.needsQuotes(entry.value)
|
|
370
|
+
? `"${entry.value}"`
|
|
371
|
+
: entry.value;
|
|
372
|
+
parts.push(`${entry.key}=${value}`);
|
|
373
|
+
}
|
|
374
|
+
return parts.join(' ');
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Deparse SHELL instruction
|
|
378
|
+
*/
|
|
379
|
+
deparseShell(shell) {
|
|
380
|
+
return `SHELL ${JSON.stringify(shell.shell)}`;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Deparse HEALTHCHECK instruction
|
|
384
|
+
*/
|
|
385
|
+
deparseHealthcheck(healthcheck) {
|
|
386
|
+
if (healthcheck.none) {
|
|
387
|
+
return 'HEALTHCHECK NONE';
|
|
388
|
+
}
|
|
389
|
+
const parts = ['HEALTHCHECK'];
|
|
390
|
+
if (healthcheck.interval)
|
|
391
|
+
parts.push(`--interval=${healthcheck.interval}`);
|
|
392
|
+
if (healthcheck.timeout)
|
|
393
|
+
parts.push(`--timeout=${healthcheck.timeout}`);
|
|
394
|
+
if (healthcheck.startPeriod)
|
|
395
|
+
parts.push(`--start-period=${healthcheck.startPeriod}`);
|
|
396
|
+
if (healthcheck.startInterval)
|
|
397
|
+
parts.push(`--start-interval=${healthcheck.startInterval}`);
|
|
398
|
+
if (healthcheck.retries !== undefined)
|
|
399
|
+
parts.push(`--retries=${healthcheck.retries}`);
|
|
400
|
+
if (healthcheck.command) {
|
|
401
|
+
parts.push(this.deparseCmd(healthcheck.command));
|
|
402
|
+
}
|
|
403
|
+
return parts.join(' ');
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Deparse STOPSIGNAL instruction
|
|
407
|
+
*/
|
|
408
|
+
deparseStopsignal(stopsignal) {
|
|
409
|
+
return `STOPSIGNAL ${stopsignal.signal}`;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Deparse ONBUILD instruction
|
|
413
|
+
*/
|
|
414
|
+
deparseOnbuild(onbuild) {
|
|
415
|
+
return `ONBUILD ${this.deparseInstruction(onbuild.trigger)}`;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Deparse MAINTAINER instruction
|
|
419
|
+
*/
|
|
420
|
+
deparseMaintainer(maintainer) {
|
|
421
|
+
return `MAINTAINER ${maintainer.maintainer}`;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Check if value needs quotes
|
|
425
|
+
*/
|
|
426
|
+
needsQuotes(value) {
|
|
427
|
+
return value.includes(' ') || value.includes('\t') || value.includes('"') || value.includes("'");
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
exports.Deparser = Deparser;
|
|
431
|
+
/**
|
|
432
|
+
* Deparse Dockerfile AST to source string
|
|
433
|
+
*/
|
|
434
|
+
function deparse(node, options) {
|
|
435
|
+
const deparser = new Deparser(options);
|
|
436
|
+
return deparser.deparse(node);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Synchronous deparse (same as deparse, for API consistency)
|
|
440
|
+
*/
|
|
441
|
+
exports.deparseSync = deparse;
|
package/esm/clean.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* No-op transform function - removes the property
|
|
3
|
+
*/
|
|
4
|
+
export const noop = () => undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Deep transform an object
|
|
7
|
+
*/
|
|
8
|
+
function transform(obj, options) {
|
|
9
|
+
if (obj === null || obj === undefined) {
|
|
10
|
+
return obj;
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(obj)) {
|
|
13
|
+
return obj.map(item => transform(item, options));
|
|
14
|
+
}
|
|
15
|
+
if (typeof obj === 'object') {
|
|
16
|
+
const result = {};
|
|
17
|
+
for (const key of Object.keys(obj)) {
|
|
18
|
+
const value = obj[key];
|
|
19
|
+
// Check if there's a transform function for this key
|
|
20
|
+
if (key in options) {
|
|
21
|
+
const transformed = options[key](value, key, obj);
|
|
22
|
+
if (transformed !== undefined) {
|
|
23
|
+
result[key] = transformed;
|
|
24
|
+
}
|
|
25
|
+
// If undefined, skip the property (remove it)
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// Recursively transform nested objects
|
|
29
|
+
result[key] = transform(value, options);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Clean AST tree by removing location/position information
|
|
38
|
+
* This is used for comparing ASTs in round-trip testing
|
|
39
|
+
*/
|
|
40
|
+
export function cleanTree(tree) {
|
|
41
|
+
return transform(tree, {
|
|
42
|
+
// Remove range/position information
|
|
43
|
+
range: noop,
|
|
44
|
+
// Normalize whitespace in command strings
|
|
45
|
+
command: (value) => {
|
|
46
|
+
if (typeof value === 'string') {
|
|
47
|
+
return value.trim().replace(/\s+/g, ' ');
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
return value.map(v => typeof v === 'string' ? v.trim() : v);
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
},
|
|
54
|
+
// Normalize path strings
|
|
55
|
+
path: (value) => {
|
|
56
|
+
if (typeof value === 'string') {
|
|
57
|
+
return value.trim();
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
},
|
|
61
|
+
// Normalize maintainer strings
|
|
62
|
+
maintainer: (value) => {
|
|
63
|
+
if (typeof value === 'string') {
|
|
64
|
+
return value.trim();
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
},
|
|
68
|
+
// Normalize signal strings
|
|
69
|
+
signal: (value) => {
|
|
70
|
+
if (typeof value === 'string') {
|
|
71
|
+
return value.trim();
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
},
|
|
75
|
+
// Normalize value strings in env/label
|
|
76
|
+
value: (value) => {
|
|
77
|
+
if (typeof value === 'string') {
|
|
78
|
+
return value.trim();
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Compare two ASTs for equality after cleaning
|
|
86
|
+
*/
|
|
87
|
+
export function compareAst(ast1, ast2) {
|
|
88
|
+
const clean1 = cleanTree(ast1);
|
|
89
|
+
const clean2 = cleanTree(ast2);
|
|
90
|
+
return JSON.stringify(clean1) === JSON.stringify(clean2);
|
|
91
|
+
}
|