Skip to content

Commit

Permalink
Fix #107 Ignore empty lines in heredocs
Browse files Browse the repository at this point in the history
Empty lines that are found in a here-document should not be flagged with
the empty continuation line warning.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Jan 22, 2022
1 parent 4e55981 commit aa9cccb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
### Fixed
- empty lines in heredoc content should not trigger the empty continuation line warning ([#107](https://github.com/rcjsuen/dockerfile-utils/issues/107))

## [0.9.3] - 2021-12-11
### Fixed
- ignore heredoc content when formatting ([#105](https://github.com/rcjsuen/dockerfile-utils/issues/105))
Expand Down
33 changes: 32 additions & 1 deletion src/dockerValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* ------------------------------------------------------------------------------------------ */
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Diagnostic, DiagnosticSeverity, DiagnosticTag, Position, Range } from 'vscode-languageserver-types';
import { Dockerfile, Flag, Instruction, JSONInstruction, Add, Arg, Cmd, Copy, Entrypoint, From, Healthcheck, Onbuild, ModifiableInstruction, PropertyInstruction, Property, DockerfileParser, Directive, Keyword, Argument } from 'dockerfile-ast';
import { Dockerfile, Flag, Instruction, JSONInstruction, Add, Arg, Cmd, Copy, Entrypoint, From, Healthcheck, Onbuild, ModifiableInstruction, PropertyInstruction, Property, DockerfileParser, Directive, Keyword, Argument, Run } from 'dockerfile-ast';
import { ValidationCode, ValidationSeverity, ValidatorSettings } from './main';

export const KEYWORDS = [
Expand Down Expand Up @@ -295,6 +295,31 @@ export class Validator {
return problems;
}

/**
* Retrieves the line numbers that corresponds to the content of
* here-documents in the given instruction. The line numbers are
* zero-based.
*
* @param instruction the instruction to check
* @returns an array of line numbers where content of
* here-documents are defined
*/
private getHeredocLines(instruction: Instruction): number[] {
if (instruction instanceof Run) {
const lines: number[] = [];
for (const heredoc of instruction.getHeredocs()) {
const range = heredoc.getContentRange();
if (range !== null) {
for (let i = range.start.line; i <= range.end.line; i++) {
lines.push(i);
}
}
}
return lines;
}
return []
}

private validateInstruction(document: TextDocument, escapeChar: string, instruction: Instruction, keyword: string, isTrigger: boolean, problems: Diagnostic[]): void {
if (KEYWORDS.indexOf(keyword) === -1) {
let range = instruction.getInstructionRange();
Expand Down Expand Up @@ -323,8 +348,14 @@ export class Validator {
// if the instruction spans multiple lines, check for empty newlines
const content = document.getText();
const endingLine = fullRange.end.line;
const skippedLines = this.getHeredocLines(instruction);
let skipIndex = 0;
let start = -1;
for (let i = fullRange.start.line; i <= endingLine; i++) {
if (i === skippedLines[skipIndex]) {
skipIndex++;
continue;
}
const lineContent = content.substring(document.offsetAt(Position.create(i, 0)), document.offsetAt(Position.create(i + 1, 0)));
if (lineContent.trim().length === 0) {
if (start === -1) {
Expand Down
22 changes: 22 additions & 0 deletions test/dockerValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,28 @@ describe("Docker Validator Tests", function() {
assertEmptyContinuationLine(diagnostics[0], DiagnosticSeverity.Error, 2, 0, 5, 0);
});
});

describe("heredocs", () => {
it("single, no content at all", () => {
const diagnostics = validateDockerfile("FROM alpine\nRUN <<eot file.txt\n\neot", { emptyContinuationLine: ValidationSeverity.WARNING });
assert.strictEqual(diagnostics.length, 0);
});

it("single, has some content", () => {
const diagnostics = validateDockerfile("FROM alpine\nRUN <<eot file.txt\nabc\n\neot", { emptyContinuationLine: ValidationSeverity.WARNING });
assert.strictEqual(diagnostics.length, 0);
});

it("multiple, no content at all", () => {
const diagnostics = validateDockerfile("FROM alpine\nRUN <<eot file.txt <<eot2 file2.txt\n\neot\n\neot2", { emptyContinuationLine: ValidationSeverity.WARNING });
assert.strictEqual(diagnostics.length, 0);
});

it("multiple, has some content", () => {
const diagnostics = validateDockerfile("FROM alpine\nRUN <<eot file.txt <<eot2 file2.txt\nabc\n\neot\nabc\n\neot2", { emptyContinuationLine: ValidationSeverity.WARNING });
assert.strictEqual(diagnostics.length, 0);
});
});
});
});

Expand Down

0 comments on commit aa9cccb

Please sign in to comment.