Skip to content

Commit 6fefbd4

Browse files
committed
remove the directive from the comments in the AST
Signed-off-by: Talon Bowler <[email protected]>
1 parent aff0479 commit 6fefbd4

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

frontend/dockerfile/dockerfile_lint_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,35 +338,30 @@ COPY $bar .
338338

339339
func testRuleCheckOption(t *testing.T, sb integration.Sandbox) {
340340
dockerfile := []byte(`#check=skip=all
341-
#
342341
FROM scratch as base
343342
copy Dockerfile .
344343
`)
345344
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
346345

347346
dockerfile = []byte(`#check=skip=all;error=true
348-
#
349347
FROM scratch as base
350348
copy Dockerfile .
351349
`)
352350
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
353351

354352
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing,FromAsCasing
355-
#
356353
FROM scratch as base
357354
copy Dockerfile .
358355
`)
359356
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
360357

361358
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing,FromAsCasing;error=true
362-
#
363359
FROM scratch as base
364360
copy Dockerfile .
365361
`)
366362
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
367363

368364
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing
369-
#
370365
FROM scratch as base
371366
copy Dockerfile .
372367
`)
@@ -378,14 +373,13 @@ copy Dockerfile .
378373
Description: "The 'as' keyword should match the case of the 'from' keyword",
379374
URL: "https://docs.docker.com/go/dockerfile/rule/from-as-casing/",
380375
Detail: "'as' and 'FROM' keywords' casing do not match",
381-
Line: 3,
376+
Line: 2,
382377
Level: 1,
383378
},
384379
},
385380
})
386381

387382
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing;error=true
388-
#
389383
FROM scratch as base
390384
copy Dockerfile .
391385
`)
@@ -397,7 +391,7 @@ copy Dockerfile .
397391
Description: "The 'as' keyword should match the case of the 'from' keyword",
398392
URL: "https://docs.docker.com/go/dockerfile/rule/from-as-casing/",
399393
Detail: "'as' and 'FROM' keywords' casing do not match",
400-
Line: 3,
394+
Line: 2,
401395
Level: 1,
402396
},
403397
},
@@ -407,7 +401,6 @@ copy Dockerfile .
407401
})
408402

409403
dockerfile = []byte(`#check=skip=all
410-
#
411404
FROM scratch as base
412405
copy Dockerfile .
413406
`)
@@ -419,7 +412,7 @@ copy Dockerfile .
419412
Description: "The 'as' keyword should match the case of the 'from' keyword",
420413
URL: "https://docs.docker.com/go/dockerfile/rule/from-as-casing/",
421414
Detail: "'as' and 'FROM' keywords' casing do not match",
422-
Line: 3,
415+
Line: 2,
423416
Level: 1,
424417
},
425418
},
@@ -432,7 +425,6 @@ copy Dockerfile .
432425
})
433426

434427
dockerfile = []byte(`#check=error=true
435-
#
436428
FROM scratch as base
437429
copy Dockerfile .
438430
`)

frontend/dockerfile/parser/parser.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,17 @@ func (d *directives) setEscapeToken(s string) error {
167167

168168
// possibleParserDirective looks for parser directives, eg '# escapeToken=<char>'.
169169
// Parser directives must precede any builder instruction or other comments,
170-
// and cannot be repeated.
171-
func (d *directives) possibleParserDirective(line []byte) error {
170+
// and cannot be repeated. Returns true if a parser directive was found.
171+
func (d *directives) possibleParserDirective(line []byte) (bool, error) {
172172
directive, err := d.parser.ParseLine(line)
173173
if err != nil {
174-
return err
174+
return false, err
175175
}
176176
if directive != nil && directive.Name == keyEscape {
177-
return d.setEscapeToken(directive.Value)
177+
err := d.setEscapeToken(directive.Value)
178+
return err == nil, err
178179
}
179-
return nil
180+
return directive != nil, nil
180181
}
181182

182183
// newDefaultDirectives returns a new directives structure with the default escapeToken token
@@ -300,7 +301,13 @@ func Parse(rwc io.Reader) (*Result, error) {
300301
comments = append(comments, comment)
301302
}
302303
}
303-
bytesRead, err = processLine(d, bytesRead, true)
304+
var directiveOk bool
305+
bytesRead, directiveOk, err = processLine(d, bytesRead, true)
306+
// If the line is a directive, strip it from the comments
307+
// so it doesn't get added to the AST.
308+
if directiveOk {
309+
comments = comments[:len(comments)-1]
310+
}
304311
if err != nil {
305312
return nil, withLocation(err, currentLine, 0)
306313
}
@@ -316,7 +323,7 @@ func Parse(rwc io.Reader) (*Result, error) {
316323

317324
var hasEmptyContinuationLine bool
318325
for !isEndOfLine && scanner.Scan() {
319-
bytesRead, err := processLine(d, scanner.Bytes(), false)
326+
bytesRead, _, err := processLine(d, scanner.Bytes(), false)
320327
if err != nil {
321328
return nil, withLocation(err, currentLine, 0)
322329
}
@@ -527,12 +534,13 @@ func trimContinuationCharacter(line []byte, d *directives) ([]byte, bool) {
527534

528535
// TODO: remove stripLeftWhitespace after deprecation period. It seems silly
529536
// to preserve whitespace on continuation lines. Why is that done?
530-
func processLine(d *directives, token []byte, stripLeftWhitespace bool) ([]byte, error) {
537+
func processLine(d *directives, token []byte, stripLeftWhitespace bool) ([]byte, bool, error) {
531538
token = trimNewline(token)
532539
if stripLeftWhitespace {
533540
token = trimLeadingWhitespace(token)
534541
}
535-
return trimComments(token), d.possibleParserDirective(token)
542+
directiveOk, err := d.possibleParserDirective(token)
543+
return trimComments(token), directiveOk, err
536544
}
537545

538546
// Variation of bufio.ScanLines that preserves the line endings

0 commit comments

Comments
 (0)