docker-parser 0.1.1 → 0.1.3
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 +41 -0
- package/deparser.d.ts +8 -0
- package/deparser.js +44 -7
- package/esm/deparser.js +44 -7
- package/esm/lexer.js +30 -2
- package/esm/parser.js +56 -7
- package/lexer.d.ts +9 -0
- package/lexer.js +30 -2
- package/package.json +11 -13
- package/parser.d.ts +17 -1
- package/parser.js +56 -7
- package/types.d.ts +17 -0
package/README.md
CHANGED
|
@@ -44,6 +44,47 @@ const output = deparse(ast);
|
|
|
44
44
|
console.log(output); // FROM node:18-alpine
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
### Comments
|
|
48
|
+
|
|
49
|
+
A comment belongs to the instruction below it, as `leadingComments`, and a blank
|
|
50
|
+
line above a node is `blankBefore`. Both are emitted by the deparser, so a
|
|
51
|
+
parsed Dockerfile keeps its comments and section spacing through a round-trip,
|
|
52
|
+
and a generated one can carry its own:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
deparse({
|
|
56
|
+
type: 'Dockerfile',
|
|
57
|
+
directives: [],
|
|
58
|
+
comments: [],
|
|
59
|
+
stages: [
|
|
60
|
+
{
|
|
61
|
+
type: 'Stage',
|
|
62
|
+
from: { type: 'FromInstruction', instruction: 'FROM', image: 'node:22-alpine' },
|
|
63
|
+
instructions: [
|
|
64
|
+
{
|
|
65
|
+
type: 'CopyInstruction',
|
|
66
|
+
instruction: 'COPY',
|
|
67
|
+
sources: ['package.json'],
|
|
68
|
+
destination: './',
|
|
69
|
+
blankBefore: true,
|
|
70
|
+
leadingComments: [{ type: 'Comment', value: 'manifests only: cache the install layer' }],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
});
|
|
76
|
+
// FROM node:22-alpine
|
|
77
|
+
// # manifests only: cache the install layer
|
|
78
|
+
//
|
|
79
|
+
// COPY package.json ./
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`Dockerfile.comments` holds every comment in source order, and comments below
|
|
83
|
+
the last instruction land in `Dockerfile.trailingComments`.
|
|
84
|
+
|
|
85
|
+
Line continuations are not preserved: a `RUN` written across several lines with
|
|
86
|
+
`\` deparses as one line.
|
|
87
|
+
|
|
47
88
|
### AST Comparison
|
|
48
89
|
|
|
49
90
|
```typescript
|
package/deparser.d.ts
CHANGED
|
@@ -20,6 +20,14 @@ export declare class Deparser {
|
|
|
20
20
|
* Deparse entire Dockerfile
|
|
21
21
|
*/
|
|
22
22
|
private deparseDockerfile;
|
|
23
|
+
/**
|
|
24
|
+
* Prefix a node's rendered lines with its blank line and leading comments
|
|
25
|
+
*/
|
|
26
|
+
private withLeading;
|
|
27
|
+
/**
|
|
28
|
+
* Whether a node already renders a blank line above itself
|
|
29
|
+
*/
|
|
30
|
+
private leadsWithBlank;
|
|
23
31
|
/**
|
|
24
32
|
* Deparse parser directive
|
|
25
33
|
*/
|
package/deparser.js
CHANGED
|
@@ -79,15 +79,51 @@ class Deparser {
|
|
|
79
79
|
for (const directive of dockerfile.directives) {
|
|
80
80
|
lines.push(this.deparseDirective(directive));
|
|
81
81
|
}
|
|
82
|
-
// Then stages
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
// Then stages, separated by a blank line — unless the stage already places
|
|
83
|
+
// that separator itself via blankBefore (on the stage, or on the first of
|
|
84
|
+
// its leading comments). A separator is never inserted where the AST does
|
|
85
|
+
// not ask for one: an invented blank line would come back as blankBefore on
|
|
86
|
+
// the next parse and break the round-trip.
|
|
87
|
+
dockerfile.stages.forEach((stage, index) => {
|
|
88
|
+
if (index > 0 && !this.leadsWithBlank(stage)) {
|
|
85
89
|
lines.push('');
|
|
86
90
|
}
|
|
87
91
|
lines.push(this.deparseStage(stage));
|
|
92
|
+
});
|
|
93
|
+
for (const comment of dockerfile.trailingComments ?? []) {
|
|
94
|
+
if (comment.blankBefore) {
|
|
95
|
+
lines.push('');
|
|
96
|
+
}
|
|
97
|
+
lines.push(this.deparseComment(comment));
|
|
98
|
+
}
|
|
99
|
+
return lines.join(this.options.newline);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Prefix a node's rendered lines with its blank line and leading comments
|
|
103
|
+
*/
|
|
104
|
+
withLeading(node, rendered) {
|
|
105
|
+
const lines = [];
|
|
106
|
+
// Each comment carries its own blank line, so a blank above the comment
|
|
107
|
+
// block and a blank between the block and the instruction stay distinct.
|
|
108
|
+
for (const comment of node.leadingComments ?? []) {
|
|
109
|
+
if (comment.blankBefore) {
|
|
110
|
+
lines.push('');
|
|
111
|
+
}
|
|
112
|
+
lines.push(this.deparseComment(comment));
|
|
113
|
+
}
|
|
114
|
+
if (node.blankBefore) {
|
|
115
|
+
lines.push('');
|
|
88
116
|
}
|
|
117
|
+
lines.push(rendered);
|
|
89
118
|
return lines.join(this.options.newline);
|
|
90
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether a node already renders a blank line above itself
|
|
122
|
+
*/
|
|
123
|
+
leadsWithBlank(node) {
|
|
124
|
+
const first = node.leadingComments?.[0];
|
|
125
|
+
return first ? Boolean(first.blankBefore) : Boolean(node.blankBefore);
|
|
126
|
+
}
|
|
91
127
|
/**
|
|
92
128
|
* Deparse parser directive
|
|
93
129
|
*/
|
|
@@ -100,18 +136,19 @@ class Deparser {
|
|
|
100
136
|
deparseStage(stage) {
|
|
101
137
|
const lines = [];
|
|
102
138
|
// FROM instruction
|
|
103
|
-
lines.push(this.deparseFrom(stage.from));
|
|
139
|
+
lines.push(this.withLeading(stage.from, this.deparseFrom(stage.from)));
|
|
104
140
|
// Other instructions
|
|
105
141
|
for (const instruction of stage.instructions) {
|
|
106
|
-
lines.push(this.deparseInstruction(instruction));
|
|
142
|
+
lines.push(this.withLeading(instruction, this.deparseInstruction(instruction)));
|
|
107
143
|
}
|
|
108
|
-
return lines.join(this.options.newline);
|
|
144
|
+
return this.withLeading(stage, lines.join(this.options.newline));
|
|
109
145
|
}
|
|
110
146
|
/**
|
|
111
147
|
* Deparse comment
|
|
112
148
|
*/
|
|
113
149
|
deparseComment(comment) {
|
|
114
|
-
|
|
150
|
+
// An empty comment is a bare `#`; `# ` would add trailing whitespace.
|
|
151
|
+
return comment.value ? `# ${comment.value}` : '#';
|
|
115
152
|
}
|
|
116
153
|
/**
|
|
117
154
|
* Deparse any instruction
|
package/esm/deparser.js
CHANGED
|
@@ -75,15 +75,51 @@ export class Deparser {
|
|
|
75
75
|
for (const directive of dockerfile.directives) {
|
|
76
76
|
lines.push(this.deparseDirective(directive));
|
|
77
77
|
}
|
|
78
|
-
// Then stages
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
// Then stages, separated by a blank line — unless the stage already places
|
|
79
|
+
// that separator itself via blankBefore (on the stage, or on the first of
|
|
80
|
+
// its leading comments). A separator is never inserted where the AST does
|
|
81
|
+
// not ask for one: an invented blank line would come back as blankBefore on
|
|
82
|
+
// the next parse and break the round-trip.
|
|
83
|
+
dockerfile.stages.forEach((stage, index) => {
|
|
84
|
+
if (index > 0 && !this.leadsWithBlank(stage)) {
|
|
81
85
|
lines.push('');
|
|
82
86
|
}
|
|
83
87
|
lines.push(this.deparseStage(stage));
|
|
88
|
+
});
|
|
89
|
+
for (const comment of dockerfile.trailingComments ?? []) {
|
|
90
|
+
if (comment.blankBefore) {
|
|
91
|
+
lines.push('');
|
|
92
|
+
}
|
|
93
|
+
lines.push(this.deparseComment(comment));
|
|
94
|
+
}
|
|
95
|
+
return lines.join(this.options.newline);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Prefix a node's rendered lines with its blank line and leading comments
|
|
99
|
+
*/
|
|
100
|
+
withLeading(node, rendered) {
|
|
101
|
+
const lines = [];
|
|
102
|
+
// Each comment carries its own blank line, so a blank above the comment
|
|
103
|
+
// block and a blank between the block and the instruction stay distinct.
|
|
104
|
+
for (const comment of node.leadingComments ?? []) {
|
|
105
|
+
if (comment.blankBefore) {
|
|
106
|
+
lines.push('');
|
|
107
|
+
}
|
|
108
|
+
lines.push(this.deparseComment(comment));
|
|
109
|
+
}
|
|
110
|
+
if (node.blankBefore) {
|
|
111
|
+
lines.push('');
|
|
84
112
|
}
|
|
113
|
+
lines.push(rendered);
|
|
85
114
|
return lines.join(this.options.newline);
|
|
86
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Whether a node already renders a blank line above itself
|
|
118
|
+
*/
|
|
119
|
+
leadsWithBlank(node) {
|
|
120
|
+
const first = node.leadingComments?.[0];
|
|
121
|
+
return first ? Boolean(first.blankBefore) : Boolean(node.blankBefore);
|
|
122
|
+
}
|
|
87
123
|
/**
|
|
88
124
|
* Deparse parser directive
|
|
89
125
|
*/
|
|
@@ -96,18 +132,19 @@ export class Deparser {
|
|
|
96
132
|
deparseStage(stage) {
|
|
97
133
|
const lines = [];
|
|
98
134
|
// FROM instruction
|
|
99
|
-
lines.push(this.deparseFrom(stage.from));
|
|
135
|
+
lines.push(this.withLeading(stage.from, this.deparseFrom(stage.from)));
|
|
100
136
|
// Other instructions
|
|
101
137
|
for (const instruction of stage.instructions) {
|
|
102
|
-
lines.push(this.deparseInstruction(instruction));
|
|
138
|
+
lines.push(this.withLeading(instruction, this.deparseInstruction(instruction)));
|
|
103
139
|
}
|
|
104
|
-
return lines.join(this.options.newline);
|
|
140
|
+
return this.withLeading(stage, lines.join(this.options.newline));
|
|
105
141
|
}
|
|
106
142
|
/**
|
|
107
143
|
* Deparse comment
|
|
108
144
|
*/
|
|
109
145
|
deparseComment(comment) {
|
|
110
|
-
|
|
146
|
+
// An empty comment is a bare `#`; `# ` would add trailing whitespace.
|
|
147
|
+
return comment.value ? `# ${comment.value}` : '#';
|
|
111
148
|
}
|
|
112
149
|
/**
|
|
113
150
|
* Deparse any instruction
|
package/esm/lexer.js
CHANGED
|
@@ -253,6 +253,34 @@ export class Lexer {
|
|
|
253
253
|
}
|
|
254
254
|
return result;
|
|
255
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Read a flag, including its `=value` if present
|
|
258
|
+
*
|
|
259
|
+
* Unlike readWord, this does not stop at `=`: a flag's value is part of the
|
|
260
|
+
* flag (`--mount=type=cache,id=store`, `--from=builder`). Stopping at the
|
|
261
|
+
* first `=` leaves the value behind as free text, where an instruction parser
|
|
262
|
+
* reads it as the start of the command.
|
|
263
|
+
*/
|
|
264
|
+
readFlag() {
|
|
265
|
+
let result = '';
|
|
266
|
+
while (!this.isAtEnd()) {
|
|
267
|
+
const char = this.peek();
|
|
268
|
+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
if (char === this.escapeChar && (this.peek(1) === '\n' || this.peek(1) === '\r')) {
|
|
272
|
+
// Line continuation
|
|
273
|
+
this.advance();
|
|
274
|
+
if (this.peek() === '\r')
|
|
275
|
+
this.advance();
|
|
276
|
+
if (this.peek() === '\n')
|
|
277
|
+
this.advance();
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
result += this.advance();
|
|
281
|
+
}
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
256
284
|
/**
|
|
257
285
|
* Read a comment or directive
|
|
258
286
|
*/
|
|
@@ -361,9 +389,9 @@ export class Lexer {
|
|
|
361
389
|
range: { start, end: this.getPosition() }
|
|
362
390
|
};
|
|
363
391
|
}
|
|
364
|
-
// Flag (--something)
|
|
392
|
+
// Flag (--something, or --something=value)
|
|
365
393
|
if (char === '-' && this.peek(1) === '-') {
|
|
366
|
-
const value = this.
|
|
394
|
+
const value = this.readFlag();
|
|
367
395
|
return {
|
|
368
396
|
type: TokenType.FLAG,
|
|
369
397
|
value,
|
package/esm/parser.js
CHANGED
|
@@ -8,6 +8,10 @@ export class Parser {
|
|
|
8
8
|
pos = 0;
|
|
9
9
|
escapeChar = '\\';
|
|
10
10
|
options;
|
|
11
|
+
/** Comments read but not yet attached to the node they lead. */
|
|
12
|
+
pendingComments = [];
|
|
13
|
+
/** Whether a blank line preceded the node about to be parsed. */
|
|
14
|
+
pendingBlank = false;
|
|
11
15
|
constructor(options = {}) {
|
|
12
16
|
this.options = {
|
|
13
17
|
includeComments: true,
|
|
@@ -55,7 +59,9 @@ export class Parser {
|
|
|
55
59
|
}
|
|
56
60
|
else if (token.type === TokenType.COMMENT) {
|
|
57
61
|
if (this.options.includeComments) {
|
|
58
|
-
|
|
62
|
+
const comment = this.parseComment();
|
|
63
|
+
dockerfile.comments.push(comment);
|
|
64
|
+
this.pendingComments.push(comment);
|
|
59
65
|
}
|
|
60
66
|
else {
|
|
61
67
|
this.advance();
|
|
@@ -74,7 +80,9 @@ export class Parser {
|
|
|
74
80
|
const token = this.peek();
|
|
75
81
|
if (token.type === TokenType.COMMENT) {
|
|
76
82
|
if (this.options.includeComments) {
|
|
77
|
-
|
|
83
|
+
const comment = this.takeBlank(this.parseComment());
|
|
84
|
+
dockerfile.comments.push(comment);
|
|
85
|
+
this.pendingComments.push(comment);
|
|
78
86
|
}
|
|
79
87
|
else {
|
|
80
88
|
this.advance();
|
|
@@ -83,18 +91,19 @@ export class Parser {
|
|
|
83
91
|
}
|
|
84
92
|
if (token.type === TokenType.FROM) {
|
|
85
93
|
const from = this.parseFrom();
|
|
86
|
-
currentStage = {
|
|
94
|
+
currentStage = this.takeLeading({
|
|
87
95
|
type: 'Stage',
|
|
88
96
|
from,
|
|
89
97
|
instructions: [],
|
|
90
98
|
name: from.name,
|
|
91
99
|
range: from.range
|
|
92
|
-
};
|
|
100
|
+
});
|
|
93
101
|
dockerfile.stages.push(currentStage);
|
|
94
102
|
continue;
|
|
95
103
|
}
|
|
96
104
|
// Parse other instructions
|
|
97
|
-
const
|
|
105
|
+
const parsed = this.parseInstruction();
|
|
106
|
+
const instruction = parsed ? this.takeLeading(parsed) : parsed;
|
|
98
107
|
if (instruction) {
|
|
99
108
|
if (currentStage) {
|
|
100
109
|
currentStage.instructions.push(instruction);
|
|
@@ -117,6 +126,12 @@ export class Parser {
|
|
|
117
126
|
}
|
|
118
127
|
}
|
|
119
128
|
}
|
|
129
|
+
// Comments after the last instruction lead nothing; keep them so the file
|
|
130
|
+
// can be deparsed without losing its tail.
|
|
131
|
+
if (this.pendingComments.length > 0) {
|
|
132
|
+
dockerfile.trailingComments = this.pendingComments;
|
|
133
|
+
this.pendingComments = [];
|
|
134
|
+
}
|
|
120
135
|
return dockerfile;
|
|
121
136
|
}
|
|
122
137
|
/**
|
|
@@ -145,18 +160,52 @@ export class Parser {
|
|
|
145
160
|
return this.tokens[this.pos - 1];
|
|
146
161
|
}
|
|
147
162
|
/**
|
|
148
|
-
* Skip whitespace and newlines
|
|
163
|
+
* Skip whitespace and newlines, recording whether a blank line was crossed
|
|
149
164
|
*/
|
|
150
165
|
skipWhitespaceAndNewlines() {
|
|
166
|
+
let newlines = 0;
|
|
151
167
|
while (!this.isAtEnd()) {
|
|
152
168
|
const token = this.peek();
|
|
153
|
-
if (token.type === TokenType.
|
|
169
|
+
if (token.type === TokenType.NEWLINE) {
|
|
170
|
+
newlines++;
|
|
171
|
+
this.advance();
|
|
172
|
+
}
|
|
173
|
+
else if (token.type === TokenType.WHITESPACE) {
|
|
154
174
|
this.advance();
|
|
155
175
|
}
|
|
156
176
|
else {
|
|
157
177
|
break;
|
|
158
178
|
}
|
|
159
179
|
}
|
|
180
|
+
// Two newlines in a row means an empty line between instructions. At the
|
|
181
|
+
// very start of the file there is no preceding instruction to separate from.
|
|
182
|
+
if (newlines > 1 && this.pos > newlines) {
|
|
183
|
+
this.pendingBlank = true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Attach the comments and blank line collected since the last node
|
|
188
|
+
*/
|
|
189
|
+
takeLeading(node) {
|
|
190
|
+
if (this.pendingComments.length > 0) {
|
|
191
|
+
node.leadingComments = this.pendingComments;
|
|
192
|
+
this.pendingComments = [];
|
|
193
|
+
}
|
|
194
|
+
return this.takeBlank(node);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Attach only the blank line collected since the last node
|
|
198
|
+
*
|
|
199
|
+
* A comment takes the blank line above it but not the comments above it:
|
|
200
|
+
* consecutive comments are siblings in one block leading the same
|
|
201
|
+
* instruction, not a chain where each leads the next.
|
|
202
|
+
*/
|
|
203
|
+
takeBlank(node) {
|
|
204
|
+
if (this.pendingBlank) {
|
|
205
|
+
node.blankBefore = true;
|
|
206
|
+
this.pendingBlank = false;
|
|
207
|
+
}
|
|
208
|
+
return node;
|
|
160
209
|
}
|
|
161
210
|
/**
|
|
162
211
|
* Skip whitespace only
|
package/lexer.d.ts
CHANGED
|
@@ -102,6 +102,15 @@ export declare class Lexer {
|
|
|
102
102
|
* Used for directive values where we don't want escape handling
|
|
103
103
|
*/
|
|
104
104
|
private readToEndOfLineRaw;
|
|
105
|
+
/**
|
|
106
|
+
* Read a flag, including its `=value` if present
|
|
107
|
+
*
|
|
108
|
+
* Unlike readWord, this does not stop at `=`: a flag's value is part of the
|
|
109
|
+
* flag (`--mount=type=cache,id=store`, `--from=builder`). Stopping at the
|
|
110
|
+
* first `=` leaves the value behind as free text, where an instruction parser
|
|
111
|
+
* reads it as the start of the command.
|
|
112
|
+
*/
|
|
113
|
+
private readFlag;
|
|
105
114
|
/**
|
|
106
115
|
* Read a comment or directive
|
|
107
116
|
*/
|
package/lexer.js
CHANGED
|
@@ -256,6 +256,34 @@ class Lexer {
|
|
|
256
256
|
}
|
|
257
257
|
return result;
|
|
258
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Read a flag, including its `=value` if present
|
|
261
|
+
*
|
|
262
|
+
* Unlike readWord, this does not stop at `=`: a flag's value is part of the
|
|
263
|
+
* flag (`--mount=type=cache,id=store`, `--from=builder`). Stopping at the
|
|
264
|
+
* first `=` leaves the value behind as free text, where an instruction parser
|
|
265
|
+
* reads it as the start of the command.
|
|
266
|
+
*/
|
|
267
|
+
readFlag() {
|
|
268
|
+
let result = '';
|
|
269
|
+
while (!this.isAtEnd()) {
|
|
270
|
+
const char = this.peek();
|
|
271
|
+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
if (char === this.escapeChar && (this.peek(1) === '\n' || this.peek(1) === '\r')) {
|
|
275
|
+
// Line continuation
|
|
276
|
+
this.advance();
|
|
277
|
+
if (this.peek() === '\r')
|
|
278
|
+
this.advance();
|
|
279
|
+
if (this.peek() === '\n')
|
|
280
|
+
this.advance();
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
result += this.advance();
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
259
287
|
/**
|
|
260
288
|
* Read a comment or directive
|
|
261
289
|
*/
|
|
@@ -364,9 +392,9 @@ class Lexer {
|
|
|
364
392
|
range: { start, end: this.getPosition() }
|
|
365
393
|
};
|
|
366
394
|
}
|
|
367
|
-
// Flag (--something)
|
|
395
|
+
// Flag (--something, or --something=value)
|
|
368
396
|
if (char === '-' && this.peek(1) === '-') {
|
|
369
|
-
const value = this.
|
|
397
|
+
const value = this.readFlag();
|
|
370
398
|
return {
|
|
371
399
|
type: TokenType.FLAG,
|
|
372
400
|
value,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docker-parser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Dockerfile parser and deparser",
|
|
6
6
|
"main": "index.js",
|
|
@@ -19,15 +19,6 @@
|
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/constructive-io/dev-utils/issues"
|
|
21
21
|
},
|
|
22
|
-
"scripts": {
|
|
23
|
-
"copy": "makage assets",
|
|
24
|
-
"clean": "makage clean",
|
|
25
|
-
"prepublishOnly": "npm run build",
|
|
26
|
-
"build": "makage build",
|
|
27
|
-
"lint": "eslint . --fix",
|
|
28
|
-
"test": "jest",
|
|
29
|
-
"test:watch": "jest --watch"
|
|
30
|
-
},
|
|
31
22
|
"keywords": [
|
|
32
23
|
"docker",
|
|
33
24
|
"dockerfile",
|
|
@@ -36,10 +27,17 @@
|
|
|
36
27
|
"deparser"
|
|
37
28
|
],
|
|
38
29
|
"dependencies": {
|
|
39
|
-
"bash-ast": "0.1.
|
|
30
|
+
"bash-ast": "0.1.2"
|
|
40
31
|
},
|
|
41
32
|
"devDependencies": {
|
|
42
33
|
"makage": "0.1.10"
|
|
43
34
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
35
|
+
"scripts": {
|
|
36
|
+
"copy": "makage assets",
|
|
37
|
+
"clean": "makage clean",
|
|
38
|
+
"build": "makage build",
|
|
39
|
+
"lint": "eslint . --fix",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"test:watch": "jest --watch"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/parser.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export declare class Parser {
|
|
|
13
13
|
private pos;
|
|
14
14
|
private escapeChar;
|
|
15
15
|
private options;
|
|
16
|
+
/** Comments read but not yet attached to the node they lead. */
|
|
17
|
+
private pendingComments;
|
|
18
|
+
/** Whether a blank line preceded the node about to be parsed. */
|
|
19
|
+
private pendingBlank;
|
|
16
20
|
constructor(options?: ParserOptions);
|
|
17
21
|
/**
|
|
18
22
|
* Parse Dockerfile source into AST
|
|
@@ -31,9 +35,21 @@ export declare class Parser {
|
|
|
31
35
|
*/
|
|
32
36
|
private advance;
|
|
33
37
|
/**
|
|
34
|
-
* Skip whitespace and newlines
|
|
38
|
+
* Skip whitespace and newlines, recording whether a blank line was crossed
|
|
35
39
|
*/
|
|
36
40
|
private skipWhitespaceAndNewlines;
|
|
41
|
+
/**
|
|
42
|
+
* Attach the comments and blank line collected since the last node
|
|
43
|
+
*/
|
|
44
|
+
private takeLeading;
|
|
45
|
+
/**
|
|
46
|
+
* Attach only the blank line collected since the last node
|
|
47
|
+
*
|
|
48
|
+
* A comment takes the blank line above it but not the comments above it:
|
|
49
|
+
* consecutive comments are siblings in one block leading the same
|
|
50
|
+
* instruction, not a chain where each leads the next.
|
|
51
|
+
*/
|
|
52
|
+
private takeBlank;
|
|
37
53
|
/**
|
|
38
54
|
* Skip whitespace only
|
|
39
55
|
*/
|
package/parser.js
CHANGED
|
@@ -12,6 +12,10 @@ class Parser {
|
|
|
12
12
|
pos = 0;
|
|
13
13
|
escapeChar = '\\';
|
|
14
14
|
options;
|
|
15
|
+
/** Comments read but not yet attached to the node they lead. */
|
|
16
|
+
pendingComments = [];
|
|
17
|
+
/** Whether a blank line preceded the node about to be parsed. */
|
|
18
|
+
pendingBlank = false;
|
|
15
19
|
constructor(options = {}) {
|
|
16
20
|
this.options = {
|
|
17
21
|
includeComments: true,
|
|
@@ -59,7 +63,9 @@ class Parser {
|
|
|
59
63
|
}
|
|
60
64
|
else if (token.type === lexer_1.TokenType.COMMENT) {
|
|
61
65
|
if (this.options.includeComments) {
|
|
62
|
-
|
|
66
|
+
const comment = this.parseComment();
|
|
67
|
+
dockerfile.comments.push(comment);
|
|
68
|
+
this.pendingComments.push(comment);
|
|
63
69
|
}
|
|
64
70
|
else {
|
|
65
71
|
this.advance();
|
|
@@ -78,7 +84,9 @@ class Parser {
|
|
|
78
84
|
const token = this.peek();
|
|
79
85
|
if (token.type === lexer_1.TokenType.COMMENT) {
|
|
80
86
|
if (this.options.includeComments) {
|
|
81
|
-
|
|
87
|
+
const comment = this.takeBlank(this.parseComment());
|
|
88
|
+
dockerfile.comments.push(comment);
|
|
89
|
+
this.pendingComments.push(comment);
|
|
82
90
|
}
|
|
83
91
|
else {
|
|
84
92
|
this.advance();
|
|
@@ -87,18 +95,19 @@ class Parser {
|
|
|
87
95
|
}
|
|
88
96
|
if (token.type === lexer_1.TokenType.FROM) {
|
|
89
97
|
const from = this.parseFrom();
|
|
90
|
-
currentStage = {
|
|
98
|
+
currentStage = this.takeLeading({
|
|
91
99
|
type: 'Stage',
|
|
92
100
|
from,
|
|
93
101
|
instructions: [],
|
|
94
102
|
name: from.name,
|
|
95
103
|
range: from.range
|
|
96
|
-
};
|
|
104
|
+
});
|
|
97
105
|
dockerfile.stages.push(currentStage);
|
|
98
106
|
continue;
|
|
99
107
|
}
|
|
100
108
|
// Parse other instructions
|
|
101
|
-
const
|
|
109
|
+
const parsed = this.parseInstruction();
|
|
110
|
+
const instruction = parsed ? this.takeLeading(parsed) : parsed;
|
|
102
111
|
if (instruction) {
|
|
103
112
|
if (currentStage) {
|
|
104
113
|
currentStage.instructions.push(instruction);
|
|
@@ -121,6 +130,12 @@ class Parser {
|
|
|
121
130
|
}
|
|
122
131
|
}
|
|
123
132
|
}
|
|
133
|
+
// Comments after the last instruction lead nothing; keep them so the file
|
|
134
|
+
// can be deparsed without losing its tail.
|
|
135
|
+
if (this.pendingComments.length > 0) {
|
|
136
|
+
dockerfile.trailingComments = this.pendingComments;
|
|
137
|
+
this.pendingComments = [];
|
|
138
|
+
}
|
|
124
139
|
return dockerfile;
|
|
125
140
|
}
|
|
126
141
|
/**
|
|
@@ -149,18 +164,52 @@ class Parser {
|
|
|
149
164
|
return this.tokens[this.pos - 1];
|
|
150
165
|
}
|
|
151
166
|
/**
|
|
152
|
-
* Skip whitespace and newlines
|
|
167
|
+
* Skip whitespace and newlines, recording whether a blank line was crossed
|
|
153
168
|
*/
|
|
154
169
|
skipWhitespaceAndNewlines() {
|
|
170
|
+
let newlines = 0;
|
|
155
171
|
while (!this.isAtEnd()) {
|
|
156
172
|
const token = this.peek();
|
|
157
|
-
if (token.type === lexer_1.TokenType.
|
|
173
|
+
if (token.type === lexer_1.TokenType.NEWLINE) {
|
|
174
|
+
newlines++;
|
|
175
|
+
this.advance();
|
|
176
|
+
}
|
|
177
|
+
else if (token.type === lexer_1.TokenType.WHITESPACE) {
|
|
158
178
|
this.advance();
|
|
159
179
|
}
|
|
160
180
|
else {
|
|
161
181
|
break;
|
|
162
182
|
}
|
|
163
183
|
}
|
|
184
|
+
// Two newlines in a row means an empty line between instructions. At the
|
|
185
|
+
// very start of the file there is no preceding instruction to separate from.
|
|
186
|
+
if (newlines > 1 && this.pos > newlines) {
|
|
187
|
+
this.pendingBlank = true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Attach the comments and blank line collected since the last node
|
|
192
|
+
*/
|
|
193
|
+
takeLeading(node) {
|
|
194
|
+
if (this.pendingComments.length > 0) {
|
|
195
|
+
node.leadingComments = this.pendingComments;
|
|
196
|
+
this.pendingComments = [];
|
|
197
|
+
}
|
|
198
|
+
return this.takeBlank(node);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Attach only the blank line collected since the last node
|
|
202
|
+
*
|
|
203
|
+
* A comment takes the blank line above it but not the comments above it:
|
|
204
|
+
* consecutive comments are siblings in one block leading the same
|
|
205
|
+
* instruction, not a chain where each leads the next.
|
|
206
|
+
*/
|
|
207
|
+
takeBlank(node) {
|
|
208
|
+
if (this.pendingBlank) {
|
|
209
|
+
node.blankBefore = true;
|
|
210
|
+
this.pendingBlank = false;
|
|
211
|
+
}
|
|
212
|
+
return node;
|
|
164
213
|
}
|
|
165
214
|
/**
|
|
166
215
|
* Skip whitespace only
|
package/types.d.ts
CHANGED
|
@@ -19,6 +19,17 @@ export interface Range {
|
|
|
19
19
|
export interface BaseNode {
|
|
20
20
|
type: string;
|
|
21
21
|
range?: Range;
|
|
22
|
+
/**
|
|
23
|
+
* Comment lines immediately above this node, in source order.
|
|
24
|
+
*
|
|
25
|
+
* A Dockerfile's comments explain the instruction they sit on — why a COPY is
|
|
26
|
+
* split out, what busts a cache layer — so they belong to that node rather
|
|
27
|
+
* than to the file. The deparser emits them, which is what lets a generated
|
|
28
|
+
* Dockerfile carry its reasoning and a parsed one survive a round-trip.
|
|
29
|
+
*/
|
|
30
|
+
leadingComments?: Comment[];
|
|
31
|
+
/** Emit one blank line above this node (and above its leading comments). */
|
|
32
|
+
blankBefore?: boolean;
|
|
22
33
|
}
|
|
23
34
|
/**
|
|
24
35
|
* Dockerfile AST root node
|
|
@@ -27,7 +38,13 @@ export interface Dockerfile extends BaseNode {
|
|
|
27
38
|
type: 'Dockerfile';
|
|
28
39
|
directives: ParserDirective[];
|
|
29
40
|
stages: Stage[];
|
|
41
|
+
/**
|
|
42
|
+
* Every comment in the file, in source order, whether or not it is also
|
|
43
|
+
* attached to a node as a leading comment.
|
|
44
|
+
*/
|
|
30
45
|
comments: Comment[];
|
|
46
|
+
/** Comments after the last instruction, which lead no node. */
|
|
47
|
+
trailingComments?: Comment[];
|
|
31
48
|
}
|
|
32
49
|
/**
|
|
33
50
|
* Parser directive (e.g., # escape=\ or # syntax=docker/dockerfile:1)
|